@@ -48,15 +48,21 @@ namespace internal {
48
48
template <typename T>
49
49
using identity_t = T;
50
50
51
- template <typename MockType>
52
- const MockType* AdjustConstness_const (const MockType* mock) {
53
- return mock;
54
- }
55
-
56
- template <typename MockType>
57
- MockType* AdjustConstness_ (const MockType* mock) {
58
- return const_cast <MockType*>(mock);
59
- }
51
+ template <typename Pattern>
52
+ struct ThisRefAdjuster {
53
+ template <typename T>
54
+ using AdjustT = typename std::conditional<
55
+ std::is_const<typename std::remove_reference<Pattern>::type>::value,
56
+ typename std::conditional<std::is_lvalue_reference<Pattern>::value,
57
+ const T&, const T&&>::type,
58
+ typename std::conditional<std::is_lvalue_reference<Pattern>::value, T&,
59
+ T&&>::type>::type;
60
+
61
+ template <typename MockType>
62
+ static AdjustT<MockType> Adjust (const MockType& mock) {
63
+ return static_cast <AdjustT<MockType>>(const_cast <MockType&>(mock));
64
+ }
65
+ };
60
66
61
67
} // namespace internal
62
68
@@ -80,17 +86,17 @@ using internal::FunctionMocker;
80
86
#define GMOCK_INTERNAL_MOCK_METHOD_ARG_3 (_Ret, _MethodName, _Args ) \
81
87
GMOCK_INTERNAL_MOCK_METHOD_ARG_4 (_Ret, _MethodName, _Args, ())
82
88
83
- #define GMOCK_INTERNAL_MOCK_METHOD_ARG_4 (_Ret, _MethodName, _Args, _Spec ) \
84
- GMOCK_INTERNAL_ASSERT_PARENTHESIS (_Args); \
85
- GMOCK_INTERNAL_ASSERT_PARENTHESIS (_Spec); \
86
- GMOCK_INTERNAL_ASSERT_VALID_SIGNATURE ( \
87
- GMOCK_PP_NARG0 _Args, GMOCK_INTERNAL_SIGNATURE(_Ret, _Args)); \
88
- GMOCK_INTERNAL_ASSERT_VALID_SPEC (_Spec) \
89
- GMOCK_INTERNAL_MOCK_METHOD_IMPL( \
90
- GMOCK_PP_NARG0 _Args, _MethodName, GMOCK_INTERNAL_HAS_CONST(_Spec), \
91
- GMOCK_INTERNAL_HAS_OVERRIDE(_Spec), GMOCK_INTERNAL_HAS_FINAL(_Spec), \
92
- GMOCK_INTERNAL_GET_NOEXCEPT_SPEC(_Spec), \
93
- GMOCK_INTERNAL_GET_CALLTYPE(_Spec), \
89
+ #define GMOCK_INTERNAL_MOCK_METHOD_ARG_4 (_Ret, _MethodName, _Args, _Spec ) \
90
+ GMOCK_INTERNAL_ASSERT_PARENTHESIS (_Args); \
91
+ GMOCK_INTERNAL_ASSERT_PARENTHESIS (_Spec); \
92
+ GMOCK_INTERNAL_ASSERT_VALID_SIGNATURE ( \
93
+ GMOCK_PP_NARG0 _Args, GMOCK_INTERNAL_SIGNATURE(_Ret, _Args)); \
94
+ GMOCK_INTERNAL_ASSERT_VALID_SPEC (_Spec) \
95
+ GMOCK_INTERNAL_MOCK_METHOD_IMPL( \
96
+ GMOCK_PP_NARG0 _Args, _MethodName, GMOCK_INTERNAL_HAS_CONST(_Spec), \
97
+ GMOCK_INTERNAL_HAS_OVERRIDE(_Spec), GMOCK_INTERNAL_HAS_FINAL(_Spec), \
98
+ GMOCK_INTERNAL_GET_NOEXCEPT_SPEC(_Spec), \
99
+ GMOCK_INTERNAL_GET_CALLTYPE(_Spec), GMOCK_INTERNAL_GET_REF_SPEC(_Spec), \
94
100
(GMOCK_INTERNAL_SIGNATURE(_Ret, _Args)))
95
101
96
102
#define GMOCK_INTERNAL_MOCK_METHOD_ARG_5 (...) \
@@ -131,12 +137,12 @@ using internal::FunctionMocker;
131
137
132
138
#define GMOCK_INTERNAL_MOCK_METHOD_IMPL (_N, _MethodName, _Constness, \
133
139
_Override, _Final, _NoexceptSpec, \
134
- _CallType, _Signature) \
140
+ _CallType, _RefSpec, _Signature) \
135
141
typename ::testing::internal::Function<GMOCK_PP_REMOVE_PARENS( \
136
142
_Signature)>::Result \
137
143
GMOCK_INTERNAL_EXPAND(_CallType) \
138
144
_MethodName(GMOCK_PP_REPEAT(GMOCK_INTERNAL_PARAMETER, _Signature, _N)) \
139
- GMOCK_PP_IF(_Constness, const , ) _NoexceptSpec \
145
+ GMOCK_PP_IF(_Constness, const , ) _RefSpec _NoexceptSpec \
140
146
GMOCK_PP_IF(_Override, override , ) GMOCK_PP_IF(_Final, final , ) { \
141
147
GMOCK_MOCKER_ (_N, _Constness, _MethodName) \
142
148
.SetOwnerAndName (this , #_MethodName); \
@@ -145,18 +151,18 @@ using internal::FunctionMocker;
145
151
} \
146
152
::testing::MockSpec<GMOCK_PP_REMOVE_PARENS(_Signature)> gmock_##_MethodName( \
147
153
GMOCK_PP_REPEAT (GMOCK_INTERNAL_MATCHER_PARAMETER, _Signature, _N)) \
148
- GMOCK_PP_IF(_Constness, const , ) { \
154
+ GMOCK_PP_IF(_Constness, const , ) _RefSpec { \
149
155
GMOCK_MOCKER_ (_N, _Constness, _MethodName).RegisterOwner (this ); \
150
156
return GMOCK_MOCKER_ (_N, _Constness, _MethodName) \
151
157
.With (GMOCK_PP_REPEAT (GMOCK_INTERNAL_MATCHER_ARGUMENT, , _N)); \
152
158
} \
153
159
::testing::MockSpec<GMOCK_PP_REMOVE_PARENS(_Signature)> gmock_##_MethodName( \
154
160
const ::testing::internal::WithoutMatchers&, \
155
161
GMOCK_PP_IF (_Constness, const , )::testing::internal::Function< \
156
- GMOCK_PP_REMOVE_PARENS(_Signature)>*) const _NoexceptSpec { \
157
- return GMOCK_PP_CAT ( ::testing::internal::AdjustConstness_, \
158
- GMOCK_PP_IF ( _Constness, const , ))( this ) \
159
- -> gmock_ ##_MethodName (GMOCK_PP_REPEAT ( \
162
+ GMOCK_PP_REMOVE_PARENS(_Signature)>*) const _RefSpec _NoexceptSpec { \
163
+ return ::testing::internal::ThisRefAdjuster< GMOCK_PP_IF ( \
164
+ _Constness, const , ) int _RefSpec>:: Adjust (* this ) \
165
+ . gmock_ ##_MethodName (GMOCK_PP_REPEAT ( \
160
166
GMOCK_INTERNAL_A_MATCHER_ARGUMENT, _Signature, _N)); \
161
167
} \
162
168
mutable ::testing::FunctionMocker<GMOCK_PP_REMOVE_PARENS(_Signature)> \
@@ -183,6 +189,13 @@ using internal::FunctionMocker;
183
189
GMOCK_PP_HAS_COMMA (GMOCK_INTERNAL_DETECT_NOEXCEPT(_i, _, _elem)), \
184
190
_elem, )
185
191
192
+ #define GMOCK_INTERNAL_GET_REF_SPEC (_Tuple ) \
193
+ GMOCK_PP_FOR_EACH (GMOCK_INTERNAL_REF_SPEC_IF_REF, ~, _Tuple)
194
+
195
+ #define GMOCK_INTERNAL_REF_SPEC_IF_REF (_i, _, _elem ) \
196
+ GMOCK_PP_IF (GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_REF(_i, _, _elem)), \
197
+ GMOCK_PP_CAT(GMOCK_INTERNAL_UNPACK_, _elem), )
198
+
186
199
#define GMOCK_INTERNAL_GET_CALLTYPE (_Tuple ) \
187
200
GMOCK_PP_FOR_EACH (GMOCK_INTERNAL_GET_CALLTYPE_IMPL, ~, _Tuple)
188
201
@@ -192,6 +205,7 @@ using internal::FunctionMocker;
192
205
GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_OVERRIDE(_i, _, _elem)) + \
193
206
GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_FINAL(_i, _, _elem)) + \
194
207
GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_NOEXCEPT(_i, _, _elem)) + \
208
+ GMOCK_PP_HAS_COMMA(GMOCK_INTERNAL_DETECT_REF(_i, _, _elem)) + \
195
209
GMOCK_INTERNAL_IS_CALLTYPE(_elem)) == 1, \
196
210
GMOCK_PP_STRINGIZE( \
197
211
_elem) " cannot be recognized as a valid specification modifier.");
@@ -217,6 +231,13 @@ using internal::FunctionMocker;
217
231
218
232
#define GMOCK_INTERNAL_DETECT_NOEXCEPT_I_noexcept ,
219
233
234
+ #define GMOCK_INTERNAL_DETECT_REF (_i, _, _elem ) \
235
+ GMOCK_PP_CAT (GMOCK_INTERNAL_DETECT_REF_I_, _elem)
236
+
237
+ #define GMOCK_INTERNAL_DETECT_REF_I_ref ,
238
+
239
+ #define GMOCK_INTERNAL_UNPACK_ref (x ) x
240
+
220
241
#define GMOCK_INTERNAL_GET_CALLTYPE_IMPL (_i, _, _elem ) \
221
242
GMOCK_PP_IF (GMOCK_INTERNAL_IS_CALLTYPE(_elem), \
222
243
GMOCK_INTERNAL_GET_VALUE_CALLTYPE, GMOCK_PP_EMPTY) \
@@ -449,7 +470,7 @@ using internal::FunctionMocker;
449
470
GMOCK_INTERNAL_ASSERT_VALID_SIGNATURE ( \
450
471
args_num, ::testing::internal::identity_t <__VA_ARGS__>); \
451
472
GMOCK_INTERNAL_MOCK_METHOD_IMPL ( \
452
- args_num, Method, GMOCK_PP_NARG0(constness), 0, 0, , ct, \
473
+ args_num, Method, GMOCK_PP_NARG0(constness), 0, 0, , ct, , \
453
474
(::testing::internal::identity_t <__VA_ARGS__>))
454
475
455
476
#define GMOCK_MOCKER_ (arity, constness, Method ) \
0 commit comments