Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #ifndef BASE_EXPORT_TEMPLATE_H_ | |
| 6 #define BASE_EXPORT_TEMPLATE_H_ | |
| 7 | |
| 8 // Synopsis | |
|
vmpstr
2015/12/19 00:26:47
Does the approach of directly checking if we're on
| |
| 9 // | |
| 10 // This header provides macros for using FOO_EXPORT macros with explicit | |
| 11 // template instantiation declarations and definitions. | |
| 12 // Generally, the FOO_EXPORT macros are used at declarations, | |
| 13 // and GCC requires them to be used at explicit instantiation declarations, | |
| 14 // but MSVC requires __declspec(dllexport) to be used at the explicit | |
| 15 // instantiation definition instead. | |
| 16 | |
| 17 // Usage | |
| 18 // | |
| 19 // In a header file, write: | |
| 20 // | |
| 21 // extern template class EXPORT_TEMPLATE_DECLARE(FOO_EXPORT) foo<bar>; | |
| 22 // | |
| 23 // In a source file, write: | |
| 24 // | |
| 25 // template class EXPORT_TEMPLATE_DEFINE(FOO_EXPORT) foo<bar>; | |
| 26 | |
| 27 // Implementation notes | |
| 28 // | |
| 29 // The implementation of this header uses some subtle macro trickery to | |
| 30 // detect what the provided FOO_EXPORT value was defined as and then | |
| 31 // to dispatch to appropriate macro definitions. Unfortunately, | |
| 32 // MSVC's C preprocessor is rather non-compliant and requires special | |
| 33 // care to make it work. | |
| 34 // | |
| 35 // Issue 1. | |
| 36 // | |
| 37 // #define F(x) | |
| 38 // F() | |
| 39 // | |
| 40 // MSVC emits warning C4003 ("not enough actual parameters for macro | |
|
danakj
2015/12/19 00:15:05
True in MSVC 2015?
mdempsky
2015/12/19 02:14:48
Version "19.00.23602.0" at http://webcompiler.clou
| |
| 41 // 'F'), even though it's a valid macro invocation. This affects the | |
| 42 // macros below that take just an "export" parameter, because export | |
| 43 // may be empty. | |
| 44 // | |
| 45 // As a workaround, we can add a dummy parameter and arguments: | |
| 46 // | |
| 47 // #define F(x,_) | |
| 48 // F(,) | |
| 49 // | |
| 50 // Issue 3. | |
|
danakj
2015/12/19 00:15:05
One wonders where Issue 2 is..
mdempsky
2015/12/19 02:14:48
Typo. Issue 2 had to do with chaining variadic ma
| |
| 51 // | |
| 52 // #define F(x) G##x | |
| 53 // #define Gj() ok | |
| 54 // F(j()) | |
| 55 // | |
| 56 // The correct replacement for "F(j())" is "ok", but MSVC replaces it | |
| 57 // with "Gj()". As a workaround, we can pass the result to an | |
| 58 // identity macro to force MSVC to look for replacements again. (This | |
| 59 // is why EXPORT_TEMPLATE_STYLE_3 exists.) | |
| 60 | |
| 61 #define EXPORT_TEMPLATE_DECLARE(export) \ | |
|
danakj
2015/12/19 00:15:05
I think this needs a bunch more comments. I've man
mdempsky
2015/12/19 02:14:48
Will do. What sort of comments would be most usef
| |
| 62 EXPORT_TEMPLATE_INVOKE(DECLARE, EXPORT_TEMPLATE_STYLE(export, ), export) | |
| 63 #define EXPORT_TEMPLATE_DEFINE(export) \ | |
| 64 EXPORT_TEMPLATE_INVOKE(DEFINE, EXPORT_TEMPLATE_STYLE(export, ), export) | |
| 65 | |
| 66 #define EXPORT_TEMPLATE_INVOKE(which, style, export) \ | |
| 67 EXPORT_TEMPLATE_INVOKE_2(which, style, export) | |
| 68 #define EXPORT_TEMPLATE_INVOKE_2(which, style, export) \ | |
| 69 EXPORT_TEMPLATE_##which##_##style(export, ) | |
| 70 | |
| 71 #define EXPORT_TEMPLATE_DECLARE_DEFAULT(export, _) export | |
| 72 #define EXPORT_TEMPLATE_DEFINE_DEFAULT(export, _) | |
| 73 | |
| 74 #define EXPORT_TEMPLATE_DECLARE_MSVC_HACK(export, _) | |
| 75 #define EXPORT_TEMPLATE_DEFINE_MSVC_HACK(export, _) export | |
| 76 | |
| 77 #define EXPORT_TEMPLATE_STYLE(export, _) EXPORT_TEMPLATE_STYLE_2(export, ) | |
| 78 #define EXPORT_TEMPLATE_STYLE_2(export, _) \ | |
| 79 EXPORT_TEMPLATE_STYLE_3(EXPORT_TEMPLATE_STYLE_MATCH##export) | |
| 80 #define EXPORT_TEMPLATE_STYLE_3(style) style | |
| 81 | |
| 82 #define EXPORT_TEMPLATE_STYLE_MATCH DEFAULT | |
| 83 #define EXPORT_TEMPLATE_STYLE_MATCH__attribute__(...) DEFAULT | |
|
dcheng
2015/12/21 18:32:37
Any name that contains a double-underscore is rese
mdempsky
2015/12/23 23:17:45
Hm, indeed. (I didn't realize C++ reserved double
| |
| 84 #define EXPORT_TEMPLATE_STYLE_MATCH__declspec(arg) \ | |
| 85 EXPORT_TEMPLATE_STYLE_MATCH_DECLSPEC_##arg | |
| 86 | |
| 87 #define EXPORT_TEMPLATE_STYLE_MATCH_DECLSPEC_dllexport MSVC_HACK | |
| 88 #define EXPORT_TEMPLATE_STYLE_MATCH_DECLSPEC_dllimport DEFAULT | |
| 89 | |
| 90 // Sanity checks. | |
| 91 | |
| 92 #define EXPORT_TEMPLATE_TEST(want, export) \ | |
| 93 static_assert(EXPORT_TEMPLATE_INVOKE( \ | |
| 94 TEST_##want, EXPORT_TEMPLATE_STYLE(export, ), export), \ | |
|
danakj
2015/12/19 00:15:05
Same even for the tests. This ends up as TEST_DEFA
mdempsky
2015/12/19 02:14:48
FWIW, what happens here is that when the macros wo
| |
| 95 #export) | |
| 96 #define EXPORT_TEMPLATE_TEST_DEFAULT_DEFAULT(...) true | |
| 97 #define EXPORT_TEMPLATE_TEST_MSVC_HACK_MSVC_HACK(...) true | |
| 98 | |
| 99 EXPORT_TEMPLATE_TEST(DEFAULT, ); | |
| 100 EXPORT_TEMPLATE_TEST(DEFAULT, __attribute__((visibility("default")))); | |
| 101 EXPORT_TEMPLATE_TEST(MSVC_HACK, __declspec(dllexport)); | |
| 102 EXPORT_TEMPLATE_TEST(DEFAULT, __declspec(dllimport)); | |
| 103 | |
| 104 #endif // BASE_EXPORT_TEMPLATE_H_ | |
| OLD | NEW |