From: Peng Huang Date: Sat, 17 Apr 2010 09:01:37 +0000 (+0800) Subject: Use boost::shared_ptr to manage object instance. X-Git-Tag: 1.3.10~201 X-Git-Url: http://review.tizen.org/git/?a=commitdiff_plain;h=414466300e91073a3af1f8c43024f8aeb454954d;p=platform%2Fupstream%2Fibus-libpinyin.git Use boost::shared_ptr to manage object instance. --- diff --git a/src/Database.cc b/src/Database.cc index cd35874..ea56786 100644 --- a/src/Database.cc +++ b/src/Database.cc @@ -118,17 +118,13 @@ Query::Query (const PinyinArray & pinyin, : m_pinyin (pinyin), m_pinyin_begin (pinyin_begin), m_pinyin_len (pinyin_len), - m_option (option), - m_stmt (NULL) + m_option (option) { g_assert (m_pinyin.size () >= pinyin_begin + pinyin_len); } Query::~Query (void) { - if (m_stmt) { - delete m_stmt; - } } gint @@ -137,9 +133,9 @@ Query::fill (PhraseArray &phrases, gint count) gint row = 0; while (m_pinyin_len > 0) { - if (G_LIKELY (m_stmt == NULL)) { + if (G_LIKELY (m_stmt.get () == NULL)) { m_stmt = Database::instance ().query (m_pinyin, m_pinyin_begin, m_pinyin_len, -1, m_option); - g_assert (m_stmt != NULL); + g_assert (m_stmt.get () != NULL); } while (m_stmt->step ()) { @@ -164,8 +160,7 @@ Query::fill (PhraseArray &phrases, gint count) } } - delete m_stmt; - m_stmt = NULL; + m_stmt.reset (); m_pinyin_len --; } @@ -413,7 +408,7 @@ pinyin_option_check_yun (guint option, gint id, gint fid) } } -SQLStmt * +SQLStmtPtr Database::query (const PinyinArray &pinyin, guint pinyin_begin, guint pinyin_len, @@ -529,11 +524,10 @@ Database::query (const PinyinArray &pinyin, #endif /* query database */ - SQLStmt *stmt = new SQLStmt (m_db); + SQLStmtPtr stmt (new SQLStmt (m_db)); if (!stmt->prepare (m_sql)) { - delete stmt; - return NULL; + stmt.reset (); } return stmt; diff --git a/src/Database.h b/src/Database.h index 389e456..ab3750d 100644 --- a/src/Database.h +++ b/src/Database.h @@ -3,6 +3,7 @@ #define __PY_DATABASE_H__ #include +#include #include "String.h" #include "Types.h" #include "PinyinArray.h" @@ -11,6 +12,8 @@ namespace PY { class SQLStmt; +typedef boost::shared_ptr SQLStmtPtr; + class Database; class Query { @@ -27,8 +30,9 @@ private: guint m_pinyin_begin; guint m_pinyin_len; guint m_option; - SQLStmt *m_stmt; + SQLStmtPtr m_stmt; }; +typedef boost::shared_ptr QueryPtr; class Database { private: @@ -36,11 +40,11 @@ private: ~Database (); public: - SQLStmt *query (const PinyinArray & pinyin, - guint pinyin_begin, - guint pinyin_len, - gint m, - guint option); + SQLStmtPtr query (const PinyinArray & pinyin, + guint pinyin_begin, + guint pinyin_len, + gint m, + guint option); void commit (const PhraseArray & phrases); void remove (const Phrase & phrase); diff --git a/src/Editor.h b/src/Editor.h index 7728db5..e518a54 100644 --- a/src/Editor.h +++ b/src/Editor.h @@ -2,6 +2,7 @@ #define __PY_EDITOR_H_ #include +#include #include "Signal.h" #include "Text.h" #include "LookupTable.h" @@ -9,6 +10,9 @@ namespace PY { +class Editor; +typedef boost::shared_ptr EditorPtr; + class Editor { public: Editor (PinyinProperties & prop); diff --git a/src/Engine.cc b/src/Engine.cc index 5c16028..d4c21f3 100644 --- a/src/Engine.cc +++ b/src/Engine.cc @@ -26,7 +26,7 @@ struct _IBusPinyinEngine { IBusEngine parent; /* members */ - PinyinEngine *engine; + PinyinEnginePtr engine; }; struct _IBusPinyinEngineClass { @@ -115,16 +115,13 @@ ibus_pinyin_engine_init (IBusPinyinEngine *pinyin) { if (g_object_is_floating (pinyin)) g_object_ref_sink (pinyin); // make engine sink - pinyin->engine = new PinyinEngine (IBUS_ENGINE (pinyin)); + new (& (pinyin->engine)) PinyinEnginePtr (new PinyinEngine (IBUS_ENGINE (pinyin))); } static void ibus_pinyin_engine_destroy (IBusPinyinEngine *pinyin) { - if (pinyin->engine) { - delete pinyin->engine; - pinyin->engine = NULL; - } + pinyin->engine.~PinyinEnginePtr (); ((IBusObjectClass *) ibus_pinyin_engine_parent_class)->destroy ((IBusObject *)pinyin); } diff --git a/src/PhraseEditor.cc b/src/PhraseEditor.cc index c52e947..252b7a0 100644 --- a/src/PhraseEditor.cc +++ b/src/PhraseEditor.cc @@ -11,15 +11,12 @@ PhraseEditor::PhraseEditor (PinyinProperties & props) m_candidate_0_phrases (8), m_pinyin (16), m_cursor (0), - m_props (props), - m_query (NULL) + m_props (props) { } PhraseEditor::~PhraseEditor (void) { - if (m_query) - delete m_query; } gboolean @@ -80,11 +77,8 @@ void PhraseEditor::updateCandidates (void) { m_candidates.clear (); + m_query.reset (); updateTheFirstCandidate (); - if (m_query) { - delete m_query; - m_query = NULL; - } if (G_UNLIKELY (m_pinyin.size () == 0)) return; @@ -97,10 +91,10 @@ PhraseEditor::updateCandidates (void) m_candidates.push_back (phrase); } - m_query = new Query (m_pinyin, - m_cursor, - m_pinyin.size () - m_cursor, - Config::option ()); + m_query.reset (new Query (m_pinyin, + m_cursor, + m_pinyin.size () - m_cursor, + Config::option ())); fillCandidates (); } diff --git a/src/PhraseEditor.h b/src/PhraseEditor.h index 71b213f..e68c4ee 100644 --- a/src/PhraseEditor.h +++ b/src/PhraseEditor.h @@ -29,7 +29,7 @@ public: } gboolean fillCandidates (void) { - if (G_UNLIKELY (m_query == NULL)) { + if (G_UNLIKELY (m_query.get () == NULL)) { return FALSE; } @@ -37,8 +37,7 @@ public: if (G_UNLIKELY (ret < FILL_GRAN)) { /* got all candidates from query */ - delete m_query; - m_query = NULL; + m_query.reset (); } return ret > 0 ? TRUE : FALSE; @@ -73,10 +72,7 @@ public: m_candidate_0_phrases.clear (); m_pinyin.clear (); m_cursor = 0; - if (m_query) { - delete m_query; - m_query = NULL; - } + m_query.reset (); } gboolean update (const PinyinArray &pinyin); @@ -111,7 +107,7 @@ private: PinyinArray m_pinyin; guint m_cursor; PinyinProperties & m_props; - Query * m_query; + QueryPtr m_query; }; }; diff --git a/src/PinyinEngine.cc b/src/PinyinEngine.cc index 1ddea12..89cb515 100644 --- a/src/PinyinEngine.cc +++ b/src/PinyinEngine.cc @@ -22,17 +22,17 @@ PinyinEngine::PinyinEngine (IBusEngine *engine) : m_engine (engine), m_prev_pressed_key (IBUS_VoidSymbol), m_input_mode (MODE_INIT), - m_fallback_editor (m_props) + m_fallback_editor (new FallbackEditor (m_props)) { gint i; /* create editors */ if (Config::doublePinyin ()) - m_editors[MODE_INIT] = new DoublePinyinEditor (m_props); + m_editors[MODE_INIT].reset (new DoublePinyinEditor (m_props)); else - m_editors[MODE_INIT] = new FullPinyinEditor (m_props); + m_editors[MODE_INIT].reset (new FullPinyinEditor (m_props)); - m_editors[MODE_RAW] = new RawEditor (m_props); - m_editors[MODE_EXTENSION] = new ExtEditor (m_props); + m_editors[MODE_RAW].reset (new RawEditor (m_props)); + m_editors[MODE_EXTENSION].reset (new ExtEditor (m_props)); m_props.signalUpdateProperty ().connect (bind (&PinyinEngine::slotUpdateProperty, this, _1)); @@ -40,15 +40,12 @@ PinyinEngine::PinyinEngine (IBusEngine *engine) connectEditorSignals (m_editors[i]); } - connectEditorSignals (&m_fallback_editor); + connectEditorSignals (m_fallback_editor); } /* destructor */ PinyinEngine::~PinyinEngine (void) { - for (gint i = 0; i < MODE_LAST; i++) { - delete m_editors[i]; - } } @@ -116,7 +113,7 @@ PinyinEngine::processKeyEvent (guint keyval, guint keycode, guint modifiers) } if (G_UNLIKELY (!retval)) - retval = m_fallback_editor.processKeyEvent (keyval, keycode, modifiers); + retval = m_fallback_editor->processKeyEvent (keyval, keycode, modifiers); /* store ignored key event by editors */ m_prev_pressed_key = retval ? IBUS_VoidSymbol : keyval; @@ -129,16 +126,16 @@ PinyinEngine::focusIn (void) { /* reset pinyin editor */ if (Config::doublePinyin ()) { - if (dynamic_cast (m_editors[MODE_INIT]) == NULL) - delete m_editors[MODE_INIT]; - m_editors[MODE_INIT] = new DoublePinyinEditor (m_props); - connectEditorSignals (m_editors[MODE_INIT]); + if (dynamic_cast (m_editors[MODE_INIT].get ()) == NULL) { + m_editors[MODE_INIT].reset (new DoublePinyinEditor (m_props)); + connectEditorSignals (m_editors[MODE_INIT]); + } } else { - if (dynamic_cast (m_editors[MODE_INIT]) == NULL) - delete m_editors[MODE_INIT]; - m_editors[MODE_INIT] = new FullPinyinEditor (m_props); - connectEditorSignals (m_editors[MODE_INIT]); + if (dynamic_cast (m_editors[MODE_INIT].get ()) == NULL) { + m_editors[MODE_INIT].reset (new FullPinyinEditor (m_props)); + connectEditorSignals (m_editors[MODE_INIT]); + } } ibus_engine_register_properties (m_engine, m_props.properties ()); } @@ -270,7 +267,7 @@ PinyinEngine::slotUpdateProperty (Property & prop) } void -PinyinEngine::connectEditorSignals (Editor *editor) +PinyinEngine::connectEditorSignals (EditorPtr editor) { editor->signalCommitText ().connect ( bind (&PinyinEngine::slotCommitText, this, _1)); diff --git a/src/PinyinEngine.h b/src/PinyinEngine.h index 23e1a6b..bbcced0 100644 --- a/src/PinyinEngine.h +++ b/src/PinyinEngine.h @@ -2,6 +2,7 @@ #ifndef __PY_PIN_YIN_ENGINE_H__ #define __PY_PIN_YIN_ENGINE_H__ +#include #include #include "Pointer.h" #include "Database.h" @@ -14,6 +15,8 @@ namespace PY { +class PinyinEngine; +typedef boost::shared_ptr PinyinEnginePtr; class PinyinEngine { public: PinyinEngine (IBusEngine *engine); @@ -31,7 +34,7 @@ public: for (gint i = 0; i < MODE_LAST; i++) { m_editors[i]->reset (); } - m_fallback_editor.reset (); + m_fallback_editor->reset (); m_last_commit_text = NULL; } @@ -51,7 +54,7 @@ private: private: void showSetupDialog (void); - void connectEditorSignals (Editor *editor); + void connectEditorSignals (EditorPtr editor); private: void slotCommitText (Text & text); @@ -85,8 +88,8 @@ private: MODE_LAST, } m_input_mode; - Editor *m_editors[MODE_LAST]; - FallbackEditor m_fallback_editor; + EditorPtr m_editors[MODE_LAST]; + EditorPtr m_fallback_editor; Text m_last_commit_text; }; diff --git a/src/SpecialPhrase.cc b/src/SpecialPhrase.cc index 23c8ce9..b88af1d 100644 --- a/src/SpecialPhrase.cc +++ b/src/SpecialPhrase.cc @@ -4,6 +4,7 @@ namespace PY { SpecialPhrase::~SpecialPhrase (void) { + g_debug ("destroy"); } }; diff --git a/src/SpecialPhrase.h b/src/SpecialPhrase.h index 97fa02e..02f27ca 100644 --- a/src/SpecialPhrase.h +++ b/src/SpecialPhrase.h @@ -2,6 +2,7 @@ #define __PY_SPECIAL_PHRASE_H_ #include +#include #include namespace PY { @@ -21,6 +22,8 @@ private: guint m_position; }; +typedef boost::shared_ptr SpecialPhrasePtr; + }; #endif diff --git a/src/SpecialPhraseTable.cc b/src/SpecialPhraseTable.cc index ee5a811..17f09b5 100644 --- a/src/SpecialPhraseTable.cc +++ b/src/SpecialPhraseTable.cc @@ -32,29 +32,6 @@ SpecialPhraseTable::SpecialPhraseTable (void) g_free (path); } -#if 0 -static bool -phraseCmp (const SpecialPhrase *first, - const SpecialPhrase *second) -{ - return first->position () <= second->position (); -} -#endif - -void -SpecialPhraseTable::insert (const std::string &command, - SpecialPhrase *phrase) -{ - if (m_map.find (command) == m_map.end ()) { - m_map[command] = List (); - } - List & list = m_map[command]; - list.push_back (phrase); -#if 0 - list.sort (phraseCmp); -#endif -} - gboolean SpecialPhraseTable::lookup (const std::string &command, std::vector &result) @@ -63,12 +40,10 @@ SpecialPhraseTable::lookup (const std::string &command, if (!Config::specialPhrases ()) return FALSE; - if (m_map.find (command) == m_map.end ()) - return FALSE; - List list = m_map[command]; - for (List::iterator it = list.begin (); it != list.end (); it ++) { - result.push_back ((*it)->text ()); + std::pair range = m_map.equal_range (command); + for (Map::iterator it = range.first; it != range.second; it ++) { + result.push_back ((*it).second->text ()); } return result.size () > 0; @@ -77,8 +52,8 @@ SpecialPhraseTable::lookup (const std::string &command, gboolean SpecialPhraseTable::load (const gchar *file) { - clear (); - + m_map.clear (); + std::ifstream in (file); if (in.fail ()) return FALSE; @@ -93,34 +68,21 @@ SpecialPhraseTable::load (const gchar *file) continue; std::string command = line.substr(0, pos); - std::string phrase = line.substr(pos + 1); - if (command.empty () || phrase.empty ()) + std::string value = line.substr(pos + 1); + if (command.empty () || value.empty ()) continue; - if (phrase[0] != '#') { - insert (command, new StaticSpecialPhrase (phrase, 0)); + if (value[0] != '#') { + SpecialPhrasePtr phrase (new StaticSpecialPhrase (value, 0)); + m_map.insert (Map::value_type (command, phrase)); } - else if (phrase.size () > 1) { - insert (command, new DynamicSpecialPhrase (phrase.substr (1), 0)); + else if (value.size () > 1) { + SpecialPhrasePtr phrase (new DynamicSpecialPhrase (value.substr (1), 0)); + m_map.insert (Map::value_type (command, phrase)); } } return TRUE; } -void -SpecialPhraseTable::clear (void) -{ - Map::iterator it; - - for (it = m_map.begin (); it != m_map.end (); it ++) { - std::list::iterator pit; - for (pit = (*it).second.begin (); pit != (*it).second.end (); pit ++) { - delete *pit; - } - } - - m_map.clear (); -} - }; diff --git a/src/SpecialPhraseTable.h b/src/SpecialPhraseTable.h index a4ba5fc..19ad35c 100644 --- a/src/SpecialPhraseTable.h +++ b/src/SpecialPhraseTable.h @@ -15,19 +15,16 @@ private: SpecialPhraseTable (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); - void clear (void); public: static SpecialPhraseTable & instance (void) { return m_instance; } private: - typedef std::list List; - typedef std::map Map; + typedef std::multimap Map; Map m_map; private: