Skip to content

Commit

Permalink
fix(schema): create a "schema" component that opens Config by schema_id
Browse files Browse the repository at this point in the history
  • Loading branch information
kionz authored and lotem committed Aug 31, 2017
1 parent 7fe4428 commit 555f990
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
5 changes: 4 additions & 1 deletion src/rime/core_module.cc
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,17 @@

// built-in components
#include <rime/config.h>
#include <rime/schema.h>

using namespace rime;

static void rime_core_initialize() {
LOG(INFO) << "registering core components.";
Registry& r = Registry::instance();

r.Register("config", new ConfigComponent);
auto config = new ConfigComponent;
r.Register("config", config);
r.Register("schema", new SchemaComponent(config));
}

static void rime_core_finalize() {
Expand Down
10 changes: 7 additions & 3 deletions src/rime/schema.cc
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ Schema::Schema()

Schema::Schema(const string& schema_id)
: schema_id_(schema_id) {
config_.reset(Config::Require("config")->Create(
boost::starts_with(schema_id_, L".") ?
schema_id.substr(1) : schema_id + ".schema"));
config_.reset(boost::starts_with(schema_id_, L".") ?
Config::Require("config")->Create(schema_id.substr(1)) :
Config::Require("schema")->Create(schema_id));
FetchUsefulConfigItems();
}

Expand All @@ -43,4 +43,8 @@ void Schema::FetchUsefulConfigItems() {
config_->GetString("menu/alternative_select_keys", &select_keys_);
}

Config* SchemaComponent::Create(const string& schema_id) {
return config_component_->Create(schema_id + ".schema");
}

} // namespace rime
13 changes: 13 additions & 0 deletions src/rime/schema.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,19 @@ class Schema {
string select_keys_;
};

class SchemaComponent : public Config::Component {
public:
SchemaComponent(ConfigComponent* config_component)
: config_component_(config_component) {
}
// NOTE: creates `Config` for the schema
Config* Create(const string& schema_id) override;
private:
// we do not own the ConfigComponent, do not try to deallocate it
// also be careful that there is no guarantee it will outlive us
ConfigComponent* config_component_;
};

} // namespace rime

#endif // RIME_SCHEMA_H_
2 changes: 1 addition & 1 deletion src/rime_api.cc
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ RIME_API Bool RimeSelectSchema(RimeSessionId session_id, const char* schema_id)

RIME_API Bool RimeSchemaOpen(const char *schema_id, RimeConfig* config) {
if (!schema_id || !config) return False;
Config::Component* cc = Config::Require("schema_config");
Config::Component* cc = Config::Require("schema");
if (!cc) return False;
Config* c = cc->Create(schema_id);
if (!c) return False;
Expand Down

0 comments on commit 555f990

Please sign in to comment.