2013-03-26 00:14:38 +01:00
|
|
|
#ifndef option_types_hh_INCLUDED
|
|
|
|
#define option_types_hh_INCLUDED
|
|
|
|
|
2013-04-09 20:05:40 +02:00
|
|
|
#include "exception.hh"
|
2013-03-26 00:14:38 +01:00
|
|
|
#include "string.hh"
|
2013-03-29 19:31:06 +01:00
|
|
|
#include "units.hh"
|
2014-10-06 20:21:32 +02:00
|
|
|
#include "coord.hh"
|
2015-01-06 14:40:56 +01:00
|
|
|
#include "array_view.hh"
|
2015-09-16 20:04:19 +02:00
|
|
|
#include "id_map.hh"
|
2015-11-19 00:43:51 +01:00
|
|
|
#include "flags.hh"
|
2015-11-20 09:50:53 +01:00
|
|
|
#include "enum.hh"
|
2013-03-26 00:14:38 +01:00
|
|
|
|
2013-03-29 19:31:06 +01:00
|
|
|
#include <tuple>
|
|
|
|
#include <vector>
|
|
|
|
|
2013-03-26 00:14:38 +01:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2015-03-10 20:33:46 +01:00
|
|
|
inline String option_to_string(StringView opt) { return opt.str(); }
|
|
|
|
inline void option_from_string(StringView str, String& opt) { opt = str.str(); }
|
2015-12-12 12:26:34 +01:00
|
|
|
inline bool option_add(String& opt, StringView val) { opt += val; return not val.empty(); }
|
2013-03-26 13:47:14 +01:00
|
|
|
|
2013-05-13 14:23:07 +02:00
|
|
|
inline String option_to_string(int opt) { return to_string(opt); }
|
2014-10-07 22:11:55 +02:00
|
|
|
inline void option_from_string(StringView str, int& opt) { opt = str_to_int(str); }
|
2015-12-12 12:26:34 +01:00
|
|
|
inline bool option_add(int& opt, StringView str)
|
|
|
|
{
|
|
|
|
auto val = str_to_int(str);
|
|
|
|
opt += val;
|
|
|
|
return val != 0;
|
|
|
|
}
|
2013-03-26 13:47:14 +01:00
|
|
|
|
|
|
|
inline String option_to_string(bool opt) { return opt ? "true" : "false"; }
|
2014-10-07 22:11:55 +02:00
|
|
|
inline void option_from_string(StringView str, bool& opt)
|
2013-03-26 13:47:14 +01:00
|
|
|
{
|
|
|
|
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");
|
|
|
|
}
|
|
|
|
|
2015-03-30 14:33:46 +02:00
|
|
|
constexpr char list_separator = ':';
|
2013-04-02 18:56:09 +02:00
|
|
|
|
2015-01-12 14:58:41 +01:00
|
|
|
template<typename T, MemoryDomain domain>
|
|
|
|
String option_to_string(const Vector<T, domain>& opt)
|
2013-03-26 13:47:14 +01:00
|
|
|
{
|
|
|
|
String res;
|
|
|
|
for (size_t i = 0; i < opt.size(); ++i)
|
|
|
|
{
|
2013-07-24 22:41:13 +02:00
|
|
|
res += escape(option_to_string(opt[i]), list_separator, '\\');
|
2013-03-26 13:47:14 +01:00
|
|
|
if (i != opt.size() - 1)
|
2013-04-02 18:56:09 +02:00
|
|
|
res += list_separator;
|
2013-03-26 13:47:14 +01:00
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-01-12 14:58:41 +01:00
|
|
|
template<typename T, MemoryDomain domain>
|
|
|
|
void option_from_string(StringView str, Vector<T, domain>& opt)
|
2013-03-26 13:47:14 +01:00
|
|
|
{
|
|
|
|
opt.clear();
|
2015-01-09 14:57:21 +01:00
|
|
|
Vector<String> elems = split(str, list_separator, '\\');
|
2013-03-26 13:47:14 +01:00
|
|
|
for (auto& elem: elems)
|
|
|
|
{
|
|
|
|
T opt_elem;
|
|
|
|
option_from_string(elem, opt_elem);
|
|
|
|
opt.push_back(opt_elem);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-01-12 14:58:41 +01:00
|
|
|
template<typename T, MemoryDomain domain>
|
2015-12-12 12:26:34 +01:00
|
|
|
bool option_add(Vector<T, domain>& opt, StringView str)
|
2013-03-29 19:34:57 +01:00
|
|
|
{
|
2015-12-12 12:26:34 +01:00
|
|
|
Vector<T, domain> vec;
|
|
|
|
option_from_string(str, vec);
|
|
|
|
std::copy(std::make_move_iterator(vec.begin()),
|
|
|
|
std::make_move_iterator(vec.end()),
|
|
|
|
back_inserter(opt));
|
2013-03-29 19:34:57 +01:00
|
|
|
return not vec.empty();
|
|
|
|
}
|
|
|
|
|
2015-09-16 20:04:19 +02:00
|
|
|
template<typename Value, MemoryDomain domain>
|
|
|
|
String option_to_string(const IdMap<Value, domain>& opt)
|
2014-11-11 00:24:02 +01:00
|
|
|
{
|
|
|
|
String res;
|
|
|
|
for (auto it = begin(opt); it != end(opt); ++it)
|
|
|
|
{
|
|
|
|
if (it != begin(opt))
|
|
|
|
res += list_separator;
|
2015-09-16 20:57:57 +02:00
|
|
|
String elem = escape(option_to_string(it->key), '=', '\\') + "=" +
|
|
|
|
escape(option_to_string(it->value), '=', '\\');
|
2014-11-11 00:24:02 +01:00
|
|
|
res += escape(elem, list_separator, '\\');
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2015-09-16 20:04:19 +02:00
|
|
|
template<typename Value, MemoryDomain domain>
|
|
|
|
void option_from_string(StringView str, IdMap<Value, domain>& opt)
|
2014-11-11 00:24:02 +01:00
|
|
|
{
|
|
|
|
opt.clear();
|
2015-01-01 13:09:30 +01:00
|
|
|
for (auto& elem : split(str, list_separator, '\\'))
|
2014-11-11 00:24:02 +01:00
|
|
|
{
|
2015-01-09 14:57:21 +01:00
|
|
|
Vector<String> pair_str = split(elem, '=', '\\');
|
2014-11-11 00:24:02 +01:00
|
|
|
if (pair_str.size() != 2)
|
|
|
|
throw runtime_error("map option expects key=value");
|
2015-09-16 20:04:19 +02:00
|
|
|
String key;
|
|
|
|
Value value;
|
|
|
|
option_from_string(pair_str[0], key);
|
|
|
|
option_from_string(pair_str[1], value);
|
|
|
|
opt.append({ std::move(key), std::move(value) });
|
2014-11-11 00:24:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-08-23 13:13:14 +02:00
|
|
|
constexpr char tuple_separator = '|';
|
2013-03-26 13:47:14 +01:00
|
|
|
|
2013-03-29 19:31:06 +01:00
|
|
|
template<size_t I, typename... Types>
|
|
|
|
struct TupleOptionDetail
|
2013-03-26 00:14:38 +01:00
|
|
|
{
|
2013-03-29 19:31:06 +01:00
|
|
|
static String to_string(const std::tuple<Types...>& opt)
|
|
|
|
{
|
2013-04-02 18:56:09 +02:00
|
|
|
return TupleOptionDetail<I-1, Types...>::to_string(opt) +
|
2013-07-24 22:41:13 +02:00
|
|
|
tuple_separator + escape(option_to_string(std::get<I>(opt)), tuple_separator, '\\');
|
2013-03-29 19:31:06 +01:00
|
|
|
}
|
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
static void from_string(ConstArrayView<String> elems, std::tuple<Types...>& opt)
|
2013-03-29 19:31:06 +01:00
|
|
|
{
|
2013-04-02 18:56:09 +02:00
|
|
|
option_from_string(elems[I], std::get<I>(opt));
|
|
|
|
TupleOptionDetail<I-1, Types...>::from_string(elems, opt);
|
2013-03-29 19:31:06 +01:00
|
|
|
}
|
|
|
|
};
|
2013-03-26 00:14:38 +01:00
|
|
|
|
2013-03-29 19:31:06 +01: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 00:14:38 +01:00
|
|
|
|
2015-03-09 14:48:41 +01:00
|
|
|
static void from_string(ConstArrayView<String> elems, std::tuple<Types...>& opt)
|
2013-03-29 19:31:06 +01:00
|
|
|
{
|
2013-04-02 18:56:09 +02:00
|
|
|
option_from_string(elems[0], std::get<0>(opt));
|
2013-03-29 19:31:06 +01:00
|
|
|
}
|
2013-03-26 00:14:38 +01:00
|
|
|
};
|
|
|
|
|
2013-03-29 19:31:06 +01: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>
|
2014-10-07 22:11:55 +02:00
|
|
|
void option_from_string(StringView str, std::tuple<Types...>& opt)
|
2013-03-29 19:31:06 +01:00
|
|
|
{
|
2013-07-24 22:41:13 +02:00
|
|
|
auto elems = split(str, tuple_separator, '\\');
|
2013-04-02 18:56:09 +02:00
|
|
|
if (elems.size() != sizeof...(Types))
|
2015-01-22 14:10:21 +01:00
|
|
|
throw runtime_error(elems.size() < sizeof...(Types) ?
|
|
|
|
"not enough elements in tuple"
|
|
|
|
: "to many elements in tuple");
|
2013-04-02 18:56:09 +02:00
|
|
|
TupleOptionDetail<sizeof...(Types)-1, Types...>::from_string(elems, opt);
|
2013-03-29 19:31:06 +01:00
|
|
|
}
|
|
|
|
|
2013-05-13 14:23:07 +02:00
|
|
|
template<typename RealType, typename ValueType>
|
|
|
|
inline String option_to_string(const StronglyTypedNumber<RealType, ValueType>& opt)
|
|
|
|
{
|
|
|
|
return to_string(opt);
|
|
|
|
}
|
2013-03-29 19:31:06 +01:00
|
|
|
|
2013-05-13 14:23:07 +02:00
|
|
|
template<typename RealType, typename ValueType>
|
2014-10-07 22:11:55 +02:00
|
|
|
inline void option_from_string(StringView str, StronglyTypedNumber<RealType, ValueType>& opt)
|
2013-03-29 19:31:06 +01:00
|
|
|
{
|
2013-05-17 14:09:42 +02:00
|
|
|
opt = StronglyTypedNumber<RealType, ValueType>{str_to_int(str)};
|
2013-03-29 19:31:06 +01:00
|
|
|
}
|
2013-03-26 00:14:38 +01:00
|
|
|
|
2013-05-13 14:23:07 +02:00
|
|
|
template<typename RealType, typename ValueType>
|
2013-03-29 19:34:57 +01:00
|
|
|
inline bool option_add(StronglyTypedNumber<RealType, ValueType>& opt,
|
2015-12-12 12:26:34 +01:00
|
|
|
StringView str)
|
2013-03-29 19:34:57 +01:00
|
|
|
{
|
2015-12-12 12:26:34 +01:00
|
|
|
int val = str_to_int(str);
|
2013-05-06 13:52:20 +02:00
|
|
|
opt += val;
|
|
|
|
return val != 0;
|
2013-03-29 19:34:57 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
2015-12-12 12:26:34 +01:00
|
|
|
bool option_add(T&, StringView str)
|
2013-03-29 19:34:57 +01:00
|
|
|
{
|
|
|
|
throw runtime_error("no add operation supported for this option type");
|
|
|
|
}
|
|
|
|
|
2014-10-06 20:21:32 +02:00
|
|
|
template<typename EffectiveType, typename LineType, typename ColumnType>
|
2014-10-07 22:11:55 +02:00
|
|
|
inline void option_from_string(StringView str, LineAndColumn<EffectiveType, LineType, ColumnType>& opt)
|
2014-10-06 20:21:32 +02:00
|
|
|
{
|
2015-08-23 13:13:14 +02:00
|
|
|
auto vals = split(str, ',');
|
2014-10-06 20:21:32 +02:00
|
|
|
if (vals.size() != 2)
|
2015-08-23 13:13:14 +02:00
|
|
|
throw runtime_error("expected <line>,<column>");
|
2014-10-06 20:21:32 +02:00
|
|
|
opt.line = str_to_int(vals[0]);
|
|
|
|
opt.column = str_to_int(vals[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename EffectiveType, typename LineType, typename ColumnType>
|
|
|
|
inline String option_to_string(const LineAndColumn<EffectiveType, LineType, ColumnType>& opt)
|
|
|
|
{
|
2015-08-23 13:13:14 +02:00
|
|
|
return format("{},{}", opt.line, opt.column);
|
2014-10-06 20:21:32 +02:00
|
|
|
}
|
|
|
|
|
2015-11-19 22:58:26 +01:00
|
|
|
enum class DebugFlags
|
|
|
|
{
|
|
|
|
None = 0,
|
|
|
|
Hooks = 1 << 0,
|
|
|
|
Shell = 1 << 1,
|
2015-11-21 13:11:19 +01:00
|
|
|
Profile = 1 << 2,
|
2015-11-19 22:58:26 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct WithBitOps<DebugFlags> : std::true_type {};
|
|
|
|
|
2015-11-21 13:11:19 +01:00
|
|
|
constexpr Array<EnumDesc<DebugFlags>, 3> enum_desc(DebugFlags)
|
2015-11-19 22:58:26 +01:00
|
|
|
{
|
2015-11-20 09:50:53 +01:00
|
|
|
return { {
|
|
|
|
{ DebugFlags::Hooks, "hooks" },
|
2015-11-21 13:11:19 +01:00
|
|
|
{ DebugFlags::Shell, "shell" },
|
|
|
|
{ DebugFlags::Profile, "profile" }
|
2015-11-20 09:50:53 +01:00
|
|
|
} };
|
|
|
|
}
|
2015-11-19 00:43:51 +01:00
|
|
|
|
2013-03-26 00:14:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif // option_types_hh_INCLUDED
|