history.h Source File

CPP API: history.h Source File
history.h
Go to the documentation of this file.
1 /*
2 * Copyright (C) 2020-2026 MEmilio
3 *
4 * Authors: Sascha Korf, Rene Schmieding
5 *
6 * Contact: Martin J. Kuehn <Martin.Kuehn@DLR.de>
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 #ifndef MIO_IO_HISTORY_H
21 #define MIO_IO_HISTORY_H
22 
24 #include <vector>
25 #include <tuple>
26 
27 namespace mio
28 {
29 
34 template <class... Loggers>
36  using Data = std::tuple<std::vector<typename Loggers::Type>...>;
45  template <class Logger>
46  static void add_record(const typename Logger::Type& t, Data& data)
47  {
48  std::get<mio::index_of_type_v<Logger, Loggers...>>(data).push_back(t);
49  }
50 };
51 
68 template <template <class... /*Loggers*/> class Writer, class... Loggers>
69 class History
70 {
71  static_assert(!has_duplicates_v<Loggers...>, "The Loggers used for a History must be unique.");
72 
73 public:
74  using WriteWrapper = Writer<Loggers...>;
75 
76  History() = default;
77 
78  History(typename WriteWrapper::Data data)
79  : m_data(data)
80  {
81  }
82 
83  History(typename Loggers::Type... args)
84  : m_data(std::tie(args...))
85  {
86  }
87 
95  template <class T>
96  void log(const T& t)
97  {
98  (log_impl(t, std::get<index_of_type_v<Loggers, Loggers...>>(m_loggers)), ...);
99  }
100 
106  const typename WriteWrapper::Data& get_log() const
107  {
108  return m_data;
109  }
110 
111 private:
112  typename WriteWrapper::Data m_data;
113  std::tuple<Loggers...> m_loggers;
114 
121  template <class T, class Logger>
122  void log_impl(const T& t, Logger& logger)
123  {
124  if (logger.should_log(t)) {
125  WriteWrapper::template add_record<Logger>(logger.log(t), m_data);
126  }
127  }
128 };
129 
130 template <class... Loggers>
132 
143 struct LogOnce {
144  bool was_logged = false;
145 
147  template <class T>
148  bool should_log(const T&)
149  {
150  return was_logged ? false : (was_logged = true);
151  }
152 };
153 
154 struct LogAlways {
156  template <class T>
157  constexpr bool should_log(const T&)
158  {
159  return true;
160  }
161 };
164 } // namespace mio
165 
166 #endif // MIO_IO_HISTORY_H
History class that handles writers and loggers.
Definition: history.h:70
History(typename Loggers::Type... args)
Definition: history.h:83
const WriteWrapper::Data & get_log() const
Get the data object.
Definition: history.h:106
History(typename WriteWrapper::Data data)
Definition: history.h:78
void log(const T &t)
Logs new records according to the Writer and Loggers.
Definition: history.h:96
std::tuple< Loggers... > m_loggers
Definition: history.h:113
Writer< Loggers... > WriteWrapper
Definition: history.h:74
void log_impl(const T &t, Logger &logger)
Checks if the given logger should log.
Definition: history.h:122
History()=default
WriteWrapper::Data m_data
Definition: history.h:112
A collection of classes to simplify handling of matrix shapes in meta programming.
Definition: models/abm/analyze_result.h:30
constexpr std::size_t index_of_type_v
The index of Type in the list Types.
Definition: metaprogramming.h:191
constexpr std::tuple_element< I, std::tuple< Index< CategoryTags >... > >::type & get(Index< CategoryTags... > &i) noexcept
Retrieves the Index (by reference) at the Ith position of a MultiIndex.
Definition: index.h:294
Definition: io.h:94
This class writes data retrieved from loggers to memory.
Definition: history.h:35
std::tuple< std::vector< typename Loggers::Type >... > Data
Definition: history.h:36
static void add_record(const typename Logger::Type &t, Data &data)
Adds a new record for a given log result t to data.
Definition: history.h:46
Definition: history.h:154
constexpr bool should_log(const T &)
Always returns true, for any type T.
Definition: history.h:157
LogOnce and LogAlways can be used as a base class to write a logger for History.
Definition: history.h:143
bool should_log(const T &)
For any type T, returns true on the first call only, and false thereafter.
Definition: history.h:148
bool was_logged
Remember if this Logger was logged already.
Definition: history.h:144