Program Listing for File Enforce.hpp

Return to documentation for file (include/networkit/auxiliary/Enforce.hpp)

#ifndef NETWORKIT_AUXILIARY_ENFORCE_HPP_
#define NETWORKIT_AUXILIARY_ENFORCE_HPP_

#include <cassert>
#include <stdexcept>
#include <string>

namespace Aux {

template <typename Exception = std::runtime_error>
inline void enforce(bool b, const char *msg = "") {
    assert(msg && "Message to enforce must not be nullptr");
    if (!b) {
        throw Exception{msg};
    }
}

template <typename Exception = std::runtime_error>
inline void enforce(bool b, const std::string &msg) {
    enforce<Exception>(b, msg.c_str());
}

template <typename Stream>
inline void enforceOpened(const Stream &stream) {
    enforce(stream.is_open());
}

namespace Checkers {

struct Asserter {
    static void enforce(bool b) {
        assert(b);
        (void)b; // prevent warnings in release-builds
    }
};

struct Enforcer {
    static void enforce(bool b) { ::Aux::enforce(b); }
};

struct Terminator {
    static void enforce(bool b) {
        if (!b) {
            std::terminate();
        }
    }
};

struct Ignorer {
    static void enforce(bool) {}
};
} // namespace Checkers

} // namespace Aux

#endif // NETWORKIT_AUXILIARY_ENFORCE_HPP_