diff --git a/sample/lua/expand_translator.lua b/sample/lua/expand_translator.lua index b343952..1aee36b 100644 --- a/sample/lua/expand_translator.lua +++ b/sample/lua/expand_translator.lua @@ -28,11 +28,20 @@ local function memoryCallback(memory, commit) return true end +local function memoryCallback1(commit) + for i, dictentry in ipairs(commit:get()) do + commits:update_entry(dictentry, 1, "") + end + --commits:update(1) -- +end + + local function init(env) env.mem = Memory(env.engine,env.engine.schema) -- ns= "translator" -- env.mem = Memory(env.engine,env.engine.schema, env.name_space ) -- env.mem = Memory(env.engine,Schema("cangjie5") ) -- ns= "translator- - env.mem:memorize(function(commit) memoryCallback(env.mem, commit) end) + --env.mem:memorize(function(commit) memoryCallback(env.mem, commit) end) + env.mem:memorize(memoryCallback1) -- or use -- schema = Schema("cangjie5") -- schema_id -- env.mem = Memory(env.engine, schema, "translator") diff --git a/src/types.cc b/src/types.cc index dcaffb3..b63b1d6 100644 --- a/src/types.cc +++ b/src/types.cc @@ -20,11 +20,12 @@ #include #include "lib/lua_export_type.h" -#include "optional.h" +#include "lib/luatype_boost_optional.h" #define ENABLE_TYPES_EXT using namespace rime; +using boost::optional; namespace { @@ -1356,10 +1357,33 @@ namespace LogReg { } namespace CommitEntryReg { using T = CommitEntry; + using D = DictEntry; vector get(const T& ce) { return ce.elements; } + bool update_entry(const T &t, const D& entry, int commit, const string& prefix_str) { + if (!t.memory) + return false; + auto user_dict = t.memory->user_dict(); + if (!user_dict || !user_dict->loaded()) + return false; + + return user_dict->UpdateEntry(entry, commit, prefix_str); + } + + bool update(const T& t, int commit) { + if (!t.memory) + return false; + auto user_dict = t.memory->user_dict(); + if (!user_dict || !user_dict->loaded()) + return false; + + for (const DictEntry* e : t.elements) { + user_dict->UpdateEntry(*e, commit); + } + return true; + } static const luaL_Reg funcs[] = { { NULL, NULL }, @@ -1367,6 +1391,8 @@ namespace CommitEntryReg { static const luaL_Reg methods[] = { {"get",WRAP(get)}, + {"update_entry",WRAP(update_entry)}, + {"update",WRAP(update)}, { NULL, NULL }, }; @@ -1380,12 +1406,17 @@ namespace CommitEntryReg { } namespace DictEntryReg { using T = DictEntry; - an make() { - return an(new T()); + + int raw_make(lua_State* L) { + an t = (lua_gettop(L)>0) + ? New(LuaType::todata(L,1)) : New(); + + LuaType>::pushdata(L, t); + return 1; } static const luaL_Reg funcs[] = { - {"DictEntry",WRAP(make)}, + {"DictEntry",raw_make}, { NULL, NULL }, };