shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1 | // Copyright 2007, Google Inc. |
| 2 | // All rights reserved. |
| 3 | // |
| 4 | // Redistribution and use in source and binary forms, with or without |
| 5 | // modification, are permitted provided that the following conditions are |
| 6 | // met: |
| 7 | // |
| 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 |
| 13 | // distribution. |
| 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. |
| 17 | // |
| 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. |
| 29 | // |
| 30 | // Author: wan@google.com (Zhanyong Wan) |
| 31 | |
| 32 | // Google Mock - a framework for writing C++ mock classes. |
| 33 | // |
| 34 | // This file implements some commonly used actions. |
| 35 | |
| 36 | #ifndef GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ |
| 37 | #define GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ |
| 38 | |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 39 | #ifndef _WIN32_WCE |
zhanyong.wan | 658ac0b | 2011-02-24 07:29:13 +0000 | [diff] [blame] | 40 | # include <errno.h> |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 41 | #endif |
| 42 | |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 43 | #include <algorithm> |
| 44 | #include <string> |
| 45 | |
zhanyong.wan | 53e08c4 | 2010-09-14 05:38:21 +0000 | [diff] [blame] | 46 | #include "gmock/internal/gmock-internal-utils.h" |
| 47 | #include "gmock/internal/gmock-port.h" |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 48 | |
| 49 | namespace testing { |
| 50 | |
| 51 | // To implement an action Foo, define: |
| 52 | // 1. a class FooAction that implements the ActionInterface interface, and |
| 53 | // 2. a factory function that creates an Action object from a |
| 54 | // const FooAction*. |
| 55 | // |
| 56 | // The two-level delegation design follows that of Matcher, providing |
| 57 | // consistency for extension developers. It also eases ownership |
| 58 | // management as Action objects can now be copied like plain values. |
| 59 | |
| 60 | namespace internal { |
| 61 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 62 | template <typename F1, typename F2> |
| 63 | class ActionAdaptor; |
| 64 | |
| 65 | // BuiltInDefaultValue<T>::Get() returns the "built-in" default |
| 66 | // value for type T, which is NULL when T is a pointer type, 0 when T |
| 67 | // is a numeric type, false when T is bool, or "" when T is string or |
| 68 | // std::string. For any other type T, this value is undefined and the |
| 69 | // function will abort the process. |
| 70 | template <typename T> |
| 71 | class BuiltInDefaultValue { |
| 72 | public: |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 73 | // This function returns true iff type T has a built-in default value. |
| 74 | static bool Exists() { return false; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 75 | static T Get() { |
| 76 | Assert(false, __FILE__, __LINE__, |
| 77 | "Default action undefined for the function return type."); |
| 78 | return internal::Invalid<T>(); |
| 79 | // The above statement will never be reached, but is required in |
| 80 | // order for this function to compile. |
| 81 | } |
| 82 | }; |
| 83 | |
| 84 | // This partial specialization says that we use the same built-in |
| 85 | // default value for T and const T. |
| 86 | template <typename T> |
| 87 | class BuiltInDefaultValue<const T> { |
| 88 | public: |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 89 | static bool Exists() { return BuiltInDefaultValue<T>::Exists(); } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 90 | static T Get() { return BuiltInDefaultValue<T>::Get(); } |
| 91 | }; |
| 92 | |
| 93 | // This partial specialization defines the default values for pointer |
| 94 | // types. |
| 95 | template <typename T> |
| 96 | class BuiltInDefaultValue<T*> { |
| 97 | public: |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 98 | static bool Exists() { return true; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 99 | static T* Get() { return NULL; } |
| 100 | }; |
| 101 | |
| 102 | // The following specializations define the default values for |
| 103 | // specific types we care about. |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 104 | #define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 105 | template <> \ |
| 106 | class BuiltInDefaultValue<type> { \ |
| 107 | public: \ |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 108 | static bool Exists() { return true; } \ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 109 | static type Get() { return value; } \ |
| 110 | } |
| 111 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 112 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void, ); // NOLINT |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 113 | #if GTEST_HAS_GLOBAL_STRING |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 114 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::string, ""); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 115 | #endif // GTEST_HAS_GLOBAL_STRING |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 116 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::std::string, ""); |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 117 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(bool, false); |
| 118 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0'); |
| 119 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0'); |
| 120 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0'); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 121 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 122 | // There's no need for a default action for signed wchar_t, as that |
| 123 | // type is the same as wchar_t for gcc, and invalid for MSVC. |
| 124 | // |
| 125 | // There's also no need for a default action for unsigned wchar_t, as |
| 126 | // that type is the same as unsigned int for gcc, and invalid for |
| 127 | // MSVC. |
zhanyong.wan | 95b1233 | 2009-09-25 18:55:50 +0000 | [diff] [blame] | 128 | #if GMOCK_WCHAR_T_IS_NATIVE_ |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 129 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U); // NOLINT |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 130 | #endif |
| 131 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 132 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U); // NOLINT |
| 133 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0); // NOLINT |
| 134 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned int, 0U); |
| 135 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed int, 0); |
| 136 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL); // NOLINT |
| 137 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L); // NOLINT |
| 138 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(UInt64, 0); |
| 139 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(Int64, 0); |
| 140 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(float, 0); |
| 141 | GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 142 | |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 143 | #undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_ |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 144 | |
| 145 | } // namespace internal |
| 146 | |
| 147 | // When an unexpected function call is encountered, Google Mock will |
| 148 | // let it return a default value if the user has specified one for its |
| 149 | // return type, or if the return type has a built-in default value; |
| 150 | // otherwise Google Mock won't know what value to return and will have |
| 151 | // to abort the process. |
| 152 | // |
| 153 | // The DefaultValue<T> class allows a user to specify the |
| 154 | // default value for a type T that is both copyable and publicly |
| 155 | // destructible (i.e. anything that can be used as a function return |
| 156 | // type). The usage is: |
| 157 | // |
| 158 | // // Sets the default value for type T to be foo. |
| 159 | // DefaultValue<T>::Set(foo); |
| 160 | template <typename T> |
| 161 | class DefaultValue { |
| 162 | public: |
| 163 | // Sets the default value for type T; requires T to be |
| 164 | // copy-constructable and have a public destructor. |
| 165 | static void Set(T x) { |
kosak | b5c8109 | 2014-01-29 06:41:44 +0000 | [diff] [blame] | 166 | delete producer_; |
| 167 | producer_ = new FixedValueProducer(x); |
| 168 | } |
| 169 | |
| 170 | // Provides a factory function to be called to generate the default value. |
| 171 | // This method can be used even if T is only move-constructible, but it is not |
| 172 | // limited to that case. |
| 173 | typedef T (*FactoryFunction)(); |
| 174 | static void SetFactory(FactoryFunction factory) { |
| 175 | delete producer_; |
| 176 | producer_ = new FactoryValueProducer(factory); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 177 | } |
| 178 | |
| 179 | // Unsets the default value for type T. |
| 180 | static void Clear() { |
kosak | b5c8109 | 2014-01-29 06:41:44 +0000 | [diff] [blame] | 181 | delete producer_; |
| 182 | producer_ = NULL; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | // Returns true iff the user has set the default value for type T. |
kosak | b5c8109 | 2014-01-29 06:41:44 +0000 | [diff] [blame] | 186 | static bool IsSet() { return producer_ != NULL; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 187 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 188 | // Returns true if T has a default return value set by the user or there |
| 189 | // exists a built-in default value. |
| 190 | static bool Exists() { |
| 191 | return IsSet() || internal::BuiltInDefaultValue<T>::Exists(); |
| 192 | } |
| 193 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 194 | // Returns the default value for type T if the user has set one; |
kosak | b5c8109 | 2014-01-29 06:41:44 +0000 | [diff] [blame] | 195 | // otherwise returns the built-in default value. Requires that Exists() |
| 196 | // is true, which ensures that the return value is well-defined. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 197 | static T Get() { |
kosak | b5c8109 | 2014-01-29 06:41:44 +0000 | [diff] [blame] | 198 | return producer_ == NULL ? |
| 199 | internal::BuiltInDefaultValue<T>::Get() : producer_->Produce(); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 200 | } |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 201 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 202 | private: |
kosak | b5c8109 | 2014-01-29 06:41:44 +0000 | [diff] [blame] | 203 | class ValueProducer { |
| 204 | public: |
| 205 | virtual ~ValueProducer() {} |
| 206 | virtual T Produce() = 0; |
| 207 | }; |
| 208 | |
| 209 | class FixedValueProducer : public ValueProducer { |
| 210 | public: |
| 211 | explicit FixedValueProducer(T value) : value_(value) {} |
| 212 | virtual T Produce() { return value_; } |
| 213 | |
| 214 | private: |
| 215 | const T value_; |
| 216 | GTEST_DISALLOW_COPY_AND_ASSIGN_(FixedValueProducer); |
| 217 | }; |
| 218 | |
| 219 | class FactoryValueProducer : public ValueProducer { |
| 220 | public: |
| 221 | explicit FactoryValueProducer(FactoryFunction factory) |
| 222 | : factory_(factory) {} |
| 223 | virtual T Produce() { return factory_(); } |
| 224 | |
| 225 | private: |
| 226 | const FactoryFunction factory_; |
| 227 | GTEST_DISALLOW_COPY_AND_ASSIGN_(FactoryValueProducer); |
| 228 | }; |
| 229 | |
| 230 | static ValueProducer* producer_; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 231 | }; |
| 232 | |
| 233 | // This partial specialization allows a user to set default values for |
| 234 | // reference types. |
| 235 | template <typename T> |
| 236 | class DefaultValue<T&> { |
| 237 | public: |
| 238 | // Sets the default value for type T&. |
| 239 | static void Set(T& x) { // NOLINT |
| 240 | address_ = &x; |
| 241 | } |
| 242 | |
| 243 | // Unsets the default value for type T&. |
| 244 | static void Clear() { |
| 245 | address_ = NULL; |
| 246 | } |
| 247 | |
| 248 | // Returns true iff the user has set the default value for type T&. |
| 249 | static bool IsSet() { return address_ != NULL; } |
| 250 | |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 251 | // Returns true if T has a default return value set by the user or there |
| 252 | // exists a built-in default value. |
| 253 | static bool Exists() { |
| 254 | return IsSet() || internal::BuiltInDefaultValue<T&>::Exists(); |
| 255 | } |
| 256 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 257 | // Returns the default value for type T& if the user has set one; |
| 258 | // otherwise returns the built-in default value if there is one; |
| 259 | // otherwise aborts the process. |
| 260 | static T& Get() { |
| 261 | return address_ == NULL ? |
| 262 | internal::BuiltInDefaultValue<T&>::Get() : *address_; |
| 263 | } |
jgm | 79a367e | 2012-04-10 16:02:11 +0000 | [diff] [blame] | 264 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 265 | private: |
| 266 | static T* address_; |
| 267 | }; |
| 268 | |
| 269 | // This specialization allows DefaultValue<void>::Get() to |
| 270 | // compile. |
| 271 | template <> |
| 272 | class DefaultValue<void> { |
| 273 | public: |
zhanyong.wan | 5b95fa7 | 2009-01-27 22:28:45 +0000 | [diff] [blame] | 274 | static bool Exists() { return true; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 275 | static void Get() {} |
| 276 | }; |
| 277 | |
| 278 | // Points to the user-set default value for type T. |
| 279 | template <typename T> |
kosak | b5c8109 | 2014-01-29 06:41:44 +0000 | [diff] [blame] | 280 | typename DefaultValue<T>::ValueProducer* DefaultValue<T>::producer_ = NULL; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 281 | |
| 282 | // Points to the user-set default value for type T&. |
| 283 | template <typename T> |
| 284 | T* DefaultValue<T&>::address_ = NULL; |
| 285 | |
| 286 | // Implement this interface to define an action for function type F. |
| 287 | template <typename F> |
| 288 | class ActionInterface { |
| 289 | public: |
| 290 | typedef typename internal::Function<F>::Result Result; |
| 291 | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 292 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 293 | ActionInterface() {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 294 | virtual ~ActionInterface() {} |
| 295 | |
| 296 | // Performs the action. This method is not const, as in general an |
| 297 | // action can have side effects and be stateful. For example, a |
| 298 | // get-the-next-element-from-the-collection action will need to |
| 299 | // remember the current element. |
| 300 | virtual Result Perform(const ArgumentTuple& args) = 0; |
| 301 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 302 | private: |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 303 | GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 304 | }; |
| 305 | |
| 306 | // An Action<F> is a copyable and IMMUTABLE (except by assignment) |
| 307 | // object that represents an action to be taken when a mock function |
| 308 | // of type F is called. The implementation of Action<T> is just a |
| 309 | // linked_ptr to const ActionInterface<T>, so copying is fairly cheap. |
| 310 | // Don't inherit from Action! |
| 311 | // |
| 312 | // You can view an object implementing ActionInterface<F> as a |
| 313 | // concrete action (including its current state), and an Action<F> |
| 314 | // object as a handle to it. |
| 315 | template <typename F> |
| 316 | class Action { |
| 317 | public: |
| 318 | typedef typename internal::Function<F>::Result Result; |
| 319 | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 320 | |
| 321 | // Constructs a null Action. Needed for storing Action objects in |
| 322 | // STL containers. |
| 323 | Action() : impl_(NULL) {} |
| 324 | |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 325 | // Constructs an Action from its implementation. A NULL impl is |
| 326 | // used to represent the "do-default" action. |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 327 | explicit Action(ActionInterface<F>* impl) : impl_(impl) {} |
| 328 | |
| 329 | // Copy constructor. |
| 330 | Action(const Action& action) : impl_(action.impl_) {} |
| 331 | |
| 332 | // This constructor allows us to turn an Action<Func> object into an |
| 333 | // Action<F>, as long as F's arguments can be implicitly converted |
vladlosev | a070cbd | 2009-11-18 00:09:28 +0000 | [diff] [blame] | 334 | // to Func's and Func's return type can be implicitly converted to |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 335 | // F's. |
| 336 | template <typename Func> |
| 337 | explicit Action(const Action<Func>& action); |
| 338 | |
| 339 | // Returns true iff this is the DoDefault() action. |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 340 | bool IsDoDefault() const { return impl_.get() == NULL; } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 341 | |
| 342 | // Performs the action. Note that this method is const even though |
| 343 | // the corresponding method in ActionInterface is not. The reason |
| 344 | // is that a const Action<F> means that it cannot be re-bound to |
| 345 | // another concrete action, not that the concrete action it binds to |
| 346 | // cannot change state. (Think of the difference between a const |
| 347 | // pointer and a pointer to const.) |
| 348 | Result Perform(const ArgumentTuple& args) const { |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 349 | internal::Assert( |
| 350 | !IsDoDefault(), __FILE__, __LINE__, |
| 351 | "You are using DoDefault() inside a composite action like " |
| 352 | "DoAll() or WithArgs(). This is not supported for technical " |
| 353 | "reasons. Please instead spell out the default action, or " |
| 354 | "assign the default action to an Action variable and use " |
| 355 | "the variable in various places."); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 356 | return impl_->Perform(args); |
| 357 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 358 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 359 | private: |
| 360 | template <typename F1, typename F2> |
| 361 | friend class internal::ActionAdaptor; |
| 362 | |
| 363 | internal::linked_ptr<ActionInterface<F> > impl_; |
| 364 | }; |
| 365 | |
| 366 | // The PolymorphicAction class template makes it easy to implement a |
| 367 | // polymorphic action (i.e. an action that can be used in mock |
| 368 | // functions of than one type, e.g. Return()). |
| 369 | // |
| 370 | // To define a polymorphic action, a user first provides a COPYABLE |
| 371 | // implementation class that has a Perform() method template: |
| 372 | // |
| 373 | // class FooAction { |
| 374 | // public: |
| 375 | // template <typename Result, typename ArgumentTuple> |
| 376 | // Result Perform(const ArgumentTuple& args) const { |
| 377 | // // Processes the arguments and returns a result, using |
| 378 | // // tr1::get<N>(args) to get the N-th (0-based) argument in the tuple. |
| 379 | // } |
| 380 | // ... |
| 381 | // }; |
| 382 | // |
| 383 | // Then the user creates the polymorphic action using |
| 384 | // MakePolymorphicAction(object) where object has type FooAction. See |
| 385 | // the definition of Return(void) and SetArgumentPointee<N>(value) for |
| 386 | // complete examples. |
| 387 | template <typename Impl> |
| 388 | class PolymorphicAction { |
| 389 | public: |
| 390 | explicit PolymorphicAction(const Impl& impl) : impl_(impl) {} |
| 391 | |
| 392 | template <typename F> |
| 393 | operator Action<F>() const { |
| 394 | return Action<F>(new MonomorphicImpl<F>(impl_)); |
| 395 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 396 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 397 | private: |
| 398 | template <typename F> |
| 399 | class MonomorphicImpl : public ActionInterface<F> { |
| 400 | public: |
| 401 | typedef typename internal::Function<F>::Result Result; |
| 402 | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 403 | |
| 404 | explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {} |
| 405 | |
| 406 | virtual Result Perform(const ArgumentTuple& args) { |
| 407 | return impl_.template Perform<Result>(args); |
| 408 | } |
| 409 | |
| 410 | private: |
| 411 | Impl impl_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 412 | |
| 413 | GTEST_DISALLOW_ASSIGN_(MonomorphicImpl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 414 | }; |
| 415 | |
| 416 | Impl impl_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 417 | |
| 418 | GTEST_DISALLOW_ASSIGN_(PolymorphicAction); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 419 | }; |
| 420 | |
| 421 | // Creates an Action from its implementation and returns it. The |
| 422 | // created Action object owns the implementation. |
| 423 | template <typename F> |
| 424 | Action<F> MakeAction(ActionInterface<F>* impl) { |
| 425 | return Action<F>(impl); |
| 426 | } |
| 427 | |
| 428 | // Creates a polymorphic action from its implementation. This is |
| 429 | // easier to use than the PolymorphicAction<Impl> constructor as it |
| 430 | // doesn't require you to explicitly write the template argument, e.g. |
| 431 | // |
| 432 | // MakePolymorphicAction(foo); |
| 433 | // vs |
| 434 | // PolymorphicAction<TypeOfFoo>(foo); |
| 435 | template <typename Impl> |
| 436 | inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) { |
| 437 | return PolymorphicAction<Impl>(impl); |
| 438 | } |
| 439 | |
| 440 | namespace internal { |
| 441 | |
| 442 | // Allows an Action<F2> object to pose as an Action<F1>, as long as F2 |
| 443 | // and F1 are compatible. |
| 444 | template <typename F1, typename F2> |
| 445 | class ActionAdaptor : public ActionInterface<F1> { |
| 446 | public: |
| 447 | typedef typename internal::Function<F1>::Result Result; |
| 448 | typedef typename internal::Function<F1>::ArgumentTuple ArgumentTuple; |
| 449 | |
| 450 | explicit ActionAdaptor(const Action<F2>& from) : impl_(from.impl_) {} |
| 451 | |
| 452 | virtual Result Perform(const ArgumentTuple& args) { |
| 453 | return impl_->Perform(args); |
| 454 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 455 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 456 | private: |
| 457 | const internal::linked_ptr<ActionInterface<F2> > impl_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 458 | |
| 459 | GTEST_DISALLOW_ASSIGN_(ActionAdaptor); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 460 | }; |
| 461 | |
kosak | 3d1c78b | 2014-11-17 00:56:52 +0000 | [diff] [blame] | 462 | // Helper struct to specialize ReturnAction to execute a move instead of a copy |
| 463 | // on return. Useful for move-only types, but could be used on any type. |
| 464 | template <typename T> |
| 465 | struct ByMoveWrapper { |
| 466 | explicit ByMoveWrapper(T value) : payload(move(value)) {} |
| 467 | T payload; |
| 468 | }; |
| 469 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 470 | // Implements the polymorphic Return(x) action, which can be used in |
| 471 | // any function that returns the type of x, regardless of the argument |
| 472 | // types. |
vladlosev | a070cbd | 2009-11-18 00:09:28 +0000 | [diff] [blame] | 473 | // |
| 474 | // Note: The value passed into Return must be converted into |
| 475 | // Function<F>::Result when this action is cast to Action<F> rather than |
| 476 | // when that action is performed. This is important in scenarios like |
| 477 | // |
| 478 | // MOCK_METHOD1(Method, T(U)); |
| 479 | // ... |
| 480 | // { |
| 481 | // Foo foo; |
| 482 | // X x(&foo); |
| 483 | // EXPECT_CALL(mock, Method(_)).WillOnce(Return(x)); |
| 484 | // } |
| 485 | // |
| 486 | // In the example above the variable x holds reference to foo which leaves |
| 487 | // scope and gets destroyed. If copying X just copies a reference to foo, |
| 488 | // that copy will be left with a hanging reference. If conversion to T |
| 489 | // makes a copy of foo, the above code is safe. To support that scenario, we |
| 490 | // need to make sure that the type conversion happens inside the EXPECT_CALL |
| 491 | // statement, and conversion of the result of Return to Action<T(U)> is a |
| 492 | // good place for that. |
| 493 | // |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 494 | template <typename R> |
| 495 | class ReturnAction { |
| 496 | public: |
| 497 | // Constructs a ReturnAction object from the value to be returned. |
| 498 | // 'value' is passed by value instead of by const reference in order |
| 499 | // to allow Return("string literal") to compile. |
kosak | 3d1c78b | 2014-11-17 00:56:52 +0000 | [diff] [blame] | 500 | explicit ReturnAction(R value) : value_(new R(move(value))) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 501 | |
| 502 | // This template type conversion operator allows Return(x) to be |
| 503 | // used in ANY function that returns x's type. |
| 504 | template <typename F> |
| 505 | operator Action<F>() const { |
| 506 | // Assert statement belongs here because this is the best place to verify |
| 507 | // conditions on F. It produces the clearest error messages |
| 508 | // in most compilers. |
| 509 | // Impl really belongs in this scope as a local class but can't |
| 510 | // because MSVC produces duplicate symbols in different translation units |
| 511 | // in this case. Until MS fixes that bug we put Impl into the class scope |
| 512 | // and put the typedef both here (for use in assert statement) and |
| 513 | // in the Impl class. But both definitions must be the same. |
| 514 | typedef typename Function<F>::Result Result; |
zhanyong.wan | 02f7106 | 2010-05-10 17:14:29 +0000 | [diff] [blame] | 515 | GTEST_COMPILE_ASSERT_( |
kosak | 3d1c78b | 2014-11-17 00:56:52 +0000 | [diff] [blame] | 516 | !is_reference<Result>::value, |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 517 | use_ReturnRef_instead_of_Return_to_return_a_reference); |
kosak | 3d1c78b | 2014-11-17 00:56:52 +0000 | [diff] [blame] | 518 | return Action<F>(new Impl<R, F>(value_)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 519 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 520 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 521 | private: |
| 522 | // Implements the Return(x) action for a particular function type F. |
kosak | 3d1c78b | 2014-11-17 00:56:52 +0000 | [diff] [blame] | 523 | template <typename R_, typename F> |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 524 | class Impl : public ActionInterface<F> { |
| 525 | public: |
| 526 | typedef typename Function<F>::Result Result; |
| 527 | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 528 | |
vladlosev | a070cbd | 2009-11-18 00:09:28 +0000 | [diff] [blame] | 529 | // The implicit cast is necessary when Result has more than one |
| 530 | // single-argument constructor (e.g. Result is std::vector<int>) and R |
| 531 | // has a type conversion operator template. In that case, value_(value) |
| 532 | // won't compile as the compiler doesn't known which constructor of |
zhanyong.wan | 5b61ce3 | 2011-02-01 00:00:03 +0000 | [diff] [blame] | 533 | // Result to call. ImplicitCast_ forces the compiler to convert R to |
vladlosev | a070cbd | 2009-11-18 00:09:28 +0000 | [diff] [blame] | 534 | // Result without considering explicit constructors, thus resolving the |
| 535 | // ambiguity. value_ is then initialized using its copy constructor. |
kosak | 3d1c78b | 2014-11-17 00:56:52 +0000 | [diff] [blame] | 536 | explicit Impl(const linked_ptr<R>& value) |
| 537 | : value_(ImplicitCast_<Result>(*value)) {} |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 538 | |
| 539 | virtual Result Perform(const ArgumentTuple&) { return value_; } |
| 540 | |
| 541 | private: |
kosak | 3d1c78b | 2014-11-17 00:56:52 +0000 | [diff] [blame] | 542 | GTEST_COMPILE_ASSERT_(!is_reference<Result>::value, |
vladlosev | a070cbd | 2009-11-18 00:09:28 +0000 | [diff] [blame] | 543 | Result_cannot_be_a_reference_type); |
| 544 | Result value_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 545 | |
| 546 | GTEST_DISALLOW_ASSIGN_(Impl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 547 | }; |
| 548 | |
kosak | 3d1c78b | 2014-11-17 00:56:52 +0000 | [diff] [blame] | 549 | // Partially specialize for ByMoveWrapper. This version of ReturnAction will |
| 550 | // move its contents instead. |
| 551 | template <typename R_, typename F> |
| 552 | class Impl<ByMoveWrapper<R_>, F> : public ActionInterface<F> { |
| 553 | public: |
| 554 | typedef typename Function<F>::Result Result; |
| 555 | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 556 | |
| 557 | explicit Impl(const linked_ptr<R>& wrapper) |
| 558 | : performed_(false), wrapper_(wrapper) {} |
| 559 | |
| 560 | virtual Result Perform(const ArgumentTuple&) { |
| 561 | GTEST_CHECK_(!performed_) |
| 562 | << "A ByMove() action should only be performed once."; |
| 563 | performed_ = true; |
| 564 | return move(wrapper_->payload); |
| 565 | } |
| 566 | |
| 567 | private: |
| 568 | bool performed_; |
| 569 | const linked_ptr<R> wrapper_; |
| 570 | |
| 571 | GTEST_DISALLOW_ASSIGN_(Impl); |
| 572 | }; |
| 573 | |
| 574 | const linked_ptr<R> value_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 575 | |
| 576 | GTEST_DISALLOW_ASSIGN_(ReturnAction); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 577 | }; |
| 578 | |
| 579 | // Implements the ReturnNull() action. |
| 580 | class ReturnNullAction { |
| 581 | public: |
| 582 | // Allows ReturnNull() to be used in any pointer-returning function. |
| 583 | template <typename Result, typename ArgumentTuple> |
| 584 | static Result Perform(const ArgumentTuple&) { |
zhanyong.wan | 02f7106 | 2010-05-10 17:14:29 +0000 | [diff] [blame] | 585 | GTEST_COMPILE_ASSERT_(internal::is_pointer<Result>::value, |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 586 | ReturnNull_can_be_used_to_return_a_pointer_only); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 587 | return NULL; |
| 588 | } |
| 589 | }; |
| 590 | |
| 591 | // Implements the Return() action. |
| 592 | class ReturnVoidAction { |
| 593 | public: |
| 594 | // Allows Return() to be used in any void-returning function. |
| 595 | template <typename Result, typename ArgumentTuple> |
| 596 | static void Perform(const ArgumentTuple&) { |
| 597 | CompileAssertTypesEqual<void, Result>(); |
| 598 | } |
| 599 | }; |
| 600 | |
| 601 | // Implements the polymorphic ReturnRef(x) action, which can be used |
| 602 | // in any function that returns a reference to the type of x, |
| 603 | // regardless of the argument types. |
| 604 | template <typename T> |
| 605 | class ReturnRefAction { |
| 606 | public: |
| 607 | // Constructs a ReturnRefAction object from the reference to be returned. |
| 608 | explicit ReturnRefAction(T& ref) : ref_(ref) {} // NOLINT |
| 609 | |
| 610 | // This template type conversion operator allows ReturnRef(x) to be |
| 611 | // used in ANY function that returns a reference to x's type. |
| 612 | template <typename F> |
| 613 | operator Action<F>() const { |
| 614 | typedef typename Function<F>::Result Result; |
| 615 | // Asserts that the function return type is a reference. This |
| 616 | // catches the user error of using ReturnRef(x) when Return(x) |
| 617 | // should be used, and generates some helpful error message. |
zhanyong.wan | 02f7106 | 2010-05-10 17:14:29 +0000 | [diff] [blame] | 618 | GTEST_COMPILE_ASSERT_(internal::is_reference<Result>::value, |
zhanyong.wan | e0d051e | 2009-02-19 00:33:37 +0000 | [diff] [blame] | 619 | use_Return_instead_of_ReturnRef_to_return_a_value); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 620 | return Action<F>(new Impl<F>(ref_)); |
| 621 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 622 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 623 | private: |
| 624 | // Implements the ReturnRef(x) action for a particular function type F. |
| 625 | template <typename F> |
| 626 | class Impl : public ActionInterface<F> { |
| 627 | public: |
| 628 | typedef typename Function<F>::Result Result; |
| 629 | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 630 | |
| 631 | explicit Impl(T& ref) : ref_(ref) {} // NOLINT |
| 632 | |
| 633 | virtual Result Perform(const ArgumentTuple&) { |
| 634 | return ref_; |
| 635 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 636 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 637 | private: |
| 638 | T& ref_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 639 | |
| 640 | GTEST_DISALLOW_ASSIGN_(Impl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 641 | }; |
| 642 | |
| 643 | T& ref_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 644 | |
| 645 | GTEST_DISALLOW_ASSIGN_(ReturnRefAction); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 646 | }; |
| 647 | |
zhanyong.wan | e3bd098 | 2010-07-03 00:16:42 +0000 | [diff] [blame] | 648 | // Implements the polymorphic ReturnRefOfCopy(x) action, which can be |
| 649 | // used in any function that returns a reference to the type of x, |
| 650 | // regardless of the argument types. |
| 651 | template <typename T> |
| 652 | class ReturnRefOfCopyAction { |
| 653 | public: |
| 654 | // Constructs a ReturnRefOfCopyAction object from the reference to |
| 655 | // be returned. |
| 656 | explicit ReturnRefOfCopyAction(const T& value) : value_(value) {} // NOLINT |
| 657 | |
| 658 | // This template type conversion operator allows ReturnRefOfCopy(x) to be |
| 659 | // used in ANY function that returns a reference to x's type. |
| 660 | template <typename F> |
| 661 | operator Action<F>() const { |
| 662 | typedef typename Function<F>::Result Result; |
| 663 | // Asserts that the function return type is a reference. This |
| 664 | // catches the user error of using ReturnRefOfCopy(x) when Return(x) |
| 665 | // should be used, and generates some helpful error message. |
| 666 | GTEST_COMPILE_ASSERT_( |
| 667 | internal::is_reference<Result>::value, |
| 668 | use_Return_instead_of_ReturnRefOfCopy_to_return_a_value); |
| 669 | return Action<F>(new Impl<F>(value_)); |
| 670 | } |
| 671 | |
| 672 | private: |
| 673 | // Implements the ReturnRefOfCopy(x) action for a particular function type F. |
| 674 | template <typename F> |
| 675 | class Impl : public ActionInterface<F> { |
| 676 | public: |
| 677 | typedef typename Function<F>::Result Result; |
| 678 | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 679 | |
| 680 | explicit Impl(const T& value) : value_(value) {} // NOLINT |
| 681 | |
| 682 | virtual Result Perform(const ArgumentTuple&) { |
| 683 | return value_; |
| 684 | } |
| 685 | |
| 686 | private: |
| 687 | T value_; |
| 688 | |
| 689 | GTEST_DISALLOW_ASSIGN_(Impl); |
| 690 | }; |
| 691 | |
| 692 | const T value_; |
| 693 | |
| 694 | GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction); |
| 695 | }; |
| 696 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 697 | // Implements the polymorphic DoDefault() action. |
| 698 | class DoDefaultAction { |
| 699 | public: |
| 700 | // This template type conversion operator allows DoDefault() to be |
| 701 | // used in any function. |
| 702 | template <typename F> |
zhanyong.wan | ed6c927 | 2011-02-23 19:39:27 +0000 | [diff] [blame] | 703 | operator Action<F>() const { return Action<F>(NULL); } |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 704 | }; |
| 705 | |
| 706 | // Implements the Assign action to set a given pointer referent to a |
| 707 | // particular value. |
| 708 | template <typename T1, typename T2> |
| 709 | class AssignAction { |
| 710 | public: |
| 711 | AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {} |
| 712 | |
| 713 | template <typename Result, typename ArgumentTuple> |
zhanyong.wan | 3fbd2dd | 2009-03-26 19:06:45 +0000 | [diff] [blame] | 714 | void Perform(const ArgumentTuple& /* args */) const { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 715 | *ptr_ = value_; |
| 716 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 717 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 718 | private: |
| 719 | T1* const ptr_; |
| 720 | const T2 value_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 721 | |
| 722 | GTEST_DISALLOW_ASSIGN_(AssignAction); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 723 | }; |
| 724 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 725 | #if !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 726 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 727 | // Implements the SetErrnoAndReturn action to simulate return from |
| 728 | // various system calls and libc functions. |
| 729 | template <typename T> |
| 730 | class SetErrnoAndReturnAction { |
| 731 | public: |
| 732 | SetErrnoAndReturnAction(int errno_value, T result) |
| 733 | : errno_(errno_value), |
| 734 | result_(result) {} |
| 735 | template <typename Result, typename ArgumentTuple> |
zhanyong.wan | 3fbd2dd | 2009-03-26 19:06:45 +0000 | [diff] [blame] | 736 | Result Perform(const ArgumentTuple& /* args */) const { |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 737 | errno = errno_; |
| 738 | return result_; |
| 739 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 740 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 741 | private: |
| 742 | const int errno_; |
| 743 | const T result_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 744 | |
| 745 | GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 746 | }; |
| 747 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 748 | #endif // !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 749 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 750 | // Implements the SetArgumentPointee<N>(x) action for any function |
| 751 | // whose N-th argument (0-based) is a pointer to x's type. The |
| 752 | // template parameter kIsProto is true iff type A is ProtocolMessage, |
| 753 | // proto2::Message, or a sub-class of those. |
| 754 | template <size_t N, typename A, bool kIsProto> |
| 755 | class SetArgumentPointeeAction { |
| 756 | public: |
| 757 | // Constructs an action that sets the variable pointed to by the |
| 758 | // N-th function argument to 'value'. |
| 759 | explicit SetArgumentPointeeAction(const A& value) : value_(value) {} |
| 760 | |
| 761 | template <typename Result, typename ArgumentTuple> |
| 762 | void Perform(const ArgumentTuple& args) const { |
| 763 | CompileAssertTypesEqual<void, Result>(); |
kosak | bd01883 | 2014-04-02 20:30:00 +0000 | [diff] [blame] | 764 | *::testing::get<N>(args) = value_; |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 765 | } |
| 766 | |
| 767 | private: |
| 768 | const A value_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 769 | |
| 770 | GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 771 | }; |
| 772 | |
| 773 | template <size_t N, typename Proto> |
| 774 | class SetArgumentPointeeAction<N, Proto, true> { |
| 775 | public: |
| 776 | // Constructs an action that sets the variable pointed to by the |
| 777 | // N-th function argument to 'proto'. Both ProtocolMessage and |
| 778 | // proto2::Message have the CopyFrom() method, so the same |
| 779 | // implementation works for both. |
| 780 | explicit SetArgumentPointeeAction(const Proto& proto) : proto_(new Proto) { |
| 781 | proto_->CopyFrom(proto); |
| 782 | } |
| 783 | |
| 784 | template <typename Result, typename ArgumentTuple> |
| 785 | void Perform(const ArgumentTuple& args) const { |
| 786 | CompileAssertTypesEqual<void, Result>(); |
kosak | bd01883 | 2014-04-02 20:30:00 +0000 | [diff] [blame] | 787 | ::testing::get<N>(args)->CopyFrom(*proto_); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 788 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 789 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 790 | private: |
| 791 | const internal::linked_ptr<Proto> proto_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 792 | |
| 793 | GTEST_DISALLOW_ASSIGN_(SetArgumentPointeeAction); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 794 | }; |
| 795 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 796 | // Implements the InvokeWithoutArgs(f) action. The template argument |
| 797 | // FunctionImpl is the implementation type of f, which can be either a |
| 798 | // function pointer or a functor. InvokeWithoutArgs(f) can be used as an |
| 799 | // Action<F> as long as f's type is compatible with F (i.e. f can be |
| 800 | // assigned to a tr1::function<F>). |
| 801 | template <typename FunctionImpl> |
| 802 | class InvokeWithoutArgsAction { |
| 803 | public: |
| 804 | // The c'tor makes a copy of function_impl (either a function |
| 805 | // pointer or a functor). |
| 806 | explicit InvokeWithoutArgsAction(FunctionImpl function_impl) |
| 807 | : function_impl_(function_impl) {} |
| 808 | |
| 809 | // Allows InvokeWithoutArgs(f) to be used as any action whose type is |
| 810 | // compatible with f. |
| 811 | template <typename Result, typename ArgumentTuple> |
| 812 | Result Perform(const ArgumentTuple&) { return function_impl_(); } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 813 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 814 | private: |
| 815 | FunctionImpl function_impl_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 816 | |
| 817 | GTEST_DISALLOW_ASSIGN_(InvokeWithoutArgsAction); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 818 | }; |
| 819 | |
| 820 | // Implements the InvokeWithoutArgs(object_ptr, &Class::Method) action. |
| 821 | template <class Class, typename MethodPtr> |
| 822 | class InvokeMethodWithoutArgsAction { |
| 823 | public: |
| 824 | InvokeMethodWithoutArgsAction(Class* obj_ptr, MethodPtr method_ptr) |
| 825 | : obj_ptr_(obj_ptr), method_ptr_(method_ptr) {} |
| 826 | |
| 827 | template <typename Result, typename ArgumentTuple> |
| 828 | Result Perform(const ArgumentTuple&) const { |
| 829 | return (obj_ptr_->*method_ptr_)(); |
| 830 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 831 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 832 | private: |
| 833 | Class* const obj_ptr_; |
| 834 | const MethodPtr method_ptr_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 835 | |
| 836 | GTEST_DISALLOW_ASSIGN_(InvokeMethodWithoutArgsAction); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 837 | }; |
| 838 | |
| 839 | // Implements the IgnoreResult(action) action. |
| 840 | template <typename A> |
| 841 | class IgnoreResultAction { |
| 842 | public: |
| 843 | explicit IgnoreResultAction(const A& action) : action_(action) {} |
| 844 | |
| 845 | template <typename F> |
| 846 | operator Action<F>() const { |
| 847 | // Assert statement belongs here because this is the best place to verify |
| 848 | // conditions on F. It produces the clearest error messages |
| 849 | // in most compilers. |
| 850 | // Impl really belongs in this scope as a local class but can't |
| 851 | // because MSVC produces duplicate symbols in different translation units |
| 852 | // in this case. Until MS fixes that bug we put Impl into the class scope |
| 853 | // and put the typedef both here (for use in assert statement) and |
| 854 | // in the Impl class. But both definitions must be the same. |
| 855 | typedef typename internal::Function<F>::Result Result; |
| 856 | |
| 857 | // Asserts at compile time that F returns void. |
| 858 | CompileAssertTypesEqual<void, Result>(); |
| 859 | |
| 860 | return Action<F>(new Impl<F>(action_)); |
| 861 | } |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 862 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 863 | private: |
| 864 | template <typename F> |
| 865 | class Impl : public ActionInterface<F> { |
| 866 | public: |
| 867 | typedef typename internal::Function<F>::Result Result; |
| 868 | typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 869 | |
| 870 | explicit Impl(const A& action) : action_(action) {} |
| 871 | |
| 872 | virtual void Perform(const ArgumentTuple& args) { |
| 873 | // Performs the action and ignores its result. |
| 874 | action_.Perform(args); |
| 875 | } |
| 876 | |
| 877 | private: |
| 878 | // Type OriginalFunction is the same as F except that its return |
| 879 | // type is IgnoredValue. |
| 880 | typedef typename internal::Function<F>::MakeResultIgnoredValue |
| 881 | OriginalFunction; |
| 882 | |
| 883 | const Action<OriginalFunction> action_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 884 | |
| 885 | GTEST_DISALLOW_ASSIGN_(Impl); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 886 | }; |
| 887 | |
| 888 | const A action_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 889 | |
| 890 | GTEST_DISALLOW_ASSIGN_(IgnoreResultAction); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 891 | }; |
| 892 | |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 893 | // A ReferenceWrapper<T> object represents a reference to type T, |
| 894 | // which can be either const or not. It can be explicitly converted |
| 895 | // from, and implicitly converted to, a T&. Unlike a reference, |
| 896 | // ReferenceWrapper<T> can be copied and can survive template type |
| 897 | // inference. This is used to support by-reference arguments in the |
| 898 | // InvokeArgument<N>(...) action. The idea was from "reference |
| 899 | // wrappers" in tr1, which we don't have in our source tree yet. |
| 900 | template <typename T> |
| 901 | class ReferenceWrapper { |
| 902 | public: |
| 903 | // Constructs a ReferenceWrapper<T> object from a T&. |
| 904 | explicit ReferenceWrapper(T& l_value) : pointer_(&l_value) {} // NOLINT |
| 905 | |
| 906 | // Allows a ReferenceWrapper<T> object to be implicitly converted to |
| 907 | // a T&. |
| 908 | operator T&() const { return *pointer_; } |
| 909 | private: |
| 910 | T* pointer_; |
| 911 | }; |
| 912 | |
| 913 | // Allows the expression ByRef(x) to be printed as a reference to x. |
| 914 | template <typename T> |
| 915 | void PrintTo(const ReferenceWrapper<T>& ref, ::std::ostream* os) { |
| 916 | T& value = ref; |
| 917 | UniversalPrinter<T&>::Print(value, os); |
| 918 | } |
| 919 | |
| 920 | // Does two actions sequentially. Used for implementing the DoAll(a1, |
| 921 | // a2, ...) action. |
| 922 | template <typename Action1, typename Action2> |
| 923 | class DoBothAction { |
| 924 | public: |
| 925 | DoBothAction(Action1 action1, Action2 action2) |
| 926 | : action1_(action1), action2_(action2) {} |
| 927 | |
| 928 | // This template type conversion operator allows DoAll(a1, ..., a_n) |
| 929 | // to be used in ANY function of compatible type. |
| 930 | template <typename F> |
| 931 | operator Action<F>() const { |
| 932 | return Action<F>(new Impl<F>(action1_, action2_)); |
| 933 | } |
| 934 | |
| 935 | private: |
| 936 | // Implements the DoAll(...) action for a particular function type F. |
| 937 | template <typename F> |
| 938 | class Impl : public ActionInterface<F> { |
| 939 | public: |
| 940 | typedef typename Function<F>::Result Result; |
| 941 | typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 942 | typedef typename Function<F>::MakeResultVoid VoidResult; |
| 943 | |
| 944 | Impl(const Action<VoidResult>& action1, const Action<F>& action2) |
| 945 | : action1_(action1), action2_(action2) {} |
| 946 | |
| 947 | virtual Result Perform(const ArgumentTuple& args) { |
| 948 | action1_.Perform(args); |
| 949 | return action2_.Perform(args); |
| 950 | } |
| 951 | |
| 952 | private: |
| 953 | const Action<VoidResult> action1_; |
| 954 | const Action<F> action2_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 955 | |
| 956 | GTEST_DISALLOW_ASSIGN_(Impl); |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 957 | }; |
| 958 | |
| 959 | Action1 action1_; |
| 960 | Action2 action2_; |
zhanyong.wan | 32de5f5 | 2009-12-23 00:13:23 +0000 | [diff] [blame] | 961 | |
| 962 | GTEST_DISALLOW_ASSIGN_(DoBothAction); |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 963 | }; |
| 964 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 965 | } // namespace internal |
| 966 | |
| 967 | // An Unused object can be implicitly constructed from ANY value. |
| 968 | // This is handy when defining actions that ignore some or all of the |
| 969 | // mock function arguments. For example, given |
| 970 | // |
| 971 | // MOCK_METHOD3(Foo, double(const string& label, double x, double y)); |
| 972 | // MOCK_METHOD3(Bar, double(int index, double x, double y)); |
| 973 | // |
| 974 | // instead of |
| 975 | // |
| 976 | // double DistanceToOriginWithLabel(const string& label, double x, double y) { |
| 977 | // return sqrt(x*x + y*y); |
| 978 | // } |
| 979 | // double DistanceToOriginWithIndex(int index, double x, double y) { |
| 980 | // return sqrt(x*x + y*y); |
| 981 | // } |
| 982 | // ... |
| 983 | // EXEPCT_CALL(mock, Foo("abc", _, _)) |
| 984 | // .WillOnce(Invoke(DistanceToOriginWithLabel)); |
| 985 | // EXEPCT_CALL(mock, Bar(5, _, _)) |
| 986 | // .WillOnce(Invoke(DistanceToOriginWithIndex)); |
| 987 | // |
| 988 | // you could write |
| 989 | // |
| 990 | // // We can declare any uninteresting argument as Unused. |
| 991 | // double DistanceToOrigin(Unused, double x, double y) { |
| 992 | // return sqrt(x*x + y*y); |
| 993 | // } |
| 994 | // ... |
| 995 | // EXEPCT_CALL(mock, Foo("abc", _, _)).WillOnce(Invoke(DistanceToOrigin)); |
| 996 | // EXEPCT_CALL(mock, Bar(5, _, _)).WillOnce(Invoke(DistanceToOrigin)); |
| 997 | typedef internal::IgnoredValue Unused; |
| 998 | |
| 999 | // This constructor allows us to turn an Action<From> object into an |
| 1000 | // Action<To>, as long as To's arguments can be implicitly converted |
| 1001 | // to From's and From's return type cann be implicitly converted to |
| 1002 | // To's. |
| 1003 | template <typename To> |
| 1004 | template <typename From> |
| 1005 | Action<To>::Action(const Action<From>& from) |
| 1006 | : impl_(new internal::ActionAdaptor<To, From>(from)) {} |
| 1007 | |
| 1008 | // Creates an action that returns 'value'. 'value' is passed by value |
| 1009 | // instead of const reference - otherwise Return("string literal") |
| 1010 | // will trigger a compiler error about using array as initializer. |
| 1011 | template <typename R> |
| 1012 | internal::ReturnAction<R> Return(R value) { |
kosak | 3d1c78b | 2014-11-17 00:56:52 +0000 | [diff] [blame] | 1013 | return internal::ReturnAction<R>(internal::move(value)); |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | // Creates an action that returns NULL. |
| 1017 | inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() { |
| 1018 | return MakePolymorphicAction(internal::ReturnNullAction()); |
| 1019 | } |
| 1020 | |
| 1021 | // Creates an action that returns from a void function. |
| 1022 | inline PolymorphicAction<internal::ReturnVoidAction> Return() { |
| 1023 | return MakePolymorphicAction(internal::ReturnVoidAction()); |
| 1024 | } |
| 1025 | |
| 1026 | // Creates an action that returns the reference to a variable. |
| 1027 | template <typename R> |
| 1028 | inline internal::ReturnRefAction<R> ReturnRef(R& x) { // NOLINT |
| 1029 | return internal::ReturnRefAction<R>(x); |
| 1030 | } |
| 1031 | |
zhanyong.wan | e3bd098 | 2010-07-03 00:16:42 +0000 | [diff] [blame] | 1032 | // Creates an action that returns the reference to a copy of the |
| 1033 | // argument. The copy is created when the action is constructed and |
| 1034 | // lives as long as the action. |
| 1035 | template <typename R> |
| 1036 | inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) { |
| 1037 | return internal::ReturnRefOfCopyAction<R>(x); |
| 1038 | } |
| 1039 | |
kosak | 3d1c78b | 2014-11-17 00:56:52 +0000 | [diff] [blame] | 1040 | // Modifies the parent action (a Return() action) to perform a move of the |
| 1041 | // argument instead of a copy. |
| 1042 | // Return(ByMove()) actions can only be executed once and will assert this |
| 1043 | // invariant. |
| 1044 | template <typename R> |
| 1045 | internal::ByMoveWrapper<R> ByMove(R x) { |
| 1046 | return internal::ByMoveWrapper<R>(internal::move(x)); |
| 1047 | } |
| 1048 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1049 | // Creates an action that does the default action for the give mock function. |
| 1050 | inline internal::DoDefaultAction DoDefault() { |
| 1051 | return internal::DoDefaultAction(); |
| 1052 | } |
| 1053 | |
| 1054 | // Creates an action that sets the variable pointed by the N-th |
| 1055 | // (0-based) function argument to 'value'. |
| 1056 | template <size_t N, typename T> |
| 1057 | PolymorphicAction< |
| 1058 | internal::SetArgumentPointeeAction< |
| 1059 | N, T, internal::IsAProtocolMessage<T>::value> > |
zhanyong.wan | 5921483 | 2010-10-05 05:58:51 +0000 | [diff] [blame] | 1060 | SetArgPointee(const T& x) { |
| 1061 | return MakePolymorphicAction(internal::SetArgumentPointeeAction< |
| 1062 | N, T, internal::IsAProtocolMessage<T>::value>(x)); |
| 1063 | } |
zhanyong.wan | fc8c6c4 | 2011-03-09 01:18:08 +0000 | [diff] [blame] | 1064 | |
| 1065 | #if !((GTEST_GCC_VER_ && GTEST_GCC_VER_ < 40000) || GTEST_OS_SYMBIAN) |
zhanyong.wan | a684b5a | 2010-12-02 23:30:50 +0000 | [diff] [blame] | 1066 | // This overload allows SetArgPointee() to accept a string literal. |
zhanyong.wan | fc8c6c4 | 2011-03-09 01:18:08 +0000 | [diff] [blame] | 1067 | // GCC prior to the version 4.0 and Symbian C++ compiler cannot distinguish |
| 1068 | // this overload from the templated version and emit a compile error. |
zhanyong.wan | a684b5a | 2010-12-02 23:30:50 +0000 | [diff] [blame] | 1069 | template <size_t N> |
| 1070 | PolymorphicAction< |
| 1071 | internal::SetArgumentPointeeAction<N, const char*, false> > |
| 1072 | SetArgPointee(const char* p) { |
| 1073 | return MakePolymorphicAction(internal::SetArgumentPointeeAction< |
| 1074 | N, const char*, false>(p)); |
| 1075 | } |
zhanyong.wan | fc8c6c4 | 2011-03-09 01:18:08 +0000 | [diff] [blame] | 1076 | |
| 1077 | template <size_t N> |
| 1078 | PolymorphicAction< |
| 1079 | internal::SetArgumentPointeeAction<N, const wchar_t*, false> > |
| 1080 | SetArgPointee(const wchar_t* p) { |
| 1081 | return MakePolymorphicAction(internal::SetArgumentPointeeAction< |
| 1082 | N, const wchar_t*, false>(p)); |
| 1083 | } |
| 1084 | #endif |
| 1085 | |
zhanyong.wan | 5921483 | 2010-10-05 05:58:51 +0000 | [diff] [blame] | 1086 | // The following version is DEPRECATED. |
| 1087 | template <size_t N, typename T> |
| 1088 | PolymorphicAction< |
| 1089 | internal::SetArgumentPointeeAction< |
| 1090 | N, T, internal::IsAProtocolMessage<T>::value> > |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1091 | SetArgumentPointee(const T& x) { |
| 1092 | return MakePolymorphicAction(internal::SetArgumentPointeeAction< |
| 1093 | N, T, internal::IsAProtocolMessage<T>::value>(x)); |
| 1094 | } |
| 1095 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1096 | // Creates an action that sets a pointer referent to a given value. |
| 1097 | template <typename T1, typename T2> |
| 1098 | PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) { |
| 1099 | return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val)); |
| 1100 | } |
| 1101 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 1102 | #if !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 1103 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1104 | // Creates an action that sets errno and returns the appropriate error. |
| 1105 | template <typename T> |
| 1106 | PolymorphicAction<internal::SetErrnoAndReturnAction<T> > |
| 1107 | SetErrnoAndReturn(int errval, T result) { |
| 1108 | return MakePolymorphicAction( |
| 1109 | internal::SetErrnoAndReturnAction<T>(errval, result)); |
| 1110 | } |
| 1111 | |
zhanyong.wan | f7af24c | 2009-09-24 21:17:24 +0000 | [diff] [blame] | 1112 | #endif // !GTEST_OS_WINDOWS_MOBILE |
zhanyong.wan | 5b5d62f | 2009-03-11 23:37:56 +0000 | [diff] [blame] | 1113 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1114 | // Various overloads for InvokeWithoutArgs(). |
| 1115 | |
| 1116 | // Creates an action that invokes 'function_impl' with no argument. |
| 1117 | template <typename FunctionImpl> |
| 1118 | PolymorphicAction<internal::InvokeWithoutArgsAction<FunctionImpl> > |
| 1119 | InvokeWithoutArgs(FunctionImpl function_impl) { |
| 1120 | return MakePolymorphicAction( |
| 1121 | internal::InvokeWithoutArgsAction<FunctionImpl>(function_impl)); |
| 1122 | } |
| 1123 | |
| 1124 | // Creates an action that invokes the given method on the given object |
| 1125 | // with no argument. |
| 1126 | template <class Class, typename MethodPtr> |
| 1127 | PolymorphicAction<internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> > |
| 1128 | InvokeWithoutArgs(Class* obj_ptr, MethodPtr method_ptr) { |
| 1129 | return MakePolymorphicAction( |
| 1130 | internal::InvokeMethodWithoutArgsAction<Class, MethodPtr>( |
| 1131 | obj_ptr, method_ptr)); |
| 1132 | } |
| 1133 | |
| 1134 | // Creates an action that performs an_action and throws away its |
| 1135 | // result. In other words, it changes the return type of an_action to |
| 1136 | // void. an_action MUST NOT return void, or the code won't compile. |
| 1137 | template <typename A> |
| 1138 | inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) { |
| 1139 | return internal::IgnoreResultAction<A>(an_action); |
| 1140 | } |
| 1141 | |
zhanyong.wan | a18423e | 2009-07-22 23:58:19 +0000 | [diff] [blame] | 1142 | // Creates a reference wrapper for the given L-value. If necessary, |
| 1143 | // you can explicitly specify the type of the reference. For example, |
| 1144 | // suppose 'derived' is an object of type Derived, ByRef(derived) |
| 1145 | // would wrap a Derived&. If you want to wrap a const Base& instead, |
| 1146 | // where Base is a base class of Derived, just write: |
| 1147 | // |
| 1148 | // ByRef<const Base>(derived) |
| 1149 | template <typename T> |
| 1150 | inline internal::ReferenceWrapper<T> ByRef(T& l_value) { // NOLINT |
| 1151 | return internal::ReferenceWrapper<T>(l_value); |
| 1152 | } |
| 1153 | |
shiqian | e35fdd9 | 2008-12-10 05:08:54 +0000 | [diff] [blame] | 1154 | } // namespace testing |
| 1155 | |
| 1156 | #endif // GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ |