bactria  0.0.1
The bactria library is a header-only C++14 library for profiling and tracing.
IncidentRecorder.hpp
Go to the documentation of this file.
1 /* Copyright 2021 Jan Stephan
2  *
3  * Licensed under the EUPL, Version 1.2 or - as soon they will be approved by
4  * the European Commission - subsequent versions of the EUPL (the “Licence”).
5  * You may not use this work except in compliance with the Licence.
6  * You may obtain a copy of the Licence at:
7  *
8  * http://ec.europa.eu/idabc/eupl.html
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the Licence is distributed on an “AS IS” basis, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * Licence permissions and limitations under the Licence.
14  */
15 
23 #pragma once
24 
26 
27 #include <cstddef>
28 #include <functional>
29 #include <string>
30 #include <tuple>
31 #include <type_traits>
32 #include <utility>
33 
34 namespace bactria
35 {
36  namespace reports
37  {
48  template<typename... TValues>
50  {
51  public:
62  using record_t = IncidentRecorder<TValues...>;
63 
64  public:
75  template<typename TFunc>
77  TFunc&& f,
78  /* Poor man's std::is_invocable until we have access to C++17 */
79  std::enable_if_t<
80  std::is_constructible<
81  std::function<void(record_t&)>,
82  std::reference_wrapper<typename std::remove_reference<TFunc>::type>>::value,
83  int> = 0)
84  {
85  f(*this);
86  }
87 
98  template<typename TFunc>
100  TFunc&& f,
101  std::enable_if_t<
102  std::is_constructible<
103  std::function<void(void)>,
104  std::reference_wrapper<typename std::remove_reference<TFunc>::type>>::value,
105  int> = 0)
106  {
107  f();
108  }
109 
119  template<std::size_t TIndex>
120  auto load()
121  {
122  static_assert(TIndex < std::tuple_size<decltype(m_values)>::value, "Data index out of bounds");
123  return std::get<TIndex>(m_values);
124  }
125 
137  template<std::size_t TIndex, typename TValue>
138  auto store(TValue&& value)
139  {
140  static_assert(TIndex < std::tuple_size<decltype(m_values)>::value, "Data index out of bounds");
141  std::get<TIndex>(m_values) = value;
142  }
143 
156  template<std::size_t... TIndices>
157  auto submit_report(std::string name) const
158  {
159  auto const r = make_report(std::move(name), std::get<TIndices>(m_values)...);
160  r.submit();
161  }
162 
163  private:
164  std::tuple<TValues...> m_values{std::make_tuple(TValues{}...)};
165  };
166  } // namespace reports
167 
168 } // namespace bactria
bactria::reports::IncidentRecorder
Class for dynamic recording of Incidents.
Definition: IncidentRecorder.hpp:49
bactria::reports::IncidentRecorder::record_step
auto record_step(TFunc &&f, std::enable_if_t< std::is_constructible< std::function< void(record_t &)>, std::reference_wrapper< typename std::remove_reference< TFunc >::type >>::value, int >=0)
Perform a recording step.
Definition: IncidentRecorder.hpp:76
bactria::reports::IncidentRecorder::load
auto load()
Load a value.
Definition: IncidentRecorder.hpp:120
Report.hpp
Report definition.
bactria::reports::IncidentRecorder::submit_report
auto submit_report(std::string name) const
Generate report from stored Incidents.
Definition: IncidentRecorder.hpp:157
bactria::reports::IncidentRecorder::store
auto store(TValue &&value)
Record a value.
Definition: IncidentRecorder.hpp:138
bactria::reports::make_report
auto make_report(std::string name, TIncidents &&... incidents) -> Report< TIncidents... >
Create a Report from several Incidents.
Definition: Report.hpp:190
bactria::reports::IncidentRecorder::record_step
auto record_step(TFunc &&f, std::enable_if_t< std::is_constructible< std::function< void(void)>, std::reference_wrapper< typename std::remove_reference< TFunc >::type >>::value, int >=0)
Perform a recording step without loading or storing data.
Definition: IncidentRecorder.hpp:99