Open Chinese Convert  1.0.3
A project for conversion between Traditional and Simplified Chinese
DictEntry.hpp
00001 /*
00002  * Open Chinese Convert
00003  *
00004  * Copyright 2010-2014 BYVoid <byvoid@byvoid.com>
00005  *
00006  * Licensed under the Apache License, Version 2.0 (the "License");
00007  * you may not use this file except in compliance with the License.
00008  * You may obtain a copy of the License at
00009  *
00010  *      http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing, software
00013  * distributed under the License is distributed on an "AS IS" BASIS,
00014  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00015  * See the License for the specific language governing permissions and
00016  * limitations under the License.
00017  */
00018 
00019 #pragma once
00020 
00021 #include "Common.hpp"
00022 #include "UTF8Util.hpp"
00023 #include "Segments.hpp"
00024 
00025 namespace opencc {
00030 class OPENCC_EXPORT DictEntry {
00031 public:
00032   virtual ~DictEntry() {}
00033 
00034   virtual const char* Key() const = 0;
00035 
00036   virtual vector<const char*> Values() const = 0;
00037 
00038   virtual const char* GetDefault() const = 0;
00039 
00040   virtual size_t NumValues() const = 0;
00041 
00042   virtual string ToString() const = 0;
00043 
00044   size_t KeyLength() const { return strlen(Key()); }
00045 
00046   bool operator<(const DictEntry& that) const {
00047     return strcmp(Key(), that.Key()) < 0;
00048   }
00049 
00050   bool operator==(const DictEntry& that) const {
00051     return strcmp(Key(), that.Key()) == 0;
00052   }
00053 
00054   static bool PtrLessThan(const DictEntry* a, const DictEntry* b) {
00055     return *a < *b;
00056   }
00057 };
00058 
00059 class OPENCC_EXPORT NoValueDictEntry : public DictEntry {
00060 public:
00061   NoValueDictEntry(const string& _key) : key(_key) {}
00062 
00063   virtual ~NoValueDictEntry() {}
00064 
00065   virtual const char* Key() const { return key.c_str(); }
00066 
00067   virtual vector<const char*> Values() const { return vector<const char*>(); }
00068 
00069   virtual const char* GetDefault() const { return Key(); }
00070 
00071   virtual size_t NumValues() const { return 0; }
00072 
00073   virtual string ToString() const { return key; }
00074 
00075 private:
00076   string key;
00077 };
00078 
00079 class OPENCC_EXPORT SingleValueDictEntry : public DictEntry {
00080 public:
00081   virtual const char* Value() const = 0;
00082 
00083   virtual vector<const char*> Values() const {
00084     return vector<const char*>{Value()};
00085   }
00086 
00087   virtual const char* GetDefault() const { return Value(); }
00088 
00089   virtual size_t NumValues() const { return 1; }
00090 
00091   virtual string ToString() const { return string(Key()) + "\t" + Value(); }
00092 };
00093 
00094 class OPENCC_EXPORT StrSingleValueDictEntry : public SingleValueDictEntry {
00095 public:
00096   StrSingleValueDictEntry(const string& _key, const string& _value)
00097       : key(_key), value(_value) {}
00098 
00099   virtual ~StrSingleValueDictEntry() {}
00100 
00101   virtual const char* Key() const { return key.c_str(); }
00102 
00103   virtual const char* Value() const { return value.c_str(); }
00104 
00105 private:
00106   string key;
00107   string value;
00108 };
00109 
00110 class OPENCC_EXPORT MultiValueDictEntry : public DictEntry {
00111 public:
00112   virtual const char* GetDefault() const {
00113     if (NumValues() > 0) {
00114       return Values().at(0);
00115     } else {
00116       return Key();
00117     }
00118   }
00119 
00120   virtual string ToString() const;
00121 };
00122 
00123 class OPENCC_EXPORT StrMultiValueDictEntry : public MultiValueDictEntry {
00124 public:
00125   StrMultiValueDictEntry(const string& _key, const vector<string>& _values)
00126       : key(_key), values(_values) {}
00127 
00128   StrMultiValueDictEntry(const string& _key, const vector<const char*>& _values)
00129       : key(_key) {
00130     values.reserve(_values.size());
00131     for (const char* str : _values) {
00132       values.push_back(str);
00133     }
00134   }
00135 
00136   virtual ~StrMultiValueDictEntry() {}
00137 
00138   virtual const char* Key() const { return key.c_str(); }
00139 
00140   size_t NumValues() const { return values.size(); }
00141 
00142   vector<const char*> Values() const {
00143     vector<const char*> values;
00144     for (const string& value : this->values) {
00145       values.push_back(value.c_str());
00146     }
00147     return values;
00148   }
00149 
00150 private:
00151   string key;
00152   vector<string> values;
00153 };
00154 
00155 class OPENCC_EXPORT PtrDictEntry : public MultiValueDictEntry {
00156 public:
00157   PtrDictEntry(const char* _key, const vector<const char*>& _values)
00158       : key(_key), values(_values) {}
00159 
00160   virtual ~PtrDictEntry() {}
00161 
00162   virtual const char* Key() const { return key; }
00163 
00164   size_t NumValues() const { return values.size(); }
00165 
00166   vector<const char*> Values() const { return values; }
00167 
00168 private:
00169   const char* key;
00170   vector<const char*> values;
00171 };
00172 
00173 class OPENCC_EXPORT DictEntryFactory {
00174 public:
00175   static DictEntry* New(const string& key) { return new NoValueDictEntry(key); }
00176 
00177   static DictEntry* New(const string& key, const string& value) {
00178     return new StrSingleValueDictEntry(key, value);
00179   }
00180 
00181   static DictEntry* New(const string& key, const vector<string>& values) {
00182     return new StrMultiValueDictEntry(key, values);
00183   }
00184 
00185   static DictEntry* New(const DictEntry* entry) {
00186     if (entry->NumValues() == 0) {
00187       return new NoValueDictEntry(entry->Key());
00188     } else if (entry->NumValues() == 1) {
00189       const auto svEntry = static_cast<const SingleValueDictEntry*>(entry);
00190       return new StrSingleValueDictEntry(svEntry->Key(), svEntry->Value());
00191     } else {
00192       const auto mvEntry = static_cast<const MultiValueDictEntry*>(entry);
00193       return new StrMultiValueDictEntry(mvEntry->Key(), mvEntry->Values());
00194     }
00195   }
00196 };
00197 }
 All Classes Functions