diff options
-rw-r--r-- | include/jau/cpp_lang_util.hpp | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/include/jau/cpp_lang_util.hpp b/include/jau/cpp_lang_util.hpp index 7b3726a..e29bf6e 100644 --- a/include/jau/cpp_lang_util.hpp +++ b/include/jau/cpp_lang_util.hpp @@ -29,6 +29,7 @@ #include <typeinfo> #include <string> #include <cstring> +#include <ostream> namespace jau { @@ -844,6 +845,64 @@ namespace jau { f(); } + enum class Bool : bool { + False = false, + True = true + }; + constexpr Bool True() noexcept { return Bool::True; } + constexpr Bool False() noexcept { return Bool::False; } + + constexpr bool value(const Bool rhs) noexcept { + return static_cast<bool>(rhs); + } + constexpr Bool operator!(const Bool rhs) noexcept { + return static_cast<Bool>(!value(rhs)); + } + constexpr Bool operator&&(const Bool lhs, const Bool rhs) noexcept { + return static_cast<Bool>(value(lhs) && value(rhs)); + } + constexpr Bool operator||(const Bool lhs, const Bool rhs) noexcept { + return static_cast<Bool>(value(lhs) || value(rhs)); + } + constexpr Bool operator^(const Bool lhs, const Bool rhs) noexcept { + return static_cast<Bool>(value(lhs) ^ value(rhs)); + } + constexpr Bool operator|(const Bool lhs, const Bool rhs) noexcept { + return static_cast<Bool>(value(lhs) || value(rhs)); + } + constexpr Bool operator&(const Bool lhs, const Bool rhs) noexcept { + return static_cast<Bool>(value(lhs) && value(rhs)); + } + constexpr Bool& operator|=(Bool& lhs, const Bool rhs) noexcept { + lhs = lhs | rhs; + return lhs; + } + constexpr Bool& operator&=(Bool& lhs, const Bool rhs) noexcept { + lhs = lhs & rhs; + return lhs; + } + constexpr Bool& operator^=(Bool& lhs, const Bool rhs) noexcept { + lhs = lhs ^ rhs; + return lhs; + } + constexpr bool operator==(const Bool lhs, const Bool rhs) noexcept { + return value(lhs) == value(rhs); + } + constexpr bool operator!=(const Bool lhs, const Bool rhs) noexcept { + return !(lhs == rhs); + } + constexpr const char* to_string(const Bool v) noexcept { + if( value(v) ) { + return "true"; + } else { + return "false"; + } + } + inline std::ostream & operator << (std::ostream &out, const Bool c) { + out << to_string(c); + return out; + } + /**@}*/ } // namespace jau |