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 template <typename Function>
82 void visit(Function f) {
83 trie.visit(f);
84 }
85
86 protected:
87 Parameter::ParameterTrie trie;
88 std::string name_;
89};
90
95class SIMO_PUBLIC Module {
96 public:
101 [[nodiscard]]
102 virtual InitializationStatus initialize(Context& sim_ctx_v,
103 const Parameters& parameters);
104
105 [[nodiscard]] std::string_view name() const;
106
107 [[nodiscard]] constexpr std::string name_of_child(
108 const std::string_view child) const {
109 return name_ + "/" + std::string(child);
110 }
111
112 [[nodiscard]] Context& sim_ctx() const;
113
114 virtual ~Module() {}
115
118
120 [[nodiscard]] Port* get_port(std::string_view);
121
125 template <typename T>
126 [[nodiscard]]
127 T get_port(std::string_view name)
128 requires std::is_pointer_v<T>
129 {
130 Port* port = get_port(name);
131 if (port == nullptr) {
132 return nullptr;
133 }
134 return boost::typeindex::runtime_cast<T>(port);
135 }
136
140 template <typename T>
141 requires(!std::is_pointer_v<T>)
142 [[nodiscard]]
143 T* get_port(std::string_view name) {
144 return get_port<T*>(name);
145 }
146
148 std::string full_name;
149 Port* port;
150 };
151
153 std::vector<PortWithFullName> get_unconnected_ports(
154 bool include_nested_components) const;
155
156 Time current_time() const;
157
158 template <typename Stat>
159 Stat* get_statistic(const std::string_view name) {
160 return statistics.get<Stat>(name);
161 }
162
163 template <typename Function>
164 void visit_statistics(Function f) {
165 statistics.visit(f);
166 }
167
172 virtual InitializationStatus log_setup(const std::filesystem::path& out_file);
174 InitializationStatus log_setup();
175
177 void log_enable(bool new_value);
178
179 void log_level(size_t level);
180 void log_level(std::string_view level_name);
181
187 template <typename Callable>
188 void log(size_t level, Callable&& callable) {
189 auto f = [this, callable]() {
190 return std::format("[{}] [{}] {}\n", current_time(), name(), callable());
191 };
192 log_raw_callable(level, true, f);
193 }
194
196 template <typename Callable>
197 void log_raw_callable(size_t level, bool print_level, Callable&& callable) {
198 logger.log_callable(level, print_level, std::forward<Callable>(callable));
199 }
200
201 void populate_default_log_levels();
202
203 Log::Logger& get_logger() { return logger; }
204
206 template <typename T, typename... Args>
207 T& create_statistic(Args... args) {
208 T& s = statistics.emplace<T>(std::forward<Args>(args)...);
209 return s;
210 }
211
213 template <typename T, typename... Args>
214 T& create_port(const std::string_view name, Args... args) {
215 auto ptr = std::make_unique<T>(std::forward<Args>(args)...);
216 T& out_ref = *ptr;
217 ports[std::string(name)] = std::move(ptr);
218 return out_ref;
219 }
220
223 template <typename T, typename... Args>
224 T& create_child(Args... args) {
225 auto ptr = std::make_unique<T>(std::forward<Args>(args)...);
226 T& out_ref = *ptr;
227 children.emplace_back(std::move(ptr));
228 return out_ref;
229 }
230
231 protected:
232 Statistics::StatStorage statistics;
233 std::unordered_map<std::string, std::unique_ptr<Port>> ports;
234 std::vector<std::unique_ptr<Module>> children;
235 Log::Logger logger;
236
237 private:
238 std::string name_;
239 Context* sim_ctx_ = nullptr;
240};
241} // namespace Simo
242
243#endif // SIMO_MODULE_HH
Definition Context.h:55
Definition InitializationStatus.h:32
Definition Log.h:38
Definition Module.h:95
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:197
T & create_child(Args... args)
Definition Module.h:224
void record_statistics(Statistics::StatMapper &mapper)
Record a statistic in a StatMapper to dump statistics.
Definition Module.cc:62
std::vector< PortWithFullName > get_unconnected_ports(bool include_nested_components) const
Return all the unconnected ports that a module exposes.
Definition Module.cc:71
T & create_port(const std::string_view name, Args... args)
Create a new port of type T.
Definition Module.h:214
T get_port(std::string_view name)
Definition Module.h:127
void log(size_t level, Callable &&callable)
Definition Module.h:188
virtual InitializationStatus initialize(Context &sim_ctx_v, const Parameters &parameters)
Definition Module.cc:47
Port * get_port(std::string_view)
Get a single port.
Definition Module.cc:66
T & create_statistic(Args... args)
Create a new statistic of type T.
Definition Module.h:207
T * get_port(std::string_view name)
Definition Module.h:143
A Parameter with a type.
Definition Parameter.h:74
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
Definition Module.h:147