option_types.hh (13709B)
1 #ifndef option_types_hh_INCLUDED 2 #define option_types_hh_INCLUDED 3 4 #include "array_view.hh" 5 #include "coord.hh" 6 #include "exception.hh" 7 #include "flags.hh" 8 #include "hash_map.hh" 9 #include "option.hh" 10 #include "string.hh" 11 #include "string_utils.hh" 12 #include "format.hh" 13 #include "units.hh" 14 #include "ranges.hh" 15 16 #include <tuple> 17 18 namespace Kakoune 19 { 20 21 template<typename T> 22 String option_to_string(const T& value, Quoting) 23 requires std::is_same_v<decltype(option_to_string(std::declval<T>())), String> 24 { 25 return option_to_string(value); 26 } 27 28 template<typename T> 29 constexpr decltype(T::option_type_name) option_type_name(Meta::Type<T>) 30 { 31 return T::option_type_name; 32 } 33 34 template<typename Enum> requires std::is_enum_v<Enum> 35 String option_type_name(Meta::Type<Enum>) 36 { 37 return format("{}({})", with_bit_ops(Meta::Type<Enum>{}) ? "flags" : "enum", 38 join(enum_desc(Meta::Type<Enum>{}) | 39 transform(&EnumDesc<Enum>::name), '|')); 40 } 41 42 inline String option_to_string(int opt) { return to_string(opt); } 43 inline int option_from_string(Meta::Type<int>, StringView str) { return str_to_int(str); } 44 inline bool option_add(int& opt, StringView str) 45 { 46 auto val = str_to_int(str); 47 opt += val; 48 return val != 0; 49 } 50 inline bool option_remove(int& opt, StringView str) 51 { 52 auto val = str_to_int(str); 53 opt -= val; 54 return val != 0; 55 } 56 constexpr StringView option_type_name(Meta::Type<int>) { return "int"; } 57 58 inline String option_to_string(size_t opt) { return to_string(opt); } 59 inline size_t option_from_string(Meta::Type<size_t>, StringView str) { return str_to_int(str); } 60 61 inline String option_to_string(bool opt) { return opt ? "true" : "false"; } 62 inline bool option_from_string(Meta::Type<bool>, StringView str) 63 { 64 if (str == "true" or str == "yes") 65 return true; 66 else if (str == "false" or str == "no") 67 return false; 68 else 69 throw runtime_error("boolean values are either true, yes, false or no"); 70 } 71 constexpr StringView option_type_name(Meta::Type<bool>) { return "bool"; } 72 73 inline String option_to_string(Codepoint opt, Quoting quoting) { return quoter(quoting)(to_string(opt)); } 74 inline Codepoint option_from_string(Meta::Type<Codepoint>, StringView str) 75 { 76 if (str.char_length() != 1) 77 throw runtime_error{format("'{}' is not a single codepoint", str)}; 78 return str[0_char]; 79 } 80 constexpr StringView option_type_name(Meta::Type<Codepoint>) { return "codepoint"; } 81 82 template<typename T, MemoryDomain domain> 83 Vector<String> option_to_strings(const Vector<T, domain>& opt) 84 { 85 return opt | transform([](const T& t) { return option_to_string(t, Quoting::Raw); }) | gather<Vector<String>>(); 86 } 87 88 template<typename T, MemoryDomain domain> 89 String option_to_string(const Vector<T, domain>& opt, Quoting quoting) 90 { 91 return join(opt | transform([=](const T& t) { return option_to_string(t, quoting); }), ' ', false); 92 } 93 94 template<typename T, MemoryDomain domain> 95 void option_list_postprocess(Vector<T, domain>& opt) 96 {} 97 98 template<typename T, MemoryDomain domain> 99 Vector<T, domain> option_from_strings(Meta::Type<Vector<T, domain>>, ConstArrayView<String> strs) 100 { 101 auto res = strs | transform([](auto&& s) { return option_from_string(Meta::Type<T>{}, s); }) 102 | gather<Vector<T, domain>>(); 103 option_list_postprocess(res); 104 return res; 105 } 106 107 template<typename T, MemoryDomain domain> 108 bool option_add_from_strings(Vector<T, domain>& opt, ConstArrayView<String> strs) 109 { 110 auto vec = option_from_strings(Meta::Type<Vector<T, domain>>{}, strs); 111 opt.insert(opt.end(), 112 std::make_move_iterator(vec.begin()), 113 std::make_move_iterator(vec.end())); 114 option_list_postprocess(opt); 115 return not vec.empty(); 116 } 117 118 template<typename T, MemoryDomain domain> 119 bool option_remove_from_strings(Vector<T, domain>& opt, ConstArrayView<String> strs) 120 { 121 bool did_remove = false; 122 for (auto&& val : strs | transform([](auto&& s) { return option_from_string(Meta::Type<T>{}, s); })) 123 { 124 auto it = find(opt, val); 125 if (it == opt.end()) 126 continue; 127 opt.erase(it); 128 did_remove = true; 129 } 130 return did_remove; 131 } 132 133 template<typename T, MemoryDomain D> 134 String option_type_name(Meta::Type<Vector<T, D>>) 135 { 136 return option_type_name(Meta::Type<T>{}) + "-list"_sv; 137 } 138 139 template<typename Key, typename Value, MemoryDomain domain> 140 Vector<String> option_to_strings(const HashMap<Key, Value, domain>& opt) 141 { 142 return opt | transform([](auto&& item) { 143 return format("{}={}", 144 escape(option_to_string(item.key, Quoting::Raw), '=', '\\'), 145 escape(option_to_string(item.value, Quoting::Raw), '=', '\\')); 146 }) | gather<Vector<String>>(); 147 } 148 149 template<typename Key, typename Value, MemoryDomain domain> 150 String option_to_string(const HashMap<Key, Value, domain>& opt, Quoting quoting) 151 { 152 return join(opt | transform([=](auto&& item) { 153 return quoter(quoting)( 154 format("{}={}", 155 escape(option_to_string(item.key, Quoting::Raw), '=', '\\'), 156 escape(option_to_string(item.value, Quoting::Raw), '=', '\\'))); 157 }), ' ', false); 158 } 159 160 template<typename Key, typename Value, MemoryDomain domain> 161 bool option_add_from_strings(HashMap<Key, Value, domain>& opt, ConstArrayView<String> strs) 162 { 163 struct error : runtime_error { error(size_t) : runtime_error{"map option expects key=value"} {} }; 164 165 bool changed = false; 166 for (auto&& str : strs) 167 { 168 auto key_value = str | split<StringView>('=', '\\') 169 | transform(unescape<'=', '\\'>) 170 | static_gather<error, 2>(); 171 172 opt[option_from_string(Meta::Type<Key>{}, key_value[0])] = option_from_string(Meta::Type<Value>{}, key_value[1]); 173 changed = true; 174 } 175 return changed; 176 } 177 178 template<typename Key, typename Value, MemoryDomain domain> 179 bool option_remove_from_strings(HashMap<Key, Value, domain>& opt, ConstArrayView<String> strs) 180 { 181 struct error : runtime_error { error(size_t) : runtime_error{"map option expects key=value"} {} }; 182 183 bool changed = false; 184 for (auto&& str : strs) 185 { 186 auto key_value = str | split<StringView>('=', '\\') 187 | transform(unescape<'=', '\\'>) 188 | static_gather<error, 2>(); 189 190 if (auto it = opt.find(key_value[0]); it != opt.end() and (key_value[1].empty() or key_value[1] == it->value)) 191 { 192 opt.remove(it->key); 193 changed = true; 194 } 195 } 196 return changed; 197 } 198 199 template<typename Key, typename Value, MemoryDomain domain> 200 HashMap<Key, Value, domain> option_from_strings(Meta::Type<HashMap<Key, Value, domain>>, ConstArrayView<String> str) 201 { 202 HashMap<Key, Value, domain> res; 203 option_add_from_strings(res, str); 204 return res; 205 } 206 207 template<typename K, typename V, MemoryDomain D> 208 String option_type_name(Meta::Type<HashMap<K, V, D>>) 209 { 210 return format("{}-to-{}-map", option_type_name(Meta::Type<K>{}), 211 option_type_name(Meta::Type<V>{})); 212 } 213 214 constexpr char tuple_separator = '|'; 215 216 template<typename... Types, size_t... I> 217 String option_to_string_impl(Quoting quoting, const std::tuple<Types...>& opt, std::index_sequence<I...>) 218 { 219 return quoter(quoting)(join(make_array({option_to_string(std::get<I>(opt), Quoting::Raw)...}), tuple_separator)); 220 } 221 222 template<typename... Types> 223 String option_to_string(const std::tuple<Types...>& opt, Quoting quoting) 224 { 225 return option_to_string_impl(quoting, opt, std::make_index_sequence<sizeof...(Types)>()); 226 } 227 228 template<typename... Types, size_t... I> 229 std::tuple<Types...> option_from_string_impl(Meta::Type<std::tuple<Types...>>, StringView str, 230 std::index_sequence<I...>) 231 { 232 struct error : runtime_error 233 { 234 error(size_t i) : runtime_error{i < sizeof...(Types) ? 235 "not enough elements in tuple" 236 : "too many elements in tuple"} {} 237 }; 238 auto elems = str | split<StringView>(tuple_separator, '\\') 239 | transform(unescape<tuple_separator, '\\'>) 240 | static_gather<error, sizeof...(Types)>(); 241 return std::tuple<Types...>{option_from_string(Meta::Type<Types>{}, elems[I])...}; 242 } 243 244 template<typename... Types> 245 std::tuple<Types...> option_from_string(Meta::Type<std::tuple<Types...>>, StringView str) 246 { 247 return option_from_string_impl(Meta::Type<std::tuple<Types...>>{}, str, 248 std::make_index_sequence<sizeof...(Types)>()); 249 } 250 251 template<typename RealType, typename ValueType> 252 inline String option_to_string(const StronglyTypedNumber<RealType, ValueType>& opt) 253 { 254 return to_string(opt); 255 } 256 257 template<typename Number> 258 requires std::is_base_of_v<StronglyTypedNumber<Number, int>, Number> 259 Number option_from_string(Meta::Type<Number>, StringView str) 260 { 261 return Number{str_to_int(str)}; 262 } 263 264 template<typename RealType, typename ValueType> 265 inline bool option_add(StronglyTypedNumber<RealType, ValueType>& opt, StringView str) 266 { 267 int val = str_to_int(str); 268 opt += val; 269 return val != 0; 270 } 271 272 struct WorstMatch { template<typename T> WorstMatch(T&&) {} }; 273 274 inline bool option_add(WorstMatch, StringView) 275 { 276 throw runtime_error("no add operation supported for this option type"); 277 } 278 279 inline bool option_remove(WorstMatch, StringView) 280 { 281 throw runtime_error("no remove operation supported for this option type"); 282 } 283 284 class Context; 285 286 inline void option_update(WorstMatch, const Context&) 287 { 288 throw runtime_error("no update operation supported for this option type"); 289 } 290 291 template<typename Coord> 292 requires std::is_base_of_v<LineAndColumn<Coord, decltype(Coord::line), decltype(Coord::column)>, Coord> 293 Coord option_from_string(Meta::Type<Coord>, StringView str) 294 { 295 struct error : runtime_error { error(size_t) : runtime_error{"expected <line>,<column>"} {} }; 296 auto vals = str | split<StringView>(',') 297 | static_gather<error, 2>(); 298 return {str_to_int(vals[0]), str_to_int(vals[1])}; 299 } 300 301 template<typename EffectiveType, typename LineType, typename ColumnType> 302 inline String option_to_string(const LineAndColumn<EffectiveType, LineType, ColumnType>& opt) 303 { 304 return format("{},{}", opt.line, opt.column); 305 } 306 307 template<DescribedEnum Flags> requires WithBitOps<Flags> 308 String option_to_string(Flags flags) 309 { 310 constexpr auto desc = enum_desc(Meta::Type<Flags>{}); 311 String res; 312 for (int i = 0; i < desc.size(); ++i) 313 { 314 if (not (flags & desc[i].value)) 315 continue; 316 if (not res.empty()) 317 res += "|"; 318 res += desc[i].name; 319 } 320 return res; 321 } 322 323 template<DescribedEnum Enum> requires (not WithBitOps<Enum>) 324 String option_to_string(Enum e) 325 { 326 constexpr auto desc = enum_desc(Meta::Type<Enum>{}); 327 auto it = find_if(desc, [e](const EnumDesc<Enum>& d) { return d.value == e; }); 328 if (it != desc.end()) 329 return it->name.str(); 330 kak_assert(false); 331 return {}; 332 } 333 334 template<DescribedEnum Flags> requires WithBitOps<Flags> 335 Flags option_from_string(Meta::Type<Flags>, StringView str) 336 { 337 constexpr auto desc = enum_desc(Meta::Type<Flags>{}); 338 Flags flags{}; 339 for (auto s : str | split<StringView>('|')) 340 { 341 auto it = find_if(desc, [s](const EnumDesc<Flags>& d) { return d.name == s; }); 342 if (it == desc.end()) 343 throw runtime_error(format("invalid flag value '{}'", s)); 344 flags |= it->value; 345 } 346 return flags; 347 } 348 349 template<DescribedEnum Enum> requires (not WithBitOps<Enum>) 350 Enum option_from_string(Meta::Type<Enum>, StringView str) 351 { 352 constexpr auto desc = enum_desc(Meta::Type<Enum>{}); 353 auto it = find_if(desc, [str](const EnumDesc<Enum>& d) { return d.name == str; }); 354 if (it == desc.end()) 355 throw runtime_error(format("invalid enum value '{}'", str)); 356 return it->value; 357 } 358 359 template<DescribedEnum Flags> requires WithBitOps<Flags> 360 bool option_add(Flags& opt, StringView str) 361 { 362 const Flags old = opt; 363 opt |= option_from_string(Meta::Type<Flags>{}, str); 364 return opt != old; 365 } 366 367 template<DescribedEnum Flags> requires WithBitOps<Flags> 368 bool option_remove(Flags& opt, StringView str) 369 { 370 const Flags old = opt; 371 opt &= ~option_from_string(Meta::Type<Flags>{}, str); 372 return opt != old; 373 } 374 375 template<typename P, typename T> 376 inline Vector<String> option_to_strings(const PrefixedList<P, T>& opt) 377 { 378 Vector<String> res{option_to_string(opt.prefix, Quoting::Raw)}; 379 auto list = option_to_strings(opt.list); 380 res.insert(res.end(), std::make_move_iterator(list.begin()), std::make_move_iterator(list.end())); 381 return res; 382 } 383 384 template<typename P, typename T> 385 inline String option_to_string(const PrefixedList<P, T>& opt, Quoting quoting) 386 { 387 return option_to_string(opt.prefix, quoting) + " " + option_to_string(opt.list, quoting); 388 } 389 390 template<typename P, typename T> 391 inline PrefixedList<P, T> option_from_strings(Meta::Type<PrefixedList<P, T>>, ConstArrayView<String> strs) 392 { 393 if (strs.empty()) 394 return {{}, {}}; 395 396 return {option_from_string(Meta::Type<P>{}, strs[0]), 397 option_from_strings(Meta::Type<Vector<T, MemoryDomain::Options>>{}, strs.subrange(1))}; 398 } 399 400 template<typename P, typename T> 401 inline bool option_add_from_strings(PrefixedList<P, T>& opt, ConstArrayView<String> str) 402 { 403 return option_add_from_strings(opt.list, str); 404 } 405 406 template<typename P, typename T> 407 inline bool option_remove_from_strings(PrefixedList<P, T>& opt, ConstArrayView<String> str) 408 { 409 return option_remove_from_strings(opt.list, str); 410 } 411 412 } 413 414 #endif // option_types_hh_INCLUDED