Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions include/result/result.h
Original file line number Diff line number Diff line change
Expand Up @@ -143,17 +143,21 @@ class Result {
}

template <typename F>
void NotOKThen(F&& f) {
R& NotOKThen(F&& f) {
if (IsOK()) {
return;
return static_cast<R&>(*this);
}

f(*this);

return static_cast<R&>(*this);
}

template <typename F>
void AlwaysThen(F&& f) {
R& AlwaysThen(F&& f) {
f(*this);

return static_cast<R&>(*this);
}

void PushHistory(const std::string& file_name, int32_t line) {
Expand Down
22 changes: 21 additions & 1 deletion test/result_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -384,7 +384,7 @@ TEST_F(ResultTest, smart_pointer_with_nullptr) {
}
}

TEST_F(ResultTest, NotOKThen) {
TEST_F(ResultTest, NotOKThenAndAlwaysThen) {
int flag = 0;

std::invoke([]() -> CustomResult {
Expand Down Expand Up @@ -418,6 +418,26 @@ TEST_F(ResultTest, NotOKThen) {
});

EXPECT_EQ(flag, 3);

{
int n_flag = 0;
int a_flag = 0;

std::invoke([]() -> CustomResult {
return CustomResult::Builder(CustomResult::ErrorCode::OtherError).Build();
})
.NotOKThen([&n_flag](auto&& res) {
EXPECT_FALSE(res.IsOK());
n_flag = 1;
})
.AlwaysThen([&a_flag](auto&& res) {
EXPECT_FALSE(res.IsOK());
a_flag = 1;
});

EXPECT_EQ(n_flag, 1);
EXPECT_EQ(a_flag, 1);
}
}

TEST_F(ResultTest, GetHistoryInfoNode) {
Expand Down