diff --git a/source/mijin/util/bitflags.hpp b/source/mijin/util/bitflags.hpp index 7c5f41d..e34446a 100644 --- a/source/mijin/util/bitflags.hpp +++ b/source/mijin/util/bitflags.hpp @@ -5,8 +5,11 @@ #define MIJIN_UTIL_BITFLAGS_HPP_INCLUDED 1 #include +#include #include +#include "../util/traits.hpp" + namespace mijin { @@ -90,10 +93,40 @@ private: template constexpr bool is_bitflags_v = std::is_base_of_v, T>; +template +concept BitFlagsType = is_bitflags_v; + // // public functions // +template +TInt bitFlagsToInt(const BitFlags& flags) noexcept +{ + static constexpr std::size_t BYTES = std::min(sizeof(T), sizeof(TInt)); + TInt result = 0; + + for (std::size_t off = 0; off < BYTES; ++off) + { + result |= static_cast(*(std::bit_cast(&flags) + off)) << (off * 8); + } + + return result; +} + +template +T bitFlagsFromInt(TInt intVal) noexcept +{ + static constexpr std::size_t BYTES = std::min(sizeof(T), sizeof(TInt)); + T result = {}; + + for (std::size_t off = 0; off < BYTES; ++off) + { + (*(std::bit_cast(&result) + off)) = static_cast(intVal >> (off * 8)); + } + + return result; +} } // namespace mijin #endif // !defined(MIJIN_UTIL_BITFLAGS_HPP_INCLUDED)