feat add MakeStatusOr
All checks were successful
linux-mips64-gcc / linux-gcc-mips64el (push) Successful in 2m4s
linux-x64-gcc / linux-gcc (Debug) (push) Successful in 54s
linux-x64-gcc / linux-gcc (Release) (push) Successful in 48s

This commit is contained in:
tqcq 2024-03-30 20:17:55 +08:00
parent c96cb23a3d
commit c7fe469c1b
3 changed files with 25 additions and 19 deletions

View File

@ -150,7 +150,7 @@ if(SLED_BUILD_TESTS)
sled_add_test( sled_add_test(
NAME NAME
sled_base_test sled_all_tests
SRCS SRCS
src/sled/async/async_test.cc src/sled/async/async_test.cc
src/sled/any_test.cc src/sled/any_test.cc
@ -161,6 +161,8 @@ if(SLED_BUILD_TESTS)
src/sled/cleanup_test.cc src/sled/cleanup_test.cc
src/sled/status_test.cc src/sled/status_test.cc
src/sled/status_or_test.cc src/sled/status_or_test.cc
src/sled/strings/utils_test.cc
src/sled/strings/base64_test.cc
src/sled/system/fiber/fiber_test.cc src/sled/system/fiber/fiber_test.cc
src/sled/system/thread_pool_test.cc src/sled/system/thread_pool_test.cc
src/sled/rx_test.cc src/sled/rx_test.cc
@ -169,16 +171,6 @@ if(SLED_BUILD_TESTS)
sled sled
GTest::gtest_main) GTest::gtest_main)
sled_add_test(
NAME
sled_strings_test
SRCS
src/sled/strings/utils_test.cc
src/sled/strings/base64_test.cc
LIBS
sled
GTest::gtest_main)
sled_add_test(NAME sled_symbolize_test SRCS sled_add_test(NAME sled_symbolize_test SRCS
src/sled/debugging/symbolize_test.cc LIBS sled) src/sled/debugging/symbolize_test.cc LIBS sled)
endif(SLED_BUILD_TESTS) endif(SLED_BUILD_TESTS)

View File

@ -7,6 +7,7 @@
#pragma once #pragma once
#ifndef SLED_STATUS_OR_H #ifndef SLED_STATUS_OR_H
#define SLED_STATUS_OR_H #define SLED_STATUS_OR_H
#include "sled/lang/attributes.h"
#include "sled/optional.h" #include "sled/optional.h"
#include "sled/status.h" #include "sled/status.h"
#include <stdexcept> #include <stdexcept>
@ -22,7 +23,7 @@ public:
StatusOr() : StatusOr(MakeDefaultStatus()) {} StatusOr() : StatusOr(MakeDefaultStatus()) {}
StatusOr(StatusOr const &) = default; StatusOr(StatusOr const &) = default;
StatusOr &operator=(StatusOr const &) = default; StatusOr &operator=(StatusOr const &) = default;
StatusOr(StatusOr &&other) : status_(std::move(other.status_)), value_(std::move(other.value_)) StatusOr(StatusOr &&other) : status_(std::move(other.status_)), value_(std::move(other.value_))
@ -32,8 +33,8 @@ public:
StatusOr &operator=(StatusOr &&other) StatusOr &operator=(StatusOr &&other)
{ {
status_ = std::move(other.status_); status_ = std::move(other.status_);
value_ = std::move(other.value_); value_ = std::move(other.value_);
other.status_ = MakeDefaultStatus(); other.status_ = MakeDefaultStatus();
return *this; return *this;
} }
@ -57,7 +58,7 @@ public:
StatusOr &operator=(U &&rhs) StatusOr &operator=(U &&rhs)
{ {
status_ = Status(); status_ = Status();
value_ = std::forward<U>(rhs); value_ = std::forward<U>(rhs);
return *this; return *this;
} }
@ -160,19 +161,32 @@ operator!=(StatusOr<T> const &a, StatusOr<T> const &b)
} }
template<typename T> template<typename T>
StatusOr<T> SLED_DEPRECATED StatusOr<T>
make_status_or(T rhs) make_status_or(T rhs)
{ {
return StatusOr<T>(std::move(rhs)); return StatusOr<T>(std::move(rhs));
} }
template<typename T> template<typename T>
StatusOr<T> SLED_DEPRECATED StatusOr<T>
make_status_or(StatusCode code, std::string message = "", ErrorInfo info = {}) make_status_or(StatusCode code, std::string message = "", ErrorInfo info = {})
{ {
return StatusOr<T>(Status(code, std::move(message))); return StatusOr<T>(Status(code, std::move(message)));
} }
template<typename T>
StatusOr<T>
MakeStatusOr(T rhs)
{
return StatusOr<T>(std::move(rhs));
}
template<typename T>
StatusOr<T>
MakeStatusOr(StatusCode code, std::string message = "", ErrorInfo info = {})
{
return StatusOr<T>(Status(code, std::move(message)));
}
}// namespace sled }// namespace sled
#endif// SLED_STATUS_OR_H #endif// SLED_STATUS_OR_H

View File

@ -14,8 +14,8 @@ TEST(StatusOr, TestStatusOr)
TEST(StatusOr, make_status_or) TEST(StatusOr, make_status_or)
{ {
auto from_raw_str = sled::make_status_or("hello"); auto from_raw_str = sled::MakeStatusOr("hello");
auto from_string = sled::make_status_or(std::string("world")); auto from_string = sled::MakeStatusOr(std::string("world"));
EXPECT_TRUE(from_raw_str.ok()); EXPECT_TRUE(from_raw_str.ok());
EXPECT_TRUE(from_string.ok()); EXPECT_TRUE(from_string.ok());
EXPECT_EQ(from_raw_str.value(), "hello"); EXPECT_EQ(from_raw_str.value(), "hello");