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 <string>
19
20#include "Simo/core/InitializationStatus.h"
21#include "Simo/parameter/ParameterTrie.h"
22#include "Simo/port/Port.h"
23#include "Simo/statistics/StatMapper.h"
24
25namespace Simo {
26
27class Context;
28
30class SIMO_PUBLIC Parameters {
31 public:
32 virtual ~Parameters() = default;
33
34 [[nodiscard]] std::string_view name() const;
35
36 void name(std::string_view name);
37
38 // TODO make it to return a list of errors
39
43 [[nodiscard]] virtual bool check() const;
44
46 template <typename T>
47 [[maybe_unused]]
48 Parameter::ParameterTyped<T>& set(const std::string& name, const T& value) {
49 return trie.add<T>(name, value);
50 }
51
56 template <typename T>
58 const std::string& name) const {
59 return trie.find<T>(name);
60 }
61
62 [[nodiscard]] Parameter::Parameter* get(const std::string& name) const {
63 return trie.find(name);
64 }
65
71 [[nodiscard]] std::optional<Parameters> get_subtree(
72 const std::string& name) const;
73
74 protected:
75 Parameter::ParameterTrie trie;
76 std::string name_;
77};
78
83class SIMO_PUBLIC Module {
84 public:
89 [[nodiscard]]
90 virtual InitializationStatus initialize(Context& sim_ctx_v,
91 const Parameters& parameters);
92
93 [[nodiscard]] std::string_view name() const;
94
95 [[nodiscard]] Context& sim_ctx() const;
96
97 virtual ~Module() = default;
98
101
102 [[nodiscard]] Port* get_port(std::string_view);
103
104 template <typename Stat>
105 Stat* get_statistic(const std::string_view name) {
106 return statistics.get<Stat>(name);
107 }
108
109 template <typename Function>
110 void visit_statistics(Function f) {
111 statistics.visit(f);
112 }
113
114 protected:
116 template <typename T, typename... Args>
117 T& create_statistic(Args... args) {
118 T& s = statistics.emplace<T>(std::forward<Args>(args)...);
119 return s;
120 }
121
123 template <typename T, typename... Args>
124 T& create_port(const std::string_view name, Args... args) {
125 auto ptr = std::make_unique<T>(std::forward<Args>(args)...);
126 T& out_ref = *ptr;
127 ports[std::string(name)] = std::move(ptr);
128 return out_ref;
129 }
130
131 Statistics::StatStorage statistics;
132 std::unordered_map<std::string, std::unique_ptr<Port>> ports;
133
134 private:
135 std::string name_;
136 Context* sim_ctx_ = nullptr;
137};
138} // namespace Simo
139
140#endif // SIMO_MODULE_HH
Definition Context.h:54
Definition InitializationStatus.h:30
Definition Module.h:83
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:124
virtual InitializationStatus initialize(Context &sim_ctx_v, const Parameters &parameters)
Definition Module.cc:49
T & create_statistic(Args... args)
Create a new statistic of type T.
Definition Module.h:117
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
virtual bool check() const
Definition Module.cc:29
Parameter::ParameterTyped< T > * get(const std::string &name) const
Definition Module.h:57
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:48
Generic port. It offers the virtual method connect.
Definition Port.h:28
Definition StatMapper.h:33
Definition StatMapper.h:68