$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 #ifndef CLOE_SIMULATOR_HPP_
27 #define CLOE_SIMULATOR_HPP_
28 
29 #include <memory> // for shared_ptr<>, unique_ptr<>, make_unique<>
30 #include <string> // for string
31 #include <type_traits> // for decay
32 
33 #include <cloe/model.hpp> // for Model, ModelFactory
34 
50 #define DEFINE_SIMULATOR_FACTORY(xFactoryType, xConfigType, xName, xDescription) \
51  class xFactoryType : public ::cloe::SimulatorFactory { \
52  public: \
53  xFactoryType() : ::cloe::SimulatorFactory(xName, xDescription) {} \
54  std::unique_ptr<::cloe::SimulatorFactory> clone() const override { \
55  return std::make_unique<std::decay<decltype(*this)>::type>(*this); \
56  } \
57  std::unique_ptr<::cloe::Simulator> make(const ::cloe::Conf&) const override; \
58  \
59  protected: \
60  ::cloe::Schema schema_impl() override { return config_.schema(); } \
61  \
62  private: \
63  xConfigType config_; \
64  };
65 
74 #define DEFINE_SIMULATOR_FACTORY_MAKE(xFactoryType, xSimulatorType) \
75  std::unique_ptr<::cloe::Simulator> xFactoryType::make(const ::cloe::Conf& c) const { \
76  decltype(config_) conf{config_}; \
77  if (!c->is_null()) { \
78  conf.from_conf(c); \
79  } \
80  return std::make_unique<xSimulatorType>(this->name(), conf); \
81  }
82 
83 namespace cloe {
84 
85 // Forward declarations:
86 class Sync; // from cloe/sync.hpp
87 class Vehicle; // from cloe/vehicle.hpp
88 
141 class Simulator : public Model {
142  public:
143  using Model::Model;
144  virtual ~Simulator() noexcept = default;
145 
151  virtual size_t num_vehicles() const = 0;
152 
168  virtual std::shared_ptr<Vehicle> get_vehicle(size_t i) const = 0;
169 
176  virtual std::shared_ptr<Vehicle> get_vehicle(const std::string& key) const = 0;
177 
194  Duration process(const Sync&) override = 0;
195 };
196 
202  public:
203  static constexpr char const* PLUGIN_TYPE = "simulator";
204  static constexpr char const* PLUGIN_API_VERSION = "2.0";
205 
209  using ModelFactory::ModelFactory;
210  virtual ~SimulatorFactory() noexcept = default;
211 
224  virtual std::unique_ptr<SimulatorFactory> clone() const = 0;
225 
232  virtual std::unique_ptr<Simulator> make(const Conf& c) const = 0;
233 };
234 
235 } // namespace cloe
236 
237 #endif // CLOE_SIMULATOR_HPP_
Definition: conf.hpp:74
Definition: simulator.hpp:141
virtual size_t num_vehicles() const =0
Definition: simulator.hpp:201
virtual std::shared_ptr< Vehicle > get_vehicle(size_t i) const =0
Definition: sync.hpp:35
Definition: coordinator.hpp:36
std::chrono::nanoseconds Duration
Definition: duration.hpp:45
Definition: model.hpp:414
Definition: model.hpp:214
Duration process(const Sync &) override=0