/fancy_assert

A fancy assertion facility for C++

Primary LanguageC++BSD 3-Clause "New" or "Revised" LicenseBSD-3-Clause

Fancy Assert

A really fancy assertion facility for C++.

Prerequisites

A header prettyprint.hpp must be accessable in the include-dir, which can be found here.

Usage

#include "fassert.hpp"

int main()
{
    int x = 0;

    // nothing happens
    FASSERT(x == 0)
        (x);

    // assert triggers.
    // and outputs `x` value, together with file name and line number
    FASSERT(x != 0)
        (x);

    // assert triggers.
    // and outputs `x` value, together with file name and line number
    // with an error message.
    FASSERT(x != 0)
        (x)
        .what("Something goes wrong!");

    return 0;
}

For some cases, programs must do some cleanups before aborting.

#include "fassert.hpp"

void cleanup()
{
    // do something
}

int main()
{
    fassert::Finalizers::singleton()->register_finalize(cleanup);

    // Assert triggers.
    // Before aborting, `cleanup()` will be invoked.
    FASSERT(false);

    return 0;
}