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
30namespace Simo {
31
32namespace Parameter {
33class Parameter;
34template <typename T>
35class ParameterTyped;
36} // namespace Parameter
37
38class Module;
39class Parameters;
40
41class Period;
42using SimulationCallable = std::function<void(Time)>;
43
54class SIMO_PUBLIC Context {
55 public:
56 enum struct State : uint8_t {
57 INITIALIZATION,
58 PORT_CONNECTION,
59 RUNNING,
60 STOPPED,
61 ERROR
62 };
63
64 enum struct RunStatus : uint8_t {
65 EVENTS_IN_QUEUE,
66 STOPPED,
67 };
68
69 Context();
70
71 ~Context() = default;
72
76 [[nodiscard]]
78
85 std::expected<RunStatus, InitializationStatus> run(const Time& time_delta);
86
90 std::expected<RunStatus, InitializationStatus> run_at(const Time& final_time);
91
93 void schedule_at(const Time& time_target, const SimulationCallable& callable);
95 void schedule_at(const Time& time_target,
96 const std::function<void()>& callable);
97
99 void schedule_in(const Time& time_delta, const SimulationCallable& callable);
101 void schedule_in(const Time& time_delta,
102 const std::function<void()>& callable);
103
104 // Methods to initialize the context
105
107 template <typename T>
108 [[maybe_unused]]
110 const T& value) {
111 return parameters.add<T>(name, value);
112 }
113
117 void add(Module& module, Parameters&);
118
120 void remove(const Module& module);
121
122 template <typename Function>
123 void foreach_module(Function f) const {
124 for (const auto& module : modules | std::views::keys) {
125 f(*module);
126 }
127 }
128
129 [[nodiscard]] State get_state() const;
130 [[nodiscard]] Time current_time() const;
131
132 private:
133 State state{State::INITIALIZATION};
134 Time currentTime = Time::zero;
135 std::unique_ptr<Internal::RadixHeap> nextTickHeap;
136 std::unordered_map<Time, std::vector<SimulationCallable>> scheduledTasks;
137
138 Parameter::ParameterTrie parameters;
139
140 std::vector<std::pair<Module*, Parameters*>> modules;
141};
142} // namespace Simo
143
144#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:109
Definition InitializationStatus.h:30
Definition Module.h:83
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:30
Definition Time.h:40