$darkmode
simulator.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 <string> // for string
29 #include <type_traits> // for decay
30 
31 #include <cloe/model.hpp> // for Model, ModelFactory
32 
48 #define DEFINE_SIMULATOR_FACTORY(xFactoryType, xConfigType, xName, xDescription) \
49  class xFactoryType : public ::cloe::SimulatorFactory { \
50  public: \
51  xFactoryType() : ::cloe::SimulatorFactory(xName, xDescription) {} \
52  std::unique_ptr<::cloe::SimulatorFactory> clone() const override { \
53  return std::make_unique<std::decay<decltype(*this)>::type>(*this); \
54  } \
55  std::unique_ptr<::cloe::Simulator> make(const ::fable::Conf&) const override; \
56  \
57  protected: \
58  ::cloe::Schema schema_impl() override { return config_.schema(); } \
59  \
60  private: \
61  xConfigType config_; \
62  };
63 
72 #define DEFINE_SIMULATOR_FACTORY_MAKE(xFactoryType, xSimulatorType) \
73  std::unique_ptr<::cloe::Simulator> xFactoryType::make(const ::fable::Conf& c) const { \
74  decltype(config_) conf{config_}; \
75  if (!c->is_null()) { \
76  conf.from_conf(c); \
77  } \
78  return std::make_unique<xSimulatorType>(this->name(), conf); \
79  }
80 
81 namespace cloe {
82 
83 // Forward declarations:
84 class Sync; // from cloe/sync.hpp
85 class Vehicle; // from cloe/vehicle.hpp
86 
139 class Simulator : public Model {
140  public:
141  using Model::Model;
142  virtual ~Simulator() noexcept = default;
143 
149  virtual size_t num_vehicles() const = 0;
150 
166  virtual std::shared_ptr<Vehicle> get_vehicle(size_t i) const = 0;
167 
174  virtual std::shared_ptr<Vehicle> get_vehicle(const std::string& key) const = 0;
175 
192  Duration process(const Sync&) override = 0;
193 };
194 
200  public:
201  static constexpr char const* PLUGIN_TYPE = "simulator";
202  static constexpr char const* PLUGIN_API_VERSION = "2.0";
203 
207  using ModelFactory::ModelFactory;
208  virtual ~SimulatorFactory() noexcept = default;
209 
222  virtual std::unique_ptr<SimulatorFactory> clone() const = 0;
223 
230  virtual std::unique_ptr<Simulator> make(const fable::Conf& c) const = 0;
231 };
232 
233 } // namespace cloe
Definition: model.hpp:403
Definition: model.hpp:203
Definition: simulator.hpp:199
virtual std::unique_ptr< Simulator > make(const fable::Conf &c) const =0
virtual std::unique_ptr< SimulatorFactory > clone() const =0
Definition: simulator.hpp:139
virtual size_t num_vehicles() const =0
virtual std::shared_ptr< Vehicle > get_vehicle(size_t i) const =0
virtual std::shared_ptr< Vehicle > get_vehicle(const std::string &key) const =0
Duration process(const Sync &) override=0
Definition: sync.hpp:34
Definition: conf.hpp:81
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36