centralize bit operation support for enum used as flags

This commit is contained in:
Maxime Coste 2014-10-23 18:55:45 +01:00
parent 5eb8989192
commit 3e797a3d15
7 changed files with 69 additions and 60 deletions

View File

@ -2,6 +2,7 @@
#define buffer_hh_INCLUDED #define buffer_hh_INCLUDED
#include "coord.hh" #include "coord.hh"
#include "flags.hh"
#include "hook_manager.hh" #include "hook_manager.hh"
#include "option_manager.hh" #include "option_manager.hh"
#include "keymap_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 // Values are just data holding by the buffer, so it is part of its
// observable state // observable state
mutable ValueMap m_values; 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" #include "buffer.inl.hh"

View File

@ -3,6 +3,7 @@
#include "coord.hh" #include "coord.hh"
#include "completion.hh" #include "completion.hh"
#include "flags.hh"
#include "memoryview.hh" #include "memoryview.hh"
#include "shell_manager.hh" #include "shell_manager.hh"
#include "parameters_parser.hh" #include "parameters_parser.hh"
@ -28,14 +29,8 @@ enum class CommandFlags
None = 0, None = 0,
Hidden = 1, Hidden = 1,
}; };
constexpr CommandFlags operator|(CommandFlags lhs, CommandFlags rhs)
{ template<> struct WithBitOps<CommandFlags> : std::true_type {};
return (CommandFlags)((int)lhs | (int)rhs);
}
constexpr bool operator&(CommandFlags lhs, CommandFlags rhs)
{
return (bool)((int)lhs & (int)rhs);
}
class PerArgumentCommandCompleter class PerArgumentCommandCompleter
{ {

52
src/flags.hh Normal file
View File

@ -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

View File

@ -8,6 +8,7 @@
#include "context.hh" #include "context.hh"
#include "debug.hh" #include "debug.hh"
#include "face_registry.hh" #include "face_registry.hh"
#include "flags.hh"
#include "file.hh" #include "file.hh"
#include "option_manager.hh" #include "option_manager.hh"
#include "register_manager.hh" #include "register_manager.hh"
@ -956,14 +957,8 @@ enum class SelectFlags
Inclusive = 2, Inclusive = 2,
Extend = 4 Extend = 4
}; };
constexpr SelectFlags operator|(SelectFlags lhs, SelectFlags rhs)
{ template<> struct WithBitOps<SelectFlags> : std::true_type {};
return (SelectFlags)((int) lhs | (int) rhs);
}
constexpr bool operator&(SelectFlags lhs, SelectFlags rhs)
{
return ((int) lhs & (int) rhs) != 0;
}
template<SelectFlags flags> template<SelectFlags flags>
void select_to_next_char(Context& context, int param) void select_to_next_char(Context& context, int param)

View File

@ -3,6 +3,7 @@
#include "completion.hh" #include "completion.hh"
#include "exception.hh" #include "exception.hh"
#include "flags.hh"
#include "option_types.hh" #include "option_types.hh"
#include "utils.hh" #include "utils.hh"
#include "regex.hh" #include "regex.hh"
@ -25,12 +26,8 @@ enum class OptionFlags
None = 0, None = 0,
Hidden = 1, 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 class OptionDesc
{ {

View File

@ -4,6 +4,7 @@
#include "exception.hh" #include "exception.hh"
#include "id_map.hh" #include "id_map.hh"
#include "memoryview.hh" #include "memoryview.hh"
#include "flags.hh"
#include "string.hh" #include "string.hh"
namespace Kakoune namespace Kakoune
@ -51,14 +52,6 @@ struct ParameterDesc
SwitchesOnlyAtStart = 1, SwitchesOnlyAtStart = 1,
SwitchesAsPositional = 2, 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() = default;
ParameterDesc(SwitchMap switches, Flags flags = Flags::None, ParameterDesc(SwitchMap switches, Flags flags = Flags::None,
@ -72,6 +65,8 @@ struct ParameterDesc
size_t max_positionals = -1; size_t max_positionals = -1;
}; };
template<> struct WithBitOps<ParameterDesc::Flags> : std::true_type {};
// ParametersParser provides tools to parse command parameters. // ParametersParser provides tools to parse command parameters.
// There are 3 types of parameters: // There are 3 types of parameters:
// * unnamed options, which are accessed by position (ignoring named ones) // * unnamed options, which are accessed by position (ignoring named ones)

View File

@ -1,6 +1,7 @@
#ifndef selectors_hh_INCLUDED #ifndef selectors_hh_INCLUDED
#define selectors_hh_INCLUDED #define selectors_hh_INCLUDED
#include "flags.hh"
#include "selection.hh" #include "selection.hh"
#include "buffer_utils.hh" #include "buffer_utils.hh"
#include "unicode.hh" #include "unicode.hh"
@ -152,10 +153,8 @@ enum class ObjectFlags
ToEnd = 2, ToEnd = 2,
Inner = 4 Inner = 4
}; };
constexpr bool operator&(ObjectFlags lhs, ObjectFlags rhs)
{ return (bool)((int)lhs & (int) rhs); } template<> struct WithBitOps<ObjectFlags> : std::true_type {};
constexpr ObjectFlags operator|(ObjectFlags lhs, ObjectFlags rhs)
{ return (ObjectFlags)((int)lhs | (int) rhs); }
template<WordType word_type> template<WordType word_type>
Selection select_word(const Buffer& buffer, Selection select_word(const Buffer& buffer,