option.hh (1899B)
1 #ifndef option_hh_INCLUDED 2 #define option_hh_INCLUDED 3 4 #include "exception.hh" 5 #include "meta.hh" 6 #include "string.hh" 7 #include "vector.hh" 8 9 namespace Kakoune 10 { 11 12 class String; 13 enum class Quoting; 14 15 // Forward declare functions that wont get found by ADL 16 inline String option_to_string(int opt); 17 inline String option_to_string(size_t opt); 18 inline String option_to_string(bool opt); 19 20 // Default fallback to single value functions 21 template<typename T> 22 decltype(option_from_string(Meta::Type<T>{}, StringView{})) 23 option_from_strings(Meta::Type<T>, ConstArrayView<String> strs) 24 { 25 if (strs.size() != 1) 26 throw runtime_error("expected a single value for option"); 27 return option_from_string(Meta::Type<T>{}, strs[0]); 28 } 29 30 template<typename T> 31 Vector<decltype(option_to_string(std::declval<T>(), Quoting{}))> 32 option_to_strings(const T& opt) 33 { 34 return Vector<String>{option_to_string(opt, Quoting{})}; 35 } 36 37 template<typename T> 38 decltype(option_add(std::declval<T>(), std::declval<String>())) 39 option_add_from_strings(T& opt, ConstArrayView<String> strs) 40 { 41 if (strs.size() != 1) 42 throw runtime_error("expected a single value for option"); 43 return option_add(opt, strs[0]); 44 } 45 46 template<typename T> 47 decltype(option_add(std::declval<T>(), std::declval<String>())) 48 option_remove_from_strings(T& opt, ConstArrayView<String> strs) 49 { 50 if (strs.size() != 1) 51 throw runtime_error("expected a single value for option"); 52 return option_remove(opt, strs[0]); 53 } 54 55 template<typename P, typename T> 56 struct PrefixedList 57 { 58 P prefix; 59 Vector<T, MemoryDomain::Options> list; 60 61 friend bool operator==(const PrefixedList& lhs, const PrefixedList& rhs) = default; 62 }; 63 64 template<typename T> 65 using TimestampedList = PrefixedList<size_t, T>; 66 67 68 class Option; 69 70 class OptionWatcher 71 { 72 public: 73 virtual void on_option_changed(const Option& option) = 0; 74 }; 75 76 } 77 78 #endif // option_hh_INCLUDED