diff options
Diffstat (limited to 'include/cru/base')
| -rw-r--r-- | include/cru/base/Bitmask.h | 47 | ||||
| -rw-r--r-- | include/cru/base/StringUtil.h | 14 | ||||
| -rw-r--r-- | include/cru/base/io/Base.h | 7 | ||||
| -rw-r--r-- | include/cru/base/platform/unix/UnixFile.h | 6 | ||||
| -rw-r--r-- | include/cru/base/platform/win/Base.h | 6 |
5 files changed, 35 insertions, 45 deletions
diff --git a/include/cru/base/Bitmask.h b/include/cru/base/Bitmask.h index 7606f784..ba5b37f2 100644 --- a/include/cru/base/Bitmask.h +++ b/include/cru/base/Bitmask.h @@ -8,50 +8,63 @@ struct Bitmask final { using Underlying = TUnderlying; constexpr Bitmask() : value(0) {} - constexpr explicit Bitmask(TUnderlying value) : value(value) {} + constexpr explicit Bitmask(Underlying value) : value(value) {} // Start from 1. static constexpr Bitmask FromOffset(int offset) { if (offset == 0) return {}; - return Bitmask(static_cast<TUnderlying>(1u << (offset - 1))); + return Bitmask(static_cast<Underlying>(Underlying(1) << (offset - 1))); } constexpr bool Has(Bitmask rhs) const { return (value & rhs.value) != 0; } - Bitmask operator|(Bitmask rhs) const { return Bitmask(value | rhs.value); } - Bitmask operator&(Bitmask rhs) const { return Bitmask(value & rhs.value); } - Bitmask operator^(Bitmask rhs) const { return Bitmask(value ^ rhs.value); } - Bitmask operator~() const { return Bitmask(~value); } - Bitmask& operator|=(Bitmask rhs) { + constexpr Bitmask operator|(Bitmask rhs) const { + return Bitmask(value | rhs.value); + } + constexpr Bitmask operator&(Bitmask rhs) const { + return Bitmask(value & rhs.value); + } + constexpr Bitmask operator^(Bitmask rhs) const { + return Bitmask(value ^ rhs.value); + } + constexpr Bitmask operator~() const { return Bitmask(~value); } + constexpr Bitmask& operator|=(Bitmask rhs) { value |= rhs.value; return *this; } - Bitmask& operator&=(Bitmask rhs) { + constexpr Bitmask& operator&=(Bitmask rhs) { value &= rhs.value; return *this; } - Bitmask& operator^=(Bitmask rhs) { + constexpr Bitmask& operator^=(Bitmask rhs) { value ^= rhs.value; return *this; } - bool operator==(Bitmask rhs) const { return this->value == rhs.value; } - bool operator!=(Bitmask rhs) const { return this->value != rhs.value; } + constexpr bool operator==(Bitmask rhs) const { + return this->value == rhs.value; + } - explicit operator TUnderlying() const { return value; } - operator bool() const { return value != 0; } + constexpr explicit operator Underlying() const { return value; } + constexpr explicit operator bool() const { return value != 0; } - TUnderlying value; + Underlying value; }; } // namespace cru namespace std { template <typename Tag, typename TUnderlying> struct hash<cru::Bitmask<Tag, TUnderlying>> { - using Bitmask = cru::Bitmask<Tag, TUnderlying>; - - std::size_t operator()(Bitmask bitmask) const { + constexpr std::size_t operator()( + cru::Bitmask<Tag, TUnderlying> bitmask) const { return std::hash<TUnderlying>{}(bitmask.value); } }; } // namespace std + +#define CRU_DEFINE_BITMASK(name) \ + namespace details { \ + struct name##Tag {}; \ + } \ + using name = ::cru::Bitmask<details::name##Tag>; \ + struct name##s diff --git a/include/cru/base/StringUtil.h b/include/cru/base/StringUtil.h index 363493a1..2242bcd3 100644 --- a/include/cru/base/StringUtil.h +++ b/include/cru/base/StringUtil.h @@ -30,11 +30,7 @@ std::string CRU_BASE_API TrimEnd(std::string_view str); std::string CRU_BASE_API Trim(std::string_view str); bool CRU_BASE_API IsSpace(std::string_view str); -namespace details { -struct SplitOptionsTag {}; -} // namespace details -using SplitOption = Bitmask<details::SplitOptionsTag>; -struct SplitOptions { +CRU_DEFINE_BITMASK(SplitOption) { static constexpr SplitOption RemoveEmpty = SplitOption::FromOffset(1); static constexpr SplitOption RemoveSpace = SplitOption::FromOffset(2); }; @@ -105,13 +101,7 @@ CRU_DEFINE_FLOAT_FROM_CHARS(long double, strtold, HUGE_VALL) #undef CRU_DEFINE_FLOAT_FROM_CHARS } // namespace details -namespace details { -struct ParseToNumberFlagTag {}; -} // namespace details - -using ParseToNumberFlag = Bitmask<details::ParseToNumberFlagTag>; - -struct ParseToNumberFlags { +CRU_DEFINE_BITMASK(ParseToNumberFlag) { constexpr static ParseToNumberFlag AllowLeadingSpaces = ParseToNumberFlag::FromOffset(1); constexpr static ParseToNumberFlag AllowTrailingSpaces = diff --git a/include/cru/base/io/Base.h b/include/cru/base/io/Base.h index d5e2abff..85ea5343 100644 --- a/include/cru/base/io/Base.h +++ b/include/cru/base/io/Base.h @@ -9,12 +9,7 @@ class CRU_BASE_API FileNotExistException : public Exception { using Exception::Exception; }; -namespace details { -struct OpenFileFlagTag {}; -} // namespace details -using OpenFileFlag = Bitmask<details::OpenFileFlagTag>; - -struct OpenFileFlags { +CRU_DEFINE_BITMASK(OpenFileFlag) { /** * \brief for reading * If the file does not exist, FileNotExistException should be thrown. diff --git a/include/cru/base/platform/unix/UnixFile.h b/include/cru/base/platform/unix/UnixFile.h index 5754b07b..65364c57 100644 --- a/include/cru/base/platform/unix/UnixFile.h +++ b/include/cru/base/platform/unix/UnixFile.h @@ -61,11 +61,7 @@ struct UniDirectionalUnixPipeResult { UnixFileDescriptor write; }; -namespace details { -struct UnixPipeFlagTag; -} -using UnixPipeFlag = Bitmask<details::UnixPipeFlagTag>; -struct UnixPipeFlags { +CRU_DEFINE_BITMASK(UnixPipeFlag) { constexpr static auto NonBlock = UnixPipeFlag::FromOffset(1); }; diff --git a/include/cru/base/platform/win/Base.h b/include/cru/base/platform/win/Base.h index cd5007e3..7503ca5e 100644 --- a/include/cru/base/platform/win/Base.h +++ b/include/cru/base/platform/win/Base.h @@ -67,11 +67,7 @@ struct UniDirectionalWin32PipeResult { Win32Handle write; }; -namespace details { -struct Win32PipeFlagTag; -} -using Win32PipeFlag = Bitmask<details::Win32PipeFlagTag>; -struct Win32PipeFlags { +CRU_DEFINE_BITMASK(Win32PipeFlag) { constexpr static auto ReadInheritable = Win32PipeFlag::FromOffset(1); constexpr static auto WriteInheritable = Win32PipeFlag::FromOffset(2); }; |
