8 #ifndef Guit_PropComp_hpp
9 #define Guit_PropComp_hpp
19 virtual ~GFieldSpec() =
default;
20 virtual bool toValue(GString
const& from, Value* to) = 0;
21 virtual bool toString(
const Value* from, GString& to) = 0;
27 std::vector<GFieldSpec*> fields_;
31 GPropSpec(std::initializer_list<GFieldSpec*> fields);
32 int toValue(GString
const& str, Value*);
33 GString toString(
const Value*);
39 template <
class T,
class Var>
40 class GNumFieldSpec :
public GFieldSpec {
43 GNumFieldSpec(Var T::* var,
bool isoptional =
false)
44 : var_(var) {isoptional_ = isoptional;}
46 bool toValue(GString
const& from, Value* to)
override {
47 return gconvert(from, ((T*)to)->*var_);
50 bool toString(
const Value* from, GString& to)
override {
51 return gconvert(((
const T*)from)->*var_, to);
57 class GFieldSpecValue {
67 class GFieldSpecValues {
69 std::vector<GFieldSpecValue> list_;
71 GFieldSpecValues(std::initializer_list<GFieldSpecValue> values);
72 GFieldSpecValue
const* find1(GString
const& str)
const;
73 GFieldSpecValue
const* find2(GString
const& str,
float&)
const;
74 bool find1(
int type, GString& to)
const;
75 bool find2(
int type,
float val, GString& to)
const;
79 template <
class T,
class Var>
80 class GListFieldSpec :
public GFieldSpec {
82 GFieldSpecValues
const& values_;
86 GListFieldSpec(Var T::* var, GFieldSpecValues
const& values)
87 : var_(var), values_(values) {}
89 bool toValue(GString
const& str, Value* to)
override {
90 if (
auto* it = values_.find1(str)) {
91 ((T*)to)->*var_ = (Var) it->type_;
97 bool toString(
const Value* from, GString& to)
override {
98 return values_.find1(((
const T*)from)->*var_, to);
103 template <
class T1,
class T2,
class Var1,
class Var2>
104 class GListFieldSpec2 :
public GFieldSpec {
107 GFieldSpecValues
const& values_;
111 GListFieldSpec2(Var1 T1::* var1, Var2 T2::* var2, GFieldSpecValues
const& values)
112 : var1_(var1), var2_(var2), values_(values) {}
114 bool toValue(GString
const& str, Value* to)
override {
116 if (
auto* it = values_.find2(str, val)) {
117 ((T1*)to)->*var1_ = Var1(it->type_);
118 ((T2*)to)->*var2_ = Var2(val);
124 bool toString(
const Value* from, GString& to)
override {
125 return values_.find2(((
const T1*)from)->*var1_, ((T2*)from)->*var2_, to);