Skip to content

Commit 8026cea

Browse files
committed
merge_into as member function
Signed-off-by: Martijn Govers <[email protected]>
1 parent 3bb015e commit 8026cea

File tree

5 files changed

+36
-27
lines changed

5 files changed

+36
-27
lines changed

power_grid_model_c/power_grid_model/include/power_grid_model/common/calculation_info.hpp

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -85,14 +85,17 @@ class CalculationInfo : public Logger {
8585
public:
8686
Report report() const { return data_; }
8787
void clear() { data_.clear(); }
88-
};
8988

90-
inline Logger& merge_into(Logger& destination, CalculationInfo const& source) {
91-
for (const auto& [tag, value] : source.report()) {
92-
destination.log(tag, value);
89+
template <std::derived_from<Logger> T> T& merge_into(T& destination) const {
90+
if (&destination == this) {
91+
return destination; // nothing to do
92+
}
93+
for (const auto& [tag, value] : report()) {
94+
destination.log(tag, value);
95+
}
96+
return destination;
9397
}
94-
return destination;
95-
}
98+
};
9699

97100
class MultiThreadedCalculationInfo : public MultiThreadedLoggerImpl<CalculationInfo> {
98101
public:

power_grid_model_c/power_grid_model/include/power_grid_model/common/dummy_logging.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,8 @@ class NoLogger : public Logger {
2424
void log(LogEvent /*tag*/, Idx /*value*/) override {
2525
// no logging
2626
}
27-
};
2827

29-
inline Logger& merge_into(Logger& destination, NoLogger const& /*source*/) { return destination; }
28+
template <std::derived_from<Logger> T> T& merge_into(T& destination) const { return destination; }
29+
};
3030

3131
} // namespace power_grid_model::common::logging

power_grid_model_c/power_grid_model/include/power_grid_model/common/multi_threaded_logging.hpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,14 @@
66

77
#include "dummy_logging.hpp"
88

9+
#include <cassert>
910
#include <mutex>
1011

1112
namespace power_grid_model::common::logging {
1213

1314
template <std::derived_from<Logger> LoggerType>
1415
requires requires(LoggerType& destination, LoggerType const& source) {
15-
{ merge_into(destination, source) };
16+
{ source.merge_into(destination) };
1617
}
1718
class MultiThreadedLoggerImpl : public MultiThreadedLogger {
1819
public:
@@ -46,8 +47,10 @@ class MultiThreadedLoggerImpl : public MultiThreadedLogger {
4647
std::mutex mutex_;
4748

4849
void sync(ThreadLogger const& logger) {
50+
assert(&logger != &log_);
51+
4952
std::lock_guard const lock{mutex_};
50-
merge_into(log_, static_cast<LoggerType const&>(logger));
53+
logger.merge_into(log_);
5154
}
5255
};
5356

tests/benchmark_cpp/benchmark.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ struct PowerGridBenchmark {
153153
try {
154154
// calculate
155155
main_model->calculate(model_options, output.get_dataset(), batch_data.get_dataset());
156-
common::logging::merge_into(info, main_model->calculation_info());
156+
main_model->calculation_info().merge_into(info);
157157
} catch (std::exception const& e) {
158158
std::cout << std::format("\nAn exception was raised during execution: {}\n", e.what());
159159
}

tests/cpp_unit_tests/test_job_dispatch.cpp

Lines changed: 19 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -129,26 +129,29 @@ class TestLogger : public common::logging::Logger {
129129

130130
Data const& report() const { return log_; }
131131

132+
template <std::derived_from<Logger> T> T& merge_into(T& destination) const {
133+
if (&destination == this) {
134+
return destination; // nothing to do
135+
}
136+
for (const auto& entry : report()) {
137+
std::visit(
138+
[&destination, event = entry.event](auto&& arg) {
139+
using U = std::decay_t<decltype(arg)>;
140+
if constexpr (std::same_as<U, TestLogger::EmptyEvent>) {
141+
destination.log(event);
142+
} else {
143+
destination.log(event, arg);
144+
}
145+
},
146+
entry.data);
147+
}
148+
return destination;
149+
}
150+
132151
private:
133152
Data log_;
134153
};
135154

136-
inline Logger& merge_into(Logger& destination, TestLogger const& source) {
137-
for (const auto& entry : source.report()) {
138-
std::visit(
139-
[&destination, event = entry.event](auto&& arg) {
140-
using T = std::decay_t<decltype(arg)>;
141-
if constexpr (std::same_as<T, TestLogger::EmptyEvent>) {
142-
destination.log(event);
143-
} else {
144-
destination.log(event, arg);
145-
}
146-
},
147-
entry.data);
148-
}
149-
return destination;
150-
}
151-
152155
class MultiThreadedTestLogger : public common::logging::MultiThreadedLoggerImpl<TestLogger> {
153156
public:
154157
using MultiThreadedLoggerImpl::MultiThreadedLoggerImpl;

0 commit comments

Comments
 (0)