xtd 0.2.0
Loading...
Searching...
No Matches
__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 auto operator()(const any_object& o) const -> type_t {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 auto operator()(const any_object& o) const -> type_t {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 auto operator()(const any_object& o) const -> type_t {return as<type_t>(o.value());}
32 };
33
34 template<class type_t>
35 struct __polymorphic_any_object__<type_t, std::false_type> {
36 auto operator()(const any_object& o) const -> type_t {return __enum_any_object__<type_t, typename std::is_enum<type_t>::type> {}(o);}
37 };
39
58 template<class type_t>
59 requires (!std::integral<type_t> || std::same_as<type_t, bool>)
60 [[nodiscard]] inline auto as(any_object& o) -> type_t {
61 if (is<box<type_t>>(o.value())) return as<box<type_t >> (o.value()).value;
62 return __polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type> {}(o);
63 }
64
66 template<class type_t>
67 requires (std::integral<type_t> && !std::same_as<type_t, bool>)
68 [[nodiscard]] inline auto as(any_object& o) -> type_t {
69 if (xtd::is<xtd::box_integer<xtd::byte>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::byte>&>(o.value()).value);
70 if (xtd::is<xtd::box_integer<xtd::int16>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::int16>&>(o.value()).value);
71 if (xtd::is<xtd::box_integer<xtd::int32>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::int32>&>(o.value()).value);
72 if (xtd::is<xtd::box_integer<xtd::int64>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::int64>&>(o.value()).value);
73 if (xtd::is<xtd::box_integer<xtd::sbyte>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::sbyte>&>(o.value()).value);
74 if (xtd::is<xtd::box_integer<xtd::slong>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::slong>&>(o.value()).value);
75 if (xtd::is<xtd::box_integer<xtd::uint16>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::uint16>&>(o.value()).value);
76 if (xtd::is<xtd::box_integer<xtd::uint32>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::uint32>&>(o.value()).value);
77 if (xtd::is<xtd::box_integer<xtd::uint64>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::uint64>&>(o.value()).value);
78 if (xtd::is<xtd::box_integer<xtd::ulong>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::ulong>&>(o.value()).value);
80 }
81
82 template<>
83 [[nodiscard]] inline auto as<xtd::null_ptr>(any_object& o) -> xtd::null_ptr {
85 return xtd::null;
86 }
88
107 template<class type_t>
108 requires (!std::integral<type_t> || std::same_as<type_t, bool>)
109 [[nodiscard]] inline auto as(const any_object& o) -> type_t {
110 if (is<box<type_t>>(o.value())) return as<box<type_t >> (o.value()).value;
111 return __polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type> {}(o);
112 }
113
115 template<class type_t>
116 requires (std::integral<type_t> && !std::same_as<type_t, bool>)
117 [[nodiscard]] inline auto as(const any_object& o) -> type_t {
118 if (xtd::is<xtd::box_integer<xtd::byte>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::byte>&>(o.value()).value);
119 if (xtd::is<xtd::box_integer<xtd::int16>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::int16>&>(o.value()).value);
120 if (xtd::is<xtd::box_integer<xtd::int32>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::int32>&>(o.value()).value);
121 if (xtd::is<xtd::box_integer<xtd::int64>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::int64>&>(o.value()).value);
122 if (xtd::is<xtd::box_integer<xtd::sbyte>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::sbyte>&>(o.value()).value);
123 if (xtd::is<xtd::box_integer<xtd::slong>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::slong>&>(o.value()).value);
124 if (xtd::is<xtd::box_integer<xtd::uint16>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::uint16>&>(o.value()).value);
125 if (xtd::is<xtd::box_integer<xtd::uint32>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::uint32>&>(o.value()).value);
126 if (xtd::is<xtd::box_integer<xtd::uint64>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::uint64>&>(o.value()).value);
127 if (xtd::is<xtd::box_integer<xtd::ulong>>(o.value())) return xtd::as<type_t>(static_cast<const xtd::box_integer<xtd::ulong>&>(o.value()).value);
129 }
130
131 template<>
132 [[nodiscard]] inline auto as<xtd::null_ptr>(const any_object& o) -> xtd::null_ptr {
134 return xtd::null;
135 }
137
156 template<>
157 [[nodiscard]] inline auto as<string>(xtd::any_object& value) -> string {
158 return xtd::convert::to_string(value);
159 }
160
179 template<>
180 [[nodiscard]] inline auto as<string>(const xtd::any_object& value) -> string {
181 return xtd::convert::to_string(value);
182 }
183}
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:29
Represents a boxed integer object.
Definition box_integer.hpp:54
static string to_string(xtd::any value)
Convert xtd::any to string.
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
@ invalid_cast
The cast is not valid.
Definition exception_case.hpp:63
null_ptr null
Represents a null pointer value.
std::nullptr_t null_ptr
Represents the null_opt alias on std::nullptr_t.
Definition null_ptr.hpp:19
auto as(any_object &o) -> type_t
Casts a type into another type.
Definition __as_any_object.hpp:60
bool is(xtd::any value)
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:484
auto as< string >(xtd::any_object &value) -> string
Casts a type into another type.
Definition __as_any_object.hpp:157
@ o
The O key.
Definition console_key.hpp:116
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Represents a boxed object.
Definition box.hpp:56
value_type value
Gets or sets the underlying value.
Definition box.hpp:106