$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 #ifndef CLOE_MODEL_HPP_
51 #define CLOE_MODEL_HPP_
52 
53 #include <cloe/core.hpp> // for Duration, Error, Confable
54 #include <cloe/entity.hpp> // for Entity
55 
56 namespace cloe {
57 
58 // Forward declaration:
59 class Sync; // from cloe/sync.hpp
60 class Registrar; // from cloe/registrar.hpp
61 
65 class ModelError : public Error {
66  public:
67  using Error::Error;
68  virtual ~ModelError() noexcept = default;
69 
70  const std::string& explanation() const { return Error::explanation(); }
71 
72  ModelError explanation(const std::string& explanation) && {
73  this->set_explanation(explanation);
74  return std::move(*this);
75  }
76 
77  template <typename... Args>
78  ModelError explanation(const char* format, const Args&... args) && {
79  this->set_explanation(fmt::format(format, args...));
80  return std::move(*this);
81  }
82 };
83 
90 class ModelAbort : public ModelError {
91  public:
92  using ModelError::ModelError;
93  virtual ~ModelAbort() noexcept = default;
94 
95  const std::string& explanation() const { return Error::explanation(); }
96 
97  ModelError explanation(const std::string& explanation) && {
98  this->set_explanation(explanation);
99  return std::move(*this);
100  }
101 
102  template <typename... Args>
103  ModelError explanation(const char* format, const Args&... args) && {
104  this->set_explanation(fmt::format(format, args...));
105  return std::move(*this);
106  }
107 };
108 
113 class ModelReset : public ModelError {
114  public:
115  using ModelError::ModelError;
116  virtual ~ModelReset() noexcept = default;
117 
118  const std::string& explanation() const { return Error::explanation(); }
119 
120  ModelError explanation(const std::string& explanation) && {
121  this->set_explanation(explanation);
122  return std::move(*this);
123  }
124 
125  template <typename... Args>
126  ModelError explanation(const char* format, const Args&... args) && {
127  this->set_explanation(fmt::format(format, args...));
128  return std::move(*this);
129  }
130 };
131 
136 class ModelStop : public ModelError {
137  public:
138  using ModelError::ModelError;
139  virtual ~ModelStop() noexcept = default;
140 
141  const std::string& explanation() const { return Error::explanation(); }
142 
143  ModelError explanation(const std::string& explanation) && {
144  this->set_explanation(explanation);
145  return std::move(*this);
146  }
147 
148  template <typename... Args>
149  ModelError explanation(const char* format, const Args&... args) && {
150  this->set_explanation(fmt::format(format, args...));
151  return std::move(*this);
152  }
153 };
154 
214 class Model : public Entity {
215  public:
225  using Entity::Entity;
226 
233  virtual ~Model() noexcept = default;
234 
243  virtual Duration resolution() const;
244 
248  virtual bool is_connected() const { return connected_; }
249 
253  virtual bool is_operational() const { return operational_; }
254 
276  virtual void connect() { connected_ = true; }
277 
296  virtual void disconnect() { connected_ = false; }
297 
306  virtual void enroll(Registrar&) {}
307 
318  virtual void start(const Sync&) { operational_ = true; }
319 
338  virtual Duration process(const Sync&) = 0;
339 
349  virtual void pause(const Sync&) {}
350 
361  virtual void resume(const Sync&) {}
362 
373  virtual void stop(const Sync&) { operational_ = false; }
374 
386  virtual void reset() { throw ModelError("reset not supported by this model"); }
387 
403  virtual void abort() { throw ModelError("abort not supported by this model"); }
404 
405  protected:
406  bool connected_{false};
407  bool operational_{false};
408 };
409 
414 class ModelFactory : public Entity, public Confable {
415  public:
416  using Entity::Entity;
417  virtual ~ModelFactory() = default;
418 };
419 
420 } // namespace cloe
421 
422 #endif // CLOE_MODEL_HPP_
virtual void disconnect()
Definition: model.hpp:296
Definition: confable.hpp:46
virtual bool is_connected() const
Definition: model.hpp:248
virtual void pause(const Sync &)
Definition: model.hpp:349
Definition: registrar.hpp:117
Definition: error.hpp:35
Definition: sync.hpp:35
Definition: model.hpp:136
virtual void enroll(Registrar &)
Definition: model.hpp:306
Definition: coordinator.hpp:36
std::chrono::nanoseconds Duration
Definition: duration.hpp:45
virtual void abort()
Definition: model.hpp:403
Definition: entity.hpp:51
Definition: model.hpp:65
Definition: model.hpp:414
virtual bool is_operational() const
Definition: model.hpp:253
Definition: model.hpp:214
Definition: model.hpp:90
virtual void connect()
Definition: model.hpp:276
virtual void stop(const Sync &)
Definition: model.hpp:373
virtual void resume(const Sync &)
Definition: model.hpp:361
Definition: model.hpp:113
virtual void reset()
Definition: model.hpp:386
virtual void start(const Sync &)
Definition: model.hpp:318