Static.hpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. ///
  2. /// \file Plugin/Static.hpp
  3. ///
  4. /// Static initialization implementation for load-time registration.
  5. ///
  6. /// \copyright
  7. /// Copyright (c) 2013-2014 Josh Blum
  8. /// SPDX-License-Identifier: BSL-1.0
  9. ///
  10. #pragma once
  11. #include <Pothos/Config.hpp>
  12. #include <Pothos/System/Version.hpp>
  13. /*!
  14. * Define the tokens used to declare a static block's function.
  15. * The address of the function is used to make the static fixture
  16. * class unique across the complete library + module address space.
  17. *
  18. * Clang can only use static, inline functions of the same name will overlap.
  19. * MSVC can only use inline because static cannot be used as a template argument.
  20. * And GCC can handle all possibilities: static, inline, and static inline.
  21. *
  22. * The specific reason for this implementation and its caveats
  23. * is because clang will cause symbol overlap for class and functions
  24. * with the same name across modules, even if they are not exported.
  25. */
  26. #if defined(__clang__)
  27. #define POTHOS_STATIC_FIXTURE_DECL static
  28. #elif defined(_MSC_VER)
  29. #define POTHOS_STATIC_FIXTURE_DECL inline
  30. #elif defined(__GNUC__)
  31. #define POTHOS_STATIC_FIXTURE_DECL static inline
  32. #else
  33. #define POTHOS_STATIC_FIXTURE_DECL
  34. #endif
  35. /*!
  36. * pothos_static_block - macro that declares code to be run at init time
  37. * Example usage:
  38. * pothos_static_block(someValidFunctionName)
  39. * {
  40. * //your code here to be run at init time
  41. * }
  42. */
  43. #define pothos_static_block(name) \
  44. POTHOS_STATIC_FIXTURE_DECL void name ## StaticFixtureInit__(void); \
  45. template <Pothos::Detail::InitFcn init> \
  46. struct name ## StaticFixture__ \
  47. { \
  48. name ## StaticFixture__(void) \
  49. { \
  50. Pothos::Detail::safeInit(POTHOS_ABI_VERSION, #name, init); \
  51. } \
  52. }; \
  53. static name ## StaticFixture__<&name ## StaticFixtureInit__> name ## StaticFixtureInstance__; \
  54. POTHOS_STATIC_FIXTURE_DECL void name ## StaticFixtureInit__(void)
  55. namespace Pothos {
  56. namespace Detail {
  57. typedef void (*InitFcn)(void);
  58. POTHOS_API void safeInit(const std::string &clientAbi, const std::string &name, InitFcn init);
  59. } //namespace Detail
  60. } //namespace Pothos