librime: config_component.h Source File

| librime 1.2 Rime Input Method Engine, the core library

config_component.h
Go to the documentation of this file.
1//
2// Copyright RIME Developers
3// Distributed under the BSD License
4//
5// 2011-4-6 Zou xu <zouivex@gmail.com>
6//
7#ifndef RIME_CONFIG_COMPONENT_H_
8#define RIME_CONFIG_COMPONENT_H_
9
10#include <iostream>
11#include <type_traits>
12#include <rime/common.h>
13#include <rime/component.h>
14#include <rime/config/config_types.h>
15#include <rime/resource.h>
16
17namespace rime {
18
19class ConfigData;
20
21 class Config : public Class<Config, const string&>, public ConfigItemRef {
22 public:
23 // CAVEAT: Config instances created without argument will NOT
24 // be managed by ConfigComponent
25 RIME_DLL Config();
26 RIME_DLL virtual ~Config();
27 // instances of Config with identical config id share a copy of config data
28 // in the ConfigComponent
29 explicit Config(an<ConfigData> data);
30
31 // returns whether actually saved to file.
32 bool Save();
33 bool LoadFromStream(std::istream& stream);
34 bool SaveToStream(std::ostream& stream);
35 RIME_DLL bool LoadFromFile(const path& file_path);
36 RIME_DLL bool SaveToFile(const path& file_path);
37
38 // access a tree node of a particular type with "path/to/node"
39 RIME_DLL bool IsNull(const string& path);
40 bool IsValue(const string& path);
41 RIME_DLL bool IsList(const string& path);
42 RIME_DLL bool IsMap(const string& path);
43 RIME_DLL bool GetBool(const string& path, bool* value);
44 RIME_DLL bool GetInt(const string& path, int* value);
45 RIME_DLL bool GetDouble(const string& path, double* value);
46 RIME_DLL bool GetString(const string& path, string* value);
47 RIME_DLL size_t GetListSize(const string& path);
48
49 an<ConfigItem> GetItem(const string& path);
50 an<ConfigValue> GetValue(const string& path);
51 RIME_DLL an<ConfigList> GetList(const string& path);
52 RIME_DLL an<ConfigMap> GetMap(const string& path);
53
54 // setters
55 bool SetBool(const string& path, bool value);
56 RIME_DLL bool SetInt(const string& path, int value);
57 bool SetDouble(const string& path, double value);
58 RIME_DLL bool SetString(const string& path, const char* value);
59 bool SetString(const string& path, const string& value);
60 // setter for adding or replacing items in the tree
61 RIME_DLL bool SetItem(const string& path, an<ConfigItem> item);
62 using ConfigItemRef::operator=;
63
64 protected:
65 an<ConfigItem> GetItem() const;
66 void SetItem(an<ConfigItem> item);
67
68 an<ConfigData> data_;
69};
70
71class ConfigCompiler;
72class ConfigCompilerPlugin;
73struct ConfigResource;
74
75 struct ConfigResourceProvider {
76 RIME_DLL static const ResourceType kDefaultResourceType;
77 RIME_DLL static ResourceResolver* CreateResourceResolver(
78 const ResourceType& resource_type);
79};
80
81 struct DeployedConfigResourceProvider {
82 RIME_DLL static const ResourceType kDefaultResourceType;
83 RIME_DLL static ResourceResolver* CreateResourceResolver(
84 const ResourceType& resource_type);
85};
86
87 struct UserConfigResourceProvider {
88 RIME_DLL static const ResourceType kDefaultResourceType;
89 RIME_DLL static ResourceResolver* CreateResourceResolver(
90 const ResourceType& resource_type);
91};
92
93 class ConfigComponentBase : public Config::Component {
94 public:
95 RIME_DLL ConfigComponentBase(ResourceResolver* resource_resolver);
96 RIME_DLL virtual ~ConfigComponentBase();
97 RIME_DLL Config* Create(const string& file_name);
98
99 protected:
100 virtual an<ConfigData> LoadConfig(const string& config_id) = 0;
101 the<ResourceResolver> resource_resolver_;
102
103 private:
104 an<ConfigData> GetConfigData(const string& file_name);
105 map<string, weak<ConfigData>> cache_;
106};
107
108template <class Loader, class ResourceProvider = ConfigResourceProvider>
109 class ConfigComponent : public ConfigComponentBase {
110 public:
111 ConfigComponent(const ResourceType& resource_type =
112 ResourceProvider::kDefaultResourceType)
113 : ConfigComponentBase(
114 ResourceProvider::CreateResourceResolver(resource_type)) {}
115 ConfigComponent(function<void(Loader* loader)> setup)
116 : ConfigComponentBase(ResourceProvider::CreateResourceResolver(
117 ResourceProvider::kDefaultResourceType)) {
118 setup(&loader_);
119 }
120
121 private:
122 an<ConfigData> LoadConfig(const string& config_id) override {
123 return loader_.LoadConfig(resource_resolver_.get(), config_id);
124 }
125 Loader loader_;
126};
127
128 class ConfigLoader {
129 public:
130 RIME_DLL an<ConfigData> LoadConfig(ResourceResolver* resource_resolver,
131 const string& config_id);
132 void set_auto_save(bool auto_save) { auto_save_ = auto_save; }
133
134 private:
135 bool auto_save_ = false;
136};
137
138 class ConfigBuilder {
139 public:
140 RIME_DLL ConfigBuilder();
141 RIME_DLL virtual ~ConfigBuilder();
142 RIME_DLL an<ConfigData> LoadConfig(ResourceResolver* resource_resolver,
143 const string& config_id);
144 void InstallPlugin(ConfigCompilerPlugin* plugin);
145 bool ApplyPlugins(ConfigCompiler* compiler, an<ConfigResource> resource);
146
147 private:
148 vector<the<ConfigCompilerPlugin>> plugins_;
149};
150
151} // namespace rime
152
153#endif // RIME_CONFIG_COMPONENT_H_
common.h
component.h
config_types.h
resource.h
RIME_DLL
#define RIME_DLL
Definition rime_api.h:33
rime
Definition algebra.cc:12
rime::an
std::shared_ptr< T > an
Definition common.h:60
rime::the
std::unique_ptr< T > the
Definition common.h:58
rime::path
Definition common.h:84
rime::Class
Definition component.h:21
rime::ConfigResource
Definition config_compiler.h:15
rime::ConfigCompiler
Definition config_compiler.h:41
rime::Config
Definition config_component.h:21
rime::Config::GetString
RIME_DLL bool GetString(const string &path, string *value)
Definition config_component.cc:84
rime::Config::GetBool
RIME_DLL bool GetBool(const string &path, bool *value)
Definition config_component.cc:66
rime::Config::GetList
RIME_DLL an< ConfigList > GetList(const string &path)
Definition config_component.cc:106
rime::Config::IsMap
RIME_DLL bool IsMap(const string &path)
Definition config_component.cc:61
rime::Config::SetItem
RIME_DLL bool SetItem(const string &path, an< ConfigItem > item)
Definition config_component.cc:136
rime::Config::SetString
RIME_DLL bool SetString(const string &path, const char *value)
Definition config_component.cc:128
rime::Config::Save
bool Save()
Definition config_component.cc:26
rime::Config::IsValue
bool IsValue(const string &path)
Definition config_component.cc:51
rime::Config::SaveToFile
RIME_DLL bool SaveToFile(const path &file_path)
Definition config_component.cc:42
rime::Config::LoadFromFile
RIME_DLL bool LoadFromFile(const path &file_path)
Definition config_component.cc:38
rime::Config::data_
an< ConfigData > data_
Definition config_component.h:68
rime::Config::SaveToStream
bool SaveToStream(std::ostream &stream)
Definition config_component.cc:34
rime::Config::IsList
RIME_DLL bool IsList(const string &path)
Definition config_component.cc:56
rime::Config::SetBool
bool SetBool(const string &path, bool value)
Definition config_component.cc:116
rime::Config::GetListSize
RIME_DLL size_t GetListSize(const string &path)
Definition config_component.cc:90
rime::Config::SetInt
RIME_DLL bool SetInt(const string &path, int value)
Definition config_component.cc:120
rime::Config::GetMap
RIME_DLL an< ConfigMap > GetMap(const string &path)
Definition config_component.cc:111
rime::Config::Config
RIME_DLL Config()
Definition config_component.cc:18
rime::Config::LoadFromStream
bool LoadFromStream(std::istream &stream)
Definition config_component.cc:30
rime::Config::GetItem
an< ConfigItem > GetItem(const string &path)
Definition config_component.cc:96
rime::Config::GetDouble
RIME_DLL bool GetDouble(const string &path, double *value)
Definition config_component.cc:78
rime::Config::SetDouble
bool SetDouble(const string &path, double value)
Definition config_component.cc:124
rime::Config::GetValue
an< ConfigValue > GetValue(const string &path)
Definition config_component.cc:101
rime::Config::GetInt
RIME_DLL bool GetInt(const string &path, int *value)
Definition config_component.cc:72
rime::Config::IsNull
RIME_DLL bool IsNull(const string &path)
Definition config_component.cc:46
rime::ConfigResourceProvider
Definition config_component.h:75
rime::ConfigResourceProvider::CreateResourceResolver
static RIME_DLL ResourceResolver * CreateResourceResolver(const ResourceType &resource_type)
Definition config_component.cc:152
rime::ConfigResourceProvider::kDefaultResourceType
static RIME_DLL const ResourceType kDefaultResourceType
Definition config_component.h:76
rime::DeployedConfigResourceProvider
Definition config_component.h:81
rime::DeployedConfigResourceProvider::kDefaultResourceType
static RIME_DLL const ResourceType kDefaultResourceType
Definition config_component.h:82
rime::DeployedConfigResourceProvider::CreateResourceResolver
static RIME_DLL ResourceResolver * CreateResourceResolver(const ResourceType &resource_type)
Definition config_component.cc:160
rime::UserConfigResourceProvider
Definition config_component.h:87
rime::UserConfigResourceProvider::kDefaultResourceType
static RIME_DLL const ResourceType kDefaultResourceType
Definition config_component.h:88
rime::UserConfigResourceProvider::CreateResourceResolver
static RIME_DLL ResourceResolver * CreateResourceResolver(const ResourceType &resource_type)
Definition config_component.cc:168
rime::ConfigComponentBase::LoadConfig
virtual an< ConfigData > LoadConfig(const string &config_id)=0
rime::ConfigComponentBase::resource_resolver_
the< ResourceResolver > resource_resolver_
Definition config_component.h:101
rime::ConfigComponentBase::Create
RIME_DLL Config * Create(const string &file_name)
Definition config_component.cc:178
rime::ConfigComponentBase::ConfigComponentBase
RIME_DLL ConfigComponentBase(ResourceResolver *resource_resolver)
Definition config_component.cc:173
rime::ConfigComponent::ConfigComponent
ConfigComponent(function< void(Loader *loader)> setup)
Definition config_component.h:115
rime::ConfigComponent::ConfigComponent
ConfigComponent(const ResourceType &resource_type=ResourceProvider::kDefaultResourceType)
Definition config_component.h:111
rime::ConfigLoader
Definition config_component.h:128
rime::ConfigLoader::LoadConfig
RIME_DLL an< ConfigData > LoadConfig(ResourceResolver *resource_resolver, const string &config_id)
Definition config_component.cc:195
rime::ConfigLoader::set_auto_save
void set_auto_save(bool auto_save)
Definition config_component.h:132
rime::ConfigBuilder::InstallPlugin
void InstallPlugin(ConfigCompilerPlugin *plugin)
Definition config_component.cc:207
rime::ConfigBuilder::LoadConfig
RIME_DLL an< ConfigData > LoadConfig(ResourceResolver *resource_resolver, const string &config_id)
Definition config_component.cc:244
rime::ConfigBuilder::ConfigBuilder
RIME_DLL ConfigBuilder()
Definition config_component.cc:203
rime::ConfigBuilder::ApplyPlugins
bool ApplyPlugins(ConfigCompiler *compiler, an< ConfigResource > resource)
rime::ConfigData
Definition config_data.h:17
rime::ConfigItemRef::ConfigItemRef
ConfigItemRef(ConfigData *data)
Definition config_types.h:128
rime::ConfigCompilerPlugin
Definition plugins.h:15
rime::ResourceType
Definition resource.h:16
rime::ResourceResolver
Definition resource.h:22