Open Chinese Convert  1.0.2
A project for conversion between Traditional and Simplified Chinese
 All Classes Functions Typedefs Modules
DictEntry.hpp
1 /*
2  * Open Chinese Convert
3  *
4  * Copyright 2010-2014 BYVoid <byvoid@byvoid.com>
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18 
19 #pragma once
20 
21 #include "Common.hpp"
22 #include "UTF8Util.hpp"
23 #include "Segments.hpp"
24 
25 namespace opencc {
30 class OPENCC_EXPORT DictEntry {
31 public:
32  virtual ~DictEntry() {
33  }
34 
35  virtual const char* Key() const = 0;
36 
37  virtual vector<const char*> Values() const = 0;
38 
39  virtual const char* GetDefault() const = 0;
40 
41  virtual size_t NumValues() const = 0;
42 
43  virtual string ToString() const = 0;
44 
45  size_t KeyLength() const {
46  return strlen(Key());
47  }
48 
49  bool operator<(const DictEntry& that) const {
50  return strcmp(Key(), that.Key()) < 0;
51  }
52 
53  bool operator==(const DictEntry& that) const {
54  return strcmp(Key(), that.Key()) == 0;
55  }
56 
57  static bool PtrLessThan(const DictEntry* a, const DictEntry* b) {
58  return *a < *b;
59  }
60 };
61 
62 class OPENCC_EXPORT NoValueDictEntry : public DictEntry {
63 public:
64  NoValueDictEntry(const string& _key) : key(_key) {
65  }
66 
67  virtual ~NoValueDictEntry() {
68  }
69 
70  virtual const char* Key() const {
71  return key.c_str();
72  }
73 
74  virtual vector<const char*> Values() const {
75  return vector<const char*>();
76  }
77 
78  virtual const char* GetDefault() const {
79  return Key();
80  }
81 
82  virtual size_t NumValues() const {
83  return 0;
84  }
85 
86  virtual string ToString() const {
87  return key;
88  }
89 
90 private:
91  string key;
92 };
93 
94 class OPENCC_EXPORT SingleValueDictEntry : public DictEntry {
95 public:
96  virtual const char* Value() const = 0;
97 
98  virtual vector<const char*> Values() const {
99  return vector<const char*>{Value()};
100  }
101 
102  virtual const char* GetDefault() const {
103  return Value();
104  }
105 
106  virtual size_t NumValues() const {
107  return 1;
108  }
109 
110  virtual string ToString() const {
111  return string(Key()) + "\t" + Value();
112  }
113 };
114 
115 class OPENCC_EXPORT StrSingleValueDictEntry : public SingleValueDictEntry {
116 public:
117  StrSingleValueDictEntry(const string& _key, const string& _value)
118  : key(_key), value(_value) {
119  }
120 
121  virtual ~StrSingleValueDictEntry() {
122  }
123 
124  virtual const char* Key() const {
125  return key.c_str();
126  }
127 
128  virtual const char* Value() const {
129  return value.c_str();
130  }
131 
132 private:
133  string key;
134  string value;
135 };
136 
137 class OPENCC_EXPORT MultiValueDictEntry : public DictEntry {
138 public:
139  virtual const char* GetDefault() const {
140  if (NumValues() > 0) {
141  return Values().at(0);
142  } else {
143  return Key();
144  }
145  }
146 
147  virtual string ToString() const;
148 };
149 
150 class OPENCC_EXPORT StrMultiValueDictEntry : public MultiValueDictEntry {
151 public:
152  StrMultiValueDictEntry(const string& _key, const vector<string>& _values)
153  : key(_key), values(_values) {
154  }
155 
156  StrMultiValueDictEntry(const string& _key, const vector<const char*>& _values)
157  : key(_key) {
158  values.reserve(_values.size());
159  for (const char* str : _values) {
160  values.push_back(str);
161  }
162  }
163 
164  virtual ~StrMultiValueDictEntry() {
165  }
166 
167  virtual const char* Key() const {
168  return key.c_str();
169  }
170 
171  size_t NumValues() const {
172  return values.size();
173  }
174 
175  vector<const char*> Values() const {
176  vector<const char*> values;
177  for (const string& value : this->values) {
178  values.push_back(value.c_str());
179  }
180  return values;
181  }
182 
183 private:
184  string key;
185  vector<string> values;
186 };
187 
188 class OPENCC_EXPORT PtrDictEntry : public MultiValueDictEntry {
189 public:
190  PtrDictEntry(const char* _key, const vector<const char*>& _values)
191  : key(_key), values(_values) {
192  }
193 
194  virtual ~PtrDictEntry() {
195  }
196 
197  virtual const char* Key() const {
198  return key;
199  }
200 
201  size_t NumValues() const {
202  return values.size();
203  }
204 
205  vector<const char*> Values() const {
206  return values;
207  }
208 
209 private:
210  const char* key;
211  vector<const char*> values;
212 };
213 
214 class OPENCC_EXPORT DictEntryFactory {
215 public:
216  static DictEntry* New(const string& key) {
217  return new NoValueDictEntry(key);
218  }
219 
220  static DictEntry* New(const string& key, const string& value) {
221  return new StrSingleValueDictEntry(key, value);
222  }
223 
224  static DictEntry* New(const string& key, const vector<string>& values) {
225  return new StrMultiValueDictEntry(key, values);
226  }
227 
228  static DictEntry* New(const DictEntry* entry) {
229  if (entry->NumValues() == 0) {
230  return new NoValueDictEntry(entry->Key());
231  } else if (entry->NumValues() == 1) {
232  const auto svEntry = static_cast<const SingleValueDictEntry*>(entry);
233  return new StrSingleValueDictEntry(svEntry->Key(), svEntry->Value());
234  } else {
235  const auto mvEntry = static_cast<const MultiValueDictEntry*>(entry);
236  return new StrMultiValueDictEntry(mvEntry->Key(), mvEntry->Values());
237  }
238  }
239 };
240 
241 }
Definition: DictEntry.hpp:137
Definition: DictEntry.hpp:188
Key-values pair entry.
Definition: DictEntry.hpp:30
Definition: DictEntry.hpp:150
Definition: BinaryDict.hpp:24
Definition: DictEntry.hpp:214
Definition: DictEntry.hpp:115
Definition: DictEntry.hpp:94
Definition: DictEntry.hpp:62