2013-03-26 03:14:38 +04:00
|
|
|
#ifndef option_types_hh_INCLUDED
|
|
|
|
#define option_types_hh_INCLUDED
|
|
|
|
|
2013-04-09 22:05:40 +04:00
|
|
|
#include "exception.hh"
|
2013-03-26 03:14:38 +04:00
|
|
|
#include "string.hh"
|
2013-03-29 22:31:06 +04:00
|
|
|
#include "units.hh"
|
2013-03-26 03:14:38 +04:00
|
|
|
|
2013-03-29 22:31:06 +04:00
|
|
|
#include <tuple>
|
|
|
|
#include <vector>
|
2013-05-06 15:52:20 +04:00
|
|
|
#include <unordered_set>
|
2013-03-29 22:31:06 +04:00
|
|
|
|
2013-03-26 03:14:38 +04:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2013-03-26 16:47:14 +04:00
|
|
|
inline String option_to_string(const String& opt) { return opt; }
|
|
|
|
inline void option_from_string(const String& str, String& opt) { opt = str; }
|
|
|
|
|
2013-05-13 16:23:07 +04:00
|
|
|
inline String option_to_string(int opt) { return to_string(opt); }
|
2013-05-17 16:09:42 +04:00
|
|
|
inline void option_from_string(const String& str, int& opt) { opt = str_to_int(str); }
|
2013-03-29 22:34:57 +04:00
|
|
|
inline bool option_add(int& opt, int val) { opt += val; return val != 0; }
|
2013-03-26 16:47:14 +04:00
|
|
|
|
|
|
|
inline String option_to_string(bool opt) { return opt ? "true" : "false"; }
|
|
|
|
inline void option_from_string(const String& str, bool& opt)
|
|
|
|
{
|
|
|
|
if (str == "true" or str == "yes")
|
|
|
|
opt = true;
|
|
|
|
else if (str == "false" or str == "no")
|
|
|
|
opt = false;
|
|
|
|
else
|
|
|
|
throw runtime_error("boolean values are either true, yes, false or no");
|
|
|
|
}
|
|
|
|
|
2013-07-25 00:41:41 +04:00
|
|
|
constexpr Codepoint list_separator = ':';
|
2013-04-02 20:56:09 +04:00
|
|
|
|
2013-03-26 16:47:14 +04:00
|
|
|
template<typename T>
|
|
|
|
String option_to_string(const std::vector<T>& opt)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
for (size_t i = 0; i < opt.size(); ++i)
|
|
|
|
{
|
2013-07-25 00:41:13 +04:00
|
|
|
res += escape(option_to_string(opt[i]), list_separator, '\\');
|
2013-03-26 16:47:14 +04:00
|
|
|
if (i != opt.size() - 1)
|
2013-04-02 20:56:09 +04:00
|
|
|
res += list_separator;
|
2013-03-26 16:47:14 +04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void option_from_string(const String& str, std::vector<T>& opt)
|
|
|
|
{
|
|
|
|
opt.clear();
|
2013-07-25 00:41:13 +04:00
|
|
|
std::vector<String> elems = split(str, list_separator, '\\');
|
2013-03-26 16:47:14 +04:00
|
|
|
for (auto& elem: elems)
|
|
|
|
{
|
|
|
|
T opt_elem;
|
|
|
|
option_from_string(elem, opt_elem);
|
|
|
|
opt.push_back(opt_elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-03-29 22:34:57 +04:00
|
|
|
template<typename T>
|
|
|
|
bool option_add(std::vector<T>& opt, const std::vector<T>& vec)
|
|
|
|
{
|
|
|
|
std::copy(vec.begin(), vec.end(), back_inserter(opt));
|
|
|
|
return not vec.empty();
|
|
|
|
}
|
|
|
|
|
2013-05-06 15:52:20 +04:00
|
|
|
template<typename T>
|
|
|
|
String option_to_string(const std::unordered_set<T>& opt)
|
|
|
|
{
|
|
|
|
String res;
|
|
|
|
for (auto it = begin(opt); it != end(opt); ++it)
|
|
|
|
{
|
|
|
|
if (it != begin(opt))
|
|
|
|
res += list_separator;
|
2013-07-25 00:41:13 +04:00
|
|
|
res += escape(option_to_string(*it), list_separator, '\\');
|
2013-05-06 15:52:20 +04:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
void option_from_string(const String& str, std::unordered_set<T>& opt)
|
|
|
|
{
|
|
|
|
opt.clear();
|
2013-07-25 00:41:13 +04:00
|
|
|
std::vector<String> elems = split(str, list_separator, '\\');
|
2013-05-06 15:52:20 +04:00
|
|
|
for (auto& elem: elems)
|
|
|
|
{
|
|
|
|
T opt_elem;
|
|
|
|
option_from_string(elem, opt_elem);
|
|
|
|
opt.insert(opt_elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool option_add(std::unordered_set<T>& opt, const std::unordered_set<T>& set)
|
|
|
|
{
|
|
|
|
std::copy(set.begin(), set.end(), std::inserter(opt, opt.begin()));
|
|
|
|
return not set.empty();
|
|
|
|
}
|
|
|
|
|
2013-04-02 20:56:09 +04:00
|
|
|
constexpr Codepoint tuple_separator = '|';
|
2013-03-26 16:47:14 +04:00
|
|
|
|
2013-03-29 22:31:06 +04:00
|
|
|
template<size_t I, typename... Types>
|
|
|
|
struct TupleOptionDetail
|
2013-03-26 03:14:38 +04:00
|
|
|
{
|
2013-03-29 22:31:06 +04:00
|
|
|
static String to_string(const std::tuple<Types...>& opt)
|
|
|
|
{
|
2013-04-02 20:56:09 +04:00
|
|
|
return TupleOptionDetail<I-1, Types...>::to_string(opt) +
|
2013-07-25 00:41:13 +04:00
|
|
|
tuple_separator + escape(option_to_string(std::get<I>(opt)), tuple_separator, '\\');
|
2013-03-29 22:31:06 +04:00
|
|
|
}
|
|
|
|
|
2013-07-26 03:17:12 +04:00
|
|
|
static void from_string(memoryview<String> elems, std::tuple<Types...>& opt)
|
2013-03-29 22:31:06 +04:00
|
|
|
{
|
2013-04-02 20:56:09 +04:00
|
|
|
option_from_string(elems[I], std::get<I>(opt));
|
|
|
|
TupleOptionDetail<I-1, Types...>::from_string(elems, opt);
|
2013-03-29 22:31:06 +04:00
|
|
|
}
|
|
|
|
};
|
2013-03-26 03:14:38 +04:00
|
|
|
|
2013-03-29 22:31:06 +04:00
|
|
|
template<typename... Types>
|
|
|
|
struct TupleOptionDetail<0, Types...>
|
|
|
|
{
|
|
|
|
static String to_string(const std::tuple<Types...>& opt)
|
|
|
|
{
|
|
|
|
return option_to_string(std::get<0>(opt));
|
|
|
|
}
|
2013-03-26 03:14:38 +04:00
|
|
|
|
2013-07-26 03:17:12 +04:00
|
|
|
static void from_string(memoryview<String> elems, std::tuple<Types...>& opt)
|
2013-03-29 22:31:06 +04:00
|
|
|
{
|
2013-04-02 20:56:09 +04:00
|
|
|
option_from_string(elems[0], std::get<0>(opt));
|
2013-03-29 22:31:06 +04:00
|
|
|
}
|
2013-03-26 03:14:38 +04:00
|
|
|
};
|
|
|
|
|
2013-03-29 22:31:06 +04:00
|
|
|
template<typename... Types>
|
|
|
|
String option_to_string(const std::tuple<Types...>& opt)
|
|
|
|
{
|
|
|
|
return TupleOptionDetail<sizeof...(Types)-1, Types...>::to_string(opt);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename... Types>
|
|
|
|
void option_from_string(const String& str, std::tuple<Types...>& opt)
|
|
|
|
{
|
2013-07-25 00:41:13 +04:00
|
|
|
auto elems = split(str, tuple_separator, '\\');
|
2013-04-02 20:56:09 +04:00
|
|
|
if (elems.size() != sizeof...(Types))
|
|
|
|
throw runtime_error("not enough elements in tuple");
|
|
|
|
TupleOptionDetail<sizeof...(Types)-1, Types...>::from_string(elems, opt);
|
2013-03-29 22:31:06 +04:00
|
|
|
}
|
|
|
|
|
2013-05-13 16:23:07 +04:00
|
|
|
template<typename RealType, typename ValueType>
|
|
|
|
inline String option_to_string(const StronglyTypedNumber<RealType, ValueType>& opt)
|
|
|
|
{
|
|
|
|
return to_string(opt);
|
|
|
|
}
|
2013-03-29 22:31:06 +04:00
|
|
|
|
2013-05-13 16:23:07 +04:00
|
|
|
template<typename RealType, typename ValueType>
|
2013-03-29 22:31:06 +04:00
|
|
|
inline void option_from_string(const String& str, StronglyTypedNumber<RealType, ValueType>& opt)
|
|
|
|
{
|
2013-05-17 16:09:42 +04:00
|
|
|
opt = StronglyTypedNumber<RealType, ValueType>{str_to_int(str)};
|
2013-03-29 22:31:06 +04:00
|
|
|
}
|
2013-03-26 03:14:38 +04:00
|
|
|
|
2013-05-13 16:23:07 +04:00
|
|
|
template<typename RealType, typename ValueType>
|
2013-03-29 22:34:57 +04:00
|
|
|
inline bool option_add(StronglyTypedNumber<RealType, ValueType>& opt,
|
|
|
|
StronglyTypedNumber<RealType, ValueType> val)
|
|
|
|
{
|
2013-05-06 15:52:20 +04:00
|
|
|
opt += val;
|
|
|
|
return val != 0;
|
2013-03-29 22:34:57 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
bool option_add(T&, const T&)
|
|
|
|
{
|
|
|
|
throw runtime_error("no add operation supported for this option type");
|
|
|
|
}
|
|
|
|
|
2013-10-21 21:57:48 +04:00
|
|
|
enum YesNoAsk
|
|
|
|
{
|
|
|
|
Yes,
|
|
|
|
No,
|
|
|
|
Ask
|
|
|
|
};
|
|
|
|
|
|
|
|
inline String option_to_string(YesNoAsk opt)
|
|
|
|
{
|
|
|
|
switch (opt)
|
|
|
|
{
|
|
|
|
case Yes: return "yes";
|
|
|
|
case No: return "no";
|
|
|
|
case Ask: return "ask";
|
|
|
|
}
|
|
|
|
kak_assert(false);
|
|
|
|
return "ask";
|
|
|
|
}
|
|
|
|
|
|
|
|
inline void option_from_string(const String& str, YesNoAsk& opt)
|
|
|
|
{
|
|
|
|
if (str == "yes" or str == "true")
|
|
|
|
opt = Yes;
|
|
|
|
else if (str == "no" or str == "false")
|
|
|
|
opt = No;
|
|
|
|
else if (str == "ask")
|
|
|
|
opt = Ask;
|
2013-11-16 04:26:56 +04:00
|
|
|
else
|
|
|
|
throw runtime_error("invalid value '" + str + "', expected yes, no or ask");
|
2013-10-21 21:57:48 +04:00
|
|
|
}
|
|
|
|
|
2013-03-26 03:14:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // option_types_hh_INCLUDED
|