feat add optional to namespace ulib
All checks were successful
rpcrypto-build / build (Debug, hisiv510.toolchain.cmake) (push) Successful in 48s
rpcrypto-build / build (Release, himix200.toolchain.cmake) (push) Successful in 57s
rpcrypto-build / build (Release, hisiv510.toolchain.cmake) (push) Successful in 1m3s
rpcrypto-build / build (Debug, himix200.toolchain.cmake) (push) Successful in 1m8s
linux-hisiv500-gcc / linux-gcc-hisiv500 (push) Successful in 1m5s
linux-x64-gcc / linux-gcc (push) Successful in 1m47s
linux-mips64-gcc / linux-gcc-mips64el (push) Successful in 3m9s

This commit is contained in:
tqcq 2024-01-05 14:07:54 +08:00
parent a977ae0f3b
commit f85932cbfa
3 changed files with 58 additions and 0 deletions

View File

@ -2495,4 +2495,42 @@ constexpr tl::nullopt_t nullopt{tl::nullopt};
}// namespace std
namespace ulib {
template<typename T>
using hash = std::hash<tl::optional<T>>;
// if <= C++14
#if __cplusplus < 201703L
template<typename T>
using optional = tl::optional<T>;
template<typename T>
using nullopt_t = tl::nullopt_t;
// make_optional
template<typename T>
constexpr tl::optional<tl::detail::decay_t<T>>
make_optional(T &&v)
{
return tl::make_optional(std::forward<T>(v));
}
template<typename T, typename... Args>
constexpr tl::optional<T>
make_optional(Args &&...args)
{
return tl::make_optional<T>(std::forward<Args>(args)...);
}
template<typename T, typename U, typename... Args>
constexpr tl::optional<T>
make_optional(std::initializer_list<U> il, Args &&...args)
{
return tl::make_optional<T>(il, std::forward<Args>(args)...);
}
constexpr tl::nullopt_t nullopt{tl::nullopt};
#endif
}// namespace ulib
#endif

View File

@ -0,0 +1,19 @@
#include <gtest/gtest.h>
#include <optional.h>
TEST(optional, has_value)
{
ulib::optional<int> int_opt = 1;
EXPECT_TRUE(int_opt.has_value());
EXPECT_EQ(*int_opt, 1);
EXPECT_EQ(int_opt.value(), 1);
EXPECT_EQ(int_opt.value_or(2), 1);
}
TEST(optional, not_has_value)
{
ulib::optional<int> int_opt;
EXPECT_FALSE(int_opt.has_value());
EXPECT_EQ(int_opt.value_or(1), 1);
EXPECT_EQ(int_opt.value_or(2), 2);
}

View File

@ -13,6 +13,7 @@ add_executable(ulib_test
ulib/system/thread_pool_unittest.cpp
ulib/system/timer_unittest.cpp
3party/inja/inja_unittest.cpp
3party/optional/optional_unittest.cpp
ulib/utils/defer_unittest.cpp
)
target_link_libraries(ulib_test PRIVATE