bactria  0.0.1
The bactria library is a header-only C++14 library for profiling and tracing.
Plugin.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/core/Plugin.hpp>
27 
28 #include <cstdint>
29 #include <cstdlib>
30 #include <stdexcept>
31 #include <string>
32 #include <type_traits>
33 
34 namespace bactria
35 {
36  namespace ranges
37  {
43  namespace plugin
44  {
59  inline auto activated() -> bool
60  {
61  static bool const activated = is_active() && (std::getenv("BACTRIA_RANGES_PLUGIN") != nullptr);
62  return activated;
63  }
64 
65  /* Standard function pointer declaration style is forbidden because of noexcept */
71  using create_event_t = std::add_pointer_t<void*(std::uint32_t, char const*, std::uint32_t) noexcept>;
72 
79 
85  using destroy_event_t = std::add_pointer_t<void(void*) noexcept>;
86 
93 
99  using fire_event_t
100  = std::add_pointer_t<void(void*, char const*, char const*, std::uint32_t, char const*) noexcept>;
101 
107  auto fire_event_ptr = fire_event_t{nullptr};
108 
114  using create_range_t
115  = std::add_pointer_t<void*(char const*, std::uint32_t, char const*, std::uint32_t) noexcept>;
116 
123 
129  using destroy_range_t = std::add_pointer_t<void(void*) noexcept>;
130 
137 
143  using start_range_t = std::add_pointer_t<void(void*) noexcept>;
144 
152 
158  using stop_range_t = std::add_pointer_t<void(void*) noexcept>;
159 
165  auto stop_range_ptr = stop_range_t{nullptr};
166 
175  [[nodiscard]] inline auto load() -> plugin_handle_t
176  {
177  auto const path = std::getenv("BACTRIA_RANGES_PLUGIN");
178  if(path != nullptr)
179  {
180  auto handle = system::open_plugin(path);
181 
182  system::load_func(handle, create_event_ptr, "bactria_ranges_create_event");
183  system::load_func(handle, destroy_event_ptr, "bactria_ranges_destroy_event");
184  system::load_func(handle, fire_event_ptr, "bactria_ranges_fire_event");
185 
186  system::load_func(handle, create_range_ptr, "bactria_ranges_create_range");
187  system::load_func(handle, destroy_range_ptr, "bactria_ranges_destroy_range");
188  system::load_func(handle, start_range_ptr, "bactria_ranges_start_range");
189  system::load_func(handle, stop_range_ptr, "bactria_ranges_stop_range");
190 
191  return handle;
192  }
193 
194  // getenv failed
195  throw std::runtime_error{std::string{"Failed to load bactria ranges plugin "} + path};
196  }
197 
205  [[nodiscard, gnu::always_inline]] inline auto create_event(
206  std::uint32_t color,
207  char const* cat_name,
208  std::uint32_t cat_id) noexcept
209  {
210  if(create_event_ptr != nullptr)
211  return (create_event_ptr) (color, cat_name, cat_id);
212 
213  return static_cast<void*>(nullptr);
214  }
215 
223  [[gnu::always_inline]] inline auto destroy_event(void* event_handle) noexcept
224  {
225  if(destroy_event_ptr != nullptr)
226  (destroy_event_ptr)(event_handle);
227  }
228 
236  [[gnu::always_inline]] inline auto fire_event(
237  void* event_handle,
238  char const* event_name,
239  char const* source,
240  std::uint32_t lineno,
241  char const* caller) noexcept
242  {
243  if(fire_event_ptr != nullptr)
244  (fire_event_ptr)(event_handle, event_name, source, lineno, caller);
245  }
246 
254  [[nodiscard, gnu::always_inline]] inline auto create_range(
255  char const* name,
256  std::uint32_t color,
257  char const* cat_name,
258  std::uint32_t cat_id) noexcept
259  {
260  if(create_range_ptr != nullptr)
261  return (create_range_ptr) (name, color, cat_name, cat_id);
262 
263  return static_cast<void*>(nullptr);
264  }
265 
273  [[gnu::always_inline]] inline auto destroy_range(void* range_handle) noexcept
274  {
275  if(destroy_range_ptr != nullptr)
276  (destroy_range_ptr)(range_handle);
277  }
278 
286  [[gnu::always_inline]] inline auto start_range(void* range_handle) noexcept
287  {
288  if(start_range_ptr != nullptr)
289  (start_range_ptr)(range_handle);
290  }
291 
299  [[gnu::always_inline]] inline auto stop_range(void* range_handle) noexcept
300  {
301  if(stop_range_ptr != nullptr)
302  (stop_range_ptr)(range_handle);
303  }
305  } // namespace plugin
306  } // namespace ranges
307 } // namespace bactria
bactria::ranges::plugin::destroy_range_ptr
auto destroy_range_ptr
Pointer to plugin function bactria_ranges_destroy_range().
Definition: Plugin.hpp:136
Plugin.hpp
bactria-internal plugin handling.
bactria::ranges::plugin::stop_range
auto stop_range(void *range_handle) noexcept
Plugin-specific range stopping.
Definition: Plugin.hpp:299
bactria::ranges::plugin::fire_event_ptr
auto fire_event_ptr
Pointer to plugin function bactria_ranges_fire_event().
Definition: Plugin.hpp:107
bactria::ranges::plugin::stop_range_t
std::add_pointer_t< void(void *) noexcept > stop_range_t
Signature for plugin function bactria_ranges_stop_range().
Definition: Plugin.hpp:158
bactria::ranges::plugin::destroy_event_t
std::add_pointer_t< void(void *) noexcept > destroy_event_t
Signature for plugin function bactria_ranges_destroy_event().
Definition: Plugin.hpp:85
bactria::is_active
auto is_active() -> bool
Checks whether the bactria library has been deactivated by the user.
Definition: Activation.hpp:52
bactria::ranges::plugin::destroy_range_t
std::add_pointer_t< void(void *) noexcept > destroy_range_t
Signature for plugin function bactria_ranges_destroy_range().
Definition: Plugin.hpp:129
bactria::system::load_func
auto load_func(plugin_handle_t handle, Sig &ptr, const char *name)
The POSIX-specific function loader.
Definition: POSIX.hpp:80
bactria::ranges::plugin::start_range
auto start_range(void *range_handle) noexcept
Plugin-specific range starting.
Definition: Plugin.hpp:286
bactria::ranges::plugin::create_event_ptr
auto create_event_ptr
Pointer to plugin function bactria_ranges_create_event().
Definition: Plugin.hpp:78
bactria::reports::plugin::activated
auto activated() -> bool
Checks for an active reports plugin.
Definition: Plugin.hpp:60
bactria::ranges::plugin::start_range_ptr
auto start_range_ptr
Pointer to plugin function bactria_ranges_start_range().
Definition: Plugin.hpp:151
bactria::ranges::plugin::destroy_event
auto destroy_event(void *event_handle) noexcept
Destroys a plugin-specific event handle.
Definition: Plugin.hpp:223
bactria::system::open_plugin
auto open_plugin(const char *path)
The POSIX-specific plugin loader.
Definition: POSIX.hpp:57
bactria::plugin_handle_t
system::plugin_handle_t plugin_handle_t
The platform-specific plugin handle type.
Definition: Plugin.hpp:41
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::plugin::create_range_ptr
auto create_range_ptr
Pointer to plugin function bactria_ranges_create_range().
Definition: Plugin.hpp:122
bactria::ranges::plugin::destroy_range
auto destroy_range(void *range_handle) noexcept
Destroys a plugin-specific range handle.
Definition: Plugin.hpp:273
bactria::ranges::plugin::create_range_t
std::add_pointer_t< void *(char const *, std::uint32_t, char const *, std::uint32_t) noexcept > create_range_t
Signature for plugin function bactria_ranges_create_range().
Definition: Plugin.hpp:115
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::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
bactria::ranges::plugin::stop_range_ptr
auto stop_range_ptr
Pointer to plugin function bactria_ranges_stop_range().
Definition: Plugin.hpp:165
bactria::reports::plugin::load
auto load() -> plugin_handle_t
Initializes the reports plugin.
Definition: Plugin.hpp:285
bactria::ranges::plugin::fire_event_t
std::add_pointer_t< void(void *, char const *, char const *, std::uint32_t, char const *) noexcept > fire_event_t
Signature for plugin function bactria_ranges_fire_event().
Definition: Plugin.hpp:100
bactria::ranges::plugin::destroy_event_ptr
auto destroy_event_ptr
Pointer to plugin function bactria_ranges_destroy_event().
Definition: Plugin.hpp:92
Activation.hpp
Plugin activation file.
bactria::ranges::plugin::start_range_t
std::add_pointer_t< void(void *) noexcept > start_range_t
Signature for plugin function bactria_ranges_start_range().
Definition: Plugin.hpp:143
bactria::ranges::plugin::create_event_t
std::add_pointer_t< void *(std::uint32_t, char const *, std::uint32_t) noexcept > create_event_t
Signature for plugin function bactria_ranges_create_event().
Definition: Plugin.hpp:71