|
|
@@ -8,6 +8,8 @@ |
|
|
|
#include <cppmp/core/conditionals.h> |
|
|
|
#include <cppmp/misc/generic_predicate.h> |
|
|
|
|
|
|
|
#include <cpprtti/types/class_type.h> |
|
|
|
|
|
|
|
#include "variant.h" |
|
|
|
#include "exception.h" |
|
|
|
#include "../registry.h" |
|
|
@@ -303,7 +305,7 @@ namespace cpprtti |
|
|
|
{ |
|
|
|
check_impl(); |
|
|
|
|
|
|
|
if (_impl->type.id() != get_type_id<cppmp::decay_t<T>>()) |
|
|
|
if (!check_type(_impl->type, get_type_id<cppmp::decay_t<T>>())) |
|
|
|
{ |
|
|
|
throw exception( |
|
|
|
error_code::variant_type_mismatch, |
|
|
@@ -326,7 +328,36 @@ namespace cpprtti |
|
|
|
variant variant::to() const |
|
|
|
{ |
|
|
|
using to_type = cppmp::decay_t<T_to>; |
|
|
|
return to(get_type_id<to_type>()); |
|
|
|
auto * t = type().registry().find<to_type>(); |
|
|
|
if (!t) |
|
|
|
{ |
|
|
|
throw exception( |
|
|
|
error_code::variant_is_empty, |
|
|
|
cppcore::string_builder() |
|
|
|
<< "No known conversion from type '" |
|
|
|
<< type().name() |
|
|
|
<< "' to type '" |
|
|
|
<< get_type_name<to_type>() |
|
|
|
<< "'!"); |
|
|
|
} |
|
|
|
return to(*t); |
|
|
|
} |
|
|
|
|
|
|
|
variant variant::to(cpprtti::type_id id) const |
|
|
|
{ |
|
|
|
auto t = type().registry().find(id); |
|
|
|
if (!t) |
|
|
|
{ |
|
|
|
throw exception( |
|
|
|
error_code::variant_is_empty, |
|
|
|
cppcore::string_builder() |
|
|
|
<< "No known conversion from type '" |
|
|
|
<< type().name() |
|
|
|
<< "' to type id '" |
|
|
|
<< id |
|
|
|
<< "'!"); |
|
|
|
} |
|
|
|
return to(*t); |
|
|
|
} |
|
|
|
|
|
|
|
variant variant::to(const cpprtti::type& t) const |
|
|
@@ -372,4 +403,25 @@ namespace cpprtti |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
bool variant::check_type(const cpprtti::type& t, type_id id) const |
|
|
|
{ |
|
|
|
if (t.id() == id) |
|
|
|
return true; |
|
|
|
|
|
|
|
if (t.rtti_type() == cpprtti::rtti_type::class_type) |
|
|
|
{ |
|
|
|
auto* _ct = dynamic_cast<const cpprtti::class_type*>(&t); |
|
|
|
assert(_ct); |
|
|
|
auto& ct = *_ct; |
|
|
|
|
|
|
|
for (auto& kvp : ct.base_classes()) |
|
|
|
{ |
|
|
|
if (check_type(*kvp, id)) |
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
} |