diff --git a/CMakeLists.txt b/CMakeLists.txt index 170222d..5becad9 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -99,6 +99,7 @@ if(SLED_BUILD_TESTS) ) FetchContent_MakeAvailable(googletest) add_executable(sled_tests + src/any_test.cc src/filesystem/path_test.cc src/strings/base64_test.cc src/cleanup_test.cc diff --git a/include/sled/any.h b/include/sled/any.h index 1eabe1d..cfe3aee 100644 --- a/include/sled/any.h +++ b/include/sled/any.h @@ -332,6 +332,92 @@ any_cast(any &&operand) return any_cast(operand); } +class Any final { +public: + inline Any() {} + + inline Any(const Any &other) : value_(other.value_) {} + + inline Any(Any &&other) noexcept : value_(std::move(other.value_)) {} + + template + inline Any(const ValueType &value) : value_(value) + {} + + template + inline Any(ValueType &&value) : value_(std::move(value)) + {} + + // ~Any() noexcept {} + + Any &operator=(const Any &rhs) + { + Any(rhs).swap(*this); + return *this; + } + + Any &operator=(Any &&rhs) noexcept + { + rhs.swap(*this); + Any().swap(rhs); + return *this; + } + + template + Any &operator=(ValueType &&rhs) + { + Any(static_cast(rhs)).swap(*this); + return *this; + } + + template + inline auto Cast() const -> ValueType + { + return any_cast(value_); + } + + template + inline auto Cast() -> ValueType + { + return any_cast(value_); + } + + template + inline auto CastOr(U &&default_value) const -> ValueType + { + try { + return any_cast(value_); + } catch (const bad_any_cast &e) { + return static_cast(default_value); + } + } + + template + inline auto CastOr(U &&default_value) -> ValueType + { + try { + return any_cast(value_); + } catch (const bad_any_cast &e) { + return static_cast(default_value); + } + } + + void Reset() noexcept { Any().swap(*this); } + + bool HasValue() const noexcept { return value_.has_value(); } + + const std::type_info &Type() const noexcept { return value_.type(); } + + Any &swap(Any &rhs) noexcept + { + std::swap(value_, rhs.value_); + return *this; + } + +private: + any value_; +}; + }// namespace sled #endif /* SLED_ANY_H */ diff --git a/src/any_test.cc b/src/any_test.cc new file mode 100644 index 0000000..1bbbc44 --- /dev/null +++ b/src/any_test.cc @@ -0,0 +1,4 @@ +#include +#include + +TEST(Any, Assign) {}