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
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:
parent
a977ae0f3b
commit
f85932cbfa
@ -2495,4 +2495,42 @@ constexpr tl::nullopt_t nullopt{tl::nullopt};
|
|||||||
|
|
||||||
}// namespace std
|
}// 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
|
#endif
|
||||||
|
19
tests/3party/optional/optional_unittest.cpp
Normal file
19
tests/3party/optional/optional_unittest.cpp
Normal 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);
|
||||||
|
}
|
@ -13,6 +13,7 @@ add_executable(ulib_test
|
|||||||
ulib/system/thread_pool_unittest.cpp
|
ulib/system/thread_pool_unittest.cpp
|
||||||
ulib/system/timer_unittest.cpp
|
ulib/system/timer_unittest.cpp
|
||||||
3party/inja/inja_unittest.cpp
|
3party/inja/inja_unittest.cpp
|
||||||
|
3party/optional/optional_unittest.cpp
|
||||||
ulib/utils/defer_unittest.cpp
|
ulib/utils/defer_unittest.cpp
|
||||||
)
|
)
|
||||||
target_link_libraries(ulib_test PRIVATE
|
target_link_libraries(ulib_test PRIVATE
|
||||||
|
Loading…
Reference in New Issue
Block a user