librime: config_types.h Source File

| librime 1.2 Rime Input Method Engine, the core library

config_types.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2011-4-6 Zou xu <zouivex@gmail.com>
6//
7#ifndef RIME_CONFIG_TYPES_H_
8#define RIME_CONFIG_TYPES_H_
9
10#include <type_traits>
11#include <rime_api.h>
12#include <rime/common.h>
13
14namespace rime {
15
16// config item base class
17 class ConfigItem {
18 public:
19 enum ValueType { kNull, kScalar, kList, kMap };
20
21 ConfigItem() = default; // null
22 virtual ~ConfigItem() = default;
23
24 ValueType type() const { return type_; }
25
26 virtual bool empty() const { return type_ == kNull; }
27
28 protected:
29 ConfigItem(ValueType type) : type_(type) {}
30
31 ValueType type_ = kNull;
32};
33
34 class ConfigValue : public ConfigItem {
35 public:
36 ConfigValue() : ConfigItem(kScalar) {}
37 RIME_DLL ConfigValue(bool value);
38 RIME_DLL ConfigValue(int value);
39 RIME_DLL ConfigValue(double value);
40 RIME_DLL ConfigValue(const char* value);
41 RIME_DLL ConfigValue(const string& value);
42
43 // schalar value accessors
44 bool GetBool(bool* value) const;
45 RIME_DLL bool GetInt(int* value) const;
46 bool GetDouble(double* value) const;
47 RIME_DLL bool GetString(string* value) const;
48 bool SetBool(bool value);
49 bool SetInt(int value);
50 bool SetDouble(double value);
51 bool SetString(const char* value);
52 bool SetString(const string& value);
53
54 const string& str() const { return value_; }
55
56 bool empty() const override { return value_.empty(); }
57
58 protected:
59 string value_;
60};
61
62 class ConfigList : public ConfigItem {
63 public:
64 using Sequence = vector<of<ConfigItem>>;
65 using Iterator = Sequence::iterator;
66
67 ConfigList() : ConfigItem(kList) {}
68 RIME_DLL an<ConfigItem> GetAt(size_t i) const;
69 RIME_DLL an<ConfigValue> GetValueAt(size_t i) const;
70 RIME_DLL bool SetAt(size_t i, an<ConfigItem> element);
71 bool Insert(size_t i, an<ConfigItem> element);
72 RIME_DLL bool Append(an<ConfigItem> element);
73 bool Resize(size_t size);
74 RIME_DLL bool Clear();
75 RIME_DLL size_t size() const;
76
77 Iterator begin();
78 Iterator end();
79
80 bool empty() const override { return seq_.empty(); }
81
82 protected:
83 Sequence seq_;
84};
85
86// limitation: map keys have to be strings, preferably alphanumeric
87 class ConfigMap : public ConfigItem {
88 public:
89 using Map = map<string, an<ConfigItem>>;
90 using Iterator = Map::iterator;
91
92 ConfigMap() : ConfigItem(kMap) {}
93 RIME_DLL bool HasKey(const string& key) const;
94 RIME_DLL an<ConfigItem> Get(const string& key) const;
95 RIME_DLL an<ConfigValue> GetValue(const string& key) const;
96 RIME_DLL bool Set(const string& key, an<ConfigItem> element);
97 bool Clear();
98
99 Iterator begin();
100 Iterator end();
101
102 bool empty() const override { return map_.empty(); }
103
104 protected:
105 Map map_;
106};
107
108namespace {
109
110template <class T>
111 an<ConfigItem> AsConfigItem(const T& x, const std::false_type&) {
112 return New<ConfigValue>(x);
113};
114
115template <class T>
116 an<ConfigItem> AsConfigItem(const T& x, const std::true_type&) {
117 return x;
118};
119
120} // namespace
121
122class ConfigData;
123class ConfigListEntryRef;
124class ConfigMapEntryRef;
125
126 class ConfigItemRef {
127 public:
128 ConfigItemRef(ConfigData* data) : data_(data) {}
129 virtual ~ConfigItemRef() = default;
130 operator an<ConfigItem>() const { return GetItem(); }
131 an<ConfigItem> operator*() const { return GetItem(); }
132 template <class T>
133 ConfigItemRef& operator=(const T& x) {
134 SetItem(AsConfigItem(x, std::is_convertible<T, an<ConfigItem>>()));
135 return *this;
136 }
137 ConfigListEntryRef operator[](size_t index);
138 ConfigMapEntryRef operator[](const string& key);
139
140 RIME_DLL bool IsNull() const;
141 bool IsValue() const;
142 RIME_DLL bool IsList() const;
143 bool IsMap() const;
144
145 RIME_DLL bool ToBool() const;
146 RIME_DLL int ToInt() const;
147 double ToDouble() const;
148 RIME_DLL string ToString() const;
149
150 RIME_DLL an<ConfigList> AsList();
151 RIME_DLL an<ConfigMap> AsMap();
152 RIME_DLL void Clear();
153
154 // list
155 RIME_DLL bool Append(an<ConfigItem> item);
156 RIME_DLL size_t size() const;
157 // map
158 RIME_DLL bool HasKey(const string& key) const;
159
160 RIME_DLL bool modified() const;
161 RIME_DLL void set_modified();
162
163 protected:
164 virtual an<ConfigItem> GetItem() const = 0;
165 virtual void SetItem(an<ConfigItem> item) = 0;
166
167 ConfigData* data_;
168};
169
170 class ConfigListEntryRef : public ConfigItemRef {
171 public:
172 ConfigListEntryRef(ConfigData* data, an<ConfigList> list, size_t index)
173 : ConfigItemRef(data), list_(list), index_(index) {}
174 using ConfigItemRef::operator=;
175
176 protected:
177 an<ConfigItem> GetItem() const { return list_->GetAt(index_); }
178 void SetItem(an<ConfigItem> item) {
179 list_->SetAt(index_, item);
180 set_modified();
181 }
182
183 private:
184 an<ConfigList> list_;
185 size_t index_;
186};
187
188 class ConfigMapEntryRef : public ConfigItemRef {
189 public:
190 ConfigMapEntryRef(ConfigData* data, an<ConfigMap> map, const string& key)
191 : ConfigItemRef(data), map_(map), key_(key) {}
192 using ConfigItemRef::operator=;
193
194 protected:
195 an<ConfigItem> GetItem() const { return map_->Get(key_); }
196 void SetItem(an<ConfigItem> item) {
197 map_->Set(key_, item);
198 set_modified();
199 }
200
201 private:
202 an<ConfigMap> map_;
203 string key_;
204};
205
206 inline ConfigListEntryRef ConfigItemRef::operator[](size_t index) {
207 return ConfigListEntryRef(data_, AsList(), index);
208}
209
210 inline ConfigMapEntryRef ConfigItemRef::operator[](const string& key) {
211 return ConfigMapEntryRef(data_, AsMap(), key);
212}
213
214} // namespace rime
215
216#endif // RIME_CONFIG_TYPES_H_
common.h
rime_api.h
RIME_DLL
#define RIME_DLL
Definition rime_api.h:33
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::New
an< T > New(Args &&... args)
Definition common.h:77
rime::ConfigData
Definition config_data.h:17
rime::ConfigItem::type
ValueType type() const
Definition config_types.h:24
rime::ConfigItem::empty
virtual bool empty() const
Definition config_types.h:26
rime::ConfigItem::ValueType
ValueType
Definition config_types.h:19
rime::ConfigItem::kMap
@ kMap
Definition config_types.h:19
rime::ConfigItem::kList
@ kList
Definition config_types.h:19
rime::ConfigItem::kNull
@ kNull
Definition config_types.h:19
rime::ConfigItem::kScalar
@ kScalar
Definition config_types.h:19
rime::ConfigItem::type_
ValueType type_
Definition config_types.h:31
rime::ConfigItem::ConfigItem
ConfigItem(ValueType type)
Definition config_types.h:29
rime::ConfigItem::~ConfigItem
virtual ~ConfigItem()=default
rime::ConfigItem::ConfigItem
ConfigItem()=default
rime::ConfigValue::SetBool
bool SetBool(bool value)
Definition config_types.cc:88
rime::ConfigValue::GetInt
RIME_DLL bool GetInt(int *value) const
Definition config_types.cc:49
rime::ConfigValue::GetDouble
bool GetDouble(double *value) const
Definition config_types.cc:70
rime::ConfigValue::SetInt
bool SetInt(int value)
Definition config_types.cc:93
rime::ConfigValue::GetBool
bool GetBool(bool *value) const
Definition config_types.cc:34
rime::ConfigValue::str
const string & str() const
Definition config_types.h:54
rime::ConfigValue::GetString
RIME_DLL bool GetString(string *value) const
Definition config_types.cc:81
rime::ConfigValue::SetString
bool SetString(const char *value)
Definition config_types.cc:103
rime::ConfigValue::ConfigValue
ConfigValue()
Definition config_types.h:36
rime::ConfigValue::empty
bool empty() const override
Definition config_types.h:56
rime::ConfigValue::SetDouble
bool SetDouble(double value)
Definition config_types.cc:98
rime::ConfigValue::value_
string value_
Definition config_types.h:59
rime::ConfigList::GetValueAt
RIME_DLL an< ConfigValue > GetValueAt(size_t i) const
Definition config_types.cc:122
rime::ConfigList::begin
Iterator begin()
Definition config_types.cc:160
rime::ConfigList::Append
RIME_DLL bool Append(an< ConfigItem > element)
Definition config_types.cc:141
rime::ConfigList::Insert
bool Insert(size_t i, an< ConfigItem > element)
Definition config_types.cc:133
rime::ConfigList::seq_
Sequence seq_
Definition config_types.h:83
rime::ConfigList::size
RIME_DLL size_t size() const
Definition config_types.cc:156
rime::ConfigList::empty
bool empty() const override
Definition config_types.h:80
rime::ConfigList::Clear
RIME_DLL bool Clear()
Definition config_types.cc:151
rime::ConfigList::Sequence
vector< of< ConfigItem > > Sequence
Definition config_types.h:64
rime::ConfigList::end
Iterator end()
Definition config_types.cc:164
rime::ConfigList::ConfigList
ConfigList()
Definition config_types.h:67
rime::ConfigList::Iterator
Sequence::iterator Iterator
Definition config_types.h:65
rime::ConfigList::SetAt
RIME_DLL bool SetAt(size_t i, an< ConfigItem > element)
Definition config_types.cc:126
rime::ConfigList::Resize
bool Resize(size_t size)
Definition config_types.cc:146
rime::ConfigList::GetAt
RIME_DLL an< ConfigItem > GetAt(size_t i) const
Definition config_types.cc:115
rime::ConfigMap::end
Iterator end()
Definition config_types.cc:200
rime::ConfigMap::begin
Iterator begin()
Definition config_types.cc:196
rime::ConfigMap::Map
map< string, an< ConfigItem > > Map
Definition config_types.h:89
rime::ConfigMap::Iterator
Map::iterator Iterator
Definition config_types.h:90
rime::ConfigMap::Clear
bool Clear()
Definition config_types.cc:191
rime::ConfigMap::map_
Map map_
Definition config_types.h:105
rime::ConfigMap::empty
bool empty() const override
Definition config_types.h:102
rime::ConfigMap::GetValue
RIME_DLL an< ConfigValue > GetValue(const string &key) const
Definition config_types.cc:182
rime::ConfigMap::ConfigMap
ConfigMap()
Definition config_types.h:92
rime::ConfigMap::HasKey
RIME_DLL bool HasKey(const string &key) const
Definition config_types.cc:170
rime::ConfigMap::Get
RIME_DLL an< ConfigItem > Get(const string &key) const
Definition config_types.cc:174
rime::ConfigMap::Set
RIME_DLL bool Set(const string &key, an< ConfigItem > element)
Definition config_types.cc:186
rime::ConfigItemRef::~ConfigItemRef
virtual ~ConfigItemRef()=default
rime::ConfigItemRef::operator=
ConfigItemRef & operator=(const T &x)
Definition config_types.h:133
rime::ConfigItemRef::Clear
RIME_DLL void Clear()
Definition config_types.cc:272
rime::ConfigItemRef::operator[]
ConfigListEntryRef operator[](size_t index)
Definition config_types.h:206
rime::ConfigItemRef::GetItem
virtual an< ConfigItem > GetItem() const =0
rime::ConfigItemRef::HasKey
RIME_DLL bool HasKey(const string &key) const
Definition config_types.cc:289
rime::ConfigItemRef::operator*
an< ConfigItem > operator*() const
Definition config_types.h:131
rime::ConfigItemRef::set_modified
RIME_DLL void set_modified()
Definition config_types.cc:298
rime::ConfigItemRef::AsList
RIME_DLL an< ConfigList > AsList()
Definition config_types.cc:258
rime::ConfigItemRef::Append
RIME_DLL bool Append(an< ConfigItem > item)
Definition config_types.cc:276
rime::ConfigItemRef::IsNull
RIME_DLL bool IsNull() const
Definition config_types.cc:206
rime::ConfigItemRef::ToDouble
double ToDouble() const
Definition config_types.cc:242
rime::ConfigItemRef::AsMap
RIME_DLL an< ConfigMap > AsMap()
Definition config_types.cc:265
rime::ConfigItemRef::IsMap
bool IsMap() const
Definition config_types.cc:221
rime::ConfigItemRef::IsValue
bool IsValue() const
Definition config_types.cc:211
rime::ConfigItemRef::ToString
RIME_DLL string ToString() const
Definition config_types.cc:250
rime::ConfigItemRef::modified
RIME_DLL bool modified() const
Definition config_types.cc:294
rime::ConfigItemRef::size
RIME_DLL size_t size() const
Definition config_types.cc:284
rime::ConfigItemRef::ConfigItemRef
ConfigItemRef(ConfigData *data)
Definition config_types.h:128
rime::ConfigItemRef::data_
ConfigData * data_
Definition config_types.h:167
rime::ConfigItemRef::IsList
RIME_DLL bool IsList() const
Definition config_types.cc:216
rime::ConfigItemRef::SetItem
virtual void SetItem(an< ConfigItem > item)=0
rime::ConfigItemRef::ToInt
RIME_DLL int ToInt() const
Definition config_types.cc:234
rime::ConfigItemRef::ToBool
RIME_DLL bool ToBool() const
Definition config_types.cc:226
rime::ConfigListEntryRef
Definition config_types.h:170
rime::ConfigListEntryRef::ConfigListEntryRef
ConfigListEntryRef(ConfigData *data, an< ConfigList > list, size_t index)
Definition config_types.h:172
rime::ConfigListEntryRef::GetItem
an< ConfigItem > GetItem() const
Definition config_types.h:177
rime::ConfigListEntryRef::SetItem
void SetItem(an< ConfigItem > item)
Definition config_types.h:178
rime::ConfigMapEntryRef
Definition config_types.h:188
rime::ConfigMapEntryRef::ConfigMapEntryRef
ConfigMapEntryRef(ConfigData *data, an< ConfigMap > map, const string &key)
Definition config_types.h:190
rime::ConfigMapEntryRef::GetItem
an< ConfigItem > GetItem() const
Definition config_types.h:195
rime::ConfigMapEntryRef::SetItem
void SetItem(an< ConfigItem > item)
Definition config_types.h:196