gmock-more-actions.h
Go to the documentation of this file.
00001 // Copyright 2007, Google Inc.
00002 // All rights reserved.
00003 //
00004 // Redistribution and use in source and binary forms, with or without
00005 // modification, are permitted provided that the following conditions are
00006 // met:
00007 //
00008 //     * Redistributions of source code must retain the above copyright
00009 // notice, this list of conditions and the following disclaimer.
00010 //     * Redistributions in binary form must reproduce the above
00011 // copyright notice, this list of conditions and the following disclaimer
00012 // in the documentation and/or other materials provided with the
00013 // distribution.
00014 //     * Neither the name of Google Inc. nor the names of its
00015 // contributors may be used to endorse or promote products derived from
00016 // this software without specific prior written permission.
00017 //
00018 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
00019 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
00020 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
00021 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
00022 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
00023 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
00024 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
00025 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
00026 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00027 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 //
00030 // Author: wan@google.com (Zhanyong Wan)
00031 
00032 // Google Mock - a framework for writing C++ mock classes.
00033 //
00034 // This file implements some actions that depend on gmock-generated-actions.h.
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 // Implements the Invoke(f) action.  The template argument
00047 // FunctionImpl is the implementation type of f, which can be either a
00048 // function pointer or a functor.  Invoke(f) can be used as an
00049 // Action<F> as long as f's type is compatible with F (i.e. f can be
00050 // assigned to a tr1::function<F>).
00051 template <typename FunctionImpl>
00052 class InvokeAction {
00053  public:
00054   // The c'tor makes a copy of function_impl (either a function
00055   // pointer or a functor).
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 // Implements the Invoke(object_ptr, &Class::Method) action.
00071 template <class Class, typename MethodPtr>
00072 class InvokeMethodAction {
00073  public:
00074   InvokeMethodAction(Class* obj_ptr, MethodPtr method_ptr)
00075       : obj_ptr_(obj_ptr), method_ptr_(method_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   Class* const obj_ptr_;
00085   const MethodPtr method_ptr_;
00086 
00087   GTEST_DISALLOW_ASSIGN_(InvokeMethodAction);
00088 };
00089 
00090 }  // namespace internal
00091 
00092 // Various overloads for Invoke().
00093 
00094 // Creates an action that invokes 'function_impl' with the mock
00095 // function's arguments.
00096 template <typename FunctionImpl>
00097 PolymorphicAction<internal::InvokeAction<FunctionImpl> > Invoke(
00098     FunctionImpl function_impl) {
00099   return MakePolymorphicAction(
00100       internal::InvokeAction<FunctionImpl>(function_impl));
00101 }
00102 
00103 // Creates an action that invokes the given method on the given object
00104 // with the mock function's arguments.
00105 template <class Class, typename MethodPtr>
00106 PolymorphicAction<internal::InvokeMethodAction<Class, MethodPtr> > Invoke(
00107     Class* obj_ptr, MethodPtr method_ptr) {
00108   return MakePolymorphicAction(
00109       internal::InvokeMethodAction<Class, MethodPtr>(obj_ptr, method_ptr));
00110 }
00111 
00112 // WithoutArgs(inner_action) can be used in a mock function with a
00113 // non-empty argument list to perform inner_action, which takes no
00114 // argument.  In other words, it adapts an action accepting no
00115 // argument to one that accepts (and ignores) arguments.
00116 template <typename InnerAction>
00117 inline internal::WithArgsAction<InnerAction>
00118 WithoutArgs(const InnerAction& action) {
00119   return internal::WithArgsAction<InnerAction>(action);
00120 }
00121 
00122 // WithArg<k>(an_action) creates an action that passes the k-th
00123 // (0-based) argument of the mock function to an_action and performs
00124 // it.  It adapts an action accepting one argument to one that accepts
00125 // multiple arguments.  For convenience, we also provide
00126 // WithArgs<k>(an_action) (defined below) as a synonym.
00127 template <int k, typename InnerAction>
00128 inline internal::WithArgsAction<InnerAction, k>
00129 WithArg(const InnerAction& action) {
00130   return internal::WithArgsAction<InnerAction, k>(action);
00131 }
00132 
00133 // The ACTION*() macros trigger warning C4100 (unreferenced formal
00134 // parameter) in MSVC with -W4.  Unfortunately they cannot be fixed in
00135 // the macro definition, as the warnings are generated when the macro
00136 // is expanded and macro expansion cannot contain #pragma.  Therefore
00137 // we suppress them here.
00138 #ifdef _MSC_VER
00139 # pragma warning(push)
00140 # pragma warning(disable:4100)
00141 #endif
00142 
00143 // Action ReturnArg<k>() returns the k-th argument of the mock function.
00144 ACTION_TEMPLATE(ReturnArg,
00145                 HAS_1_TEMPLATE_PARAMS(int, k),
00146                 AND_0_VALUE_PARAMS()) {
00147   return std::tr1::get<k>(args);
00148 }
00149 
00150 // Action SaveArg<k>(pointer) saves the k-th (0-based) argument of the
00151 // mock function to *pointer.
00152 ACTION_TEMPLATE(SaveArg,
00153                 HAS_1_TEMPLATE_PARAMS(int, k),
00154                 AND_1_VALUE_PARAMS(pointer)) {
00155   *pointer = ::std::tr1::get<k>(args);
00156 }
00157 
00158 // Action SaveArgPointee<k>(pointer) saves the value pointed to
00159 // by the k-th (0-based) argument of the mock function to *pointer.
00160 ACTION_TEMPLATE(SaveArgPointee,
00161                 HAS_1_TEMPLATE_PARAMS(int, k),
00162                 AND_1_VALUE_PARAMS(pointer)) {
00163   *pointer = *::std::tr1::get<k>(args);
00164 }
00165 
00166 // Action SetArgReferee<k>(value) assigns 'value' to the variable
00167 // referenced by the k-th (0-based) argument of the mock function.
00168 ACTION_TEMPLATE(SetArgReferee,
00169                 HAS_1_TEMPLATE_PARAMS(int, k),
00170                 AND_1_VALUE_PARAMS(value)) {
00171   typedef typename ::std::tr1::tuple_element<k, args_type>::type argk_type;
00172   // Ensures that argument #k is a reference.  If you get a compiler
00173   // error on the next line, you are using SetArgReferee<k>(value) in
00174   // a mock function whose k-th (0-based) argument is not a reference.
00175   GTEST_COMPILE_ASSERT_(internal::is_reference<argk_type>::value,
00176                         SetArgReferee_must_be_used_with_a_reference_argument);
00177   ::std::tr1::get<k>(args) = value;
00178 }
00179 
00180 // Action SetArrayArgument<k>(first, last) copies the elements in
00181 // source range [first, last) to the array pointed to by the k-th
00182 // (0-based) argument, which can be either a pointer or an
00183 // iterator. The action does not take ownership of the elements in the
00184 // source range.
00185 ACTION_TEMPLATE(SetArrayArgument,
00186                 HAS_1_TEMPLATE_PARAMS(int, k),
00187                 AND_2_VALUE_PARAMS(first, last)) {
00188   // Microsoft compiler deprecates ::std::copy, so we want to suppress warning
00189   // 4996 (Function call with parameters that may be unsafe) there.
00190 #ifdef _MSC_VER
00191 # pragma warning(push)          // Saves the current warning state.
00192 # pragma warning(disable:4996)  // Temporarily disables warning 4996.
00193 #endif
00194   ::std::copy(first, last, ::std::tr1::get<k>(args));
00195 #ifdef _MSC_VER
00196 # pragma warning(pop)           // Restores the warning state.
00197 #endif
00198 }
00199 
00200 // Action DeleteArg<k>() deletes the k-th (0-based) argument of the mock
00201 // function.
00202 ACTION_TEMPLATE(DeleteArg,
00203                 HAS_1_TEMPLATE_PARAMS(int, k),
00204                 AND_0_VALUE_PARAMS()) {
00205   delete ::std::tr1::get<k>(args);
00206 }
00207 
00208 // This action returns the value pointed to by 'pointer'.
00209 ACTION_P(ReturnPointee, pointer) { return *pointer; }
00210 
00211 // Action Throw(exception) can be used in a mock function of any type
00212 // to throw the given exception.  Any copyable value can be thrown.
00213 #if GTEST_HAS_EXCEPTIONS
00214 
00215 // Suppresses the 'unreachable code' warning that VC generates in opt modes.
00216 # ifdef _MSC_VER
00217 #  pragma warning(push)          // Saves the current warning state.
00218 #  pragma warning(disable:4702)  // Temporarily disables warning 4702.
00219 # endif
00220 ACTION_P(Throw, exception) { throw exception; }
00221 # ifdef _MSC_VER
00222 #  pragma warning(pop)           // Restores the warning state.
00223 # endif
00224 
00225 #endif  // GTEST_HAS_EXCEPTIONS
00226 
00227 #ifdef _MSC_VER
00228 # pragma warning(pop)
00229 #endif
00230 
00231 }  // namespace testing
00232 
00233 #endif  // GMOCK_INCLUDE_GMOCK_GMOCK_MORE_ACTIONS_H_


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Sat Jun 8 2019 18:24:42