Simo 0.0.1
Loading...
Searching...
No Matches
Context.h
1// Copyright 2026 Matteo Fusi and Contributors
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#ifndef SIMO_SIMULATIONCONTEXT_H
16#define SIMO_SIMULATIONCONTEXT_H
17
18#include <cstdint>
19#include <expected>
20#include <functional>
21#include <string>
22#include <vector>
23
24#include "./Time.h"
25#include "./internal/RadixHeap.h"
26#include "Simo/compiler/Compiler.h"
27#include "Simo/module/Module.h"
28#include "Simo/parameter/ParameterTrie.h"
29
31namespace Simo {
32
33namespace Parameter {
34class Parameter;
35template <typename T>
36class ParameterTyped;
37} // namespace Parameter
38
39class Module;
40class Parameters;
41
42class Period;
43using SimulationCallable = std::function<void(Time)>;
44
55class SIMO_PUBLIC Context {
56 public:
57 enum struct State : uint8_t {
58 INITIALIZATION,
59 PORT_CONNECTION,
60 RUNNING,
61 STOPPED,
62 ERROR
63 };
64
65 enum struct RunStatus : uint8_t {
66 EVENTS_IN_QUEUE,
67 STOPPED,
68 };
69
70 Context();
71
72 ~Context() = default;
73
77 [[nodiscard]]
79
86 std::expected<RunStatus, InitializationStatus> run(const Time& time_delta);
87
91 std::expected<RunStatus, InitializationStatus> run_at(const Time& final_time);
92
94 void schedule_at(const Time& time_target, const SimulationCallable& callable);
96 void schedule_at(const Time& time_target,
97 const std::function<void()>& callable);
98
100 void schedule_in(const Time& time_delta, const SimulationCallable& callable);
102 void schedule_in(const Time& time_delta,
103 const std::function<void()>& callable);
104
105 // Methods to initialize the context
106
108 template <typename T>
109 [[maybe_unused]]
111 const T& value) {
112 return parameters.add<T>(name, value);
113 }
114
118 void add(Module& module, Parameters&);
119
121 void remove(const Module& module);
122
123 template <typename Function>
124 void foreach_module(Function f) const {
125 for (const auto& module : modules | std::views::keys) {
126 f(*module);
127 }
128 }
129
130 [[nodiscard]] State get_state() const;
131 [[nodiscard]] Time current_time() const;
132
133 private:
134 State state{State::INITIALIZATION};
135 Time currentTime = Time::zero;
136 std::unique_ptr<Internal::RadixHeap> nextTickHeap;
137 std::unordered_map<Time, std::vector<SimulationCallable>> scheduledTasks;
138
139 Parameter::ParameterTrie parameters;
140
141 std::vector<std::pair<Module*, Parameters*>> modules;
142};
143} // namespace Simo
144
145#endif // SIMO_SIMULATIONCONTEXT_H
void schedule_at(const Time &time_target, const SimulationCallable &callable)
Schedule event at time_target time.
Definition Context.cc:76
std::expected< RunStatus, InitializationStatus > run(const Time &time_delta)
Definition Context.cc:41
InitializationStatus initialize()
Definition Context.cc:16
void schedule_in(const Time &time_delta, const SimulationCallable &callable)
Schedule an event for time current_time + time_delta.
Definition Context.cc:94
std::expected< RunStatus, InitializationStatus > run_at(const Time &final_time)
Definition Context.cc:47
Parameter::ParameterTyped< T > & add_parameter(const std::string &name, const T &value)
Add parameter of specific type. Return reference to parameter.
Definition Context.h:110
Definition InitializationStatus.h:32
Definition Module.h:90
A Parameter with a type.
Definition Parameter.h:68
Base class for any Parameter.
Definition Parameter.h:31
Collects parameters that are then passed to a Module instance.
Definition Module.h:33
Definition Time.h:40
General namespace for Simo.
Definition Collection.h:51