$darkmode
controller.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  */
25 #pragma once
26 
27 #include <memory> // for shared_ptr<>, unique_ptr<>, make_unique<>
28 #include <type_traits> // for decay
29 #include <utility> // for move
30 
31 #include <cloe/model.hpp> // for Model, ModelFactory
32 
48 #define DEFINE_CONTROLLER_FACTORY(xFactoryType, xConfigType, xName, xDescription) \
49  class xFactoryType : public ::cloe::ControllerFactory { \
50  public: \
51  xFactoryType() : ::cloe::ControllerFactory(xName, xDescription) {} \
52  std::unique_ptr<::cloe::ControllerFactory> clone() const override { \
53  return std::make_unique<std::decay<decltype(*this)>::type>(*this); \
54  } \
55  \
56  std::unique_ptr<::cloe::Controller> make(const ::fable::Conf&) const override; \
57  \
58  protected: \
59  ::cloe::Schema schema_impl() override { return config_.schema(); } \
60  \
61  private: \
62  xConfigType config_; \
63  };
64 
73 #define DEFINE_CONTROLLER_FACTORY_MAKE(xFactoryType, xControllerType) \
74  std::unique_ptr<::cloe::Controller> xFactoryType::make(const ::fable::Conf& c) const { \
75  decltype(config_) conf{config_}; \
76  if (!c->is_null()) { \
77  conf.from_conf(c); \
78  } \
79  return std::make_unique<xControllerType>(this->name(), conf); \
80  }
81 
82 namespace cloe {
83 
84 // Forward declarations:
85 class Vehicle; // from cloe/vehicle.hpp
86 
126 class Controller : public Model {
127  public:
128  using Model::Model;
129  virtual ~Controller() noexcept = default;
130 
136  virtual bool has_vehicle() const { return static_cast<bool>(veh_); }
137 
144  virtual std::shared_ptr<Vehicle> get_vehicle() const { return veh_; }
145 
154  virtual void set_vehicle(std::shared_ptr<Vehicle> v) { veh_ = std::move(v); }
155 
156  protected:
157  std::shared_ptr<Vehicle> veh_{nullptr};
158 };
159 
165  public:
166  static constexpr char const* PLUGIN_TYPE = "controller";
167  static constexpr char const* PLUGIN_API_VERSION = "2.0";
168 
172  using ModelFactory::ModelFactory;
173  virtual ~ControllerFactory() noexcept = default;
174 
187  virtual std::unique_ptr<ControllerFactory> clone() const = 0;
188 
195  virtual std::unique_ptr<Controller> make(const fable::Conf& c) const = 0;
196 };
197 
198 } // namespace cloe
Definition: controller.hpp:164
virtual std::unique_ptr< Controller > make(const fable::Conf &c) const =0
virtual std::unique_ptr< ControllerFactory > clone() const =0
Definition: controller.hpp:126
virtual std::shared_ptr< Vehicle > get_vehicle() const
Definition: controller.hpp:144
virtual bool has_vehicle() const
Definition: controller.hpp:136
virtual void set_vehicle(std::shared_ptr< Vehicle > v)
Definition: controller.hpp:154
Definition: model.hpp:403
Definition: model.hpp:203
Definition: conf.hpp:82