bactria  0.0.1
The bactria library is a header-only C++14 library for profiling and tracing.
Phase.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 <string>
28 #include <utility>
29 
30 namespace bactria
31 {
32  namespace metrics
33  {
42  class Phase
43  {
44  public:
54  Phase() = default;
55 
66  explicit Phase(std::string name) : m_name{std::move(name)}
67  {
68  }
69 
83  Phase(std::string name, std::string source, std::uint32_t lineno, std::string caller)
84  : m_name{std::move(name)}
85  {
86  if(plugin::activated())
87  enter(std::move(source), lineno, std::move(caller));
88  }
89 
95  Phase(const Phase&) = delete;
96 
102  auto operator=(const Phase&) -> Phase& = delete;
103 
111  Phase(Phase&& other)
112  : m_name{std::move(other.m_name)}
113  , m_entered{std::exchange(other.m_entered, bool{})}
114  , m_handle{std::exchange(other.m_handle, nullptr)}
115  {
116  }
117 
125  auto operator=(Phase&& rhs) -> Phase&
126  {
127  m_name = std::move(rhs.m_name);
128  m_handle = std::exchange(rhs.m_handle, nullptr);
129  m_entered = std::exchange(rhs.m_entered, bool{});
130 
131  return *this;
132  }
133 
143  {
144  if(plugin::activated())
145  {
146  if(m_entered)
147  leave(__FILE__, __LINE__, __func__);
148 
149  plugin::destroy_phase(m_handle);
150  }
151  }
152 
164  auto enter(std::string source, std::uint32_t lineno, std::string caller) -> void
165  {
166  if(plugin::activated())
167  {
168  plugin::enter_phase(m_handle, source.c_str(), lineno, caller.c_str());
169  m_entered = true;
170  }
171  }
172 
184  auto leave(std::string source, std::uint32_t lineno, std::string caller) -> void
185  {
186  if(plugin::activated())
187  {
188  plugin::leave_phase(m_handle, source.c_str(), lineno, caller.c_str());
189  m_entered = false;
190  }
191  }
192 
193  private:
194  std::string m_name{"BACTRIA_GENERIC_PHASE"};
195  void* m_handle{plugin::activated() ? plugin::create_phase(m_name.c_str()) : nullptr};
196  bool m_entered{false};
197  };
198  } // namespace metrics
199 } // namespace bactria
200 
212 #define bactria_Phase(name) \
213  ::bactria::metrics::Phase \
214  { \
215  name, __FILE__, __LINE__, __func__ \
216  }
bactria::metrics::Phase::leave
auto leave(std::string source, std::uint32_t lineno, std::string caller) -> void
Leave the phase.
Definition: Phase.hpp:184
bactria::metrics::plugin::create_phase
auto create_phase(char const *name) noexcept
Creates a plugin-specific phase handle.
Definition: Plugin.hpp:307
bactria::metrics::plugin::activated
auto activated() -> bool
Checks for an active metrics plugin.
Definition: Plugin.hpp:58
bactria::metrics::Phase::operator=
auto operator=(Phase &&rhs) -> Phase &
The move assignment operator.
Definition: Phase.hpp:125
bactria::metrics::plugin::destroy_phase
auto destroy_phase(void *phase_handle) noexcept
Destroys a plugin-specific phase handle.
Definition: Plugin.hpp:322
bactria::metrics::Phase::Phase
Phase(std::string name, std::string source, std::uint32_t lineno, std::string caller)
The entering constructor.
Definition: Phase.hpp:83
bactria::metrics::Phase::operator=
auto operator=(const Phase &) -> Phase &=delete
The copy assignment operator (deleted).
bactria::metrics::Phase::Phase
Phase()=default
The default constructor.
bactria::metrics::Phase::~Phase
~Phase()
The destructor.
Definition: Phase.hpp:142
bactria::metrics::Phase::Phase
Phase(std::string name)
The non-entering constructor.
Definition: Phase.hpp:66
bactria::metrics::Phase
The phase class.
Definition: Phase.hpp:42
bactria::metrics::plugin::leave_phase
auto leave_phase(void *phase_handle, char const *source, std::uint32_t lineno, char const *caller) noexcept
Plugin-specific phase leaving.
Definition: Plugin.hpp:352
bactria::metrics::Phase::enter
auto enter(std::string source, std::uint32_t lineno, std::string caller) -> void
Enter the phase.
Definition: Phase.hpp:164
bactria::metrics::Phase::Phase
Phase(Phase &&other)
The move constructor.
Definition: Phase.hpp:111
Plugin.hpp
bactria-internal handling of metrics plugins.
bactria::metrics::plugin::enter_phase
auto enter_phase(void *phase_handle, char const *source, std::uint32_t lineno, char const *caller) noexcept
Plugin-specific phase entering.
Definition: Plugin.hpp:335