00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
00037 #define GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_
00038
00039 #include <algorithm>
00040
00041 #include "gmock/gmock-generated-actions.h"
00042
00043 namespace testing {
00044 namespace internal {
00045
00046
00047
00048
00049
00050
00051 template <typename FunctionImpl>
00052 class InvokeAction {
00053 public:
00054
00055
00056 explicit InvokeAction(FunctionImpl function_impl)
00057 : function_impl_(function_impl) {}
00058
00059 template <typename Result, typename ArgumentTuple>
00060 Result Perform(const ArgumentTuple& args) {
00061 return InvokeHelper<Result, ArgumentTuple>::Invoke(function_impl_, args);
00062 }
00063
00064 private:
00065 FunctionImpl function_impl_;
00066
00067 GTEST_DISALLOW_ASSIGN_(InvokeAction);
00068 };
00069
00070
00071 template <class Class, typename MethodPtr>
00072 class InvokeMethodAction {
00073 public:
00074 InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
00075 : method_ptr_(method_ptr), obj_ptr_(obj_ptr) {}
00076
00077 template <typename Result, typename ArgumentTuple>
00078 Result Perform(const ArgumentTuple& args) const {
00079 return InvokeHelper<Result, ArgumentTuple>::InvokeMethod(
00080 obj_ptr_, method_ptr_, args);
00081 }
00082
00083 private:
00084
00085
00086
00087 const MethodPtr method_ptr_;
00088 Class* const obj_ptr_;
00089
00090 GTEST_DISALLOW_ASSIGN_(InvokeMethodAction);
00091 };
00092
00093
00094
00095
00096
00097 template<typename InputIterator, typename OutputIterator>
00098 inline OutputIterator CopyElements(InputIterator first,
00099 InputIterator last,
00100 OutputIterator output) {
00101 for (; first != last; ++first, ++output) {
00102 *output = *first;
00103 }
00104 return output;
00105 }
00106
00107 }
00108
00109
00110
00111
00112
00113 template <typename FunctionImpl>
00114 PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
00115 FunctionImpl function_impl) {
00116 return MakePolymorphicAction(
00117 internal::InvokeAction<FunctionImpl>(function_impl));
00118 }
00119
00120
00121
00122 template <class Class, typename MethodPtr>
00123 PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke(
00124 Class* obj_ptr, MethodPtr method_ptr) {
00125 return MakePolymorphicAction(
00126 internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr));
00127 }
00128
00129
00130
00131
00132
00133 template <typename InnerAction>
00134 inline internal::WithArgsAction<InnerAction>
00135 WithoutArgs(const InnerAction& action) {
00136 return internal::WithArgsAction<InnerAction>(action);
00137 }
00138
00139
00140
00141
00142
00143
00144 template <int k, typename InnerAction>
00145 inline internal::WithArgsAction<InnerAction, k>
00146 WithArg(const InnerAction& action) {
00147 return internal::WithArgsAction<InnerAction, k>(action);
00148 }
00149
00150
00151
00152
00153
00154
00155 #ifdef _MSC_VER
00156 # pragma warning(push)
00157 # pragma warning(disable:4100)
00158 #endif
00159
00160
00161 ACTION_TEMPLATE(ReturnArg,
00162 HAS_1_TEMPLATE_PARAMS(int, k),
00163 AND_0_VALUE_PARAMS()) {
00164 return ::testing::get<k>(args);
00165 }
00166
00167
00168
00169 ACTION_TEMPLATE(SaveArg,
00170 HAS_1_TEMPLATE_PARAMS(int, k),
00171 AND_1_VALUE_PARAMS(pointer)) {
00172 *pointer = ::testing::get<k>(args);
00173 }
00174
00175
00176
00177 ACTION_TEMPLATE(SaveArgPointee,
00178 HAS_1_TEMPLATE_PARAMS(int, k),
00179 AND_1_VALUE_PARAMS(pointer)) {
00180 *pointer = *::testing::get<k>(args);
00181 }
00182
00183
00184
00185 ACTION_TEMPLATE(SetArgReferee,
00186 HAS_1_TEMPLATE_PARAMS(int, k),
00187 AND_1_VALUE_PARAMS(value)) {
00188 typedef typename ::testing::tuple_element<k, args_type>::type argk_type;
00189
00190
00191
00192 GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
00193 SetArgReferee_must_be_used_with_a_reference_argument);
00194 ::testing::get<k>(args) = value;
00195 }
00196
00197
00198
00199
00200
00201
00202 ACTION_TEMPLATE(SetArrayArgument,
00203 HAS_1_TEMPLATE_PARAMS(int, k),
00204 AND_2_VALUE_PARAMS(first, last)) {
00205
00206 #ifdef _MSC_VER
00207 internal::CopyElements(first, last, ::testing::get<k>(args));
00208 #else
00209 ::std::copy(first, last, ::testing::get<k>(args));
00210 #endif
00211 }
00212
00213
00214
00215 ACTION_TEMPLATE(DeleteArg,
00216 HAS_1_TEMPLATE_PARAMS(int, k),
00217 AND_0_VALUE_PARAMS()) {
00218 delete ::testing::get<k>(args);
00219 }
00220
00221
00222 ACTION_P(ReturnPointee, pointer) { return *pointer; }
00223
00224
00225
00226 #if GTEST_HAS_EXCEPTIONS
00227
00228
00229 # ifdef _MSC_VER
00230 # pragma warning(push) // Saves the current warning state.
00231 # pragma warning(disable:4702) // Temporarily disables warning 4702.
00232 # endif
00233 ACTION_P(Throw, exception) { throw exception; }
00234 # ifdef _MSC_VER
00235 # pragma warning(pop) // Restores the warning state.
00236 # endif
00237
00238 #endif // GTEST_HAS_EXCEPTIONS
00239
00240 #ifdef _MSC_VER
00241 # pragma warning(pop)
00242 #endif
00243
00244 }
00245
00246 #endif // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_