rtc_export_template.h 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright (c) 2019 The WebRTC project authors. All Rights Reserved.
  3. *
  4. * Use of this source code is governed by a BSD-style license
  5. * that can be found in the LICENSE file in the root of the source
  6. * tree. An additional intellectual property rights grant can be found
  7. * in the file PATENTS. All contributing project authors may
  8. * be found in the AUTHORS file in the root of the source tree.
  9. */
  10. #ifndef RTC_BASE_SYSTEM_RTC_EXPORT_TEMPLATE_H_
  11. #define RTC_BASE_SYSTEM_RTC_EXPORT_TEMPLATE_H_
  12. // clang-format off
  13. // clang formating would cause cpplint errors in the macros below.
  14. // Most of this was borrowed (with minor modifications) from Chromium's
  15. // base/export_template.h.
  16. // Synopsis
  17. //
  18. // This header provides macros for using RTC_EXPORT macros with explicit
  19. // template instantiation declarations and definitions.
  20. // Generally, the RTC_EXPORT macros are used at declarations,
  21. // and GCC requires them to be used at explicit instantiation declarations,
  22. // but MSVC requires __declspec(dllexport) to be used at the explicit
  23. // instantiation definitions instead.
  24. // Usage
  25. //
  26. // In a header file, write:
  27. //
  28. // extern template class RTC_EXPORT_TEMPLATE_DECLARE(RTC_EXPORT) foo<bar>;
  29. //
  30. // In a source file, write:
  31. //
  32. // template class RTC_EXPORT_TEMPLATE_DEFINE(RTC_EXPORT) foo<bar>;
  33. // Implementation notes
  34. //
  35. // On Windows, when building when RTC_EXPORT expands to __declspec(dllexport)),
  36. // we want the two lines to expand to:
  37. //
  38. // extern template class foo<bar>;
  39. // template class RTC_EXPORT foo<bar>;
  40. //
  41. // In all other cases (non-Windows, and Windows when RTC_EXPORT expands to
  42. // __declspec(dllimport)), we want:
  43. //
  44. // extern template class RTC_EXPORT foo<bar>;
  45. // template class foo<bar>;
  46. //
  47. // The implementation of this header uses some subtle macro semantics to
  48. // detect what the provided RTC_EXPORT value was defined as and then
  49. // to dispatch to appropriate macro definitions. Unfortunately,
  50. // MSVC's C preprocessor is rather non-compliant and requires special
  51. // care to make it work.
  52. //
  53. // Issue 1.
  54. //
  55. // #define F(x)
  56. // F()
  57. //
  58. // MSVC emits warning C4003 ("not enough actual parameters for macro
  59. // 'F'), even though it's a valid macro invocation. This affects the
  60. // macros below that take just an "export" parameter, because export
  61. // may be empty.
  62. //
  63. // As a workaround, we can add a dummy parameter and arguments:
  64. //
  65. // #define F(x,_)
  66. // F(,)
  67. //
  68. // Issue 2.
  69. //
  70. // #define F(x) G##x
  71. // #define Gj() ok
  72. // F(j())
  73. //
  74. // The correct replacement for "F(j())" is "ok", but MSVC replaces it
  75. // with "Gj()". As a workaround, we can pass the result to an
  76. // identity macro to force MSVC to look for replacements again. (This
  77. // is why RTC_EXPORT_TEMPLATE_STYLE_3 exists.)
  78. #define RTC_EXPORT_TEMPLATE_DECLARE(export) \
  79. RTC_EXPORT_TEMPLATE_INVOKE( \
  80. DECLARE, \
  81. RTC_EXPORT_TEMPLATE_STYLE(export, ), export) // NOLINT
  82. #define RTC_EXPORT_TEMPLATE_DEFINE(export) \
  83. RTC_EXPORT_TEMPLATE_INVOKE( \
  84. DEFINE, \
  85. RTC_EXPORT_TEMPLATE_STYLE(export, ), export) // NOLINT
  86. // INVOKE is an internal helper macro to perform parameter replacements
  87. // and token pasting to chain invoke another macro. E.g.,
  88. // RTC_EXPORT_TEMPLATE_INVOKE(DECLARE, DEFAULT, RTC_EXPORT)
  89. // will export to call
  90. // RTC_EXPORT_TEMPLATE_DECLARE_DEFAULT(RTC_EXPORT, )
  91. // (but with RTC_EXPORT expanded too).
  92. #define RTC_EXPORT_TEMPLATE_INVOKE(which, style, export) \
  93. RTC_EXPORT_TEMPLATE_INVOKE_2(which, style, export)
  94. #define RTC_EXPORT_TEMPLATE_INVOKE_2(which, style, export) \
  95. RTC_EXPORT_TEMPLATE_##which##_##style(export, )
  96. // Default style is to apply the RTC_EXPORT macro at declaration sites.
  97. #define RTC_EXPORT_TEMPLATE_DECLARE_DEFAULT(export, _) export
  98. #define RTC_EXPORT_TEMPLATE_DEFINE_DEFAULT(export, _)
  99. // The "MSVC hack" style is used when RTC_EXPORT is defined
  100. // as __declspec(dllexport), which MSVC requires to be used at
  101. // definition sites instead.
  102. #define RTC_EXPORT_TEMPLATE_DECLARE_MSVC_HACK(export, _)
  103. #define RTC_EXPORT_TEMPLATE_DEFINE_MSVC_HACK(export, _) export
  104. // RTC_EXPORT_TEMPLATE_STYLE is an internal helper macro that identifies which
  105. // export style needs to be used for the provided RTC_EXPORT macro definition.
  106. // "", "__attribute__(...)", and "__declspec(dllimport)" are mapped
  107. // to "DEFAULT"; while "__declspec(dllexport)" is mapped to "MSVC_HACK".
  108. //
  109. // It's implemented with token pasting to transform the __attribute__ and
  110. // __declspec annotations into macro invocations. E.g., if RTC_EXPORT is
  111. // defined as "__declspec(dllimport)", it undergoes the following sequence of
  112. // macro substitutions:
  113. // RTC_EXPORT_TEMPLATE_STYLE(RTC_EXPORT,)
  114. // RTC_EXPORT_TEMPLATE_STYLE_2(__declspec(dllimport),)
  115. // RTC_EXPORT_TEMPLATE_STYLE_3(
  116. // RTC_EXPORT_TEMPLATE_STYLE_MATCH__declspec(dllimport))
  117. // RTC_EXPORT_TEMPLATE_STYLE_MATCH__declspec(dllimport)
  118. // RTC_EXPORT_TEMPLATE_STYLE_MATCH_DECLSPEC_dllimport
  119. // DEFAULT
  120. #define RTC_EXPORT_TEMPLATE_STYLE(export, _) \
  121. RTC_EXPORT_TEMPLATE_STYLE_2(export, )
  122. #define RTC_EXPORT_TEMPLATE_STYLE_2(export, _) \
  123. RTC_EXPORT_TEMPLATE_STYLE_3( \
  124. RTC_EXPORT_TEMPLATE_STYLE_MATCH_foj3FJo5StF0OvIzl7oMxA##export)
  125. #define RTC_EXPORT_TEMPLATE_STYLE_3(style) style
  126. // Internal helper macros for RTC_EXPORT_TEMPLATE_STYLE.
  127. //
  128. // XXX: C++ reserves all identifiers containing "__" for the implementation,
  129. // but "__attribute__" and "__declspec" already contain "__" and the token-paste
  130. // operator can only add characters; not remove them. To minimize the risk of
  131. // conflict with implementations, we include "foj3FJo5StF0OvIzl7oMxA" (a random
  132. // 128-bit string, encoded in Base64) in the macro name.
  133. #define RTC_EXPORT_TEMPLATE_STYLE_MATCH_foj3FJo5StF0OvIzl7oMxA DEFAULT
  134. #define RTC_EXPORT_TEMPLATE_STYLE_MATCH_foj3FJo5StF0OvIzl7oMxA__attribute__( \
  135. ...) \
  136. DEFAULT
  137. #define RTC_EXPORT_TEMPLATE_STYLE_MATCH_foj3FJo5StF0OvIzl7oMxA__declspec(arg) \
  138. RTC_EXPORT_TEMPLATE_STYLE_MATCH_DECLSPEC_##arg
  139. // Internal helper macros for RTC_EXPORT_TEMPLATE_STYLE.
  140. #define RTC_EXPORT_TEMPLATE_STYLE_MATCH_DECLSPEC_dllexport MSVC_HACK
  141. #define RTC_EXPORT_TEMPLATE_STYLE_MATCH_DECLSPEC_dllimport DEFAULT
  142. // Sanity checks.
  143. //
  144. // RTC_EXPORT_TEMPLATE_TEST uses the same macro invocation pattern as
  145. // RTC_EXPORT_TEMPLATE_DECLARE and RTC_EXPORT_TEMPLATE_DEFINE do to check that
  146. // they're working correctly. When they're working correctly, the sequence of
  147. // macro replacements should go something like:
  148. //
  149. // RTC_EXPORT_TEMPLATE_TEST(DEFAULT, __declspec(dllimport));
  150. //
  151. // static_assert(RTC_EXPORT_TEMPLATE_INVOKE(TEST_DEFAULT,
  152. // RTC_EXPORT_TEMPLATE_STYLE(__declspec(dllimport), ),
  153. // __declspec(dllimport)), "__declspec(dllimport)");
  154. //
  155. // static_assert(RTC_EXPORT_TEMPLATE_INVOKE(TEST_DEFAULT,
  156. // DEFAULT, __declspec(dllimport)), "__declspec(dllimport)");
  157. //
  158. // static_assert(RTC_EXPORT_TEMPLATE_TEST_DEFAULT_DEFAULT(
  159. // __declspec(dllimport)), "__declspec(dllimport)");
  160. //
  161. // static_assert(true, "__declspec(dllimport)");
  162. //
  163. // When they're not working correctly, a syntax error should occur instead.
  164. #define RTC_EXPORT_TEMPLATE_TEST(want, export) \
  165. static_assert( \
  166. RTC_EXPORT_TEMPLATE_INVOKE( \
  167. TEST_##want, \
  168. RTC_EXPORT_TEMPLATE_STYLE(export, ), export), #export) // NOLINT
  169. #define RTC_EXPORT_TEMPLATE_TEST_DEFAULT_DEFAULT(...) true
  170. #define RTC_EXPORT_TEMPLATE_TEST_MSVC_HACK_MSVC_HACK(...) true
  171. RTC_EXPORT_TEMPLATE_TEST(DEFAULT, ); // NOLINT
  172. RTC_EXPORT_TEMPLATE_TEST(DEFAULT, __attribute__((visibility("default"))));
  173. RTC_EXPORT_TEMPLATE_TEST(MSVC_HACK, __declspec(dllexport));
  174. RTC_EXPORT_TEMPLATE_TEST(DEFAULT, __declspec(dllimport));
  175. #undef RTC_EXPORT_TEMPLATE_TEST
  176. #undef RTC_EXPORT_TEMPLATE_TEST_DEFAULT_DEFAULT
  177. #undef RTC_EXPORT_TEMPLATE_TEST_MSVC_HACK_MSVC_HACK
  178. // clang-format on
  179. #endif // RTC_BASE_SYSTEM_RTC_EXPORT_TEMPLATE_H_