1 // Copyright 2007, Google Inc.
2 // All rights reserved.
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 // Google Mock - a framework for writing C++ mock classes.
33 // This file implements some actions that depend on gmock-generated-actions.h.
35 // GOOGLETEST_CM0002 DO NOT DELETE
37 // IWYU pragma: private, include "gmock/gmock.h"
39 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
40 #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
43 #include <type_traits>
45 #include "gmock/gmock-generated-actions.h"
50 // An internal replacement for std::copy which mimics its behavior. This is
51 // necessary because Visual Studio deprecates ::std::copy, issuing warning 4996.
52 // However Visual Studio 2010 and later do not honor #pragmas which disable that
54 template<typename InputIterator
, typename OutputIterator
>
55 inline OutputIterator
CopyElements(InputIterator first
,
57 OutputIterator output
) {
58 for (; first
!= last
; ++first
, ++output
) {
64 } // namespace internal
66 // Various overloads for Invoke().
68 // The ACTION*() macros trigger warning C4100 (unreferenced formal
69 // parameter) in MSVC with -W4. Unfortunately they cannot be fixed in
70 // the macro definition, as the warnings are generated when the macro
71 // is expanded and macro expansion cannot contain #pragma. Therefore
72 // we suppress them here.
74 # pragma warning(push)
75 # pragma warning(disable:4100)
78 // Action ReturnArg<k>() returns the k-th argument of the mock function.
79 ACTION_TEMPLATE(ReturnArg
,
80 HAS_1_TEMPLATE_PARAMS(int, k
),
81 AND_0_VALUE_PARAMS()) {
82 return ::std::get
<k
>(args
);
85 // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
86 // mock function to *pointer.
87 ACTION_TEMPLATE(SaveArg
,
88 HAS_1_TEMPLATE_PARAMS(int, k
),
89 AND_1_VALUE_PARAMS(pointer
)) {
90 *pointer
= ::std::get
<k
>(args
);
93 // Action SaveArgPointee<k>(pointer) saves the value pointed to
94 // by the k-th (0-based) argument of the mock function to *pointer.
95 ACTION_TEMPLATE(SaveArgPointee
,
96 HAS_1_TEMPLATE_PARAMS(int, k
),
97 AND_1_VALUE_PARAMS(pointer
)) {
98 *pointer
= *::std::get
<k
>(args
);
101 // Action SetArgReferee<k>(value) assigns 'value' to the variable
102 // referenced by the k-th (0-based) argument of the mock function.
103 ACTION_TEMPLATE(SetArgReferee
,
104 HAS_1_TEMPLATE_PARAMS(int, k
),
105 AND_1_VALUE_PARAMS(value
)) {
106 typedef typename ::std::tuple_element
<k
, args_type
>::type argk_type
;
107 // Ensures that argument #k is a reference. If you get a compiler
108 // error on the next line, you are using SetArgReferee<k>(value) in
109 // a mock function whose k-th (0-based) argument is not a reference.
110 GTEST_COMPILE_ASSERT_(std::is_reference
<argk_type
>::value
,
111 SetArgReferee_must_be_used_with_a_reference_argument
);
112 ::std::get
<k
>(args
) = value
;
115 // Action SetArrayArgument<k>(first, last) copies the elements in
116 // source range [first, last) to the array pointed to by the k-th
117 // (0-based) argument, which can be either a pointer or an
118 // iterator. The action does not take ownership of the elements in the
120 ACTION_TEMPLATE(SetArrayArgument
,
121 HAS_1_TEMPLATE_PARAMS(int, k
),
122 AND_2_VALUE_PARAMS(first
, last
)) {
123 // Visual Studio deprecates ::std::copy, so we use our own copy in that case.
125 internal::CopyElements(first
, last
, ::std::get
<k
>(args
));
127 ::std::copy(first
, last
, ::std::get
<k
>(args
));
131 // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
133 ACTION_TEMPLATE(DeleteArg
,
134 HAS_1_TEMPLATE_PARAMS(int, k
),
135 AND_0_VALUE_PARAMS()) {
136 delete ::std::get
<k
>(args
);
139 // This action returns the value pointed to by 'pointer'.
140 ACTION_P(ReturnPointee
, pointer
) { return *pointer
; }
142 // Action Throw(exception) can be used in a mock function of any type
143 // to throw the given exception. Any copyable value can be thrown.
144 #if GTEST_HAS_EXCEPTIONS
146 // Suppresses the 'unreachable code' warning that VC generates in opt modes.
148 # pragma warning(push) // Saves the current warning state.
149 # pragma warning(disable:4702) // Temporarily disables warning 4702.
151 ACTION_P(Throw
, exception
) { throw exception
; }
153 # pragma warning(pop) // Restores the warning state.
156 #endif // GTEST_HAS_EXCEPTIONS
159 # pragma warning(pop)
162 } // namespace testing
164 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_