$darkmode
model.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  */
49 #pragma once
50 
51 #include <fable/confable.hpp> // for Confable
52 
53 #include <cloe/cloe_fwd.hpp> // for Sync, Registrar
54 #include <cloe/core.hpp> // for Duration, Error
55 #include <cloe/entity.hpp> // for Entity
56 
57 namespace cloe {
58 
62 class ModelError : public Error {
63  public:
64  using Error::Error;
65  virtual ~ModelError() noexcept = default;
66 
67  ModelError explanation(std::string explanation) && {
68  this->set_explanation(std::move(explanation));
69  return std::move(*this);
70  }
71 
72  template <typename... Args>
73  ModelError explanation(std::string_view format, Args&&... args) && {
74  this->set_explanation(fmt::format(fmt::runtime(format), std::forward<Args>(args)...));
75  return std::move(*this);
76  }
77 };
78 
85 class ModelAbort : public ModelError {
86  public:
87  using ModelError::ModelError;
88  virtual ~ModelAbort() noexcept = default;
89 
90  ModelAbort explanation(std::string explanation) && {
91  this->set_explanation(std::move(explanation));
92  return std::move(*this);
93  }
94 
95  template <typename... Args>
96  ModelAbort explanation(std::string_view format, Args&&... args) && {
97  this->set_explanation(fmt::format(fmt::runtime(format), std::forward<Args>(args)...));
98  return std::move(*this);
99  }
100 };
101 
106 class ModelReset : public ModelError {
107  public:
108  using ModelError::ModelError;
109  virtual ~ModelReset() noexcept = default;
110 
111  ModelReset explanation(std::string explanation) && {
112  this->set_explanation(std::move(explanation));
113  return std::move(*this);
114  }
115 
116  template <typename... Args>
117  ModelReset explanation(std::string_view format, Args&&... args) && {
118  this->set_explanation(fmt::format(fmt::runtime(format), std::forward(args)...));
119  return std::move(*this);
120  }
121 };
122 
127 class ModelStop : public ModelError {
128  public:
129  using ModelError::ModelError;
130  virtual ~ModelStop() noexcept = default;
131 
132  ModelStop explanation(std::string explanation) && {
133  this->set_explanation(std::move(explanation));
134  return std::move(*this);
135  }
136 
137  template <typename... Args>
138  ModelStop explanation(std::string_view format, Args&&... args) && {
139  this->set_explanation(fmt::format(fmt::runtime(format), std::forward<Args>(args)...));
140  return std::move(*this);
141  }
142 };
143 
203 class Model : public Entity {
204  public:
214  using Entity::Entity;
215 
222  virtual ~Model() noexcept = default;
223 
232  virtual Duration resolution() const;
233 
237  virtual bool is_connected() const { return connected_; }
238 
242  virtual bool is_operational() const { return operational_; }
243 
265  virtual void connect() { connected_ = true; }
266 
285  virtual void disconnect() { connected_ = false; }
286 
295  virtual void enroll(Registrar&) {}
296 
307  virtual void start(const Sync&) { operational_ = true; }
308 
327  virtual Duration process(const Sync&) = 0;
328 
338  virtual void pause(const Sync&) {}
339 
350  virtual void resume(const Sync&) {}
351 
362  virtual void stop(const Sync&) { operational_ = false; }
363 
375  virtual void reset() { throw ModelError("reset not supported by this model"); }
376 
392  virtual void abort() { throw ModelError("abort not supported by this model"); }
393 
394  protected:
395  bool connected_{false};
396  bool operational_{false};
397 };
398 
403 class ModelFactory : public Entity, public fable::Confable {
404  public:
405  using Entity::Entity;
406  virtual ~ModelFactory() = default;
407 };
408 
409 } // namespace cloe
Definition: entity.hpp:50
Definition: error.hpp:35
Definition: model.hpp:85
Definition: model.hpp:62
Definition: model.hpp:403
Definition: model.hpp:106
Definition: model.hpp:127
Definition: model.hpp:203
virtual void disconnect()
Definition: model.hpp:285
virtual void stop(const Sync &)
Definition: model.hpp:362
virtual ~Model() noexcept=default
virtual Duration resolution() const
Definition: model.cpp:27
virtual void resume(const Sync &)
Definition: model.hpp:350
virtual void pause(const Sync &)
Definition: model.hpp:338
virtual void enroll(Registrar &)
Definition: model.hpp:295
virtual void start(const Sync &)
Definition: model.hpp:307
virtual bool is_operational() const
Definition: model.hpp:242
virtual void abort()
Definition: model.hpp:392
virtual Duration process(const Sync &)=0
virtual bool is_connected() const
Definition: model.hpp:237
virtual void reset()
Definition: model.hpp:375
virtual void connect()
Definition: model.hpp:265
Definition: registrar.hpp:121
Definition: sync.hpp:34
Definition: confable.hpp:98
std::chrono::nanoseconds Duration
Definition: cloe_fwd.hpp:36