Simo 0.0.1
Loading...
Searching...
No Matches
StatMapper.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_STATMAPPER_HH
16#define SIMO_STATMAPPER_HH
17
18#include <memory>
19#include <vector>
20
21#include "./Statistic.h"
22#include "Simo/compiler/Compiler.h"
23
24namespace Simo {
25class Module;
26}
27
28namespace Simo::Statistics {
29
33class SIMO_PUBLIC StatMapper {
34 public:
35 void add(Statistic& stat) { storage.emplace_back(stat.clone(), &stat); }
36
37 void assign() const {
38 for (const auto& [fst, snd] : storage) {
39 fst->assign_from(*snd);
40 }
41 }
42
43 std::vector<std::unique_ptr<Statistic>> compute_diff() {
44 std::vector<std::unique_ptr<Statistic>> out_v;
45 for (const auto& [fst, snd] : storage) {
46 out_v.emplace_back(snd->compute_delta(*fst));
47 }
48 return out_v;
49 }
50
51 private:
52 std::vector<std::pair<std::unique_ptr<Statistic>, Statistic*>> storage;
53};
54
55class SIMO_PUBLIC ModuleStatMapper {
56 public:
57 explicit ModuleStatMapper(Module& m);
58
59 void assign() const { statMapper.assign(); }
60
61 std::vector<std::unique_ptr<Statistic>> compute_diff();
62
63 private:
64 Module& module;
65 StatMapper statMapper;
66};
67
69 public:
70 template <typename T, typename... Args>
71 T& emplace(Args... args) {
72 storage.emplace_back(std::make_unique<T>(args...));
73 return static_cast<T&>(*storage.back().get());
74 }
75
76 [[nodiscard]] Statistic* get(const std::string_view name) const {
77 for (const auto& p : storage) {
78 if (p->name() == name) {
79 return p.get();
80 }
81 }
82 return nullptr;
83 }
84
85 template <typename T>
86 [[nodiscard]] T* get(const std::string_view name) const {
87 Statistic* stat = get(name);
88 if (stat == nullptr) {
89 return nullptr;
90 }
91 auto* ptr_cast = boost::typeindex::runtime_cast<T*>(stat);
92 if (ptr_cast == nullptr) {
93 return nullptr;
94 }
95 return ptr_cast;
96 }
97
98 template <typename Visitor>
99 void visit(Visitor f) {
100 for (const auto& p : storage) {
101 f(*p);
102 }
103 }
104
105 private:
106 std::vector<std::unique_ptr<Statistic>> storage;
107};
108
109} // namespace Simo::Statistics
110
111#endif // SIMO_STATMAPPER_HH
Definition Module.h:83
Definition StatMapper.h:33
Definition StatMapper.h:68
Base class for all statistics.
Definition Statistic.h:26
virtual std::unique_ptr< Statistic > clone() const =0
Create a new copy of this statistic.