Apollo  6.0
Open source self driving car software
factory.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright 2017 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 
22 #pragma once
23 
24 #include <map>
25 #include <memory>
26 #include <utility>
27 
28 #include "cyber/common/macros.h"
29 
30 #include "cyber/common/log.h"
31 
36 namespace apollo {
37 namespace common {
38 namespace util {
39 
57 template <typename IdentifierType, class AbstractProduct,
58  class ProductCreator = AbstractProduct *(*)(),
59  class MapContainer = std::map<IdentifierType, ProductCreator>>
60 class Factory {
61  public:
70  bool Register(const IdentifierType &id, ProductCreator creator) {
71  return producers_.insert(std::make_pair(id, creator)).second;
72  }
73 
74  bool Contains(const IdentifierType &id) {
75  return producers_.find(id) != producers_.end();
76  }
77 
82  bool Unregister(const IdentifierType &id) {
83  return producers_.erase(id) == 1;
84  }
85 
86  void Clear() { producers_.clear(); }
87 
88  bool Empty() const { return producers_.empty(); }
89 
97  template <typename... Args>
98  std::unique_ptr<AbstractProduct> CreateObjectOrNull(const IdentifierType &id,
99  Args &&... args) {
100  auto id_iter = producers_.find(id);
101  if (id_iter != producers_.end()) {
102  return std::unique_ptr<AbstractProduct>(
103  (id_iter->second)(std::forward<Args>(args)...));
104  }
105  return nullptr;
106  }
107 
114  template <typename... Args>
115  std::unique_ptr<AbstractProduct> CreateObject(const IdentifierType &id,
116  Args &&... args) {
117  auto result = CreateObjectOrNull(id, std::forward<Args>(args)...);
118  AERROR_IF(!result) << "Factory could not create Object of type : " << id;
119  return result;
120  }
121 
122  private:
123  MapContainer producers_;
124 };
125 
126 } // namespace util
127 } // namespace common
128 } // namespace apollo
void Clear()
Definition: factory.h:86
PlanningContext is the runtime context in planning. It is persistent across multiple frames...
Definition: atomic_hash_map.h:25
Implements a Factory design pattern with Register and Create methods.
Definition: factory.h:60
bool Empty() const
Definition: factory.h:88
#define AERROR_IF(cond)
Definition: log.h:74
bool Contains(const IdentifierType &id)
Definition: factory.h:74
bool Unregister(const IdentifierType &id)
Unregisters the class with the given identifier.
Definition: factory.h:82
std::unique_ptr< AbstractProduct > CreateObjectOrNull(const IdentifierType &id, Args &&... args)
Creates and transfers membership of an object of type matching id. Need to register id before CreateO...
Definition: factory.h:98
std::unique_ptr< AbstractProduct > CreateObject(const IdentifierType &id, Args &&... args)
Creates and transfers membership of an object of type matching id. Need to register id before CreateO...
Definition: factory.h:115
bool Register(const IdentifierType &id, ProductCreator creator)
Registers the class given by the creator function, linking it to id. Registration must happen prior t...
Definition: factory.h:70