bactria  0.0.1
The bactria library is a header-only C++14 library for profiling and tracing.
Event.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 
24 #pragma once
25 
30 
31 #include <functional>
32 #include <string>
33 #include <utility>
34 
35 namespace bactria
36 {
37  namespace ranges
38  {
48  class Event : public Marker
49  {
50  public:
57  Event() : Marker("BACTRIA_GENERIC_EVENT", color::bactria_orange, Category{})
58  {
59  }
60 
71  Event(std::string name, std::uint32_t color = color::bactria_orange, Category category = Category{})
72  : Marker(std::move(name), color, std::move(category))
73  {
74  }
75 
83  Event(const Event& other)
84  : Marker(other)
86  , m_action{other.m_action}
87  {
88  }
89 
97  auto operator=(const Event& rhs) -> Event&
98  {
99  Marker::operator=(rhs);
100 
101  if(plugin::activated())
103  else
104  m_handle = nullptr;
105 
106  m_action = rhs.m_action;
107 
108  return *this;
109  }
110 
119  Event(Event&& other) noexcept : Marker(std::move(other)), m_handle{std::exchange(other.m_handle, nullptr)}
120  {
121  std::swap(m_action, other.m_action);
122  }
123 
132  auto operator=(Event&& rhs) noexcept -> Event&
133  {
134  Marker::operator=(std::move(rhs));
135  m_handle = std::exchange(rhs.m_handle, nullptr);
136  std::swap(m_action, rhs.m_action);
137 
138  return *this;
139  }
140 
147  ~Event() override
148  {
149  if(plugin::activated())
150  plugin::destroy_event(m_handle);
151  }
152 
164  auto fire(std::string source, std::uint32_t lineno, std::string caller) noexcept -> void
165  {
166  if(plugin::activated())
167  {
168  plugin::fire_event(m_handle, m_action().c_str(), source.c_str(), lineno, caller.c_str());
169  }
170  }
171 
181  auto set_action(std::function<std::string(void)> a) noexcept -> void
182  {
183  m_action = std::move(a);
184  }
185 
186  private:
187  void* m_handle{
189  : nullptr};
190  std::function<std::string(void)> m_action = [this]() { return m_name; };
191  };
192  } // namespace ranges
193 } // namespace bactria
194 
207 #define bactria_Event(name, color, category) \
208  { \
209  auto e = bactria::ranges::Event(name, color, category); \
210  e.fire(__FILE__, __LINE__, __func__); \
211  }
212 
225 #define bactria_ActionEvent(action, color, category) \
226  { \
227  auto e = bactria::ranges::Event("BACTRIA_ACTION_EVENT", color, category); \
228  e.set_action(action); \
229  e.fire(__FILE__, __LINE__, __func__); \
230  }
bactria::ranges::Event::Event
Event(std::string name, std::uint32_t color=color::bactria_orange, Category category=Category{})
The constructor.
Definition: Event.hpp:71
bactria::ranges::Event::Event
Event(Event &&other) noexcept
The move constructor.
Definition: Event.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::Event::Event
Event(const Event &other)
The copy constructor.
Definition: Event.hpp:83
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::color::bactria_orange
constexpr auto bactria_orange
Definition: Colors.hpp:763
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
Category.hpp
Category definitions.
bactria::ranges::Event::Event
Event()
The default constructor.
Definition: Event.hpp:57
bactria::ranges::Category
Defines a category.
Definition: Category.hpp:39
bactria::ranges::Event
The event class.
Definition: Event.hpp:48
bactria::ranges::Event::operator=
auto operator=(const Event &rhs) -> Event &
The copy assignment operator.
Definition: Event.hpp:97
bactria::ranges::plugin::destroy_event
auto destroy_event(void *event_handle) noexcept
Destroys a plugin-specific event handle.
Definition: Plugin.hpp:223
bactria::ranges::Event::set_action
auto set_action(std::function< std::string(void)> a) noexcept -> void
Set a user-defined action for generating the event name.
Definition: Event.hpp:181
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::plugin::create_event
auto create_event(std::uint32_t color, char const *cat_name, std::uint32_t cat_id) noexcept
Creates a plugin-specific event handle.
Definition: Plugin.hpp:205
bactria::ranges::Event::~Event
~Event() override
The destructor.
Definition: Event.hpp:147
bactria::ranges::plugin::fire_event
auto fire_event(void *event_handle, char const *event_name, char const *source, std::uint32_t lineno, char const *caller) noexcept
Plugin-specific event firing.
Definition: Plugin.hpp:236
Marker.hpp
Marker definitions.
bactria::ranges::Event::operator=
auto operator=(Event &&rhs) noexcept -> Event &
The move assignment operator.
Definition: Event.hpp:132
bactria::ranges::Event::fire
auto fire(std::string source, std::uint32_t lineno, std::string caller) noexcept -> void
Fire the event.
Definition: Event.hpp:164
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::Marker::Marker
Marker()=default
Default constructor.