Apollo 10.0
自动驾驶开放平台
config_util.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
17#pragma once
18
19#include <algorithm>
20#include <string>
21
22#include "cyber/common/file.h"
23
24namespace apollo {
25namespace planning {
26
28 public:
36 static std::string TransformToPathName(const std::string& name);
37
45 static std::string GetFullPlanningClassName(const std::string& class_name);
46
56 template <typename T>
57 static bool LoadMergedConfig(const std::string& default_config_path,
58 const std::string& config_path, T* config);
59
70 template <typename T>
71 static bool LoadOverridedConfig(const std::string& default_config_path,
72 const std::string& config_path, T* config);
73};
74
75template <typename T>
76bool ConfigUtil::LoadMergedConfig(const std::string& default_config_path,
77 const std::string& config_path, T* config) {
78 CHECK_NOTNULL(config);
79 // Get the default config, will be merged by the user defined
80 // "spcific_config".
81 if (!apollo::cyber::common::LoadConfig<T>(default_config_path, config)) {
82 AERROR << "Failed to load default config file:" << default_config_path;
83 }
84 // Get the user defined "spcific_config".
85 T spcific_config;
86 if (!apollo::cyber::common::LoadConfig<T>(config_path, &spcific_config)) {
87 AWARN << "can not load user defined config file[" << config_path
88 << "], use default config instead";
89 return true;
90 }
91 config->MergeFrom(spcific_config);
92 return true;
93}
94
95template <typename T>
96bool ConfigUtil::LoadOverridedConfig(const std::string& default_config_path,
97 const std::string& config_path,
98 T* config) {
99 CHECK_NOTNULL(config);
100 // Get the user defined "spcific_config".
101 if (apollo::cyber::common::GetProtoFromFile(config_path, config)) {
102 return true;
103 }
104 // Get the default config.
105 if (!apollo::cyber::common::GetProtoFromFile(default_config_path, config)) {
106 AERROR << "Failed to load config file using the default config:"
107 << default_config_path;
108 return false;
109 }
110 return true;
111}
112
113} // namespace planning
114} // namespace apollo
static bool LoadMergedConfig(const std::string &default_config_path, const std::string &config_path, T *config)
Load the config and merge the default and user defined config.
Definition config_util.h:76
static std::string TransformToPathName(const std::string &name)
Tranform the name to part of path.
static std::string GetFullPlanningClassName(const std::string &class_name)
Given the class name of planning module, combine the namespace "apollo::planning::" with it to create...
static bool LoadOverridedConfig(const std::string &default_config_path, const std::string &config_path, T *config)
Load the config and override the default config by the user defined one.
Definition config_util.h:96
Planning module main class.
#define AERROR
Definition log.h:44
#define AWARN
Definition log.h:43
bool GetProtoFromFile(const std::string &file_name, google::protobuf::Message *message)
Parses the content of the file specified by the file_name as a representation of protobufs,...
Definition file.cc:132
class register implement
Definition arena_queue.h:37