aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--include/cru/base/Bitmask.h47
-rw-r--r--include/cru/base/StringUtil.h14
-rw-r--r--include/cru/base/io/Base.h7
-rw-r--r--include/cru/base/platform/unix/UnixFile.h6
-rw-r--r--include/cru/base/platform/win/Base.h6
-rw-r--r--include/cru/platform/gui/Input.h17
-rw-r--r--include/cru/platform/gui/Window.h9
-rw-r--r--include/cru/ui/document/TextDocumentElement.h8
8 files changed, 41 insertions, 73 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);
};
diff --git a/include/cru/platform/gui/Input.h b/include/cru/platform/gui/Input.h
index a29a9bba..007ed408 100644
--- a/include/cru/platform/gui/Input.h
+++ b/include/cru/platform/gui/Input.h
@@ -6,12 +6,7 @@
#include <string>
namespace cru::platform::gui {
-namespace details {
-struct TagMouseButton {};
-} // namespace details
-
-using MouseButton = Bitmask<details::TagMouseButton>;
-struct MouseButtons {
+CRU_DEFINE_BITMASK(MouseButton) {
constexpr static MouseButton None = MouseButton::FromOffset(0);
constexpr static MouseButton Left = MouseButton::FromOffset(1);
constexpr static MouseButton Middle = MouseButton::FromOffset(2);
@@ -76,7 +71,7 @@ enum class KeyCode {
X,
Y,
Z,
- Grave, // TODO: fix this key mapping on Windows and Xorg.
+ Grave, // TODO: fix this key mapping on Windows and Xorg.
Tab,
CapsLock,
LeftShift,
@@ -127,13 +122,7 @@ enum class KeyCode {
Space
};
-namespace details {
-struct TagKeyModifier {};
-} // namespace details
-
-using KeyModifier = Bitmask<details::TagKeyModifier>;
-
-struct KeyModifiers {
+CRU_DEFINE_BITMASK(KeyModifier) {
static constexpr KeyModifier None = KeyModifier::FromOffset(0);
static constexpr KeyModifier Shift = KeyModifier::FromOffset(1);
static constexpr KeyModifier Ctrl = KeyModifier::FromOffset(2);
diff --git a/include/cru/platform/gui/Window.h b/include/cru/platform/gui/Window.h
index db03c528..1ef9e980 100644
--- a/include/cru/platform/gui/Window.h
+++ b/include/cru/platform/gui/Window.h
@@ -3,6 +3,7 @@
#include "Input.h"
+#include <cru/base/Bitmask.h>
#include <cru/base/Event.h>
#include <cru/platform/GraphicsBase.h>
#include <cru/platform/graphics/Painter.h>
@@ -11,13 +12,7 @@ namespace cru::platform::gui {
class ICursor;
class IInputMethodContext;
-namespace details {
-struct WindowStyleFlagTag;
-}
-
-using WindowStyleFlag = Bitmask<details::WindowStyleFlagTag>;
-
-struct WindowStyleFlags {
+CRU_DEFINE_BITMASK(WindowStyleFlag) {
static constexpr WindowStyleFlag NoCaptionAndBorder =
WindowStyleFlag::FromOffset(1);
};
diff --git a/include/cru/ui/document/TextDocumentElement.h b/include/cru/ui/document/TextDocumentElement.h
index 85ea6f2e..8cdf5b1f 100644
--- a/include/cru/ui/document/TextDocumentElement.h
+++ b/include/cru/ui/document/TextDocumentElement.h
@@ -6,13 +6,7 @@
#include "cru/base/Bitmask.h"
namespace cru::ui::document {
-namespace details {
-struct TextStyleTag {};
-} // namespace details
-
-using TextStyle = Bitmask<details::TextStyleTag>;
-
-struct TextStyles {
+CRU_DEFINE_BITMASK(TextStyle) {
static constexpr TextStyle Normal{0x0};
static constexpr TextStyle Bold{0x1};
static constexpr TextStyle Italic{0x2};