Apollo 10.0
自动驾驶开放平台
component_base.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#ifndef CYBER_COMPONENT_COMPONENT_BASE_H_
18#define CYBER_COMPONENT_COMPONENT_BASE_H_
19
20#include <atomic>
21#include <memory>
22#include <string>
23#include <vector>
24
25#include "gflags/gflags.h"
26
27#include "cyber/proto/component_conf.pb.h"
28
31#include "cyber/common/file.h"
32#include "cyber/node/node.h"
34
35namespace apollo {
36namespace cyber {
37
40
41class ComponentBase : public std::enable_shared_from_this<ComponentBase> {
42 public:
43 template <typename M>
45
46 virtual ~ComponentBase() {}
47
48 virtual bool Initialize(const ComponentConfig& config) { return false; }
49 virtual bool Initialize(const TimerComponentConfig& config) { return false; }
50 virtual void Shutdown() {
51 if (is_shutdown_.exchange(true)) {
52 return;
53 }
54
55 Clear();
56 for (auto& reader : readers_) {
57 reader->Shutdown();
58 }
60 }
61
62 template <typename T>
63 bool GetProtoConfig(T* config) const {
65 }
66
67 protected:
68 virtual bool Init() = 0;
69 virtual void Clear() { return; }
70 const std::string& ConfigFilePath() const { return config_file_path_; }
71
72 void LoadConfigFiles(const ComponentConfig& config) {
73 if (!config.config_file_path().empty()) {
75 "APOLLO_CONF_PATH", &config_file_path_)) {
76 AERROR << "conf file [" << config.config_file_path() << "] not found!";
78 } else {
79 AINFO << "use config file: " << config_file_path_;
80 }
81 }
82
83 if (!config.flag_file_path().empty()) {
84 std::string flag_file_path = config.flag_file_path();
86 "APOLLO_FLAG_PATH", &flag_file_path)) {
87 AERROR << "flag file [" << config.flag_file_path() << "] not found!";
88 } else {
89 AINFO << "use flag file: " << flag_file_path;
90 }
91 google::SetCommandLineOption("flagfile", flag_file_path.c_str());
92 }
93 }
94
96 if (!config.config_file_path().empty()) {
98 "APOLLO_CONF_PATH", &config_file_path_)) {
99 AERROR << "conf file [" << config.config_file_path() << "] not found!";
101 } else {
102 AINFO << "use config file: " << config_file_path_;
103 }
104 }
105
106 if (!config.flag_file_path().empty()) {
107 std::string flag_file_path = config.flag_file_path();
109 "APOLLO_FLAG_PATH", &flag_file_path)) {
110 AERROR << "flag file [" << config.flag_file_path() << "] not found!";
111 } else {
112 AINFO << "use flag file: " << flag_file_path;
113 }
114 google::SetCommandLineOption("flagfile", flag_file_path.c_str());
115 }
116 }
117
118 std::atomic<bool> is_shutdown_ = {false};
119 std::shared_ptr<Node> node_ = nullptr;
120 std::string config_file_path_ = "";
121 std::vector<std::shared_ptr<ReaderBase>> readers_;
122};
123
124} // namespace cyber
125} // namespace apollo
126
127#endif // CYBER_COMPONENT_COMPONENT_BASE_H_
bool GetProtoConfig(T *config) const
virtual bool Initialize(const ComponentConfig &config)
virtual bool Initialize(const TimerComponentConfig &config)
std::vector< std::shared_ptr< ReaderBase > > readers_
std::atomic< bool > is_shutdown_
void LoadConfigFiles(const TimerComponentConfig &config)
const std::string & ConfigFilePath() const
std::shared_ptr< Node > node_
void LoadConfigFiles(const ComponentConfig &config)
Reader subscribes a channel, it has two main functions:
Definition reader.h:69
virtual bool RemoveTask(const std::string &name)=0
#define AERROR
Definition log.h:44
#define AINFO
Definition log.h:42
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
bool GetFilePathWithEnv(const std::string &path, const std::string &env_var, std::string *file_path)
get file path, judgement priority:
Definition file.cc:436
class register implement
Definition arena_queue.h:37