@@ -835,6 +835,10 @@ class Action<R(Args...)> {
835
835
Result operator ()(const InArgs&...) const {
836
836
return function_impl ();
837
837
}
838
+ template <typename ... InArgs>
839
+ Result operator ()(const InArgs&...) {
840
+ return function_impl ();
841
+ }
838
842
839
843
FunctionImpl function_impl;
840
844
};
@@ -1451,6 +1455,30 @@ struct WithArgsAction {
1451
1455
return OA{std::move (inner_action)};
1452
1456
}
1453
1457
1458
+ // As above, but in the case where we want to create a OnceAction from a const
1459
+ // WithArgsAction. This is fine as long as the inner action doesn't need to
1460
+ // move any of its state to create a OnceAction.
1461
+ template <
1462
+ typename R, typename ... Args,
1463
+ typename std::enable_if<
1464
+ std::is_convertible<const InnerAction&,
1465
+ OnceAction<R(internal::TupleElement<
1466
+ I, std::tuple<Args...>>...)>>::value,
1467
+ int >::type = 0 >
1468
+ operator OnceAction<R(Args...)>() const & { // NOLINT
1469
+ struct OA {
1470
+ OnceAction<InnerSignature<R, Args...>> inner_action;
1471
+
1472
+ R operator ()(Args&&... args) && {
1473
+ return std::move (inner_action)
1474
+ .Call (std::get<I>(
1475
+ std::forward_as_tuple (std::forward<Args>(args)...))...);
1476
+ }
1477
+ };
1478
+
1479
+ return OA{inner_action};
1480
+ }
1481
+
1454
1482
template <
1455
1483
typename R, typename ... Args,
1456
1484
typename std::enable_if<
@@ -1493,6 +1521,7 @@ class DoAllAction<FinalAction> {
1493
1521
// providing a call operator because even with a particular set of arguments
1494
1522
// they don't have a fixed return type.
1495
1523
1524
+ // We support conversion to OnceAction whenever the sub-action does.
1496
1525
template <typename R, typename ... Args,
1497
1526
typename std::enable_if<
1498
1527
std::is_convertible<FinalAction, OnceAction<R(Args...)>>::value,
@@ -1501,6 +1530,21 @@ class DoAllAction<FinalAction> {
1501
1530
return std::move (final_action_);
1502
1531
}
1503
1532
1533
+ // We also support conversion to OnceAction whenever the sub-action supports
1534
+ // conversion to Action (since any Action can also be a OnceAction).
1535
+ template <
1536
+ typename R, typename ... Args,
1537
+ typename std::enable_if<
1538
+ conjunction<
1539
+ negation<
1540
+ std::is_convertible<FinalAction, OnceAction<R(Args...)>>>,
1541
+ std::is_convertible<FinalAction, Action<R(Args...)>>>::value,
1542
+ int >::type = 0 >
1543
+ operator OnceAction<R(Args...)>() && { // NOLINT
1544
+ return Action<R (Args...)>(std::move (final_action_));
1545
+ }
1546
+
1547
+ // We support conversion to Action whenever the sub-action does.
1504
1548
template <
1505
1549
typename R, typename ... Args,
1506
1550
typename std::enable_if<
@@ -1580,16 +1624,16 @@ class DoAllAction<InitialAction, OtherActions...>
1580
1624
: Base({}, std::forward<U>(other_actions)...),
1581
1625
initial_action_(std::forward<T>(initial_action)) {}
1582
1626
1583
- template < typename R, typename ... Args,
1584
- typename std::enable_if<
1585
- conjunction <
1586
- // Both the initial action and the rest must support
1587
- // conversion to OnceAction.
1588
- std::is_convertible<
1589
- InitialAction,
1590
- OnceAction<void (InitialActionArgType<Args>...)>>,
1591
- std::is_convertible<Base, OnceAction<R(Args...)>>>::value,
1592
- int >::type = 0 >
1627
+ // We support conversion to OnceAction whenever both the initial action and
1628
+ // the rest support conversion to OnceAction.
1629
+ template <
1630
+ typename R, typename ... Args,
1631
+ typename std::enable_if<
1632
+ conjunction< std::is_convertible<
1633
+ InitialAction,
1634
+ OnceAction<void (InitialActionArgType<Args>...)>>,
1635
+ std::is_convertible<Base, OnceAction<R(Args...)>>>::value,
1636
+ int >::type = 0 >
1593
1637
operator OnceAction<R(Args...)>() && { // NOLINT
1594
1638
// Return an action that first calls the initial action with arguments
1595
1639
// filtered through InitialActionArgType, then forwards arguments directly
@@ -1612,12 +1656,34 @@ class DoAllAction<InitialAction, OtherActions...>
1612
1656
};
1613
1657
}
1614
1658
1659
+ // We also support conversion to OnceAction whenever the initial action
1660
+ // supports conversion to Action (since any Action can also be a OnceAction).
1661
+ //
1662
+ // The remaining sub-actions must also be compatible, but we don't need to
1663
+ // special case them because the base class deals with them.
1664
+ template <
1665
+ typename R, typename ... Args,
1666
+ typename std::enable_if<
1667
+ conjunction<
1668
+ negation<std::is_convertible<
1669
+ InitialAction,
1670
+ OnceAction<void (InitialActionArgType<Args>...)>>>,
1671
+ std::is_convertible<InitialAction,
1672
+ Action<void (InitialActionArgType<Args>...)>>,
1673
+ std::is_convertible<Base, OnceAction<R(Args...)>>>::value,
1674
+ int >::type = 0 >
1675
+ operator OnceAction<R(Args...)>() && { // NOLINT
1676
+ return DoAll (
1677
+ Action<void (InitialActionArgType<Args>...)>(std::move (initial_action_)),
1678
+ std::move (static_cast <Base&>(*this )));
1679
+ }
1680
+
1681
+ // We support conversion to Action whenever both the initial action and the
1682
+ // rest support conversion to Action.
1615
1683
template <
1616
1684
typename R, typename ... Args,
1617
1685
typename std::enable_if<
1618
1686
conjunction<
1619
- // Both the initial action and the rest must support conversion to
1620
- // Action.
1621
1687
std::is_convertible<const InitialAction&,
1622
1688
Action<void (InitialActionArgType<Args>...)>>,
1623
1689
std::is_convertible<const Base&, Action<R(Args...)>>>::value,
@@ -1681,6 +1747,16 @@ struct SaveArgAction {
1681
1747
}
1682
1748
};
1683
1749
1750
+ template <size_t k, typename Ptr>
1751
+ struct SaveArgByMoveAction {
1752
+ Ptr pointer;
1753
+
1754
+ template <typename ... Args>
1755
+ void operator ()(Args&&... args) const {
1756
+ *pointer = std::move (std::get<k>(std::tie (args...)));
1757
+ }
1758
+ };
1759
+
1684
1760
template <size_t k, typename Ptr>
1685
1761
struct SaveArgPointeeAction {
1686
1762
Ptr pointer;
@@ -2031,6 +2107,13 @@ internal::SaveArgAction<k, Ptr> SaveArg(Ptr pointer) {
2031
2107
return {pointer};
2032
2108
}
2033
2109
2110
+ // Action SaveArgByMove<k>(pointer) moves the k-th (0-based) argument of the
2111
+ // mock function into *pointer.
2112
+ template <size_t k, typename Ptr>
2113
+ internal::SaveArgByMoveAction<k, Ptr> SaveArgByMove (Ptr pointer) {
2114
+ return {pointer};
2115
+ }
2116
+
2034
2117
// Action SaveArgPointee<k>(pointer) saves the value pointed to
2035
2118
// by the k-th (0-based) argument of the mock function to *pointer.
2036
2119
template <size_t k, typename Ptr>
@@ -2174,9 +2257,9 @@ ::testing::Action<F> MakeAction(std::shared_ptr<Impl> impl) {
2174
2257
}
2175
2258
2176
2259
#define GMOCK_INTERNAL_ARG_UNUSED (i, data, el ) \
2177
- , GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED const arg##i##_type& arg##i
2178
- #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_ \
2179
- GTEST_INTERNAL_ATTRIBUTE_MAYBE_UNUSED const args_type& args GMOCK_PP_REPEAT ( \
2260
+ , [[maybe_unused]] const arg##i##_type& arg##i
2261
+ #define GMOCK_ACTION_ARG_TYPES_AND_NAMES_UNUSED_ \
2262
+ [[maybe_unused]] const args_type& args GMOCK_PP_REPEAT ( \
2180
2263
GMOCK_INTERNAL_ARG_UNUSED, , 10 )
2181
2264
2182
2265
#define GMOCK_INTERNAL_ARG (i, data, el ) , const arg##i##_type& arg##i
@@ -2241,8 +2324,8 @@ ::testing::Action<F> MakeAction(std::shared_ptr<Impl> impl) {
2241
2324
std::shared_ptr<const gmock_Impl> impl_; \
2242
2325
}; \
2243
2326
template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
2244
- inline full_name<GMOCK_ACTION_TYPE_PARAMS_(params)> name ( \
2245
- GMOCK_ACTION_TYPE_GVALUE_PARAMS_ (params)) GTEST_MUST_USE_RESULT_; \
2327
+ [[nodiscard]] inline full_name<GMOCK_ACTION_TYPE_PARAMS_(params)> name ( \
2328
+ GMOCK_ACTION_TYPE_GVALUE_PARAMS_ (params)); \
2246
2329
template <GMOCK_ACTION_TYPENAME_PARAMS_(params)> \
2247
2330
inline full_name<GMOCK_ACTION_TYPE_PARAMS_(params)> name( \
2248
2331
GMOCK_ACTION_TYPE_GVALUE_PARAMS_ (params)) { \
@@ -2277,7 +2360,7 @@ ::testing::Action<F> MakeAction(std::shared_ptr<Impl> impl) {
2277
2360
return_type gmock_PerformImpl (GMOCK_ACTION_ARG_TYPES_AND_NAMES_) const ; \
2278
2361
}; \
2279
2362
}; \
2280
- inline name##Action name () GTEST_MUST_USE_RESULT_; \
2363
+ [[nodiscard]] inline name##Action name (); \
2281
2364
inline name##Action name () { return name##Action (); } \
2282
2365
template <typename function_type, typename return_type, typename args_type, \
2283
2366
GMOCK_ACTION_TEMPLATE_ARGS_NAMES_> \
0 commit comments