diff options
| author | Maxime Coste <frrrwww@gmail.com> | 2014-10-23 18:55:45 +0100 |
|---|---|---|
| committer | Maxime Coste <frrrwww@gmail.com> | 2014-10-23 19:02:39 +0100 |
| commit | 3e797a3d152b59302365a7c11c5253292cd34ee7 (patch) | |
| tree | b9f9423e3216e38221272d8d1ca80215782ebde8 /src | |
| parent | 5eb8989192445b0505933fcdb1c4f489d4bdc7f9 (diff) | |
centralize bit operation support for enum used as flags
Diffstat (limited to 'src')
| -rw-r--r-- | src/buffer.hh | 30 | ||||
| -rw-r--r-- | src/command_manager.hh | 11 | ||||
| -rw-r--r-- | src/flags.hh | 52 | ||||
| -rw-r--r-- | src/normal.cc | 11 | ||||
| -rw-r--r-- | src/option_manager.hh | 7 | ||||
| -rw-r--r-- | src/parameters_parser.hh | 11 | ||||
| -rw-r--r-- | src/selectors.hh | 7 |
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, |
