xtd 0.2.0
__as_any_object.hpp
Go to the documentation of this file.
1
3#pragma once
5#if !defined(__XTD_CORE_INTERNAL__)
6#error "Do not include this file: Internal use only"
7#endif
9
11namespace xtd {
13 template<class type_t, class bool_t>
14 struct __enum_any_object__ {};
15
16 template<class type_t>
17 struct __enum_any_object__<type_t, std::true_type> {
18 type_t operator()(const any_object& o) const {return as<enum_object<type_t>>(o.value());}
19 };
20
21 template<class type_t>
22 struct __enum_any_object__<type_t, std::false_type> {
23 type_t operator()(const any_object& o) const {xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_cast);}
24 };
25
26 template<class type_t, class bool_t>
27 struct __polymorphic_any_object__ {};
28
29 template<class type_t>
30 struct __polymorphic_any_object__<type_t, std::true_type> {
31 type_t operator()(const any_object& o) const {return as<type_t>(o.value());}
32 };
33
34 template<class type_t>
35 struct __polymorphic_any_object__<type_t, std::false_type> {
36 type_t operator()(const any_object& o) const {return __enum_any_object__<type_t, typename std::is_enum<type_t>::type> {}(o);}
37 };
39
58 template<class type_t>
59 type_t as(any_object& o) {
60 if (is<box<type_t>>(o.value())) return as<box<type_t>>(o.value()).value;
61 return __polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type> {}(o);
62 }
63
64 template<class type_t>
65 type_t as(const any_object& o) {
66 if (is<box<type_t>>(o.value())) return as<box<type_t>>(o.value()).value;
67 return __polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type> {}(o);
68 }
69}
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:28
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
Throws an exption with specified exception case.
@ invalid_cast
The cast is not valid.
bool is(std::any value)
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:365
type_t as(any_object &o)
Casts a type into another type.
Definition __as_any_object.hpp:59
@ o
The O key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Represents a boxed object.
Definition box.hpp:56