bactria  0.0.1
The bactria library is a header-only C++14 library for profiling and tracing.
Sector.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 #include <bactria/metrics/Tags.hpp>
27 
28 #include <functional>
29 #include <iostream>
30 #include <string>
31 #include <type_traits>
32 #include <utility>
33 
34 namespace bactria
35 {
36  namespace metrics
37  {
53  template<typename TTag = Generic>
54  class Sector final
55  {
56  public:
66  Sector() = default;
67 
78  Sector(std::string sector_name) : m_name{std::move(sector_name)}
79  {
80  }
81 
95  Sector(std::string sector_name, std::string source, std::uint32_t lineno, std::string caller)
96  : m_name{std::move(sector_name)}
97  {
98  if(plugin::activated())
99  {
100  plugin::enter_sector(m_handle, source.c_str(), lineno, caller.c_str());
101  m_entered = true;
102  }
103  }
104 
110  Sector(Sector const&) = delete;
111 
117  auto operator=(Sector const&) -> Sector& = delete;
118 
126  Sector(Sector&& other)
127  : m_name{std::move(other.m_name)}
128  , m_handle{std::exchange(other.m_handle, nullptr)}
129  , m_entered{std::exchange(other.m_entered, bool{})}
130  , m_summary{std::exchange(other.m_summary, bool{})}
131  , m_on_enter{std::move(other.m_on_enter)}
132  , m_on_leave{std::move(other.m_on_leave)}
133  {
134  }
135 
143  auto operator=(Sector&& rhs) -> Sector&
144  {
145  m_name = std::move(rhs.m_name);
146  m_handle = std::exchange(rhs.m_handle, nullptr);
147  m_entered = std::exchange(rhs.m_entered, bool{});
148  m_summary = std::exchange(rhs.m_summary, bool{});
149  m_on_enter = std::move(rhs.m_on_enter);
150  m_on_leave = std::move(rhs.m_on_leave);
151 
152  return *this;
153  }
154 
164  {
165  if(plugin::activated())
166  {
167  if(m_entered)
168  leave(__FILE__, __LINE__, __func__);
169 
170  if(!m_summary)
171  summary();
172 
173  plugin::destroy_sector(m_handle);
174  }
175  }
176 
188  auto enter(std::string source, std::uint32_t lineno, std::string caller) -> void
189  {
190  if(plugin::activated())
191  {
192  plugin::enter_sector(m_handle, source.c_str(), lineno, caller.c_str());
193  m_on_enter();
194  m_entered = true;
195  }
196  }
197 
208  auto leave(std::string source, std::uint32_t lineno, std::string caller) -> void
209  {
210  if(plugin::activated())
211  {
212  m_on_leave();
213  plugin::leave_sector(m_handle, source.c_str(), lineno, caller.c_str());
214  m_entered = false;
215  }
216  }
217 
226  auto summary() -> void
227  {
228  if(plugin::activated())
229  {
230  plugin::sector_summary(m_handle);
231  m_summary = true;
232  }
233  }
234 
246  auto on_enter(std::function<void(void)> f) -> void
247  {
248  if(plugin::activated())
249  m_on_enter = f;
250  }
251 
263  auto on_leave(std::function<void(void)> f) -> void
264  {
265  if(plugin::activated())
266  m_on_leave = f;
267  }
268 
269  private:
270  std::string m_name{"BACTRIA_GENERIC_SECTOR"};
271  void* m_handle{plugin::activated() ? plugin::create_sector(m_name.c_str(), TTag::value) : nullptr};
272  bool m_entered{false};
273  bool m_summary{false};
274  std::function<void(void)> m_on_enter = []() {};
275  std::function<void(void)> m_on_leave = []() {};
276  };
277 
281  } // namespace metrics
282 } // namespace bactria
283 
296 #define bactria_Sector(name, tag) \
297  ::bactria::metrics::Sector<tag> \
298  { \
299  name, __FILE__, __LINE__, __func__ \
300  }
301 
312 #define bactria_Enter(sec) sec.enter(__FILE__, __LINE__, __func__)
313 
324 #define bactria_Leave(sec) sec.leave(__FILE__, __LINE__, __func__)
bactria::metrics::Sector::Sector
Sector(std::string sector_name, std::string source, std::uint32_t lineno, std::string caller)
The entering constructor.
Definition: Sector.hpp:95
bactria::metrics::Sector::~Sector
~Sector()
The destructor.
Definition: Sector.hpp:163
bactria::metrics::Sector::on_leave
auto on_leave(std::function< void(void)> f) -> void
Define a leave action.
Definition: Sector.hpp:263
bactria::metrics::Sector::on_enter
auto on_enter(std::function< void(void)> f) -> void
Define an enter action.
Definition: Sector.hpp:246
bactria::metrics::Sector::leave
auto leave(std::string source, std::uint32_t lineno, std::string caller) -> void
Leave the sector.
Definition: Sector.hpp:208
bactria::metrics::plugin::activated
auto activated() -> bool
Checks for an active metrics plugin.
Definition: Plugin.hpp:58
bactria::metrics::plugin::create_sector
auto create_sector(char const *name, std::uint32_t tag) noexcept
Creates a plugin-specific sector handle.
Definition: Plugin.hpp:231
bactria::metrics::plugin::destroy_sector
auto destroy_sector(void *sector_handle) noexcept
Destroys a plugin-specific sector handle.
Definition: Plugin.hpp:246
bactria::metrics::Sector::enter
auto enter(std::string source, std::uint32_t lineno, std::string caller) -> void
Enter the sector.
Definition: Sector.hpp:188
bactria::metrics::plugin::sector_summary
auto sector_summary(void *sector_handle) noexcept
Plugin-specific sector summary.
Definition: Plugin.hpp:293
bactria::metrics::Sector::summary
auto summary() -> void
Summarize the Sector.
Definition: Sector.hpp:226
bactria::metrics::Sector::Sector
Sector()=default
The default constructor.
Tags.hpp
Tag definitions.
bactria::metrics::Sector
The sector class.
Definition: Sector.hpp:54
bactria::metrics::Sector::Sector
Sector(std::string sector_name)
The non-entering constructor.
Definition: Sector.hpp:78
bactria::metrics::plugin::leave_sector
auto leave_sector(void *sector_handle, char const *source, std::uint32_t lineno, char const *caller) noexcept
Plugin-specific sector leaving.
Definition: Plugin.hpp:276
Plugin.hpp
bactria-internal handling of metrics plugins.
bactria::metrics::Sector::operator=
auto operator=(Sector &&rhs) -> Sector &
The move assignment operator.
Definition: Sector.hpp:143
bactria::metrics::plugin::enter_sector
auto enter_sector(void *sector_handle, char const *source, std::uint32_t lineno, char const *caller) noexcept
Plugin-specific sector entering.
Definition: Plugin.hpp:259
bactria::metrics::Sector::Sector
Sector(Sector &&other)
The move constructor.
Definition: Sector.hpp:126
bactria::metrics::Sector::operator=
auto operator=(Sector const &) -> Sector &=delete
The copy assignment operator (deleted).