summaryrefslogtreecommitdiff
path: root/src/option_types.hh
diff options
context:
space:
mode:
authorMaxime Coste <mawww@kakoune.org>2017-03-15 17:42:02 +0000
committerMaxime Coste <mawww@kakoune.org>2017-03-15 17:42:02 +0000
commita49e175727928b8b45c0c2ccdb01f143ea6d18c2 (patch)
tree88c1c3909c2ab06394a63af5ebab8fdcd0a389f0 /src/option_types.hh
parenta88e58763bd33e021511d2e821703f478afd85bf (diff)
Migrate to a more value based meta programming model
Introduce Meta::Type<T> to store a type as value, and pass it around, migrate enum_desc and option_type_name to this.
Diffstat (limited to 'src/option_types.hh')
-rw-r--r--src/option_types.hh44
1 files changed, 20 insertions, 24 deletions
diff --git a/src/option_types.hh b/src/option_types.hh
index d9dc7db7..ab2c35fa 100644
--- a/src/option_types.hh
+++ b/src/option_types.hh
@@ -16,25 +16,22 @@
namespace Kakoune
{
-template<typename T, typename = void> struct option_type_name;
-template<typename T> using void_t = void;
+template<typename T> using valid = std::true_type;
template<typename T>
-struct option_type_name<T, void_t<decltype(T::option_type_name)>>
+constexpr decltype(T::option_type_name) option_type_name(Meta::Type<T>)
{
- static decltype(T::option_type_name) name() { return T::option_type_name; }
-};
+ return T::option_type_name;
+}
template<typename Enum>
-struct option_type_name<Enum, typename std::enable_if<std::is_enum<Enum>::value>::type>
+typename std::enable_if<std::is_enum<Enum>::value, String>::type
+option_type_name(Meta::Type<Enum>)
{
- static String name()
- {
- constexpr StringView type = WithBitOps<Enum>::value ? "flags" : "enum";
- auto name = enum_desc(Enum{});
- return type + "(" + join(name | transform(std::mem_fn(&EnumDesc<Enum>::name)), '|') + ")";
- }
-};
+ constexpr StringView type = WithBitOps<Enum>::value ? "flags" : "enum";
+ auto name = enum_desc(Meta::Type<Enum>{});
+ return type + "(" + join(name | transform(std::mem_fn(&EnumDesc<Enum>::name)), '|') + ")";
+}
inline String option_to_string(int opt) { return to_string(opt); }
inline void option_from_string(StringView str, int& opt) { opt = str_to_int(str); }
@@ -44,7 +41,7 @@ inline bool option_add(int& opt, StringView str)
opt += val;
return val != 0;
}
-template<> struct option_type_name<int> { static StringView name() { return "int"; } };
+inline StringView option_type_name(Meta::Type<int>) { return "int"; }
inline String option_to_string(size_t opt) { return to_string(opt); }
inline void option_from_string(StringView str, size_t& opt) { opt = str_to_int(str); }
@@ -59,7 +56,7 @@ inline void option_from_string(StringView str, bool& opt)
else
throw runtime_error("boolean values are either true, yes, false or no");
}
-template<> struct option_type_name<bool> { static StringView name() { return "bool"; } };
+inline StringView option_type_name(Meta::Type<bool>) { return "bool"; }
constexpr char list_separator = ':';
@@ -101,10 +98,10 @@ bool option_add(Vector<T, domain>& opt, StringView str)
}
template<typename T, MemoryDomain D>
-struct option_type_name<Vector<T, D>>
+String option_type_name(Meta::Type<Vector<T, D>>)
{
- static String name() { return option_type_name<T>::name() + StringView{"-list"}; }
-};
+ return option_type_name(Meta::Type<T>{}) + StringView{"-list"};
+}
template<typename Key, typename Value, MemoryDomain domain>
String option_to_string(const HashMap<Key, Value, domain>& opt)
@@ -139,12 +136,11 @@ void option_from_string(StringView str, HashMap<Key, Value, domain>& opt)
}
template<typename K, typename V, MemoryDomain D>
-struct option_type_name<HashMap<K, V, D>>
+String option_type_name(Meta::Type<HashMap<K, V, D>>)
{
- static String name() { return format("{}-to-{}-map",
- option_type_name<K>::name(),
- option_type_name<V>::name()); }
-};
+ return format("{}-to-{}-map", option_type_name(Meta::Type<K>{}),
+ option_type_name(Meta::Type<V>{}));
+}
constexpr char tuple_separator = '|';
@@ -251,7 +247,7 @@ enum class DebugFlags
template<>
struct WithBitOps<DebugFlags> : std::true_type {};
-constexpr Array<EnumDesc<DebugFlags>, 4> enum_desc(DebugFlags)
+constexpr Array<EnumDesc<DebugFlags>, 4> enum_desc(Meta::Type<DebugFlags>)
{
return { {
{ DebugFlags::Hooks, "hooks" },