2015-11-20 09:50:53 +01:00
|
|
|
#ifndef enum_hh_INCLUDED
|
|
|
|
#define enum_hh_INCLUDED
|
|
|
|
|
|
|
|
#include "flags.hh"
|
|
|
|
#include "string.hh"
|
|
|
|
#include "exception.hh"
|
|
|
|
#include "containers.hh"
|
|
|
|
|
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
|
|
|
template<typename T, size_t N>
|
|
|
|
struct Array
|
|
|
|
{
|
|
|
|
constexpr size_t size() const { return N; }
|
|
|
|
constexpr const T& operator[](int i) const { return m_data[i]; }
|
|
|
|
constexpr const T* begin() const { return m_data; }
|
|
|
|
constexpr const T* end() const { return m_data+N; }
|
|
|
|
|
|
|
|
T m_data[N];
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> struct EnumDesc { T value; StringView name; };
|
|
|
|
|
|
|
|
template<typename Flags, typename = decltype(enum_desc(Flags{}))>
|
|
|
|
EnableIfWithBitOps<Flags, String> option_to_string(Flags flags)
|
|
|
|
{
|
|
|
|
constexpr auto desc = enum_desc(Flags{});
|
|
|
|
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<typename Enum, typename = decltype(enum_desc(Enum{}))>
|
|
|
|
EnableIfWithoutBitOps<Enum, String> option_to_string(Enum e)
|
|
|
|
{
|
|
|
|
constexpr auto desc = enum_desc(Enum{});
|
|
|
|
auto it = find_if(desc, [e](const EnumDesc<Enum>& d) { return d.value == e; });
|
2016-02-29 21:15:23 +01:00
|
|
|
if (it != desc.end())
|
|
|
|
return it->name.str();
|
|
|
|
kak_assert(false);
|
|
|
|
return {};
|
2015-11-20 09:50:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Flags, typename = decltype(enum_desc(Flags{}))>
|
|
|
|
EnableIfWithBitOps<Flags> option_from_string(StringView str, Flags& flags)
|
|
|
|
{
|
|
|
|
constexpr auto desc = enum_desc(Flags{});
|
|
|
|
flags = Flags{};
|
2016-03-25 01:14:56 +01:00
|
|
|
for (auto s : str | split<StringView>('|'))
|
2015-11-20 09:50:53 +01:00
|
|
|
{
|
|
|
|
auto it = find_if(desc, [s](const EnumDesc<Flags>& d) { return d.name == s; });
|
|
|
|
if (it == desc.end())
|
|
|
|
throw runtime_error(format("invalid flag value '{}'", s));
|
|
|
|
flags |= it->value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename Enum, typename = decltype(enum_desc(Enum{}))>
|
|
|
|
EnableIfWithoutBitOps<Enum> option_from_string(StringView str, Enum& e)
|
|
|
|
{
|
|
|
|
constexpr auto desc = enum_desc(Enum{});
|
|
|
|
auto it = find_if(desc, [str](const EnumDesc<Enum>& d) { return d.name == str; });
|
2016-02-28 19:30:35 +01:00
|
|
|
if (it == desc.end())
|
|
|
|
throw runtime_error(format("invalid enum value '{}'", str));
|
2015-11-20 09:50:53 +01:00
|
|
|
e = it->value;
|
|
|
|
}
|
|
|
|
|
2016-12-27 22:52:53 +01:00
|
|
|
template<typename Flags, typename = decltype(enum_desc(Flags{}))>
|
|
|
|
EnableIfWithBitOps<Flags, bool> option_add(Flags& opt, StringView str)
|
|
|
|
{
|
|
|
|
Flags res = Flags{};
|
|
|
|
option_from_string(str, res);
|
|
|
|
opt |= res;
|
|
|
|
return res != (Flags)0;
|
|
|
|
}
|
|
|
|
|
2015-11-20 09:50:53 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // enum_hh_INCLUDED
|