Open Chinese Convert 1.1.8
A project for conversion between Traditional and Simplified Chinese
Loading...
Searching...
No Matches
DictEntry.hpp
1/*
2 * Open Chinese Convert
3 *
4 * Copyright 2010-2020 Carbo Kuo <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 "Segments.hpp"
23#include "UTF8Util.hpp"
24
25namespace opencc {
30class OPENCC_EXPORT DictEntry {
31public:
32 virtual ~DictEntry() {}
33
34 virtual std::string Key() const = 0;
35
36 virtual std::vector<std::string> Values() const = 0;
37
38 virtual std::string GetDefault() const = 0;
39
40 virtual size_t NumValues() const = 0;
41
42 virtual std::string ToString() const = 0;
43
44 size_t KeyLength() const { return Key().length(); }
45
46 bool operator<(const DictEntry& that) const { return Key() < that.Key(); }
47
48 bool operator==(const DictEntry& that) const { return Key() == that.Key(); }
49
50 static bool UPtrLessThan(const std::unique_ptr<DictEntry>& a,
51 const std::unique_ptr<DictEntry>& b) {
52 return *a < *b;
53 }
54};
55
56class OPENCC_EXPORT NoValueDictEntry : public DictEntry {
57public:
58 NoValueDictEntry(const std::string& _key) : key(_key) {}
59
60 virtual ~NoValueDictEntry() {}
61
62 virtual std::string Key() const { return key; }
63
64 virtual std::vector<std::string> Values() const {
65 return std::vector<std::string>();
66 }
67
68 virtual std::string GetDefault() const { return key; }
69
70 virtual size_t NumValues() const { return 0; }
71
72 virtual std::string ToString() const { return key; }
73
74private:
75 std::string key;
76};
77
78class OPENCC_EXPORT SingleValueDictEntry : public DictEntry {
79public:
80 virtual std::string Value() const = 0;
81
82 virtual std::vector<std::string> Values() const {
83 return std::vector<std::string>{Value()};
84 }
85
86 virtual std::string GetDefault() const { return Value(); }
87
88 virtual size_t NumValues() const { return 1; }
89
90 virtual std::string ToString() const {
91 return std::string(Key()) + "\t" + Value();
92 }
93};
94
95class OPENCC_EXPORT StrSingleValueDictEntry : public SingleValueDictEntry {
96public:
97 StrSingleValueDictEntry(const std::string& _key, const std::string& _value)
98 : key(_key), value(_value) {}
99
100 virtual ~StrSingleValueDictEntry() {}
101
102 virtual std::string Key() const { return key; }
103
104 virtual std::string Value() const { return value; }
105
106private:
107 std::string key;
108 std::string value;
109};
110
111class OPENCC_EXPORT MultiValueDictEntry : public DictEntry {
112public:
113 virtual std::string GetDefault() const {
114 if (NumValues() > 0) {
115 return Values().at(0);
116 } else {
117 return Key();
118 }
119 }
120
121 virtual std::string ToString() const;
122};
123
124class OPENCC_EXPORT StrMultiValueDictEntry : public MultiValueDictEntry {
125public:
126 StrMultiValueDictEntry(const std::string& _key,
127 const std::vector<std::string>& _values)
128 : key(_key), values(_values) {}
129
130 virtual ~StrMultiValueDictEntry() {}
131
132 virtual std::string Key() const { return key; }
133
134 size_t NumValues() const { return values.size(); }
135
136 std::vector<std::string> Values() const { return values; }
137
138private:
139 std::string key;
140 std::vector<std::string> values;
141};
142
143class OPENCC_EXPORT DictEntryFactory {
144public:
145 static DictEntry* New(const std::string& key) {
146 return new NoValueDictEntry(key);
147 }
148
149 static DictEntry* New(const std::string& key, const std::string& value) {
150 return new StrSingleValueDictEntry(key, value);
151 }
152
153 static DictEntry* New(const std::string& key,
154 const std::vector<std::string>& values) {
155 if (values.size() == 0) {
156 return New(key);
157 } else if (values.size() == 1) {
158 return New(key, values.front());
159 }
160 return new StrMultiValueDictEntry(key, values);
161 }
162
163 static DictEntry* New(const DictEntry* entry) {
164 if (entry->NumValues() == 0) {
165 return new NoValueDictEntry(entry->Key());
166 } else if (entry->NumValues() == 1) {
167 return new StrSingleValueDictEntry(entry->Key(), entry->Values().front());
168 } else {
169 return new StrMultiValueDictEntry(entry->Key(), entry->Values());
170 }
171 }
172};
173} // namespace opencc
Definition DictEntry.hpp:143
Key-values pair entry.
Definition DictEntry.hpp:30
Definition DictEntry.hpp:111
Definition DictEntry.hpp:56
Definition DictEntry.hpp:78
Definition DictEntry.hpp:124
Definition DictEntry.hpp:95