| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
#ifndef GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ |
| 38 |
#define GMOCK_INCLUDE_GMOCK_GMOCK_ACTIONS_H_ |
| 39 |
|
| 40 |
#ifndef _WIN32_WCE |
| 41 |
# include <errno.h> |
| 42 |
#endif |
| 43 |
|
| 44 |
#include <algorithm> |
| 45 |
#include <functional> |
| 46 |
#include <memory> |
| 47 |
#include <string> |
| 48 |
#include <type_traits> |
| 49 |
#include <utility> |
| 50 |
|
| 51 |
#include "gmock/internal/gmock-internal-utils.h" |
| 52 |
#include "gmock/internal/gmock-port.h" |
| 53 |
|
| 54 |
#ifdef _MSC_VER |
| 55 |
# pragma warning(push) |
| 56 |
# pragma warning(disable:4100) |
| 57 |
#endif |
| 58 |
|
| 59 |
namespace testing { |
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
namespace internal { |
| 71 |
|
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
template <typename T, bool kDefaultConstructible> |
| 78 |
struct BuiltInDefaultValueGetter { |
| 79 |
static T Get() { return T(); } |
| 80 |
}; |
| 81 |
template <typename T> |
| 82 |
struct BuiltInDefaultValueGetter<T, false> { |
| 83 |
static T Get() { |
| 84 |
Assert(false, __FILE__, __LINE__, |
| 85 |
"Default action undefined for the function return type."); |
| 86 |
return internal::Invalid<T>(); |
| 87 |
|
| 88 |
|
| 89 |
} |
| 90 |
}; |
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
template <typename T> |
| 100 |
class BuiltInDefaultValue { |
| 101 |
public: |
| 102 |
|
| 103 |
|
| 104 |
static bool Exists() { |
| 105 |
return ::std::is_default_constructible<T>::value; |
| 106 |
} |
| 107 |
|
| 108 |
static T Get() { |
| 109 |
return BuiltInDefaultValueGetter< |
| 110 |
T, ::std::is_default_constructible<T>::value>::Get(); |
| 111 |
} |
| 112 |
}; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
template <typename T> |
| 117 |
class BuiltInDefaultValue<const T> { |
| 118 |
public: |
| 119 |
static bool Exists() { return BuiltInDefaultValue<T>::Exists(); } |
| 120 |
static T Get() { return BuiltInDefaultValue<T>::Get(); } |
| 121 |
}; |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
template <typename T> |
| 126 |
class BuiltInDefaultValue<T*> { |
| 127 |
public: |
| 128 |
static bool Exists() { return true; } |
| 129 |
static T* Get() { return nullptr; } |
| 130 |
}; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
#define GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(type, value) \ |
| 135 |
template <> \ |
| 136 |
class BuiltInDefaultValue<type> { \ |
| 137 |
public: \ |
| 138 |
static bool Exists() { return true; } \ |
| 139 |
static type Get() { return value; } \ |
| 140 |
} |
| 141 |
|
| 142 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(void, ); |
| 143 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(::std::string, ""); |
| 144 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(bool, false); |
| 145 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned char, '\0'); |
| 146 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed char, '\0'); |
| 147 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(char, '\0'); |
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
#if GMOCK_WCHAR_T_IS_NATIVE_ |
| 156 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(wchar_t, 0U); |
| 157 |
#endif |
| 158 |
|
| 159 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned short, 0U); |
| 160 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed short, 0); |
| 161 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned int, 0U); |
| 162 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed int, 0); |
| 163 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(unsigned long, 0UL); |
| 164 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(signed long, 0L); |
| 165 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(UInt64, 0); |
| 166 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(Int64, 0); |
| 167 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(float, 0); |
| 168 |
GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_(double, 0); |
| 169 |
|
| 170 |
#undef GMOCK_DEFINE_DEFAULT_ACTION_FOR_RETURN_TYPE_ |
| 171 |
|
| 172 |
} |
| 173 |
|
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
|
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
template <typename T> |
| 188 |
class DefaultValue { |
| 189 |
public: |
| 190 |
|
| 191 |
|
| 192 |
static void Set(T x) { |
| 193 |
delete producer_; |
| 194 |
producer_ = new FixedValueProducer(x); |
| 195 |
} |
| 196 |
|
| 197 |
|
| 198 |
|
| 199 |
|
| 200 |
typedef T (*FactoryFunction)(); |
| 201 |
static void SetFactory(FactoryFunction factory) { |
| 202 |
delete producer_; |
| 203 |
producer_ = new FactoryValueProducer(factory); |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
static void Clear() { |
| 208 |
delete producer_; |
| 209 |
producer_ = nullptr; |
| 210 |
} |
| 211 |
|
| 212 |
|
| 213 |
static bool IsSet() { return producer_ != nullptr; } |
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
static bool Exists() { |
| 218 |
return IsSet() || internal::BuiltInDefaultValue<T>::Exists(); |
| 219 |
} |
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
static T Get() { |
| 225 |
return producer_ == nullptr ? internal::BuiltInDefaultValue<T>::Get() |
| 226 |
: producer_->Produce(); |
| 227 |
} |
| 228 |
|
| 229 |
private: |
| 230 |
class ValueProducer { |
| 231 |
public: |
| 232 |
virtual ~ValueProducer() {} |
| 233 |
virtual T Produce() = 0; |
| 234 |
}; |
| 235 |
|
| 236 |
class FixedValueProducer : public ValueProducer { |
| 237 |
public: |
| 238 |
explicit FixedValueProducer(T value) : value_(value) {} |
| 239 |
T Produce() override { return value_; } |
| 240 |
|
| 241 |
private: |
| 242 |
const T value_; |
| 243 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(FixedValueProducer); |
| 244 |
}; |
| 245 |
|
| 246 |
class FactoryValueProducer : public ValueProducer { |
| 247 |
public: |
| 248 |
explicit FactoryValueProducer(FactoryFunction factory) |
| 249 |
: factory_(factory) {} |
| 250 |
T Produce() override { return factory_(); } |
| 251 |
|
| 252 |
private: |
| 253 |
const FactoryFunction factory_; |
| 254 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(FactoryValueProducer); |
| 255 |
}; |
| 256 |
|
| 257 |
static ValueProducer* producer_; |
| 258 |
}; |
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
template <typename T> |
| 263 |
class DefaultValue<T&> { |
| 264 |
public: |
| 265 |
|
| 266 |
static void Set(T& x) { |
| 267 |
address_ = &x; |
| 268 |
} |
| 269 |
|
| 270 |
|
| 271 |
static void Clear() { address_ = nullptr; } |
| 272 |
|
| 273 |
|
| 274 |
static bool IsSet() { return address_ != nullptr; } |
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
static bool Exists() { |
| 279 |
return IsSet() || internal::BuiltInDefaultValue<T&>::Exists(); |
| 280 |
} |
| 281 |
|
| 282 |
|
| 283 |
|
| 284 |
|
| 285 |
static T& Get() { |
| 286 |
return address_ == nullptr ? internal::BuiltInDefaultValue<T&>::Get() |
| 287 |
: *address_; |
| 288 |
} |
| 289 |
|
| 290 |
private: |
| 291 |
static T* address_; |
| 292 |
}; |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
template <> |
| 297 |
class DefaultValue<void> { |
| 298 |
public: |
| 299 |
static bool Exists() { return true; } |
| 300 |
static void Get() {} |
| 301 |
}; |
| 302 |
|
| 303 |
|
| 304 |
template <typename T> |
| 305 |
typename DefaultValue<T>::ValueProducer* DefaultValue<T>::producer_ = nullptr; |
| 306 |
|
| 307 |
|
| 308 |
template <typename T> |
| 309 |
T* DefaultValue<T&>::address_ = nullptr; |
| 310 |
|
| 311 |
|
| 312 |
template <typename F> |
| 313 |
class ActionInterface { |
| 314 |
public: |
| 315 |
typedef typename internal::Function<F>::Result Result; |
| 316 |
typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 317 |
|
| 318 |
ActionInterface() {} |
| 319 |
virtual ~ActionInterface() {} |
| 320 |
|
| 321 |
|
| 322 |
|
| 323 |
|
| 324 |
|
| 325 |
virtual Result Perform(const ArgumentTuple& args) = 0; |
| 326 |
|
| 327 |
private: |
| 328 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(ActionInterface); |
| 329 |
}; |
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
template <typename F> |
| 339 |
class Action { |
| 340 |
|
| 341 |
|
| 342 |
struct ActionAdapter { |
| 343 |
|
| 344 |
::std::shared_ptr<ActionInterface<F>> impl_; |
| 345 |
|
| 346 |
template <typename... Args> |
| 347 |
typename internal::Function<F>::Result operator()(Args&&... args) { |
| 348 |
return impl_->Perform( |
| 349 |
::std::forward_as_tuple(::std::forward<Args>(args)...)); |
| 350 |
} |
| 351 |
}; |
| 352 |
|
| 353 |
public: |
| 354 |
typedef typename internal::Function<F>::Result Result; |
| 355 |
typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 356 |
|
| 357 |
|
| 358 |
|
| 359 |
Action() {} |
| 360 |
|
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
template <typename G, |
| 365 |
typename = typename ::std::enable_if< |
| 366 |
::std::is_constructible<::std::function<F>, G>::value>::type> |
| 367 |
Action(G&& fun) : fun_(::std::forward<G>(fun)) {} |
| 368 |
|
| 369 |
|
| 370 |
explicit Action(ActionInterface<F>* impl) |
| 371 |
: fun_(ActionAdapter{::std::shared_ptr<ActionInterface<F>>(impl)}) {} |
| 372 |
|
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
template <typename Func> |
| 377 |
explicit Action(const Action<Func>& action) : fun_(action.fun_) {} |
| 378 |
|
| 379 |
|
| 380 |
bool IsDoDefault() const { return fun_ == nullptr; } |
| 381 |
|
| 382 |
|
| 383 |
|
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
Result Perform(ArgumentTuple args) const { |
| 389 |
if (IsDoDefault()) { |
| 390 |
internal::IllegalDoDefault(__FILE__, __LINE__); |
| 391 |
} |
| 392 |
return internal::Apply(fun_, ::std::move(args)); |
| 393 |
} |
| 394 |
|
| 395 |
private: |
| 396 |
template <typename G> |
| 397 |
friend class Action; |
| 398 |
|
| 399 |
|
| 400 |
::std::function<F> fun_; |
| 401 |
}; |
| 402 |
|
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
|
| 409 |
|
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
template <typename Impl> |
| 425 |
class PolymorphicAction { |
| 426 |
public: |
| 427 |
explicit PolymorphicAction(const Impl& impl) : impl_(impl) {} |
| 428 |
|
| 429 |
template <typename F> |
| 430 |
operator Action<F>() const { |
| 431 |
return Action<F>(new MonomorphicImpl<F>(impl_)); |
| 432 |
} |
| 433 |
|
| 434 |
private: |
| 435 |
template <typename F> |
| 436 |
class MonomorphicImpl : public ActionInterface<F> { |
| 437 |
public: |
| 438 |
typedef typename internal::Function<F>::Result Result; |
| 439 |
typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 440 |
|
| 441 |
explicit MonomorphicImpl(const Impl& impl) : impl_(impl) {} |
| 442 |
|
| 443 |
Result Perform(const ArgumentTuple& args) override { |
| 444 |
return impl_.template Perform<Result>(args); |
| 445 |
} |
| 446 |
|
| 447 |
private: |
| 448 |
Impl impl_; |
| 449 |
|
| 450 |
GTEST_DISALLOW_ASSIGN_(MonomorphicImpl); |
| 451 |
}; |
| 452 |
|
| 453 |
Impl impl_; |
| 454 |
|
| 455 |
GTEST_DISALLOW_ASSIGN_(PolymorphicAction); |
| 456 |
}; |
| 457 |
|
| 458 |
|
| 459 |
|
| 460 |
template <typename F> |
| 461 |
Action<F> MakeAction(ActionInterface<F>* impl) { |
| 462 |
return Action<F>(impl); |
| 463 |
} |
| 464 |
|
| 465 |
|
| 466 |
|
| 467 |
|
| 468 |
|
| 469 |
|
| 470 |
|
| 471 |
|
| 472 |
template <typename Impl> |
| 473 |
inline PolymorphicAction<Impl> MakePolymorphicAction(const Impl& impl) { |
| 474 |
return PolymorphicAction<Impl>(impl); |
| 475 |
} |
| 476 |
|
| 477 |
namespace internal { |
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
template <typename T> |
| 482 |
struct ByMoveWrapper { |
| 483 |
explicit ByMoveWrapper(T value) : payload(std::move(value)) {} |
| 484 |
T payload; |
| 485 |
}; |
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
|
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
|
| 507 |
|
| 508 |
|
| 509 |
|
| 510 |
|
| 511 |
|
| 512 |
|
| 513 |
|
| 514 |
template <typename R> |
| 515 |
class ReturnAction { |
| 516 |
public: |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
explicit ReturnAction(R value) : value_(new R(std::move(value))) {} |
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
template <typename F> |
| 525 |
operator Action<F>() const { |
| 526 |
|
| 527 |
|
| 528 |
|
| 529 |
|
| 530 |
|
| 531 |
|
| 532 |
|
| 533 |
|
| 534 |
typedef typename Function<F>::Result Result; |
| 535 |
GTEST_COMPILE_ASSERT_( |
| 536 |
!std::is_reference<Result>::value, |
| 537 |
use_ReturnRef_instead_of_Return_to_return_a_reference); |
| 538 |
static_assert(!std::is_void<Result>::value, |
| 539 |
"Can't use Return() on an action expected to return `void`."); |
| 540 |
return Action<F>(new Impl<R, F>(value_)); |
| 541 |
} |
| 542 |
|
| 543 |
private: |
| 544 |
|
| 545 |
template <typename R_, typename F> |
| 546 |
class Impl : public ActionInterface<F> { |
| 547 |
public: |
| 548 |
typedef typename Function<F>::Result Result; |
| 549 |
typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
explicit Impl(const std::shared_ptr<R>& value) |
| 559 |
: value_before_cast_(*value), |
| 560 |
value_(ImplicitCast_<Result>(value_before_cast_)) {} |
| 561 |
|
| 562 |
Result Perform(const ArgumentTuple&) override { return value_; } |
| 563 |
|
| 564 |
private: |
| 565 |
GTEST_COMPILE_ASSERT_(!std::is_reference<Result>::value, |
| 566 |
Result_cannot_be_a_reference_type); |
| 567 |
|
| 568 |
|
| 569 |
R value_before_cast_; |
| 570 |
Result value_; |
| 571 |
|
| 572 |
GTEST_DISALLOW_COPY_AND_ASSIGN_(Impl); |
| 573 |
}; |
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
template <typename R_, typename F> |
| 578 |
class Impl<ByMoveWrapper<R_>, F> : public ActionInterface<F> { |
| 579 |
public: |
| 580 |
typedef typename Function<F>::Result Result; |
| 581 |
typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 582 |
|
| 583 |
explicit Impl(const std::shared_ptr<R>& wrapper) |
| 584 |
: performed_(false), wrapper_(wrapper) {} |
| 585 |
|
| 586 |
Result Perform(const ArgumentTuple&) override { |
| 587 |
GTEST_CHECK_(!performed_) |
| 588 |
<< "A ByMove() action should only be performed once."; |
| 589 |
performed_ = true; |
| 590 |
return std::move(wrapper_->payload); |
| 591 |
} |
| 592 |
|
| 593 |
private: |
| 594 |
bool performed_; |
| 595 |
const std::shared_ptr<R> wrapper_; |
| 596 |
|
| 597 |
GTEST_DISALLOW_ASSIGN_(Impl); |
| 598 |
}; |
| 599 |
|
| 600 |
const std::shared_ptr<R> value_; |
| 601 |
|
| 602 |
GTEST_DISALLOW_ASSIGN_(ReturnAction); |
| 603 |
}; |
| 604 |
|
| 605 |
|
| 606 |
class ReturnNullAction { |
| 607 |
public: |
| 608 |
|
| 609 |
|
| 610 |
|
| 611 |
template <typename Result, typename ArgumentTuple> |
| 612 |
static Result Perform(const ArgumentTuple&) { |
| 613 |
return nullptr; |
| 614 |
} |
| 615 |
}; |
| 616 |
|
| 617 |
|
| 618 |
class ReturnVoidAction { |
| 619 |
public: |
| 620 |
|
| 621 |
template <typename Result, typename ArgumentTuple> |
| 622 |
static void Perform(const ArgumentTuple&) { |
| 623 |
static_assert(std::is_void<Result>::value, "Result should be void."); |
| 624 |
} |
| 625 |
}; |
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
template <typename T> |
| 631 |
class ReturnRefAction { |
| 632 |
public: |
| 633 |
|
| 634 |
explicit ReturnRefAction(T& ref) : ref_(ref) {} |
| 635 |
|
| 636 |
|
| 637 |
|
| 638 |
template <typename F> |
| 639 |
operator Action<F>() const { |
| 640 |
typedef typename Function<F>::Result Result; |
| 641 |
|
| 642 |
|
| 643 |
|
| 644 |
GTEST_COMPILE_ASSERT_(std::is_reference<Result>::value, |
| 645 |
use_Return_instead_of_ReturnRef_to_return_a_value); |
| 646 |
return Action<F>(new Impl<F>(ref_)); |
| 647 |
} |
| 648 |
|
| 649 |
private: |
| 650 |
|
| 651 |
template <typename F> |
| 652 |
class Impl : public ActionInterface<F> { |
| 653 |
public: |
| 654 |
typedef typename Function<F>::Result Result; |
| 655 |
typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 656 |
|
| 657 |
explicit Impl(T& ref) : ref_(ref) {} |
| 658 |
|
| 659 |
Result Perform(const ArgumentTuple&) override { return ref_; } |
| 660 |
|
| 661 |
private: |
| 662 |
T& ref_; |
| 663 |
|
| 664 |
GTEST_DISALLOW_ASSIGN_(Impl); |
| 665 |
}; |
| 666 |
|
| 667 |
T& ref_; |
| 668 |
|
| 669 |
GTEST_DISALLOW_ASSIGN_(ReturnRefAction); |
| 670 |
}; |
| 671 |
|
| 672 |
|
| 673 |
|
| 674 |
|
| 675 |
template <typename T> |
| 676 |
class ReturnRefOfCopyAction { |
| 677 |
public: |
| 678 |
|
| 679 |
|
| 680 |
explicit ReturnRefOfCopyAction(const T& value) : value_(value) {} |
| 681 |
|
| 682 |
|
| 683 |
|
| 684 |
template <typename F> |
| 685 |
operator Action<F>() const { |
| 686 |
typedef typename Function<F>::Result Result; |
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
GTEST_COMPILE_ASSERT_( |
| 691 |
std::is_reference<Result>::value, |
| 692 |
use_Return_instead_of_ReturnRefOfCopy_to_return_a_value); |
| 693 |
return Action<F>(new Impl<F>(value_)); |
| 694 |
} |
| 695 |
|
| 696 |
private: |
| 697 |
|
| 698 |
template <typename F> |
| 699 |
class Impl : public ActionInterface<F> { |
| 700 |
public: |
| 701 |
typedef typename Function<F>::Result Result; |
| 702 |
typedef typename Function<F>::ArgumentTuple ArgumentTuple; |
| 703 |
|
| 704 |
explicit Impl(const T& value) : value_(value) {} |
| 705 |
|
| 706 |
Result Perform(const ArgumentTuple&) override { return value_; } |
| 707 |
|
| 708 |
private: |
| 709 |
T value_; |
| 710 |
|
| 711 |
GTEST_DISALLOW_ASSIGN_(Impl); |
| 712 |
}; |
| 713 |
|
| 714 |
const T value_; |
| 715 |
|
| 716 |
GTEST_DISALLOW_ASSIGN_(ReturnRefOfCopyAction); |
| 717 |
}; |
| 718 |
|
| 719 |
|
| 720 |
class DoDefaultAction { |
| 721 |
public: |
| 722 |
|
| 723 |
|
| 724 |
template <typename F> |
| 725 |
operator Action<F>() const { return Action<F>(); } |
| 726 |
}; |
| 727 |
|
| 728 |
|
| 729 |
|
| 730 |
template <typename T1, typename T2> |
| 731 |
class AssignAction { |
| 732 |
public: |
| 733 |
AssignAction(T1* ptr, T2 value) : ptr_(ptr), value_(value) {} |
| 734 |
|
| 735 |
template <typename Result, typename ArgumentTuple> |
| 736 |
void Perform(const ArgumentTuple& ) const { |
| 737 |
*ptr_ = value_; |
| 738 |
} |
| 739 |
|
| 740 |
private: |
| 741 |
T1* const ptr_; |
| 742 |
const T2 value_; |
| 743 |
|
| 744 |
GTEST_DISALLOW_ASSIGN_(AssignAction); |
| 745 |
}; |
| 746 |
|
| 747 |
#if !GTEST_OS_WINDOWS_MOBILE |
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
template <typename T> |
| 752 |
class SetErrnoAndReturnAction { |
| 753 |
public: |
| 754 |
SetErrnoAndReturnAction(int errno_value, T result) |
| 755 |
: errno_(errno_value), |
| 756 |
result_(result) {} |
| 757 |
template <typename Result, typename ArgumentTuple> |
| 758 |
Result Perform(const ArgumentTuple& ) const { |
| 759 |
errno = errno_; |
| 760 |
return result_; |
| 761 |
} |
| 762 |
|
| 763 |
private: |
| 764 |
const int errno_; |
| 765 |
const T result_; |
| 766 |
|
| 767 |
GTEST_DISALLOW_ASSIGN_(SetErrnoAndReturnAction); |
| 768 |
}; |
| 769 |
|
| 770 |
#endif |
| 771 |
|
| 772 |
|
| 773 |
|
| 774 |
template <size_t N, typename A, typename = void> |
| 775 |
struct SetArgumentPointeeAction { |
| 776 |
A value; |
| 777 |
|
| 778 |
template <typename... Args> |
| 779 |
void operator()(const Args&... args) const { |
| 780 |
*::std::get<N>(std::tie(args...)) = value; |
| 781 |
} |
| 782 |
}; |
| 783 |
|
| 784 |
|
| 785 |
template <class Class, typename MethodPtr> |
| 786 |
struct InvokeMethodAction { |
| 787 |
Class* const obj_ptr; |
| 788 |
const MethodPtr method_ptr; |
| 789 |
|
| 790 |
template <typename... Args> |
| 791 |
auto operator()(Args&&... args) const |
| 792 |
-> decltype((obj_ptr->*method_ptr)(std::forward<Args>(args)...)) { |
| 793 |
return (obj_ptr->*method_ptr)(std::forward<Args>(args)...); |
| 794 |
} |
| 795 |
}; |
| 796 |
|
| 797 |
|
| 798 |
|
| 799 |
|
| 800 |
|
| 801 |
template <typename FunctionImpl> |
| 802 |
struct InvokeWithoutArgsAction { |
| 803 |
FunctionImpl function_impl; |
| 804 |
|
| 805 |
|
| 806 |
|
| 807 |
template <typename... Args> |
| 808 |
auto operator()(const Args&...) -> decltype(function_impl()) { |
| 809 |
return function_impl(); |
| 810 |
} |
| 811 |
}; |
| 812 |
|
| 813 |
|
| 814 |
template <class Class, typename MethodPtr> |
| 815 |
struct InvokeMethodWithoutArgsAction { |
| 816 |
Class* const obj_ptr; |
| 817 |
const MethodPtr method_ptr; |
| 818 |
|
| 819 |
using ReturnType = typename std::result_of<MethodPtr(Class*)>::type; |
| 820 |
|
| 821 |
template <typename... Args> |
| 822 |
ReturnType operator()(const Args&...) const { |
| 823 |
return (obj_ptr->*method_ptr)(); |
| 824 |
} |
| 825 |
}; |
| 826 |
|
| 827 |
|
| 828 |
template <typename A> |
| 829 |
class IgnoreResultAction { |
| 830 |
public: |
| 831 |
explicit IgnoreResultAction(const A& action) : action_(action) {} |
| 832 |
|
| 833 |
template <typename F> |
| 834 |
operator Action<F>() const { |
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
|
| 840 |
|
| 841 |
|
| 842 |
|
| 843 |
typedef typename internal::Function<F>::Result Result; |
| 844 |
|
| 845 |
|
| 846 |
static_assert(std::is_void<Result>::value, "Result type should be void."); |
| 847 |
|
| 848 |
return Action<F>(new Impl<F>(action_)); |
| 849 |
} |
| 850 |
|
| 851 |
private: |
| 852 |
template <typename F> |
| 853 |
class Impl : public ActionInterface<F> { |
| 854 |
public: |
| 855 |
typedef typename internal::Function<F>::Result Result; |
| 856 |
typedef typename internal::Function<F>::ArgumentTuple ArgumentTuple; |
| 857 |
|
| 858 |
explicit Impl(const A& action) : action_(action) {} |
| 859 |
|
| 860 |
void Perform(const ArgumentTuple& args) override { |
| 861 |
|
| 862 |
action_.Perform(args); |
| 863 |
} |
| 864 |
|
| 865 |
private: |
| 866 |
|
| 867 |
|
| 868 |
typedef typename internal::Function<F>::MakeResultIgnoredValue |
| 869 |
OriginalFunction; |
| 870 |
|
| 871 |
const Action<OriginalFunction> action_; |
| 872 |
|
| 873 |
GTEST_DISALLOW_ASSIGN_(Impl); |
| 874 |
}; |
| 875 |
|
| 876 |
const A action_; |
| 877 |
|
| 878 |
GTEST_DISALLOW_ASSIGN_(IgnoreResultAction); |
| 879 |
}; |
| 880 |
|
| 881 |
template <typename InnerAction, size_t... I> |
| 882 |
struct WithArgsAction { |
| 883 |
InnerAction action; |
| 884 |
|
| 885 |
|
| 886 |
|
| 887 |
template <typename R, typename... Args> |
| 888 |
operator Action<R(Args...)>() const { |
| 889 |
Action<R(typename std::tuple_element<I, std::tuple<Args...>>::type...)> |
| 890 |
converted(action); |
| 891 |
|
| 892 |
return [converted](Args... args) -> R { |
| 893 |
return converted.Perform(std::forward_as_tuple( |
| 894 |
std::get<I>(std::forward_as_tuple(std::forward<Args>(args)...))...)); |
| 895 |
}; |
| 896 |
} |
| 897 |
}; |
| 898 |
|
| 899 |
template <typename... Actions> |
| 900 |
struct DoAllAction { |
| 901 |
private: |
| 902 |
template <typename... Args, size_t... I> |
| 903 |
std::vector<Action<void(Args...)>> Convert(IndexSequence<I...>) const { |
| 904 |
return {std::get<I>(actions)...}; |
| 905 |
} |
| 906 |
|
| 907 |
public: |
| 908 |
std::tuple<Actions...> actions; |
| 909 |
|
| 910 |
template <typename R, typename... Args> |
| 911 |
operator Action<R(Args...)>() const { |
| 912 |
struct Op { |
| 913 |
std::vector<Action<void(Args...)>> converted; |
| 914 |
Action<R(Args...)> last; |
| 915 |
R operator()(Args... args) const { |
| 916 |
auto tuple_args = std::forward_as_tuple(std::forward<Args>(args)...); |
| 917 |
for (auto& a : converted) { |
| 918 |
a.Perform(tuple_args); |
| 919 |
} |
| 920 |
return last.Perform(tuple_args); |
| 921 |
} |
| 922 |
}; |
| 923 |
return Op{Convert<Args...>(MakeIndexSequence<sizeof...(Actions) - 1>()), |
| 924 |
std::get<sizeof...(Actions) - 1>(actions)}; |
| 925 |
} |
| 926 |
}; |
| 927 |
|
| 928 |
} |
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
|
| 960 |
typedef internal::IgnoredValue Unused; |
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
template <typename... Action> |
| 965 |
internal::DoAllAction<typename std::decay<Action>::type...> DoAll( |
| 966 |
Action&&... action) { |
| 967 |
return {std::forward_as_tuple(std::forward<Action>(action)...)}; |
| 968 |
} |
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
template <size_t k, typename InnerAction> |
| 976 |
internal::WithArgsAction<typename std::decay<InnerAction>::type, k> |
| 977 |
WithArg(InnerAction&& action) { |
| 978 |
return {std::forward<InnerAction>(action)}; |
| 979 |
} |
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
|
| 985 |
template <size_t k, size_t... ks, typename InnerAction> |
| 986 |
internal::WithArgsAction<typename std::decay<InnerAction>::type, k, ks...> |
| 987 |
WithArgs(InnerAction&& action) { |
| 988 |
return {std::forward<InnerAction>(action)}; |
| 989 |
} |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
template <typename InnerAction> |
| 996 |
internal::WithArgsAction<typename std::decay<InnerAction>::type> |
| 997 |
WithoutArgs(InnerAction&& action) { |
| 998 |
return {std::forward<InnerAction>(action)}; |
| 999 |
} |
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
template <typename R> |
| 1005 |
internal::ReturnAction<R> Return(R value) { |
| 1006 |
return internal::ReturnAction<R>(std::move(value)); |
| 1007 |
} |
| 1008 |
|
| 1009 |
|
| 1010 |
inline PolymorphicAction<internal::ReturnNullAction> ReturnNull() { |
| 1011 |
return MakePolymorphicAction(internal::ReturnNullAction()); |
| 1012 |
} |
| 1013 |
|
| 1014 |
|
| 1015 |
inline PolymorphicAction<internal::ReturnVoidAction> Return() { |
| 1016 |
return MakePolymorphicAction(internal::ReturnVoidAction()); |
| 1017 |
} |
| 1018 |
|
| 1019 |
|
| 1020 |
template <typename R> |
| 1021 |
inline internal::ReturnRefAction<R> ReturnRef(R& x) { |
| 1022 |
return internal::ReturnRefAction<R>(x); |
| 1023 |
} |
| 1024 |
|
| 1025 |
|
| 1026 |
|
| 1027 |
|
| 1028 |
template <typename R> |
| 1029 |
inline internal::ReturnRefOfCopyAction<R> ReturnRefOfCopy(const R& x) { |
| 1030 |
return internal::ReturnRefOfCopyAction<R>(x); |
| 1031 |
} |
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
template <typename R> |
| 1038 |
internal::ByMoveWrapper<R> ByMove(R x) { |
| 1039 |
return internal::ByMoveWrapper<R>(std::move(x)); |
| 1040 |
} |
| 1041 |
|
| 1042 |
|
| 1043 |
inline internal::DoDefaultAction DoDefault() { |
| 1044 |
return internal::DoDefaultAction(); |
| 1045 |
} |
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
template <size_t N, typename T> |
| 1050 |
internal::SetArgumentPointeeAction<N, T> SetArgPointee(T x) { |
| 1051 |
return {std::move(x)}; |
| 1052 |
} |
| 1053 |
|
| 1054 |
|
| 1055 |
template <size_t N, typename T> |
| 1056 |
internal::SetArgumentPointeeAction<N, T> SetArgumentPointee(T x) { |
| 1057 |
return {std::move(x)}; |
| 1058 |
} |
| 1059 |
|
| 1060 |
|
| 1061 |
template <typename T1, typename T2> |
| 1062 |
PolymorphicAction<internal::AssignAction<T1, T2> > Assign(T1* ptr, T2 val) { |
| 1063 |
return MakePolymorphicAction(internal::AssignAction<T1, T2>(ptr, val)); |
| 1064 |
} |
| 1065 |
|
| 1066 |
#if !GTEST_OS_WINDOWS_MOBILE |
| 1067 |
|
| 1068 |
|
| 1069 |
template <typename T> |
| 1070 |
PolymorphicAction<internal::SetErrnoAndReturnAction<T> > |
| 1071 |
SetErrnoAndReturn(int errval, T result) { |
| 1072 |
return MakePolymorphicAction( |
| 1073 |
internal::SetErrnoAndReturnAction<T>(errval, result)); |
| 1074 |
} |
| 1075 |
|
| 1076 |
#endif |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
template <typename FunctionImpl> |
| 1085 |
typename std::decay<FunctionImpl>::type Invoke(FunctionImpl&& function_impl) { |
| 1086 |
return std::forward<FunctionImpl>(function_impl); |
| 1087 |
} |
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
template <class Class, typename MethodPtr> |
| 1092 |
internal::InvokeMethodAction<Class, MethodPtr> Invoke(Class* obj_ptr, |
| 1093 |
MethodPtr method_ptr) { |
| 1094 |
return {obj_ptr, method_ptr}; |
| 1095 |
} |
| 1096 |
|
| 1097 |
|
| 1098 |
template <typename FunctionImpl> |
| 1099 |
internal::InvokeWithoutArgsAction<typename std::decay<FunctionImpl>::type> |
| 1100 |
InvokeWithoutArgs(FunctionImpl function_impl) { |
| 1101 |
return {std::move(function_impl)}; |
| 1102 |
} |
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
template <class Class, typename MethodPtr> |
| 1107 |
internal::InvokeMethodWithoutArgsAction<Class, MethodPtr> InvokeWithoutArgs( |
| 1108 |
Class* obj_ptr, MethodPtr method_ptr) { |
| 1109 |
return {obj_ptr, method_ptr}; |
| 1110 |
} |
| 1111 |
|
| 1112 |
|
| 1113 |
|
| 1114 |
|
| 1115 |
template <typename A> |
| 1116 |
inline internal::IgnoreResultAction<A> IgnoreResult(const A& an_action) { |
| 1117 |
return internal::IgnoreResultAction<A>(an_action); |
| 1118 |
} |
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
template <typename T> |
| 1131 |
inline ::std::reference_wrapper<T> ByRef(T& l_value) { |
| 1132 |
return ::std::reference_wrapper<T>(l_value); |
| 1133 |
} |
| 1134 |
|
| 1135 |
} |
| 1136 |
|
| 1137 |
#ifdef _MSC_VER |
| 1138 |
# pragma warning(pop) |
| 1139 |
#endif |
| 1140 |
|
| 1141 |
|
| 1142 |
#endif |