Apollo 10.0
自动驾驶开放平台
config_manager.h
浏览该文件的文档.
1/******************************************************************************
2 * Copyright 2018 The Apollo Authors. All Rights Reserved.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *****************************************************************************/
16 // #include "modules/perception/common/lib/config_manager/config_manager.h"
17
18 // ConfigManager* config_manager = lib::Singleton<ConfigManager>::
19 // get_instance();
20
21 // string model_name = "FrameClassifier";
22 // const ModelConfig* model_config = nullptr;
23 // if (!config_manager->GetModelConfig(model_name, &model_config)) {
24 // AERROR << "not found model: " << model_name;
25 // return false;
26 // }
27
28 // int int_value = 0;
29 // if (!model_config->get_value("my_param_name", &int_value)) {
30 // AERROR << "my_param_name not found."
31 // return false;
32 // }
33 // using int_value....
34//
35//
36// CONFIG FORMAT
37//
38// First you should define file: conf/config_manager.config,
39// you can set the path by gflags
40// --config_manager_path=conf/config_manager.config
41//
42// file content like as:
43// define all model config paths.
44// ############################################
45//
46// model_config_path: "./conf/frame_classifier.config"
47// model_config_path: "./conf/track_classifier.config"
48//
49// ############################################
50//
51// one line identify one model parameter config path.
52// ModelConfig config file like as:
53// file: ./conf/frame_classifier.config
54// #############################################
55//
56// model_configs {
57// # FrameClassifier model.
58// name: "FrameClassifier"
59// version: "1.0.0"
60// integer_params {
61// name: "threshold1"
62// value: 1
63// }
64//
65// integer_params {
66// name: "threshold2"
67// value: 2
68// }
69//
70// string_params {
71// name: "threshold3"
72// value: "str3"
73// }
74//
75// double_params {
76// name: "threshold4"
77// value: 4.0
78// }
79//
80// array_integer_params {
81// name: "array_p1"
82// values: 1
83// values: 2
84// values: 3
85// }
86//
87// array_string_params {
88// name: "array_p2"
89// values: "str1"
90// values: "str2"
91// values: "str3"
92// values: "str4"
93// }
94//
95// array_string_params {
96// name: "array_p3"
97// values: "str1"
98// values: "str2"
99// values: "str3"
100// values: "str4"
101// }
102//
103// array_double_params {
104// name: "array_p4"
105// values: 1.1
106// values: 1.2
107// values: 1.3
108// values: 1.4
109// }
110// }
111#pragma once
112
113#include <map>
114#include <sstream>
115#include <string>
116#include <utility>
117#include <vector>
118
119#include "cyber/common/macros.h"
121#include "modules/perception/common/proto/perception_config_schema.pb.h"
122
123namespace apollo {
124namespace perception {
125namespace lib {
126
127class ModelConfig;
128
130 public:
132
133 // thread-safe interface.
134 bool Init();
135
136 // thread-safe interface.
137 bool Reset();
138
139 bool GetModelConfig(const std::string &model_name,
140 const ModelConfig **model_config);
141
142 size_t NumModels() const { return model_config_map_.size(); }
143
144 const std::string &work_root() const { return work_root_; }
145
146 void set_work_root(const std::string &work_root) { work_root_ = work_root; }
147
148 private:
149 bool InitInternal();
150
151 // key: model_name
152 std::map<std::string, ModelConfig *> model_config_map_;
153 Mutex mutex_; // multi-thread init safe.
154 bool inited_ = false;
155 std::string work_root_; // ConfigManager work root dir.
156
158};
159
161 public:
164
166
167 std::string name() const { return name_; }
168
169 bool get_value(const std::string &name, int *value) const {
170 return get_value_from_map<int>(name, integer_param_map_, value);
171 }
172
173 bool get_value(const std::string &name, std::string *value) const {
174 return get_value_from_map<std::string>(name, string_param_map_, value);
175 }
176
177 bool get_value(const std::string &name, double *value) const {
178 return get_value_from_map<double>(name, double_param_map_, value);
179 }
180
181 bool get_value(const std::string &name, float *value) const {
182 return get_value_from_map<float>(name, float_param_map_, value);
183 }
184
185 bool get_value(const std::string &name, bool *value) const {
186 return get_value_from_map<bool>(name, bool_param_map_, value);
187 }
188
189 bool get_value(const std::string &name, std::vector<int> *values) const {
190 return get_value_from_map<std::vector<int>>(name, array_integer_param_map_,
191 values);
192 }
193
194 bool get_value(const std::string &name, std::vector<double> *values) const {
195 return get_value_from_map<std::vector<double>>(
196 name, array_double_param_map_, values);
197 }
198
199 bool get_value(const std::string &name, std::vector<float> *values) const {
200 return get_value_from_map<std::vector<float>>(name, array_float_param_map_,
201 values);
202 }
203
204 bool get_value(const std::string &name,
205 std::vector<std::string> *values) const {
206 return get_value_from_map<std::vector<std::string>>(
207 name, array_string_param_map_, values);
208 }
209
210 bool get_value(const std::string &name, std::vector<bool> *values) const {
211 return get_value_from_map<std::vector<bool>>(name, array_bool_param_map_,
212 values);
213 }
214
215 ModelConfig(const ModelConfig &) = delete;
217
218 private:
219 template <typename T>
220 bool get_value_from_map(const std::string &name,
221 const std::map<std::string, T> &container,
222 T *value) const;
223
224 template <typename T>
225 void RepeatedToVector(
226 const google::protobuf::RepeatedField<T> &repeated_values,
227 std::vector<T> *vec_values);
228
229 std::string name_;
230 std::string version_;
231
232 std::map<std::string, int> integer_param_map_;
233 std::map<std::string, std::string> string_param_map_;
234 std::map<std::string, double> double_param_map_;
235 std::map<std::string, float> float_param_map_;
236 std::map<std::string, bool> bool_param_map_;
237 std::map<std::string, std::vector<int>> array_integer_param_map_;
238 std::map<std::string, std::vector<std::string>> array_string_param_map_;
239 std::map<std::string, std::vector<double>> array_double_param_map_;
240 std::map<std::string, std::vector<float>> array_float_param_map_;
241 std::map<std::string, std::vector<bool>> array_bool_param_map_;
242};
243
244template <typename T>
245bool ModelConfig::get_value_from_map(const std::string &name,
246 const std::map<std::string, T> &container,
247 T *value) const {
248 typename std::map<std::string, T>::const_iterator citer =
249 container.find(name);
250
251 if (citer == container.end()) {
252 return false;
253 }
254
255 *value = citer->second;
256 return true;
257}
258
259template <typename T>
260void ModelConfig::RepeatedToVector(
261 const google::protobuf::RepeatedField<T> &repeated_values,
262 std::vector<T> *vec_list) {
263 vec_list->reserve(repeated_values.size());
264 for (T value : repeated_values) {
265 vec_list->push_back(value);
266 }
267}
268
270 public:
271 explicit ConfigManagerError(const std::string &error_info)
272 : error_info_(error_info) {}
273 std::string What() const { return error_info_; }
274
275 private:
276 std::string error_info_;
277};
278
279template <typename T>
281 public:
282 static T Read(const ModelConfig &config, const std::string &name) {
283 T ret;
284 if (!config.get_value(name, &ret)) {
285 std::stringstream ss;
286 ss << "Config name:" << config.name() << " read failed. "
287 << "type:" << typeid(T).name() << " name:" << name;
288 throw ConfigManagerError(ss.str());
289 }
290 return ret;
291 }
292};
293
294template <typename T>
295class ConfigRead<std::vector<T>> {
296 public:
297 static std::vector<T> Read(const ModelConfig &config,
298 const std::string &name) {
299 std::vector<T> ret;
300 if (!config.get_value(name, &ret)) {
301 std::stringstream ss;
302 ss << "Config name:" << config.name() << " read failed. "
303 << "type:vector<" << typeid(T).name() << "> name:" << name;
304 throw ConfigManagerError(ss.str());
305 }
306 return std::move(ret);
307 }
308};
309
310} // namespace lib
311} // namespace perception
312} // namespace apollo
ConfigManagerError(const std::string &error_info)
void set_work_root(const std::string &work_root)
const std::string & work_root() const
bool GetModelConfig(const std::string &model_name, const ModelConfig **model_config)
static std::vector< T > Read(const ModelConfig &config, const std::string &name)
static T Read(const ModelConfig &config, const std::string &name)
bool get_value(const std::string &name, std::vector< int > *values) const
bool get_value(const std::string &name, bool *value) const
bool get_value(const std::string &name, std::vector< double > *values) const
bool get_value(const std::string &name, float *value) const
ModelConfig operator=(const ModelConfig &)=delete
bool get_value(const std::string &name, std::vector< float > *values) const
bool get_value(const std::string &name, int *value) const
bool get_value(const std::string &name, std::string *value) const
ModelConfig(const ModelConfig &)=delete
bool get_value(const std::string &name, std::vector< std::string > *values) const
bool get_value(const std::string &name, double *value) const
bool Reset(const apollo::perception::ModelConfigProto &proto)
bool get_value(const std::string &name, std::vector< bool > *values) const
#define DECLARE_SINGLETON(classname)
Definition macros.h:52
class register implement
Definition arena_queue.h:37
Definition future.h:29