summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/buffer.hh30
-rw-r--r--src/command_manager.hh11
-rw-r--r--src/flags.hh52
-rw-r--r--src/normal.cc11
-rw-r--r--src/option_manager.hh7
-rw-r--r--src/parameters_parser.hh11
-rw-r--r--src/selectors.hh7
7 files changed, 69 insertions, 60 deletions
diff --git a/src/buffer.hh b/src/buffer.hh
index 4f0da060..70bb59b7 100644
--- a/src/buffer.hh
+++ b/src/buffer.hh
@@ -2,6 +2,7 @@
#define buffer_hh_INCLUDED
#include "coord.hh"
+#include "flags.hh"
#include "hook_manager.hh"
#include "option_manager.hh"
#include "keymap_manager.hh"
@@ -220,35 +221,10 @@ private:
// Values are just data holding by the buffer, so it is part of its
// observable state
mutable ValueMap m_values;
-
- friend constexpr Flags operator|(Flags lhs, Flags rhs)
- {
- return (Flags)((int) lhs | (int) rhs);
- }
-
- friend Flags& operator|=(Flags& lhs, Flags rhs)
- {
- (int&) lhs |= (int) rhs;
- return lhs;
- }
-
- friend constexpr bool operator&(Flags lhs, Flags rhs)
- {
- return ((int) lhs & (int) rhs) != 0;
- }
-
- friend Flags& operator&=(Flags& lhs, Flags rhs)
- {
- (int&) lhs &= (int) rhs;
- return lhs;
- }
-
- friend constexpr Flags operator~(Flags lhs)
- {
- return (Flags)(~(int)lhs);
- }
};
+template<> struct WithBitOps<Buffer::Flags> : std::true_type {};
+
}
#include "buffer.inl.hh"
diff --git a/src/command_manager.hh b/src/command_manager.hh
index 8d0cc957..283fec92 100644
--- a/src/command_manager.hh
+++ b/src/command_manager.hh
@@ -3,6 +3,7 @@
#include "coord.hh"
#include "completion.hh"
+#include "flags.hh"
#include "memoryview.hh"
#include "shell_manager.hh"
#include "parameters_parser.hh"
@@ -28,14 +29,8 @@ enum class CommandFlags
None = 0,
Hidden = 1,
};
-constexpr CommandFlags operator|(CommandFlags lhs, CommandFlags rhs)
-{
- return (CommandFlags)((int)lhs | (int)rhs);
-}
-constexpr bool operator&(CommandFlags lhs, CommandFlags rhs)
-{
- return (bool)((int)lhs & (int)rhs);
-}
+
+template<> struct WithBitOps<CommandFlags> : std::true_type {};
class PerArgumentCommandCompleter
{
diff --git a/src/flags.hh b/src/flags.hh
new file mode 100644
index 00000000..a3cbc5e0
--- /dev/null
+++ b/src/flags.hh
@@ -0,0 +1,52 @@
+#ifndef flags_hh_INCLUDED
+#define flags_hh_INCLUDED
+
+#include <type_traits>
+
+namespace Kakoune
+{
+
+template<typename Flags>
+struct WithBitOps : std::false_type {};
+
+template<typename Flags>
+using EnumStorageType = typename std::underlying_type<Flags>::type;
+
+template<typename Flags>
+using EnableIfWithBitOps = typename std::enable_if<WithBitOps<Flags>::value>::type;
+
+template<typename Flags, typename = EnableIfWithBitOps<Flags>>
+constexpr Flags operator|(Flags lhs, Flags rhs)
+{
+ return (Flags)((EnumStorageType<Flags>) lhs | (EnumStorageType<Flags>) rhs);
+}
+
+template<typename Flags, typename = EnableIfWithBitOps<Flags>>
+Flags& operator|=(Flags& lhs, Flags rhs)
+{
+ (EnumStorageType<Flags>&) lhs |= (EnumStorageType<Flags>) rhs;
+ return lhs;
+}
+
+template<typename Flags, typename = EnableIfWithBitOps<Flags>>
+constexpr bool operator&(Flags lhs, Flags rhs)
+{
+ return ((EnumStorageType<Flags>) lhs & (EnumStorageType<Flags>) rhs) != 0;
+}
+
+template<typename Flags, typename = EnableIfWithBitOps<Flags>>
+Flags& operator&=(Flags& lhs, Flags rhs)
+{
+ (EnumStorageType<Flags>&) lhs &= (EnumStorageType<Flags>) rhs;
+ return lhs;
+}
+
+template<typename Flags, typename = EnableIfWithBitOps<Flags>>
+constexpr Flags operator~(Flags lhs)
+{
+ return (Flags)(~(EnumStorageType<Flags>)lhs);
+}
+
+}
+
+#endif // flags_hh_INCLUDED
diff --git a/src/normal.cc b/src/normal.cc
index 67640b7d..a7c49b2f 100644
--- a/src/normal.cc
+++ b/src/normal.cc
@@ -8,6 +8,7 @@
#include "context.hh"
#include "debug.hh"
#include "face_registry.hh"
+#include "flags.hh"
#include "file.hh"
#include "option_manager.hh"
#include "register_manager.hh"
@@ -956,14 +957,8 @@ enum class SelectFlags
Inclusive = 2,
Extend = 4
};
-constexpr SelectFlags operator|(SelectFlags lhs, SelectFlags rhs)
-{
- return (SelectFlags)((int) lhs | (int) rhs);
-}
-constexpr bool operator&(SelectFlags lhs, SelectFlags rhs)
-{
- return ((int) lhs & (int) rhs) != 0;
-}
+
+template<> struct WithBitOps<SelectFlags> : std::true_type {};
template<SelectFlags flags>
void select_to_next_char(Context& context, int param)
diff --git a/src/option_manager.hh b/src/option_manager.hh
index 32d55e70..00cd0370 100644
--- a/src/option_manager.hh
+++ b/src/option_manager.hh
@@ -3,6 +3,7 @@
#include "completion.hh"
#include "exception.hh"
+#include "flags.hh"
#include "option_types.hh"
#include "utils.hh"
#include "regex.hh"
@@ -25,12 +26,8 @@ enum class OptionFlags
None = 0,
Hidden = 1,
};
-constexpr OptionFlags operator|(OptionFlags lhs, OptionFlags rhs)
-{ return (OptionFlags)((int)lhs | (int)rhs); }
-
-constexpr bool operator&(OptionFlags lhs, OptionFlags rhs)
-{ return (bool)((int)lhs & (int)rhs); }
+template<> struct WithBitOps<OptionFlags> : std::true_type {};
class OptionDesc
{
diff --git a/src/parameters_parser.hh b/src/parameters_parser.hh
index 219c9454..0082ada9 100644
--- a/src/parameters_parser.hh
+++ b/src/parameters_parser.hh
@@ -4,6 +4,7 @@
#include "exception.hh"
#include "id_map.hh"
#include "memoryview.hh"
+#include "flags.hh"
#include "string.hh"
namespace Kakoune
@@ -51,14 +52,6 @@ struct ParameterDesc
SwitchesOnlyAtStart = 1,
SwitchesAsPositional = 2,
};
- friend constexpr Flags operator|(Flags lhs, Flags rhs)
- {
- return (Flags)((int) lhs | (int) rhs);
- }
- friend constexpr bool operator&(Flags lhs, Flags rhs)
- {
- return ((int) lhs & (int) rhs) != 0;
- }
ParameterDesc() = default;
ParameterDesc(SwitchMap switches, Flags flags = Flags::None,
@@ -72,6 +65,8 @@ struct ParameterDesc
size_t max_positionals = -1;
};
+template<> struct WithBitOps<ParameterDesc::Flags> : std::true_type {};
+
// ParametersParser provides tools to parse command parameters.
// There are 3 types of parameters:
// * unnamed options, which are accessed by position (ignoring named ones)
diff --git a/src/selectors.hh b/src/selectors.hh
index 3df24f26..3dc4e5ba 100644
--- a/src/selectors.hh
+++ b/src/selectors.hh
@@ -1,6 +1,7 @@
#ifndef selectors_hh_INCLUDED
#define selectors_hh_INCLUDED
+#include "flags.hh"
#include "selection.hh"
#include "buffer_utils.hh"
#include "unicode.hh"
@@ -152,10 +153,8 @@ enum class ObjectFlags
ToEnd = 2,
Inner = 4
};
-constexpr bool operator&(ObjectFlags lhs, ObjectFlags rhs)
-{ return (bool)((int)lhs & (int) rhs); }
-constexpr ObjectFlags operator|(ObjectFlags lhs, ObjectFlags rhs)
-{ return (ObjectFlags)((int)lhs | (int) rhs); }
+
+template<> struct WithBitOps<ObjectFlags> : std::true_type {};
template<WordType word_type>
Selection select_word(const Buffer& buffer,