Apollo  6.0
Open source self driving car software
component_base.h
Go to the documentation of this file.
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 
35 namespace apollo {
36 namespace cyber {
37 
38 using apollo::cyber::proto::ComponentConfig;
39 using apollo::cyber::proto::TimerComponentConfig;
40 
41 class 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()) {
74  if (config.config_file_path()[0] != '/') {
76  config.config_file_path());
77  } else {
78  config_file_path_ = config.config_file_path();
79  }
80  }
81 
82  if (!config.flag_file_path().empty()) {
83  std::string flag_file_path = config.flag_file_path();
84  if (flag_file_path[0] != '/') {
85  flag_file_path =
86  common::GetAbsolutePath(common::WorkRoot(), flag_file_path);
87  }
88  google::SetCommandLineOption("flagfile", flag_file_path.c_str());
89  }
90  }
91 
92  void LoadConfigFiles(const TimerComponentConfig& config) {
93  if (!config.config_file_path().empty()) {
94  if (config.config_file_path()[0] != '/') {
96  config.config_file_path());
97  } else {
98  config_file_path_ = config.config_file_path();
99  }
100  }
101 
102  if (!config.flag_file_path().empty()) {
103  std::string flag_file_path = config.flag_file_path();
104  if (flag_file_path[0] != '/') {
105  flag_file_path =
106  common::GetAbsolutePath(common::WorkRoot(), flag_file_path);
107  }
108  google::SetCommandLineOption("flagfile", flag_file_path.c_str());
109  }
110  }
111 
112  std::atomic<bool> is_shutdown_ = {false};
113  std::shared_ptr<Node> node_ = nullptr;
114  std::string config_file_path_ = "";
115  std::vector<std::shared_ptr<ReaderBase>> readers_;
116 };
117 
118 } // namespace cyber
119 } // namespace apollo
120 
121 #endif // CYBER_COMPONENT_COMPONENT_BASE_H_
std::vector< std::shared_ptr< ReaderBase > > readers_
Definition: component_base.h:115
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Reader subscribes a channel, it has two main functions:
Definition: reader.h:68
virtual bool Initialize(const ComponentConfig &config)
Definition: component_base.h:48
std::string GetAbsolutePath(const std::string &prefix, const std::string &relative_path)
Get absolute path by concatenating prefix and relative_path.
virtual void Shutdown()
Definition: component_base.h:50
const std::string & ConfigFilePath() const
Definition: component_base.h:70
virtual bool RemoveTask(const std::string &name)=0
virtual bool Initialize(const TimerComponentConfig &config)
Definition: component_base.h:49
void LoadConfigFiles(const ComponentConfig &config)
Definition: component_base.h:72
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, and merges the parsed content to the proto.
std::string config_file_path_
Definition: component_base.h:114
void LoadConfigFiles(const TimerComponentConfig &config)
Definition: component_base.h:92
virtual void Clear()
Definition: component_base.h:69
Definition: component_base.h:41
const std::string WorkRoot()
Definition: environment.h:40
std::shared_ptr< Node > node_
Definition: component_base.h:113
virtual ~ComponentBase()
Definition: component_base.h:46
bool GetProtoConfig(T *config) const
Definition: component_base.h:63
std::atomic< bool > is_shutdown_
Definition: component_base.h:112