Use boost::shared_ptr to manage object instance.
authorPeng Huang <shawn.p.huang@gmail.com>
Sat, 17 Apr 2010 09:01:37 +0000 (17:01 +0800)
committerPeng Huang <shawn.p.huang@gmail.com>
Sat, 17 Apr 2010 11:31:38 +0000 (19:31 +0800)
12 files changed:
src/Database.cc
src/Database.h
src/Editor.h
src/Engine.cc
src/PhraseEditor.cc
src/PhraseEditor.h
src/PinyinEngine.cc
src/PinyinEngine.h
src/SpecialPhrase.cc
src/SpecialPhrase.h
src/SpecialPhraseTable.cc
src/SpecialPhraseTable.h

index cd35874..ea56786 100644 (file)
@@ -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;
index 389e456..ab3750d 100644 (file)
@@ -3,6 +3,7 @@
 #define __PY_DATABASE_H__
 
 #include <sqlite3.h>
+#include <boost/shared_ptr.hpp>
 #include "String.h"
 #include "Types.h"
 #include "PinyinArray.h"
@@ -11,6 +12,8 @@
 namespace PY {
 
 class SQLStmt;
+typedef boost::shared_ptr<SQLStmt> 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<Query> 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);
 
index 7728db5..e518a54 100644 (file)
@@ -2,6 +2,7 @@
 #define __PY_EDITOR_H_
 
 #include <glib.h>
+#include <boost/shared_ptr.hpp>
 #include "Signal.h"
 #include "Text.h"
 #include "LookupTable.h"
@@ -9,6 +10,9 @@
 
 namespace PY {
 
+class Editor;
+typedef boost::shared_ptr<Editor> EditorPtr;
+
 class Editor {
 public:
     Editor (PinyinProperties & prop);
index 5c16028..d4c21f3 100644 (file)
@@ -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);
 }
 
index c52e947..252b7a0 100644 (file)
@@ -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 ();
 }
 
index 71b213f..e68c4ee 100644 (file)
@@ -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;
 };
 
 };
index 1ddea12..89cb515 100644 (file)
@@ -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 <DoublePinyinEditor *> (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 <DoublePinyinEditor *> (m_editors[MODE_INIT].get ()) == NULL) {
+            m_editors[MODE_INIT].reset (new DoublePinyinEditor (m_props));
+            connectEditorSignals (m_editors[MODE_INIT]);
+        }
     }
     else {
-        if (dynamic_cast <FullPinyinEditor *> (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 <FullPinyinEditor *> (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));
index 23e1a6b..bbcced0 100644 (file)
@@ -2,6 +2,7 @@
 #ifndef __PY_PIN_YIN_ENGINE_H__
 #define __PY_PIN_YIN_ENGINE_H__
 
+#include <boost/shared_ptr.hpp>
 #include <ibus.h>
 #include "Pointer.h"
 #include "Database.h"
@@ -14,6 +15,8 @@
 
 namespace PY {
 
+class PinyinEngine;
+typedef boost::shared_ptr<PinyinEngine> 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;
 };
 
index 23c8ce9..b88af1d 100644 (file)
@@ -4,6 +4,7 @@ namespace PY {
 
 SpecialPhrase::~SpecialPhrase (void)
 {
+    g_debug ("destroy");
 }
 
 };
index 97fa02e..02f27ca 100644 (file)
@@ -2,6 +2,7 @@
 #define __PY_SPECIAL_PHRASE_H_
 
 #include <string>
+#include <boost/shared_ptr.hpp>
 #include <glib.h>
 
 namespace PY {
@@ -21,6 +22,8 @@ private:
     guint m_position;
 };
 
+typedef boost::shared_ptr<SpecialPhrase> SpecialPhrasePtr;
+
 };
 
 #endif
index ee5a811..17f09b5 100644 (file)
@@ -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<std::string>  &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<Map::iterator, Map::iterator> 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<SpecialPhrase *>::iterator pit;
-        for (pit = (*it).second.begin (); pit != (*it).second.end (); pit ++) {
-            delete *pit;
-        }
-    }
-
-    m_map.clear ();
-}
-
 };
 
index a4ba5fc..19ad35c 100644 (file)
@@ -15,19 +15,16 @@ private:
     SpecialPhraseTable (void);
 
 public:
-    void insert (const std::string & command, SpecialPhrase *phrase);
     gboolean lookup (const std::string &command, std::vector<std::string> &result);
 
 private:
     gboolean load (const gchar *file);
-    void clear (void);
 
 public:
     static SpecialPhraseTable & instance (void) { return m_instance; }
 
 private:
-    typedef std::list<SpecialPhrase *> List;
-    typedef std::map<std::string, List> Map;
+    typedef std::multimap<std::string, SpecialPhrasePtr> Map;
     Map m_map;
 
 private: