Apollo  6.0
Open source self driving car software
registerer.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 #pragma once
18 
19 #include <map>
20 #include <string>
21 #include <vector>
22 
23 #include "cyber/common/log.h"
24 
25 namespace apollo {
26 namespace perception {
27 namespace lib {
28 
29 // idea from boost any but make it more simple and don't use type_info.
30 class Any {
31  public:
32  Any() : content_(NULL) {}
33 
34  template <typename ValueType>
35  explicit Any(const ValueType &value)
36  : content_(new Holder<ValueType>(value)) {}
37 
38  Any(const Any &other)
39  : content_(other.content_ ? other.content_->Clone() : nullptr) {}
40 
41  ~Any() { delete content_; }
42 
43  template <typename ValueType>
44  ValueType *AnyCast() {
45  return content_ ? &(static_cast<Holder<ValueType> *>(content_)->held_)
46  : nullptr;
47  }
48 
49  private:
50  class PlaceHolder {
51  public:
52  virtual ~PlaceHolder() {}
53  virtual PlaceHolder *Clone() const = 0;
54  };
55 
56  template <typename ValueType>
57  class Holder : public PlaceHolder {
58  public:
59  explicit Holder(const ValueType &value) : held_(value) {}
60  virtual ~Holder() {}
61  virtual PlaceHolder *Clone() const { return new Holder(held_); }
62 
63  ValueType held_;
64  };
65 
66  PlaceHolder *content_;
67 };
68 
70  public:
72  virtual ~ObjectFactory() {}
73  virtual Any NewInstance() { return Any(); }
74  ObjectFactory(const ObjectFactory &) = delete;
75  ObjectFactory &operator=(const ObjectFactory &) = delete;
76 
77  private:
78 };
79 
80 typedef std::map<std::string, ObjectFactory *> FactoryMap;
81 typedef std::map<std::string, FactoryMap> BaseClassMap;
82 BaseClassMap &GlobalFactoryMap();
83 
85  const std::string &base_class_name,
86  std::vector<std::string> *registered_derived_classes_names);
87 
88 } // namespace lib
89 } // namespace perception
90 } // namespace apollo
91 
92 #define PERCEPTION_REGISTER_REGISTERER(base_class) \
93  class base_class##Registerer { \
94  typedef ::apollo::perception::lib::Any Any; \
95  typedef ::apollo::perception::lib::FactoryMap FactoryMap; \
96  \
97  public: \
98  static base_class *GetInstanceByName(const ::std::string &name) { \
99  FactoryMap &map = \
100  ::apollo::perception::lib::GlobalFactoryMap()[#base_class]; \
101  FactoryMap::iterator iter = map.find(name); \
102  if (iter == map.end()) { \
103  for (auto c : map) { \
104  AERROR << "Instance:" << c.first; \
105  } \
106  AERROR << "Get instance " << name << " failed."; \
107  return nullptr; \
108  } \
109  Any object = iter->second->NewInstance(); \
110  return *(object.AnyCast<base_class *>()); \
111  } \
112  static std::vector<base_class *> GetAllInstances() { \
113  std::vector<base_class *> instances; \
114  FactoryMap &map = \
115  ::apollo::perception::lib::GlobalFactoryMap()[#base_class]; \
116  instances.reserve(map.size()); \
117  for (auto item : map) { \
118  Any object = item.second->NewInstance(); \
119  instances.push_back(*(object.AnyCast<base_class *>())); \
120  } \
121  return instances; \
122  } \
123  static const ::std::string GetUniqInstanceName() { \
124  FactoryMap &map = \
125  ::apollo::perception::lib::GlobalFactoryMap()[#base_class]; \
126  CHECK_EQ(map.size(), 1U) << map.size(); \
127  return map.begin()->first; \
128  } \
129  static base_class *GetUniqInstance() { \
130  FactoryMap &map = \
131  ::apollo::perception::lib::GlobalFactoryMap()[#base_class]; \
132  CHECK_EQ(map.size(), 1U) << map.size(); \
133  Any object = map.begin()->second->NewInstance(); \
134  return *(object.AnyCast<base_class *>()); \
135  } \
136  static bool IsValid(const ::std::string &name) { \
137  FactoryMap &map = \
138  ::apollo::perception::lib::GlobalFactoryMap()[#base_class]; \
139  return map.find(name) != map.end(); \
140  } \
141  };
142 
143 #define PERCEPTION_REGISTER_CLASS(clazz, name) \
144  namespace { \
145  class ObjectFactory##name : public apollo::perception::lib::ObjectFactory { \
146  public: \
147  virtual ~ObjectFactory##name() {} \
148  virtual ::apollo::perception::lib::Any NewInstance() { \
149  return ::apollo::perception::lib::Any(new name()); \
150  } \
151  }; \
152  __attribute__((constructor)) void RegisterFactory##name() { \
153  ::apollo::perception::lib::FactoryMap &map = \
154  ::apollo::perception::lib::GlobalFactoryMap()[#clazz]; \
155  if (map.find(#name) == map.end()) map[#name] = new ObjectFactory##name(); \
156  } \
157  } // namespace
Any(const ValueType &value)
Definition: registerer.h:35
std::map< std::string, FactoryMap > BaseClassMap
Definition: registerer.h:81
Definition: registerer.h:30
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Any()
Definition: registerer.h:32
ObjectFactory()
Definition: registerer.h:71
ValueType * AnyCast()
Definition: registerer.h:44
virtual ~ObjectFactory()
Definition: registerer.h:72
BaseClassMap & GlobalFactoryMap()
bool GetRegisteredClasses(const std::string &base_class_name, std::vector< std::string > *registered_derived_classes_names)
Definition: registerer.h:69
Any(const Any &other)
Definition: registerer.h:38
virtual Any NewInstance()
Definition: registerer.h:73
apollo::cyber::base::std value
~Any()
Definition: registerer.h:41
std::map< std::string, ObjectFactory * > FactoryMap
Definition: registerer.h:80