gmock-actions.h 42 KB

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