Simo 0.0.1
Loading...
Searching...
No Matches
Module.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_MODULE_HH
16#define SIMO_MODULE_HH
17
18#include <Simo/core/Log.h>
19#include <Simo/core/Time.h>
20
21#include <string>
22
23#include "Simo/core/InitializationStatus.h"
24#include "Simo/parameter/ParameterTrie.h"
25#include "Simo/port/Port.h"
26#include "Simo/statistics/StatMapper.h"
27
28namespace Simo {
29
30class Context;
31
33class SIMO_PUBLIC Parameters {
34 public:
35 virtual ~Parameters() = default;
36
37 [[nodiscard]] std::string_view name() const;
38
39 template <typename Self>
40 Self& name(this Self& self, const std::string_view name) {
41 self.name_ = name;
42 return self;
43 }
44
45 // TODO make it to return a list of errors
46
50 [[nodiscard]] virtual bool check() const;
51
53 template <typename T>
54 [[maybe_unused]]
55 Parameter::ParameterTyped<T>& set(const std::string& name, const T& value) {
56 return trie.add<T>(name, value);
57 }
58
63 template <typename T>
65 const std::string& name) const {
66 return trie.find<T>(name);
67 }
68
69 [[nodiscard]] Parameter::Parameter* get(const std::string& name) const {
70 return trie.find(name);
71 }
72
78 [[nodiscard]] std::optional<Parameters> get_subtree(
79 const std::string& name) const;
80
81 protected:
82 Parameter::ParameterTrie trie;
83 std::string name_;
84};
85
90class SIMO_PUBLIC Module {
91 public:
96 [[nodiscard]]
97 virtual InitializationStatus initialize(Context& sim_ctx_v,
98 const Parameters& parameters);
99
100 [[nodiscard]] std::string_view name() const;
101
102 [[nodiscard]] constexpr std::string name_of_child(
103 const std::string_view child) const {
104 return name_ + "/" + std::string(child);
105 }
106
107 [[nodiscard]] Context& sim_ctx() const;
108
109 virtual ~Module() {}
110
113
114 [[nodiscard]] Port* get_port(std::string_view);
115
116 Time current_time() const;
117
118 template <typename Stat>
119 Stat* get_statistic(const std::string_view name) {
120 return statistics.get<Stat>(name);
121 }
122
123 template <typename Function>
124 void visit_statistics(Function f) {
125 statistics.visit(f);
126 }
127
132 virtual InitializationStatus log_setup(const std::filesystem::path& out_file);
135
137 void log_enable(bool new_value);
138
139 void log_level(size_t level);
140 void log_level(std::string_view level_name);
141
147 template <typename Callable>
148 void log(size_t level, Callable&& callable) {
149 auto f = [this, callable]() {
150 return std::format("[{}] [{}] {}\n", current_time(), name(), callable());
151 };
152 log_raw_callable(level, true, f);
153 }
154
156 template <typename Callable>
157 void log_raw_callable(size_t level, bool print_level, Callable&& callable) {
158 logger.log_callable(level, print_level, std::forward<Callable>(callable));
159 }
160
161 void populate_default_log_levels();
162
163 Log::Logger& get_logger() { return logger; }
164
166 template <typename T, typename... Args>
167 T& create_statistic(Args... args) {
168 T& s = statistics.emplace<T>(std::forward<Args>(args)...);
169 return s;
170 }
171
173 template <typename T, typename... Args>
174 T& create_port(const std::string_view name, Args... args) {
175 auto ptr = std::make_unique<T>(std::forward<Args>(args)...);
176 T& out_ref = *ptr;
177 ports[std::string(name)] = std::move(ptr);
178 return out_ref;
179 }
180
183 template <typename T, typename... Args>
184 T& create_child(Args... args) {
185 auto ptr = std::make_unique<T>(std::forward<Args>(args)...);
186 T& out_ref = *ptr;
187 children.emplace_back(std::move(ptr));
188 return out_ref;
189 }
190
191 protected:
192 Statistics::StatStorage statistics;
193 std::unordered_map<std::string, std::unique_ptr<Port>> ports;
194 std::vector<std::unique_ptr<Module>> children;
195 Log::Logger logger;
196
197 private:
198 std::string name_;
199 Context* sim_ctx_ = nullptr;
200};
201} // namespace Simo
202
203#endif // SIMO_MODULE_HH
Definition Context.h:55
Definition InitializationStatus.h:32
Definition Log.h:38
Definition Module.h:90
void log_raw_callable(size_t level, bool print_level, Callable &&callable)
Log a message with the given level without adding timestamp.
Definition Module.h:157
T & create_child(Args... args)
Definition Module.h:184
void record_statistics(Statistics::StatMapper &mapper)
Record a statistic in a StatMapper to dump statistics.
Definition Module.cc:62
T & create_port(const std::string_view name, Args... args)
Create a new port of type T.
Definition Module.h:174
void log(size_t level, Callable &&callable)
Definition Module.h:148
virtual InitializationStatus log_setup(const std::filesystem::path &out_file)
Definition Module.cc:71
virtual InitializationStatus initialize(Context &sim_ctx_v, const Parameters &parameters)
Definition Module.cc:47
T & create_statistic(Args... args)
Create a new statistic of type T.
Definition Module.h:167
void log_enable(bool new_value)
Enable/disable logging for the component.
Definition Module.cc:78
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
virtual bool check() const
Definition Module.cc:29
Parameter::ParameterTyped< T > * get(const std::string &name) const
Definition Module.h:64
Parameter::ParameterTyped< T > & set(const std::string &name, const T &value)
Create a new parameter with the given type and return a reference to it.
Definition Module.h:55
Generic port. It offers the virtual method connect.
Definition Port.h:30
Definition StatMapper.h:33
Definition StatMapper.h:68
Definition Time.h:40
General namespace for Simo.
Definition Collection.h:51