#ifndef option_types_hh_INCLUDED #define option_types_hh_INCLUDED #include "string.hh" #include "units.hh" #include "exception.hh" #include #include namespace Kakoune { inline String option_to_string(const String& opt) { return opt; } inline void option_from_string(const String& str, String& opt) { opt = str; } inline String option_to_string(int opt) { return int_to_str(opt); } inline void option_from_string(const String& str, int& opt) { opt = str_to_int(str); } inline bool option_add(int& opt, int val) { opt += val; return val != 0; } 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"); } template String option_to_string(const std::vector& opt) { String res; for (size_t i = 0; i < opt.size(); ++i) { res += option_to_string(opt[i]); if (i != opt.size() - 1) res += ','; } return res; } template void option_from_string(const String& str, std::vector& opt) { opt.clear(); std::vector elems = split(str, ','); for (auto& elem: elems) { T opt_elem; option_from_string(elem, opt_elem); opt.push_back(opt_elem); } } template bool option_add(std::vector& opt, const std::vector& vec) { std::copy(vec.begin(), vec.end(), back_inserter(opt)); return not vec.empty(); } template struct TupleOptionDetail { static String to_string(const std::tuple& opt) { return TupleOptionDetail::to_string(opt) + ":" + option_to_string(std::get(opt)); } static void from_string(const String& str, std::tuple& opt) { auto it = str.begin(); auto end = str.end(); for (size_t i = 0; i < I; ++i) it = std::find(it+1, end, ':'); if (it == end) throw runtime_error("not enough elements in tuple"); option_from_string(String{it+1, std::find(it+1, end, ':')}, std::get(opt)); TupleOptionDetail::from_string(str, opt); } }; template struct TupleOptionDetail<0, Types...> { static String to_string(const std::tuple& opt) { return option_to_string(std::get<0>(opt)); } static void from_string(const String& str, std::tuple& opt) { option_from_string(String{str.begin(), std::find(str.begin(), str.end(), ':')}, std::get<0>(opt)); } }; template String option_to_string(const std::tuple& opt) { return TupleOptionDetail::to_string(opt); } template void option_from_string(const String& str, std::tuple& opt) { TupleOptionDetail::from_string(str, opt); } template inline String option_to_string(const StronglyTypedNumber& opt) { return int_to_str((int)opt); } template inline void option_from_string(const String& str, StronglyTypedNumber& opt) { opt = StronglyTypedNumber{str_to_int(str)}; } template inline bool option_add(StronglyTypedNumber& opt, StronglyTypedNumber val) { opt += val; return val != 0; } template bool option_add(T&, const T&) { throw runtime_error("no add operation supported for this option type"); } } #endif // option_types_hh_INCLUDED