bactria  0.0.1
The bactria library is a header-only C++14 library for profiling and tracing.
Range.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 
28 
29 #include <cstdint>
30 #include <string>
31 #include <utility>
32 
33 namespace bactria
34 {
35  namespace ranges
36  {
52  class Range : public Marker
53  {
54  public:
63  Range() : Marker("BACTRIA_GENERIC_RANGE", color::bactria_cyan, Category{})
64  {
65  }
66 
81  std::string name,
82  std::uint32_t color = color::bactria_cyan,
83  Category category = Category{},
84  bool autostart = true)
85  : Marker(std::move(name), color, std::move(category))
86  {
87  if(autostart && plugin::activated())
88  start();
89  }
90 
101  Range(Range const& other)
102  : Marker(other), m_handle{plugin::activated() ? plugin::create_range(m_name.c_str(), m_color, m_category.get_c_name(), m_category.get_id()) : nullptr}
103  , m_started{other.m_started}
104  {
105  if(m_started && plugin::activated())
106  plugin::start_range(m_handle);
107  }
108 
119  auto operator=(Range const& rhs) -> Range&
120  {
121  Marker::operator=(rhs);
122  if(plugin::activated())
123  {
124  m_handle
126  m_started = rhs.m_started;
127 
128  if(m_started)
129  plugin::start_range(m_handle);
130  }
131 
132  return *this;
133  }
134 
145  Range(Range&& other) noexcept
146  : Marker(std::move(other))
147  , m_handle{std::exchange(other.m_handle, nullptr)}
148  , m_started{std::exchange(other.m_started, bool{})}
149  {
150  }
151 
162  auto operator=(Range&& rhs) noexcept -> Range&
163  {
164  Marker::operator=(std::move(rhs));
165  m_handle = std::exchange(rhs.m_handle, nullptr);
166  m_started = std::exchange(rhs.m_started, bool{});
167 
168  return *this;
169  }
170 
178  ~Range() override
179  {
180  if(plugin::activated())
181  {
182  stop();
183  plugin::destroy_range(m_handle);
184  }
185  }
186 
192  auto start() noexcept -> void
193  {
194  if(!m_started && plugin::activated())
195  {
196  plugin::start_range(m_handle);
197  m_started = true;
198  }
199  }
200 
206  auto stop() noexcept -> void
207  {
208  if(m_started && plugin::activated())
209  {
210  plugin::stop_range(m_handle);
211  m_started = false;
212  }
213  }
214 
220  auto is_running() const noexcept -> bool
221  {
222  return m_started;
223  }
224 
225  private:
226  void* m_handle{
229  : nullptr};
230  bool m_started{false};
231  };
232  } // namespace ranges
233 } // namespace bactria
bactria::ranges::plugin::stop_range
auto stop_range(void *range_handle) noexcept
Plugin-specific range stopping.
Definition: Plugin.hpp:299
bactria::ranges::Range::Range
Range()
The default constructor.
Definition: Range.hpp:63
bactria::ranges::Range::operator=
auto operator=(Range const &rhs) -> Range &
The copy assignment operator.
Definition: Range.hpp:119
bactria::ranges::Category::get_id
auto get_id() const noexcept -> std::uint32_t
The ID getter method.
Definition: Category.hpp:110
bactria::ranges::Marker::m_name
std::string m_name
The name assigned to the Marker.
Definition: Marker.hpp:177
Colors.hpp
Color definitions.
bactria::ranges::Marker
The abstract base class for markers.
Definition: Marker.hpp:50
bactria::ranges::Marker::m_color
std::uint32_t m_color
The color assigned to the Marker.
Definition: Marker.hpp:185
bactria::metrics::plugin::activated
auto activated() -> bool
Checks for an active metrics plugin.
Definition: Plugin.hpp:58
bactria::ranges::Range::Range
Range(Range &&other) noexcept
The move constructor.
Definition: Range.hpp:145
bactria::ranges::Category
Defines a category.
Definition: Category.hpp:39
bactria::ranges::Range::Range
Range(Range const &other)
The copy constructor.
Definition: Range.hpp:101
bactria::ranges::plugin::start_range
auto start_range(void *range_handle) noexcept
Plugin-specific range starting.
Definition: Plugin.hpp:286
bactria::ranges::Range
The range class.
Definition: Range.hpp:52
bactria::ranges::Range::operator=
auto operator=(Range &&rhs) noexcept -> Range &
The move assignment operator.
Definition: Range.hpp:162
bactria::ranges::color::bactria_cyan
constexpr auto bactria_cyan
Definition: Colors.hpp:733
bactria::ranges::plugin::create_range
auto create_range(char const *name, std::uint32_t color, char const *cat_name, std::uint32_t cat_id) noexcept
Creates a plugin-specific range handle.
Definition: Plugin.hpp:254
bactria::ranges::Range::Range
Range(std::string name, std::uint32_t color=color::bactria_cyan, Category category=Category{}, bool autostart=true)
The constructor.
Definition: Range.hpp:80
bactria::ranges::plugin::destroy_range
auto destroy_range(void *range_handle) noexcept
Destroys a plugin-specific range handle.
Definition: Plugin.hpp:273
Plugin.hpp
bactria-internal handling of ranges plugins.
bactria::ranges::Marker::m_category
Category m_category
The Category assigned to the Marker.
Definition: Marker.hpp:193
bactria::ranges::Range::start
auto start() noexcept -> void
Manual start.
Definition: Range.hpp:192
bactria::ranges::Range::stop
auto stop() noexcept -> void
Manual stop.
Definition: Range.hpp:206
Marker.hpp
Marker definitions.
bactria::ranges::Range::is_running
auto is_running() const noexcept -> bool
Query status.
Definition: Range.hpp:220
bactria::ranges::Marker::operator=
auto operator=(Marker const &rhs) -> Marker &=default
Copy-assignment operator.
bactria::ranges::Category::get_c_name
auto get_c_name() const noexcept -> char const *
The C string name getter method.
Definition: Category.hpp:130
bactria::ranges::Range::~Range
~Range() override
The destructor.
Definition: Range.hpp:178
bactria::ranges::Marker::Marker
Marker()=default
Default constructor.