: 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
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 ()) {
}
}
- delete m_stmt;
- m_stmt = NULL;
+ m_stmt.reset ();
m_pinyin_len --;
}
}
}
-SQLStmt *
+SQLStmtPtr
Database::query (const PinyinArray &pinyin,
guint pinyin_begin,
guint pinyin_len,
#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;
#define __PY_DATABASE_H__
#include <sqlite3.h>
+#include <boost/shared_ptr.hpp>
#include "String.h"
#include "Types.h"
#include "PinyinArray.h"
namespace PY {
class SQLStmt;
+typedef boost::shared_ptr<SQLStmt> SQLStmtPtr;
+
class Database;
class Query {
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:
~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);
#define __PY_EDITOR_H_
#include <glib.h>
+#include <boost/shared_ptr.hpp>
#include "Signal.h"
#include "Text.h"
#include "LookupTable.h"
namespace PY {
+class Editor;
+typedef boost::shared_ptr<Editor> EditorPtr;
+
class Editor {
public:
Editor (PinyinProperties & prop);
IBusEngine parent;
/* members */
- PinyinEngine *engine;
+ PinyinEnginePtr engine;
};
struct _IBusPinyinEngineClass {
{
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);
}
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
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;
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 ();
}
}
gboolean fillCandidates (void) {
- if (G_UNLIKELY (m_query == NULL)) {
+ if (G_UNLIKELY (m_query.get () == NULL)) {
return FALSE;
}
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;
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);
PinyinArray m_pinyin;
guint m_cursor;
PinyinProperties & m_props;
- Query * m_query;
+ QueryPtr m_query;
};
};
: 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));
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];
- }
}
}
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;
{
/* 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 ());
}
}
void
-PinyinEngine::connectEditorSignals (Editor *editor)
+PinyinEngine::connectEditorSignals (EditorPtr editor)
{
editor->signalCommitText ().connect (
bind (&PinyinEngine::slotCommitText, this, _1));
#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"
namespace PY {
+class PinyinEngine;
+typedef boost::shared_ptr<PinyinEngine> PinyinEnginePtr;
class PinyinEngine {
public:
PinyinEngine (IBusEngine *engine);
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;
}
private:
void showSetupDialog (void);
- void connectEditorSignals (Editor *editor);
+ void connectEditorSignals (EditorPtr editor);
private:
void slotCommitText (Text & text);
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;
};
SpecialPhrase::~SpecialPhrase (void)
{
+ g_debug ("destroy");
}
};
#define __PY_SPECIAL_PHRASE_H_
#include <string>
+#include <boost/shared_ptr.hpp>
#include <glib.h>
namespace PY {
guint m_position;
};
+typedef boost::shared_ptr<SpecialPhrase> SpecialPhrasePtr;
+
};
#endif
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)
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;
gboolean
SpecialPhraseTable::load (const gchar *file)
{
- clear ();
-
+ m_map.clear ();
+
std::ifstream in (file);
if (in.fail ())
return FALSE;
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 ();
-}
-
};
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: