Skip to content

Commit

Permalink
Add lambda support to Require (#1705)
Browse files Browse the repository at this point in the history
* Add lambda support to Require

* Capture by copy

---------

Co-authored-by: Prasanna Loganathar <[email protected]>
  • Loading branch information
Bushstar and prasannavl authored Feb 1, 2023
1 parent fdb5624 commit 0e1a130
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 5 deletions.
6 changes: 3 additions & 3 deletions src/amount.h
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ struct CTokenAmount { // simple std::pair is less informative

Res Add(CAmount amount) {
// safety checks
Require(amount >= 0, "negative amount: %s", GetDecimaleString(amount));
Require(amount >= 0, [=]{ return strprintf("negative amount: %s", GetDecimaleString(amount)); });

// add
auto sumRes = SafeAdd(nValue, amount);
Expand All @@ -137,8 +137,8 @@ struct CTokenAmount { // simple std::pair is less informative

Res Sub(CAmount amount) {
// safety checks
Require(amount >= 0, "negative amount: %s", GetDecimaleString(amount));
Require(nValue >= amount, "amount %s is less than %s", GetDecimaleString(nValue), GetDecimaleString(amount));
Require(amount >= 0, [=]{ return strprintf("negative amount: %s", GetDecimaleString(amount)); });
Require(nValue >= amount, [=]{ return strprintf("amount %s is less than %s", GetDecimaleString(nValue), GetDecimaleString(amount)); });

// sub
nValue -= amount;
Expand Down
5 changes: 3 additions & 2 deletions src/masternodes/res.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,10 +113,11 @@ Res CheckRes(T &&res, std::tuple<Args...> &&args) {
if constexpr (size == 0) {
static_assert(std::is_convertible_v<T, Res>);
return std::forward<T>(res);
} else if constexpr (std::
is_invocable_r_v<std::string, std::tuple_element_t<0, std::tuple<Args...>>, std::string>) {
} else if constexpr (std::is_invocable_r_v<std::string, std::tuple_element_t<0, std::tuple<Args...>>, std::string>) {
static_assert(std::is_convertible_v<T, Res>);
return Res::Err(std::invoke(std::get<0>(args), res.msg));
} else if constexpr (size == 1 && std::is_invocable_r_v<std::string, std::tuple_element_t<0, std::tuple<Args...>>>) {
return Res::Err(std::invoke(std::get<0>(args)));
} else {
return Res::Err(args, std::make_index_sequence<size>{});
}
Expand Down

0 comments on commit 0e1a130

Please sign in to comment.