Open Chinese Convert: DictEntry.hpp Source File

Open Chinese Convert 1.0.5 A project for conversion between Traditional and Simplified Chinese

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 virtual const char* Key() const = 0;
35
36 virtual vector<const char*> Values() const = 0;
37
38 virtual const char* GetDefault() const = 0;
39
40 virtual size_t NumValues() const = 0;
41
42 virtual string ToString() const = 0;
43
44 size_t KeyLength() const { return strlen(Key()); }
45
46 bool operator<(const DictEntry& that) const {
47 return strcmp(Key(), that.Key()) < 0;
48 }
49
50 bool operator==(const DictEntry& that) const {
51 return strcmp(Key(), that.Key()) == 0;
52 }
53
54 static bool PtrLessThan(const DictEntry* a, const DictEntry* b) {
55 return *a < *b;
56 }
57 };
58
59 class OPENCC_EXPORT NoValueDictEntry : public DictEntry {
60 public:
61 NoValueDictEntry(const string& _key) : key(_key) {}
62
63 virtual ~NoValueDictEntry() {}
64
65 virtual const char* Key() const { return key.c_str(); }
66
67 virtual vector<const char*> Values() const { return vector<const char*>(); }
68
69 virtual const char* GetDefault() const { return Key(); }
70
71 virtual size_t NumValues() const { return 0; }
72
73 virtual string ToString() const { return key; }
74
75 private:
76 string key;
77 };
78
79 class OPENCC_EXPORT SingleValueDictEntry : public DictEntry {
80 public:
81 virtual const char* Value() const = 0;
82
83 virtual vector<const char*> Values() const {
84 return vector<const char*>{Value()};
85 }
86
87 virtual const char* GetDefault() const { return Value(); }
88
89 virtual size_t NumValues() const { return 1; }
90
91 virtual string ToString() const { return string(Key()) + "\t" + Value(); }
92 };
93
94 class OPENCC_EXPORT StrSingleValueDictEntry : public SingleValueDictEntry {
95 public:
96 StrSingleValueDictEntry(const string& _key, const string& _value)
97 : key(_key), value(_value) {}
98
99 virtual ~StrSingleValueDictEntry() {}
100
101 virtual const char* Key() const { return key.c_str(); }
102
103 virtual const char* Value() const { return value.c_str(); }
104
105 private:
106 string key;
107 string value;
108 };
109
110 class OPENCC_EXPORT MultiValueDictEntry : public DictEntry {
111 public:
112 virtual const char* GetDefault() const {
113 if (NumValues() > 0) {
114 return Values().at(0);
115 } else {
116 return Key();
117 }
118 }
119
120 virtual string ToString() const;
121 };
122
123 class OPENCC_EXPORT StrMultiValueDictEntry : public MultiValueDictEntry {
124 public:
125 StrMultiValueDictEntry(const string& _key, const vector<string>& _values)
126 : key(_key), values(_values) {}
127
128 StrMultiValueDictEntry(const string& _key, const vector<const char*>& _values)
129 : key(_key) {
130 values.reserve(_values.size());
131 for (const char* str : _values) {
132 values.push_back(str);
133 }
134 }
135
136 virtual ~StrMultiValueDictEntry() {}
137
138 virtual const char* Key() const { return key.c_str(); }
139
140 size_t NumValues() const { return values.size(); }
141
142 vector<const char*> Values() const {
143 vector<const char*> retsult;
144 for (const string& value : this->values) {
145 retsult.push_back(value.c_str());
146 }
147 return retsult;
148 }
149
150 private:
151 string key;
152 vector<string> values;
153 };
154
155 class OPENCC_EXPORT PtrDictEntry : public MultiValueDictEntry {
156 public:
157 PtrDictEntry(const char* _key, const vector<const char*>& _values)
158 : key(_key), values(_values) {}
159
160 virtual ~PtrDictEntry() {}
161
162 virtual const char* Key() const { return key; }
163
164 size_t NumValues() const { return values.size(); }
165
166 vector<const char*> Values() const { return values; }
167
168 private:
169 const char* key;
170 vector<const char*> values;
171 };
172
173 class OPENCC_EXPORT DictEntryFactory {
174 public:
175 static DictEntry* New(const string& key) { return new NoValueDictEntry(key); }
176
177 static DictEntry* New(const string& key, const string& value) {
178 return new StrSingleValueDictEntry(key, value);
179 }
180
181 static DictEntry* New(const string& key, const vector<string>& values) {
182 return new StrMultiValueDictEntry(key, values);
183 }
184
185 static DictEntry* New(const DictEntry* entry) {
186 if (entry->NumValues() == 0) {
187 return new NoValueDictEntry(entry->Key());
188 } else if (entry->NumValues() == 1) {
189 const auto svEntry = static_cast<const SingleValueDictEntry*>(entry);
190 return new StrSingleValueDictEntry(svEntry->Key(), svEntry->Value());
191 } else {
192 const auto mvEntry = static_cast<const MultiValueDictEntry*>(entry);
193 return new StrMultiValueDictEntry(mvEntry->Key(), mvEntry->Values());
194 }
195 }
196 };
197 }
opencc::MultiValueDictEntry
Definition: DictEntry.hpp:110
opencc::PtrDictEntry
Definition: DictEntry.hpp:155
opencc::DictEntry
Key-values pair entry.
Definition: DictEntry.hpp:30
opencc::StrMultiValueDictEntry
Definition: DictEntry.hpp:123
opencc
Definition: BinaryDict.hpp:24
opencc::DictEntryFactory
Definition: DictEntry.hpp:173
opencc::StrSingleValueDictEntry
Definition: DictEntry.hpp:94
opencc::SingleValueDictEntry
Definition: DictEntry.hpp:79
opencc::NoValueDictEntry
Definition: DictEntry.hpp:59