From 3e797a3d152b59302365a7c11c5253292cd34ee7 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 23 Oct 2014 18:55:45 +0100 Subject: [PATCH] centralize bit operation support for enum used as flags --- src/buffer.hh | 30 +++-------------------- src/command_manager.hh | 11 +++------ src/flags.hh | 52 ++++++++++++++++++++++++++++++++++++++++ src/normal.cc | 11 +++------ src/option_manager.hh | 7 ++---- src/parameters_parser.hh | 11 +++------ src/selectors.hh | 7 +++--- 7 files changed, 69 insertions(+), 60 deletions(-) create mode 100644 src/flags.hh 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 : 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 : 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 + +namespace Kakoune +{ + +template +struct WithBitOps : std::false_type {}; + +template +using EnumStorageType = typename std::underlying_type::type; + +template +using EnableIfWithBitOps = typename std::enable_if::value>::type; + +template> +constexpr Flags operator|(Flags lhs, Flags rhs) +{ + return (Flags)((EnumStorageType) lhs | (EnumStorageType) rhs); +} + +template> +Flags& operator|=(Flags& lhs, Flags rhs) +{ + (EnumStorageType&) lhs |= (EnumStorageType) rhs; + return lhs; +} + +template> +constexpr bool operator&(Flags lhs, Flags rhs) +{ + return ((EnumStorageType) lhs & (EnumStorageType) rhs) != 0; +} + +template> +Flags& operator&=(Flags& lhs, Flags rhs) +{ + (EnumStorageType&) lhs &= (EnumStorageType) rhs; + return lhs; +} + +template> +constexpr Flags operator~(Flags lhs) +{ + return (Flags)(~(EnumStorageType)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 : std::true_type {}; template 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 : 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 : 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 : std::true_type {}; template Selection select_word(const Buffer& buffer,