begin to write import iterator
[platform/upstream/libpinyin.git] / src / storage / facade_phrase_table.h
1 /* 
2  *  libpinyin
3  *  Library to deal with pinyin.
4  *  
5  *  Copyright (C) 2011 Peng Wu <alexepico@gmail.com>
6  *  
7  *  This program is free software; you can redistribute it and/or modify
8  *  it under the terms of the GNU General Public License as published by
9  *  the Free Software Foundation; either version 2 of the License, or
10  *  (at your option) any later version.
11  * 
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  *  GNU General Public License for more details.
16  *  
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
20  */
21
22 #ifndef FACADE_PHRASE_TABLE_H
23 #define FACADE_PHRASE_TABLE_H
24
25 #include "phrase_large_table.h"
26
27 namespace pinyin{
28
29 /**
30  * FacadePhraseTable:
31  *
32  * The facade class of phrase large table.
33  *
34  */
35
36 class FacadePhraseTable{
37 private:
38     PhraseLargeTable * m_system_phrase_table;
39     PhraseLargeTable * m_user_phrase_table;
40
41 public:
42     /**
43      * FacadePhraseTable::FacadePhraseTable:
44      *
45      * The constructor of the FacadePhraseTable.
46      *
47      */
48     FacadePhraseTable() {
49         m_system_phrase_table = NULL;
50         m_user_phrase_table = NULL;
51     }
52
53     /**
54      * FacadePhraseTable::load:
55      * @system: the memory chunk of the system phrase table.
56      * @user: the memory chunk of the user phrase table.
57      * @returns: whether the load operation is successful.
58      *
59      * Load the system or user phrase table from the memory chunks.
60      *
61      */
62     bool load(MemoryChunk * system, MemoryChunk * user) {
63         bool result = false;
64         if (system) {
65             m_system_phrase_table = new PhraseLargeTable;
66             result = m_system_phrase_table->load(system) || result;
67         }
68         if (user) {
69             m_user_phrase_table = new PhraseLargeTable;
70             result = m_user_phrase_table->load(user) || result;
71         }
72         return result;
73     }
74
75     /**
76      * FacadePhraseTable::store:
77      * @new_user: the memory chunk to store the user phrase table.
78      * @returns: whether the store operation is successful.
79      *
80      * Store the user phrase table to the memory chunk.
81      *
82      */
83     bool store(MemoryChunk * new_user) {
84         if (NULL == m_user_phrase_table)
85             return false;
86         return m_user_phrase_table->store(new_user);
87     }
88
89     /**
90      * FacadePhraseTable::search:
91      * @phrase_length: the length of the phrase to be searched.
92      * @phrase: the ucs4 characters of the phrase to be searched.
93      * @token: the token to store the matched phrase.
94      * @returns: the search result of enum SearchResult.
95      *
96      * Search the phrase token according to the ucs4 characters.
97      *
98      */
99     int search(int phrase_length, /* in */ ucs4_t phrase[],
100                /* out */ phrase_token_t & token){
101         int result = SEARCH_NONE;
102         token = null_token;
103
104         if (NULL != m_system_phrase_table)
105             result |= m_system_phrase_table->search
106                 (phrase_length, phrase, token);
107
108         if (NULL != m_user_phrase_table)
109             result |= m_user_phrase_table->search
110                 (phrase_length, phrase, token);
111         return result;
112     }
113
114     /**
115      * FacadePhraseTable::add_index:
116      * @phrase_length: the length of the phrase to be added.
117      * @phrase: the ucs4 characters of the phrase to be added.
118      * @token: the token of the phrase to be added.
119      * @returns: the add result of enum AddIndexResult.
120      *
121      * Add the phrase token to the user phrase table.
122      *
123      */
124     int add_index(int phrase_length, /* in */ ucs4_t phrase[],
125                   /* in */ phrase_token_t token) {
126         assert(NULL != m_user_phrase_table);
127         return m_user_phrase_table->add_index
128             (phrase_length, phrase, token);
129     }
130
131     /**
132      * FacadePhraseTable::remove_index:
133      * @phrase_length: the length of the phrase to be removed.
134      * @phrase: the ucs4 characters of the phrase to be removed.
135      * @token: the token of the phrase to be removed.
136      * @returns: the remove result of enum RemoveIndexResult.
137      *
138      * Remove the phrase token from the user phrase table.
139      *
140      */
141     int remove_index(int phrase_length, /* in */ ucs4_t phrase[],
142                      /* out */ phrase_token_t & token){
143         assert(NULL != m_user_phrase_table);
144         return m_user_phrase_table->remove_index
145             (phrase_length, phrase, token);
146     }
147 };
148
149 };
150
151
152 #endif