diff --git a/3party/optional/optional.h b/3party/optional/optional.h index bbfe257..ce64a90 100644 --- a/3party/optional/optional.h +++ b/3party/optional/optional.h @@ -2495,4 +2495,42 @@ constexpr tl::nullopt_t nullopt{tl::nullopt}; }// namespace std +namespace ulib { +template +using hash = std::hash>; + +// if <= C++14 +#if __cplusplus < 201703L +template +using optional = tl::optional; + +template +using nullopt_t = tl::nullopt_t; + +// make_optional +template +constexpr tl::optional> +make_optional(T &&v) +{ + return tl::make_optional(std::forward(v)); +} + +template +constexpr tl::optional +make_optional(Args &&...args) +{ + return tl::make_optional(std::forward(args)...); +} + +template +constexpr tl::optional +make_optional(std::initializer_list il, Args &&...args) +{ + return tl::make_optional(il, std::forward(args)...); +} + +constexpr tl::nullopt_t nullopt{tl::nullopt}; +#endif + +}// namespace ulib #endif diff --git a/tests/3party/optional/optional_unittest.cpp b/tests/3party/optional/optional_unittest.cpp new file mode 100644 index 0000000..75c7060 --- /dev/null +++ b/tests/3party/optional/optional_unittest.cpp @@ -0,0 +1,19 @@ +#include +#include + +TEST(optional, has_value) +{ + ulib::optional 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_opt; + EXPECT_FALSE(int_opt.has_value()); + EXPECT_EQ(int_opt.value_or(1), 1); + EXPECT_EQ(int_opt.value_or(2), 2); +} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 1b1689d..779c746 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -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