mirror of
https://github.com/BlackMATov/meta.hpp.git
synced 2025-12-14 11:40:35 +07:00
add value helper-functions
This commit is contained in:
@@ -8,6 +8,8 @@
|
||||
|
||||
#include <any>
|
||||
#include <array>
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
#include <functional>
|
||||
#include <map>
|
||||
#include <optional>
|
||||
|
||||
@@ -18,6 +18,10 @@ namespace meta_hpp
|
||||
value(T&& value)
|
||||
: raw_{std::forward<T>(value)} {}
|
||||
|
||||
template < typename T, typename... Args >
|
||||
value(std::in_place_type_t<T>, Args&&... args)
|
||||
: raw_{std::in_place_type<T>, std::forward<Args>(args)...} {}
|
||||
|
||||
template < typename T >
|
||||
auto cast() const {
|
||||
return std::any_cast<T>(raw_);
|
||||
@@ -32,6 +36,25 @@ namespace meta_hpp
|
||||
auto try_cast() const noexcept {
|
||||
return std::any_cast<T>(&raw_);
|
||||
}
|
||||
public:
|
||||
bool to_bool() const { return cast<bool>(); }
|
||||
int to_int() const { return cast<int>(); }
|
||||
float to_float() const { return cast<float>(); }
|
||||
double to_double() const { return cast<double>(); }
|
||||
std::string to_string() const { return cast<std::string>(); }
|
||||
|
||||
std::int8_t to_int8() const { return cast<std::int8_t>(); }
|
||||
std::int16_t to_int16() const { return cast<std::int16_t>(); }
|
||||
std::int32_t to_int32() const { return cast<std::int32_t>(); }
|
||||
std::int64_t to_int64() const { return cast<std::int64_t>(); }
|
||||
|
||||
std::uint8_t to_uint8() const { return cast<std::uint8_t>(); }
|
||||
std::uint16_t to_uint16() const { return cast<std::uint16_t>(); }
|
||||
std::uint32_t to_uint32() const { return cast<std::uint32_t>(); }
|
||||
std::uint64_t to_uint64() const { return cast<std::uint64_t>(); }
|
||||
|
||||
std::size_t to_size_t() const { return cast<std::size_t>(); }
|
||||
std::ptrdiff_t to_ptrdiff_t() const { return cast<std::ptrdiff_t>(); }
|
||||
private:
|
||||
std::any raw_;
|
||||
};
|
||||
|
||||
@@ -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<std::int8_t>, 1}.to_int8() == 1);
|
||||
CHECK(meta::value{std::in_place_type<std::int16_t>, 1}.to_int16() == 1);
|
||||
CHECK(meta::value{std::in_place_type<std::int32_t>, 1}.to_int32() == 1);
|
||||
CHECK(meta::value{std::in_place_type<std::int64_t>, 1}.to_int64() == 1);
|
||||
|
||||
CHECK(meta::value{std::in_place_type<std::uint8_t>, 1}.to_uint8() == 1u);
|
||||
CHECK(meta::value{std::in_place_type<std::uint16_t>, 1}.to_uint16() == 1u);
|
||||
CHECK(meta::value{std::in_place_type<std::uint32_t>, 1}.to_uint32() == 1u);
|
||||
CHECK(meta::value{std::in_place_type<std::uint64_t>, 1}.to_uint64() == 1u);
|
||||
|
||||
CHECK(meta::value{std::in_place_type<std::size_t>, 1}.to_size_t() == 1u);
|
||||
CHECK(meta::value{std::in_place_type<std::ptrdiff_t>, 1}.to_ptrdiff_t() == 1u);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user