From 7eaa05845043420c8f8c58932667b71b6784714a Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 15 Mar 2017 18:28:32 +0000 Subject: [PATCH] Move enum/flags option functions to option_types.hh --- src/enum.hh | 64 --------------------------------------------- src/option_types.hh | 60 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 60 insertions(+), 64 deletions(-) diff --git a/src/enum.hh b/src/enum.hh index 56242508..ce3adb33 100644 --- a/src/enum.hh +++ b/src/enum.hh @@ -1,11 +1,7 @@ #ifndef enum_hh_INCLUDED #define enum_hh_INCLUDED -#include "flags.hh" #include "string.hh" -#include "exception.hh" -#include "containers.hh" -#include "meta.hh" namespace Kakoune { @@ -23,66 +19,6 @@ struct Array template struct EnumDesc { T value; StringView name; }; -template{}))> -EnableIfWithBitOps option_to_string(Flags flags) -{ - constexpr auto desc = enum_desc(Meta::Type{}); - String res; - for (int i = 0; i < desc.size(); ++i) - { - if (not (flags & desc[i].value)) - continue; - if (not res.empty()) - res += "|"; - res += desc[i].name; - } - return res; -} - -template{}))> -EnableIfWithoutBitOps option_to_string(Enum e) -{ - constexpr auto desc = enum_desc(Meta::Type{}); - auto it = find_if(desc, [e](const EnumDesc& d) { return d.value == e; }); - if (it != desc.end()) - return it->name.str(); - kak_assert(false); - return {}; -} - -template{}))> -EnableIfWithBitOps option_from_string(StringView str, Flags& flags) -{ - constexpr auto desc = enum_desc(Meta::Type{}); - flags = Flags{}; - for (auto s : str | split('|')) - { - auto it = find_if(desc, [s](const EnumDesc& d) { return d.name == s; }); - if (it == desc.end()) - throw runtime_error(format("invalid flag value '{}'", s)); - flags |= it->value; - } -} - -template{}))> -EnableIfWithoutBitOps option_from_string(StringView str, Enum& e) -{ - constexpr auto desc = enum_desc(Meta::Type{}); - auto it = find_if(desc, [str](const EnumDesc& d) { return d.name == str; }); - if (it == desc.end()) - throw runtime_error(format("invalid enum value '{}'", str)); - e = it->value; -} - -template{}))> -EnableIfWithBitOps option_add(Flags& opt, StringView str) -{ - Flags res = Flags{}; - option_from_string(str, res); - opt |= res; - return res != (Flags)0; -} - } #endif // enum_hh_INCLUDED diff --git a/src/option_types.hh b/src/option_types.hh index 96af9f70..0193c821 100644 --- a/src/option_types.hh +++ b/src/option_types.hh @@ -254,6 +254,66 @@ constexpr Array, 4> enum_desc(Meta::Type) } }; } +template{}))> +EnableIfWithBitOps option_to_string(Flags flags) +{ + constexpr auto desc = enum_desc(Meta::Type{}); + String res; + for (int i = 0; i < desc.size(); ++i) + { + if (not (flags & desc[i].value)) + continue; + if (not res.empty()) + res += "|"; + res += desc[i].name; + } + return res; +} + +template{}))> +EnableIfWithoutBitOps option_to_string(Enum e) +{ + constexpr auto desc = enum_desc(Meta::Type{}); + auto it = find_if(desc, [e](const EnumDesc& d) { return d.value == e; }); + if (it != desc.end()) + return it->name.str(); + kak_assert(false); + return {}; +} + +template{}))> +EnableIfWithBitOps option_from_string(StringView str, Flags& flags) +{ + constexpr auto desc = enum_desc(Meta::Type{}); + flags = Flags{}; + for (auto s : str | split('|')) + { + auto it = find_if(desc, [s](const EnumDesc& d) { return d.name == s; }); + if (it == desc.end()) + throw runtime_error(format("invalid flag value '{}'", s)); + flags |= it->value; + } +} + +template{}))> +EnableIfWithoutBitOps option_from_string(StringView str, Enum& e) +{ + constexpr auto desc = enum_desc(Meta::Type{}); + auto it = find_if(desc, [str](const EnumDesc& d) { return d.name == str; }); + if (it == desc.end()) + throw runtime_error(format("invalid enum value '{}'", str)); + e = it->value; +} + +template{}))> +EnableIfWithBitOps option_add(Flags& opt, StringView str) +{ + Flags res = Flags{}; + option_from_string(str, res); + opt |= res; + return res != (Flags)0; +} + template struct PrefixedList {