From 4707357783c1bcf44cfab5d7915182f38f231398 Mon Sep 17 00:00:00 2001 From: Peng Huang Date: Tue, 13 Apr 2010 17:41:38 +0800 Subject: [PATCH] Refine code --- src/Database.cc | 10 +++++----- src/Database.h | 13 +++++++++---- src/PhraseEditor.cc | 10 +++------- src/PhraseEditor.h | 5 +---- src/PinyinEditor.cc | 1 - src/PinyinEditor.h | 3 +-- src/SpecialTable.cc | 2 ++ src/SpecialTable.h | 12 +++++++++++- 8 files changed, 32 insertions(+), 24 deletions(-) diff --git a/src/Database.cc b/src/Database.cc index 57e854b..ba18e1b 100644 --- a/src/Database.cc +++ b/src/Database.cc @@ -20,6 +20,8 @@ namespace PY { #define DB_PREFETCH_LEN (6) +Database Database::m_instance; + class Conditions : public std::vector { public: Conditions (void) : std::vector (1) {} @@ -109,13 +111,11 @@ private: sqlite3_stmt *m_stmt; }; -Query::Query (Database & db, - const PinyinArray & pinyin, +Query::Query (const PinyinArray & pinyin, guint pinyin_begin, guint pinyin_len, guint option) - : m_db (db), - m_pinyin (pinyin), + : m_pinyin (pinyin), m_pinyin_begin (pinyin_begin), m_pinyin_len (pinyin_len), m_option (option), @@ -138,7 +138,7 @@ Query::fill (PhraseArray &phrases, gint count) while (m_pinyin_len > 0) { if (G_LIKELY (m_stmt == NULL)) { - m_stmt = m_db.query (m_pinyin, m_pinyin_begin, m_pinyin_len, -1, m_option); + m_stmt = Database::instance ().query (m_pinyin, m_pinyin_begin, m_pinyin_len, -1, m_option); g_assert (m_stmt != NULL); } diff --git a/src/Database.h b/src/Database.h index ae91332..389e456 100644 --- a/src/Database.h +++ b/src/Database.h @@ -15,8 +15,7 @@ class Database; class Query { public: - Query (Database & db, - const PinyinArray & pinyin, + Query (const PinyinArray & pinyin, guint pinyin_begin, guint pinyin_len, guint option); @@ -24,7 +23,6 @@ public: gint fill (PhraseArray &phrases, gint count); private: - Database & m_db; const PinyinArray & m_pinyin; guint m_pinyin_begin; guint m_pinyin_len; @@ -33,9 +31,11 @@ private: }; class Database { -public: +private: Database (); ~Database (); + +public: SQLStmt *query (const PinyinArray & pinyin, guint pinyin_begin, guint pinyin_len, @@ -47,6 +47,8 @@ public: void conditionsDouble (void); void conditionsTriple (void); + static Database & instance (void) {return m_instance; } + private: gboolean init (void); gboolean initUserDatabase (const gchar *userdb); @@ -60,6 +62,9 @@ private: String m_sql; /* sql stmt */ String m_buffer; /* temp buffer */ + +private: + static Database m_instance; }; diff --git a/src/PhraseEditor.cc b/src/PhraseEditor.cc index 7584a03..c52e947 100644 --- a/src/PhraseEditor.cc +++ b/src/PhraseEditor.cc @@ -4,8 +4,6 @@ namespace PY { -Database PhraseEditor::m_database; - PhraseEditor::PhraseEditor (PinyinProperties & props) : m_candidates (32), m_selected_phrases (8), @@ -43,7 +41,7 @@ PhraseEditor::update (const PinyinArray &pinyin) gboolean PhraseEditor::resetCandidate (guint i) { - m_database.remove (m_candidates[i]); + Database::instance ().remove (m_candidates[i]); updateCandidates (); return TRUE; @@ -99,8 +97,7 @@ PhraseEditor::updateCandidates (void) m_candidates.push_back (phrase); } - m_query = new Query (m_database, - m_pinyin, + m_query = new Query (m_pinyin, m_cursor, m_pinyin.size () - m_cursor, Config::option ()); @@ -123,8 +120,7 @@ PhraseEditor::updateTheFirstCandidate (void) while (begin != end) { gint ret; - Query query (m_database, - m_pinyin, + Query query (m_pinyin, begin, end - begin, Config::option ()); diff --git a/src/PhraseEditor.h b/src/PhraseEditor.h index 1bc924a..71b213f 100644 --- a/src/PhraseEditor.h +++ b/src/PhraseEditor.h @@ -87,7 +87,7 @@ public: m_selected_phrases.insert (m_selected_phrases.end (), m_candidate_0_phrases.begin (), m_candidate_0_phrases.end ()); #endif - m_database.commit (m_selected_phrases); + Database::instance ().commit (m_selected_phrases); reset (); } @@ -112,9 +112,6 @@ private: guint m_cursor; PinyinProperties & m_props; Query * m_query; - -private: - static Database m_database; }; }; diff --git a/src/PinyinEditor.cc b/src/PinyinEditor.cc index 51caa59..e05110e 100644 --- a/src/PinyinEditor.cc +++ b/src/PinyinEditor.cc @@ -9,7 +9,6 @@ namespace PY { /* init static members */ PinyinParser PinyinEditor::m_parser; -SpecialTable PinyinEditor::m_special_table; PinyinEditor::PinyinEditor (PinyinProperties & props) : Editor (props), diff --git a/src/PinyinEditor.h b/src/PinyinEditor.h index b929bf7..d237fd6 100644 --- a/src/PinyinEditor.h +++ b/src/PinyinEditor.h @@ -78,7 +78,7 @@ protected: if (begin < end && m_selected_special_phrase.empty () && - m_special_table.lookup (m_text.substr (begin, m_cursor - begin), m_special_phrases)) { + SpecialTable::instance ().lookup (m_text.substr (begin, m_cursor - begin), m_special_phrases)) { return TRUE; } return oldsize > 0; @@ -108,7 +108,6 @@ protected: protected: static PinyinParser m_parser; - static SpecialTable m_special_table; }; }; diff --git a/src/SpecialTable.cc b/src/SpecialTable.cc index 600ae43..20e57b5 100644 --- a/src/SpecialTable.cc +++ b/src/SpecialTable.cc @@ -2,6 +2,8 @@ namespace PY { +SpecialTable SpecialTable::m_instance; + class StaticPhrase : public SpecialPhrase { public: StaticPhrase (const std::string &text, guint pos) : diff --git a/src/SpecialTable.h b/src/SpecialTable.h index 0a3e229..851d32e 100644 --- a/src/SpecialTable.h +++ b/src/SpecialTable.h @@ -23,16 +23,26 @@ private: }; class SpecialTable { -public: +private: SpecialTable (void); + +public: void insert (const std::string & command, SpecialPhrase *phrase); gboolean lookup (const std::string &command, std::vector &result); + private: gboolean load (const gchar *file); + +public: + static SpecialTable & instance (void) { return m_instance; } + private: typedef std::list List; typedef std::map Map; Map m_map; + +private: + static SpecialTable m_instance; }; }; -- 2.7.4