$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 
29 #include <cstdint> // for uint64_t
30 #include <memory> // for shared_ptr<>, unique_ptr<>, make_unique<>
31 #include <string> // for string
32 #include <type_traits> // for decay
33 #include <utility> // for move
34 
35 #include <fable/fable_fwd.hpp> // for Json
36 
37 #include <cloe/model.hpp> // for Model, ModelFactory
38 #include <cloe/sync.hpp> // for Sync
39 
55 #define DEFINE_COMPONENT_FACTORY(xFactoryType, xConfigType, xName, xDescription) \
56  class xFactoryType : public ::cloe::ComponentFactory { \
57  public: \
58  xFactoryType() : ComponentFactory(xName, xDescription) {} \
59  std::unique_ptr<::cloe::ComponentFactory> clone() const override { \
60  return std::make_unique<std::decay<decltype(*this)>::type>(*this); \
61  } \
62  std::unique_ptr<::cloe::Component> make( \
63  const ::fable::Conf&, std::vector<std::shared_ptr<::cloe::Component>>) const override; \
64  \
65  protected: \
66  ::cloe::Schema schema_impl() override { return config_.schema(); } \
67  \
68  private: \
69  xConfigType config_; \
70  };
71 
81 #define DEFINE_COMPONENT_FACTORY_MAKE(xFactoryType, xComponentType, xInputType) \
82  std::unique_ptr<::cloe::Component> xFactoryType::make( \
83  const ::fable::Conf& c, std::vector<std::shared_ptr<::cloe::Component>> comp) const { \
84  decltype(config_) conf{config_}; \
85  assert(comp.size() == 1); \
86  if (!c->is_null()) { \
87  conf.from_conf(c); \
88  } \
89  return std::make_unique<xComponentType>( \
90  this->name(), conf, std::dynamic_pointer_cast<xInputType>(std::move(comp.front()))); \
91  }
92 
93 namespace cloe {
94 
143 class Component : public Model {
144  public:
145  explicit Component(const std::string& name, const std::string& description = "")
146  : Model(name, description), id_(++gid_) {}
147  explicit Component(std::string&& name, std::string&& description = "")
148  : Model(std::move(name), std::move(description)), id_(++gid_) {}
149  virtual ~Component() noexcept = default;
150 
158  uint64_t id() const { return id_; }
159 
165  template <typename T>
166  T* as() {
167  return dynamic_cast<T*>(this);
168  }
169 
173  virtual fable::Json active_state() const = 0;
174 
181  Duration process(const Sync& sync) override { return sync.time(); }
182 
186  void reset() override {}
187 
191  void abort() override {}
192 
193  protected:
194  friend void to_json(fable::Json& j, const Component& c) {
195  j = c.active_state();
196  j["id"] = c.id();
197  j["name"] = c.name();
198  }
199 
200  private:
201  static uint64_t gid_;
202  uint64_t id_;
203 };
204 
210  public:
211  static constexpr char const* PLUGIN_TYPE = "component";
212  static constexpr char const* PLUGIN_API_VERSION = "2.0";
213 
217  using ModelFactory::ModelFactory;
218  virtual ~ComponentFactory() noexcept = default;
219 
232  virtual std::unique_ptr<ComponentFactory> clone() const = 0;
233 
240  virtual std::unique_ptr<Component> make(const fable::Conf& c,
241  std::vector<std::shared_ptr<Component>>) const = 0;
242 };
243 
244 } // namespace cloe
Definition: component.hpp:209
virtual std::unique_ptr< ComponentFactory > clone() const =0
virtual std::unique_ptr< Component > make(const fable::Conf &c, std::vector< std::shared_ptr< Component >>) const =0
Definition: component.hpp:143
uint64_t id() const
Definition: component.hpp:158
T * as()
Definition: component.hpp:166
Duration process(const Sync &sync) override
Definition: component.hpp:181
virtual fable::Json active_state() const =0
void abort() override
Definition: component.hpp:191
void reset() override
Definition: component.hpp:186
const std::string & description() const
Definition: entity.hpp:90
const std::string & name() const
Definition: entity.hpp:67
Definition: model.hpp:403
Definition: model.hpp:203
Definition: sync.hpp:34
virtual Duration time() const =0
Definition: conf.hpp:82
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36
nlohmann::json Json
Definition: fable_fwd.hpp:35