diff --git a/headers/meta.hpp/meta_fwd.hpp b/headers/meta.hpp/meta_fwd.hpp index ab9ac34..f27f9c1 100644 --- a/headers/meta.hpp/meta_fwd.hpp +++ b/headers/meta.hpp/meta_fwd.hpp @@ -8,6 +8,8 @@ #include #include +#include +#include #include #include #include diff --git a/headers/meta.hpp/meta_value.hpp b/headers/meta.hpp/meta_value.hpp index b059626..aa6a910 100644 --- a/headers/meta.hpp/meta_value.hpp +++ b/headers/meta.hpp/meta_value.hpp @@ -18,6 +18,10 @@ namespace meta_hpp value(T&& value) : raw_{std::forward(value)} {} + template < typename T, typename... Args > + value(std::in_place_type_t, Args&&... args) + : raw_{std::in_place_type, std::forward(args)...} {} + template < typename T > auto cast() const { return std::any_cast(raw_); @@ -32,6 +36,25 @@ namespace meta_hpp auto try_cast() const noexcept { return std::any_cast(&raw_); } + public: + bool to_bool() const { return cast(); } + int to_int() const { return cast(); } + float to_float() const { return cast(); } + double to_double() const { return cast(); } + std::string to_string() const { return cast(); } + + std::int8_t to_int8() const { return cast(); } + std::int16_t to_int16() const { return cast(); } + std::int32_t to_int32() const { return cast(); } + std::int64_t to_int64() const { return cast(); } + + std::uint8_t to_uint8() const { return cast(); } + std::uint16_t to_uint16() const { return cast(); } + std::uint32_t to_uint32() const { return cast(); } + std::uint64_t to_uint64() const { return cast(); } + + std::size_t to_size_t() const { return cast(); } + std::ptrdiff_t to_ptrdiff_t() const { return cast(); } private: std::any raw_; }; diff --git a/untests/meta_value_tests.cpp b/untests/meta_value_tests.cpp index f01d424..6e4a5aa 100644 --- a/untests/meta_value_tests.cpp +++ b/untests/meta_value_tests.cpp @@ -13,4 +13,24 @@ namespace TEST_CASE("meta/value") { namespace meta = meta_hpp; + using namespace std::string_literals; + + CHECK(meta::value{true}.to_bool() == true); + CHECK(meta::value{1}.to_int() == 1); + CHECK(meta::value{1.f}.to_float() == 1.f); + CHECK(meta::value{1.0}.to_double() == 1.0); + CHECK(meta::value{"meta"s}.to_string() == "meta"); + + CHECK(meta::value{std::in_place_type, 1}.to_int8() == 1); + CHECK(meta::value{std::in_place_type, 1}.to_int16() == 1); + CHECK(meta::value{std::in_place_type, 1}.to_int32() == 1); + CHECK(meta::value{std::in_place_type, 1}.to_int64() == 1); + + CHECK(meta::value{std::in_place_type, 1}.to_uint8() == 1u); + CHECK(meta::value{std::in_place_type, 1}.to_uint16() == 1u); + CHECK(meta::value{std::in_place_type, 1}.to_uint32() == 1u); + CHECK(meta::value{std::in_place_type, 1}.to_uint64() == 1u); + + CHECK(meta::value{std::in_place_type, 1}.to_size_t() == 1u); + CHECK(meta::value{std::in_place_type, 1}.to_ptrdiff_t() == 1u); }