@@ -143,25 +143,51 @@ void Config::SetItem(an<ConfigItem> item) {
143
143
set_modified ();
144
144
}
145
145
146
- static const ResourceType kConfigResourceType = {
146
+ const ResourceType ConfigComponentBase:: kConfigResourceType = {
147
147
" config" ,
148
148
" " ,
149
149
" .yaml" ,
150
150
};
151
151
152
- ConfigComponent::ConfigComponent ( )
152
+ ConfigComponentBase::ConfigComponentBase ( const ResourceType& resource_type )
153
153
: resource_resolver_(
154
- Service::instance ().CreateResourceResolver(kConfigResourceType )) {
154
+ Service::instance ().CreateResourceResolver(resource_type )) {
155
155
}
156
156
157
- ConfigComponent ::~ConfigComponent () {
157
+ ConfigComponentBase ::~ConfigComponentBase () {
158
158
}
159
159
160
- Config* ConfigComponent ::Create (const string& file_name) {
160
+ Config* ConfigComponentBase ::Create (const string& file_name) {
161
161
return new Config (GetConfigData (file_name));
162
162
}
163
163
164
- void ConfigComponent::InstallPlugin (ConfigCompilerPlugin* plugin) {
164
+ an<ConfigData> ConfigComponentBase::GetConfigData (const string& file_name) {
165
+ auto config_id = resource_resolver_->ToResourceId (file_name);
166
+ // keep a weak reference to the shared config data in the component
167
+ weak<ConfigData>& wp (cache_[config_id]);
168
+ if (wp.expired ()) { // create a new copy and load it
169
+ auto data = LoadConfig (config_id);
170
+ wp = data;
171
+ return data;
172
+ }
173
+ // obtain the shared copy
174
+ return wp.lock ();
175
+ }
176
+
177
+ an<ConfigData> ConfigLoader::LoadConfig (ResourceResolver* resource_resolver,
178
+ const string& config_id) {
179
+ auto data = New<ConfigData>();
180
+ data->LoadFromFile (
181
+ resource_resolver->ResolvePath (config_id).string (), nullptr );
182
+ data->set_auto_save (auto_save_);
183
+ return data;
184
+ }
185
+
186
+ ConfigBuilder::ConfigBuilder () {}
187
+
188
+ ConfigBuilder::~ConfigBuilder () {}
189
+
190
+ void ConfigBuilder::InstallPlugin (ConfigCompilerPlugin* plugin) {
165
191
plugins_.push_back (the<ConfigCompilerPlugin>(plugin));
166
192
}
167
193
@@ -182,7 +208,6 @@ struct MultiplePlugins : ConfigCompilerPlugin {
182
208
return ReviewedByAll (&ConfigCompilerPlugin::ReviewLinkOutput,
183
209
compiler, resource);
184
210
}
185
-
186
211
typedef bool (ConfigCompilerPlugin::*Reviewer)(ConfigCompiler* compiler,
187
212
an<ConfigResource> resource);
188
213
bool ReviewedByAll (Reviewer reviewer,
@@ -201,22 +226,15 @@ bool MultiplePlugins<Container>::ReviewedByAll(Reviewer reviewer,
201
226
return true ;
202
227
}
203
228
204
- an<ConfigData> ConfigComponent::GetConfigData (const string& file_name) {
205
- auto config_id = resource_resolver_->ToResourceId (file_name);
206
- // keep a weak reference to the shared config data in the component
207
- weak<ConfigData>& wp (cache_[config_id]);
208
- if (wp.expired ()) { // create a new copy and load it
209
- MultiplePlugins<decltype (plugins_)> multiple_plugins (plugins_);
210
- ConfigCompiler compiler (resource_resolver_.get (), &multiple_plugins);
211
- auto resource = compiler.Compile (file_name);
212
- if (resource->loaded && !compiler.Link (resource)) {
213
- LOG (ERROR) << " error loading config from: " << file_name;
214
- }
215
- wp = resource->data ;
216
- return resource->data ;
229
+ an<ConfigData> ConfigBuilder::LoadConfig (ResourceResolver* resource_resolver,
230
+ const string& config_id) {
231
+ MultiplePlugins<decltype (plugins_)> multiple_plugins (plugins_);
232
+ ConfigCompiler compiler (resource_resolver, &multiple_plugins);
233
+ auto resource = compiler.Compile (config_id);
234
+ if (resource->loaded && !compiler.Link (resource)) {
235
+ LOG (ERROR) << " error building config: " << config_id;
217
236
}
218
- // obtain the shared copy
219
- return wp.lock ();
237
+ return resource->data ;
220
238
}
221
239
222
240
} // namespace rime
0 commit comments