$darkmode
component.hpp
Go to the documentation of this file.
1 /*
2  * Copyright 2020 Robert Bosch GmbH
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  * SPDX-License-Identifier: Apache-2.0
17  */
27 #pragma once
28 #ifndef CLOE_COMPONENT_HPP_
29 #define CLOE_COMPONENT_HPP_
30 
31 #include <cstdint> // for uint64_t
32 #include <memory> // for shared_ptr<>, unique_ptr<>, make_unique<>
33 #include <string> // for string
34 #include <type_traits> // for decay
35 #include <utility> // for move
36 
37 #include <cloe/model.hpp> // for Model, ModelFactory
38 #include <cloe/sync.hpp> // for Sync
39 #include <fable/enum.hpp> // for ENUM_SERIALIZATION
40 
56 #define DEFINE_COMPONENT_FACTORY(xFactoryType, xConfigType, xName, xDescription) \
57  class xFactoryType : public ::cloe::ComponentFactory { \
58  public: \
59  xFactoryType() : ComponentFactory(xName, xDescription) {} \
60  std::unique_ptr<::cloe::ComponentFactory> clone() const override { \
61  return std::make_unique<std::decay<decltype(*this)>::type>(*this); \
62  } \
63  std::unique_ptr<::cloe::Component> make( \
64  const ::cloe::Conf&, std::vector<std::shared_ptr<::cloe::Component>>) const override; \
65  \
66  protected: \
67  ::cloe::Schema schema_impl() override { return config_.schema(); } \
68  \
69  private: \
70  xConfigType config_; \
71  };
72 
82 #define DEFINE_COMPONENT_FACTORY_MAKE(xFactoryType, xComponentType, xInputType) \
83  std::unique_ptr<::cloe::Component> xFactoryType::make( \
84  const ::cloe::Conf& c, std::vector<std::shared_ptr<::cloe::Component>> comp) const { \
85  decltype(config_) conf{config_}; \
86  assert(comp.size() == 1); \
87  if (!c->is_null()) { \
88  conf.from_conf(c); \
89  } \
90  return std::make_unique<xComponentType>( \
91  this->name(), conf, std::dynamic_pointer_cast<xInputType>(std::move(comp.front()))); \
92  }
93 
94 namespace cloe {
95 
144 class Component : public Model {
145  public:
146  explicit Component(const std::string& name, const std::string& description = "")
147  : Model(name, description), id_(++gid_) {}
148  explicit Component(std::string&& name, std::string&& description = "")
149  : Model(std::move(name), std::move(description)), id_(++gid_) {}
150  virtual ~Component() noexcept = default;
151 
159  uint64_t id() const { return id_; }
160 
166  template <typename T>
167  T* as() {
168  return dynamic_cast<T*>(this);
169  }
170 
174  virtual Json active_state() const = 0;
175 
182  Duration process(const Sync& sync) override { return sync.time(); }
183 
187  void reset() override {}
188 
192  void abort() override {}
193 
194  protected:
195  friend void to_json(Json& j, const Component& c) {
196  j = c.active_state();
197  j["id"] = c.id();
198  j["name"] = c.name();
199  }
200 
201  private:
202  static uint64_t gid_;
203  uint64_t id_;
204 };
205 
211  public:
212  static constexpr char const* PLUGIN_TYPE = "component";
213  static constexpr char const* PLUGIN_API_VERSION = "2.0";
214 
218  using ModelFactory::ModelFactory;
219  virtual ~ComponentFactory() noexcept = default;
220 
233  virtual std::unique_ptr<ComponentFactory> clone() const = 0;
234 
241  virtual std::unique_ptr<Component> make(const Conf& c,
242  std::vector<std::shared_ptr<Component>>) const = 0;
243 };
244 
245 } // namespace cloe
246 
247 #endif // CLOE_COMPONENT_HPP_
Definition: conf.hpp:74
uint64_t id() const
Definition: component.hpp:159
virtual Duration time() const =0
T * as()
Definition: component.hpp:167
Definition: sync.hpp:35
Definition: coordinator.hpp:36
Duration process(const Sync &sync) override
Definition: component.hpp:182
std::chrono::nanoseconds Duration
Definition: duration.hpp:45
Definition: component.hpp:210
virtual Json active_state() const =0
void abort() override
Definition: component.hpp:192
std::string name() const
Definition: entity.hpp:68
Definition: component.hpp:144
std::string description() const
Definition: entity.hpp:91
Definition: model.hpp:414
Definition: model.hpp:214
void reset() override
Definition: component.hpp:187