$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 #include <cloe/version.hpp> // for Version information
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 ::fable::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 ::fable::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 fable::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(fable::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 fable::Conf& c,
242  std::vector<std::shared_ptr<Component>>) const = 0;
243 };
244 
245 } // namespace cloe
Definition: component.hpp:210
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:144
uint64_t id() const
Definition: component.hpp:159
T * as()
Definition: component.hpp:167
Duration process(const Sync &sync) override
Definition: component.hpp:182
virtual fable::Json active_state() const =0
void abort() override
Definition: component.hpp:192
void reset() override
Definition: component.hpp:187
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:76
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36
nlohmann::json Json
Definition: fable_fwd.hpp:35