bactria  0.0.1
The bactria library is a header-only C++14 library for profiling and tracing.
Report.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 
27 
28 #include <functional>
29 #include <string>
30 #include <tuple>
31 #include <utility>
32 
33 namespace bactria
34 {
35  namespace reports
36  {
53  template<typename... TIncidents>
54  class Report
55  {
56  public:
62  Report() = default;
63 
72  Report(std::string name, TIncidents&&... incidents)
73  : m_name{std::move(name)}
74  , m_incidents{std::forward<TIncidents>(incidents)...}
75  {
76  }
77 
86  Report(Report const& other) : m_name{other.m_name}, m_incidents{other.m_incidents}
87  {
88  }
89 
98  auto operator=(Report const& rhs) -> Report&
99  {
100  m_name = rhs.m_name;
101  m_incidents = rhs.m_incidents;
102  return *this;
103  }
104 
113  Report(Report&& other) noexcept
114  : m_name{std::move(other.m_name)}
115  , m_incidents{std::move(other.m_incidents)}
116  {
117  }
118 
127  auto operator=(Report&& rhs) noexcept -> Report&
128  {
129  m_name = std::move(rhs.m_name);
130  m_incidents = std::move(rhs.m_incidents);
131  return *this;
132  }
133 
138  {
139  if(plugin::activated())
140  plugin::destroy_report(m_handle);
141  }
142 
151  auto submit() const
152  {
153  using namespace std::placeholders;
154  using TTuple = decltype(m_incidents);
155 
156  if(plugin::activated())
157  {
158  submit_incidents(std::make_index_sequence<std::tuple_size<TTuple>::value>{});
159  plugin::write_report(m_handle);
160  }
161  }
162 
163  private:
164  template<std::size_t... Is>
165  auto submit_incidents(std::index_sequence<Is...>) const
166  {
167  auto dummy = {(submit_incident(std::get<Is>(m_incidents)), 0)...};
168  }
169 
170  template<typename TIncident>
171  auto submit_incident(TIncident const& incident) const
172  {
173  plugin::record_value(m_handle, incident.m_key.c_str(), incident.m_value);
174  }
175 
176  std::string m_name{"BACTRIA_REPORT"};
177  std::tuple<TIncidents...> m_incidents{};
178  void* m_handle{plugin::activated() ? plugin::create_report(m_name.c_str()) : nullptr};
179  };
180 
189  template<typename... TIncidents>
190  auto make_report(std::string name, TIncidents&&... incidents) -> Report<TIncidents...>
191  {
192  return Report<TIncidents...>{std::move(name), std::forward<TIncidents>(incidents)...};
193  }
194 
196  } // namespace reports
197 } // namespace bactria
Plugin.hpp
bactria-internal handling of reports plugins.
bactria::reports::Report::operator=
auto operator=(Report &&rhs) noexcept -> Report &
Move-assignment operator.
Definition: Report.hpp:127
bactria::reports::Report
The report class.
Definition: Incident.hpp:34
bactria::reports::Report::Report
Report(Report const &other)
Copy constructor.
Definition: Report.hpp:86
bactria::reports::plugin::create_report
auto create_report(char const *name)
Creates a plugin-specific report handle.
Definition: Plugin.hpp:323
bactria::metrics::plugin::activated
auto activated() -> bool
Checks for an active metrics plugin.
Definition: Plugin.hpp:58
bactria::reports::plugin::record_value
auto record_value(void *report_handle, char const *key, bool value) -> void
Plugin-specific boolean recording.
Definition: Plugin.hpp:364
bactria::reports::Report::Report
Report(Report &&other) noexcept
Move constructor.
Definition: Report.hpp:113
bactria::reports::plugin::destroy_report
auto destroy_report(void *report_handle) noexcept
Destroys a plugin-specific report handle.
Definition: Plugin.hpp:338
bactria::reports::Report::operator=
auto operator=(Report const &rhs) -> Report &
Copy-assignment operator.
Definition: Report.hpp:98
bactria::reports::Report::submit
auto submit() const
Save the report.
Definition: Report.hpp:151
bactria::reports::Report::Report
Report()=default
Default constructor.
bactria::reports::plugin::write_report
auto write_report(void *report_handle)
Plugin-specific report writing.
Definition: Plugin.hpp:351
Incident.hpp
Incident definition.
bactria::reports::Report::~Report
~Report()
Destructor.
Definition: Report.hpp:137
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::Report::Report
Report(std::string name, TIncidents &&... incidents)
Constructor.
Definition: Report.hpp:72