$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 #ifndef CLOE_CONTROLLER_HPP_
27 #define CLOE_CONTROLLER_HPP_
28 
29 #include <memory> // for shared_ptr<>, unique_ptr<>, make_unique<>
30 #include <type_traits> // for decay
31 #include <utility> // for move
32 
33 #include <cloe/model.hpp> // for Model, ModelFactory
34 
50 #define DEFINE_CONTROLLER_FACTORY(xFactoryType, xConfigType, xName, xDescription) \
51  class xFactoryType : public ::cloe::ControllerFactory { \
52  public: \
53  xFactoryType() : ::cloe::ControllerFactory(xName, xDescription) {} \
54  std::unique_ptr<::cloe::ControllerFactory> clone() const override { \
55  return std::make_unique<std::decay<decltype(*this)>::type>(*this); \
56  } \
57  \
58  std::unique_ptr<::cloe::Controller> make(const ::cloe::Conf&) const override; \
59  \
60  protected: \
61  ::cloe::Schema schema_impl() override { return config_.schema(); } \
62  \
63  private: \
64  xConfigType config_; \
65  };
66 
75 #define DEFINE_CONTROLLER_FACTORY_MAKE(xFactoryType, xControllerType) \
76  std::unique_ptr<::cloe::Controller> xFactoryType::make(const ::cloe::Conf& c) const { \
77  decltype(config_) conf{config_}; \
78  if (!c->is_null()) { \
79  conf.from_conf(c); \
80  } \
81  return std::make_unique<xControllerType>(this->name(), conf); \
82  }
83 
84 namespace cloe {
85 
86 // Forward declarations:
87 class Vehicle; // from cloe/vehicle.hpp
88 
128 class Controller : public Model {
129  public:
130  using Model::Model;
131  virtual ~Controller() noexcept = default;
132 
138  virtual bool has_vehicle() const { return static_cast<bool>(veh_); }
139 
146  virtual std::shared_ptr<Vehicle> get_vehicle() const { return veh_; }
147 
156  virtual void set_vehicle(std::shared_ptr<Vehicle> v) { veh_ = std::move(v); }
157 
158  protected:
159  std::shared_ptr<Vehicle> veh_{nullptr};
160 };
161 
167  public:
168  static constexpr char const* PLUGIN_TYPE = "controller";
169  static constexpr char const* PLUGIN_API_VERSION = "2.0";
170 
174  using ModelFactory::ModelFactory;
175  virtual ~ControllerFactory() noexcept = default;
176 
189  virtual std::unique_ptr<ControllerFactory> clone() const = 0;
190 
197  virtual std::unique_ptr<Controller> make(const Conf& c) const = 0;
198 };
199 
200 } // namespace cloe
201 
202 #endif // CLOE_CONTROLLER_HPP_
Definition: conf.hpp:74
Definition: coordinator.hpp:36
virtual bool has_vehicle() const
Definition: controller.hpp:138
Definition: controller.hpp:166
virtual std::shared_ptr< Vehicle > get_vehicle() const
Definition: controller.hpp:146
Definition: model.hpp:414
Definition: model.hpp:214
virtual void set_vehicle(std::shared_ptr< Vehicle > v)
Definition: controller.hpp:156
Definition: controller.hpp:128