xtd 0.2.0
Loading...
Searching...
No Matches
any_object.hpp
Go to the documentation of this file.
1
3#pragma once
5#include "boxing.hpp"
6#include "enum_object.hpp"
7#include "icomparable.hpp"
8#include "iequatable.hpp"
10#include "is.hpp"
11#include "object.hpp"
12
14namespace xtd {
29 class any_object : public object, public icomparable<any_object>, public iequatable<any_object> {
30 public:
32
35 any_object() noexcept = default;
38 template<class type_t> // Can't be explicit by design.
39 any_object(type_t&& value) noexcept : value_(boxing_ptr(value)) {}
42 template<class type_t> // Can't be explicit by design.
43 any_object(type_t& value) noexcept : value_(boxing_ptr(value)) {}
46 template<class type_t> // Can't be explicit by design.
47 any_object(const type_t& value) noexcept : value_(boxing_ptr(value)) {}
49
51 any_object(any_object&&) noexcept = default;
52 any_object(any_object&) noexcept = default;
53 any_object(const any_object&) noexcept = default;
55
57
62 bool has_value() const noexcept;
63
67 const object& value() const;
69
71
83 int32 compare_to(const any_object& other) const noexcept override;
84
88 bool equals(const object& other) const noexcept override;
92 bool equals(const any_object& other) const noexcept override;
93
96 xtd::size get_hash_code() const noexcept override;
97
100 string to_string() const noexcept override;
101
104 void reset() noexcept;
106
108
113 any_object& operator =(const any_object& other) noexcept = default;
117 any_object& operator =(any_object&& other) noexcept;
119
120 private:
121 template<class type_t>
123 template<class type_t, class ...args_t>
124 ptr<object> boxing_ptr(args_t&& ...args) noexcept {return new box<type_t>(args...);}
125 //ptr<object> boxing_ptr(const object& value) noexcept;
126 ptr<object> boxing_ptr(const char* value) noexcept;
127 ptr<object> boxing_ptr(const char8* value) noexcept;
128 ptr<object> boxing_ptr(const char16* value) noexcept;
129 ptr<object> boxing_ptr(const char32* value) noexcept;
130 ptr<object> boxing_ptr(const wchar* value) noexcept;
131 ptr<object> boxing_ptr(const char& value) noexcept;
132 ptr<object> boxing_ptr(const char8& value) noexcept;
133 ptr<object> boxing_ptr(const char16& value) noexcept;
134 ptr<object> boxing_ptr(const char32& value) noexcept;
135 ptr<object> boxing_ptr(const wchar& value) noexcept;
136 ptr<object> boxing_ptr(char& value) noexcept;
137 ptr<object> boxing_ptr(char8& value) noexcept;
138 ptr<object> boxing_ptr(char16& value) noexcept;
139 ptr<object> boxing_ptr(char32& value) noexcept;
140 ptr<object> boxing_ptr(wchar& value) noexcept;
141 ptr<object> boxing_ptr(const xtd::byte& value) noexcept;
142 ptr<object> boxing_ptr(const int16& value) noexcept;
143 ptr<object> boxing_ptr(const int32& value) noexcept;
144 ptr<object> boxing_ptr(const int64& value) noexcept;
145 ptr<object> boxing_ptr(const slong& value) noexcept;
146 ptr<object> boxing_ptr(const sbyte& value) noexcept;
147 ptr<object> boxing_ptr(const uint16& value) noexcept;
148 ptr<object> boxing_ptr(const uint32& value) noexcept;
149 ptr<object> boxing_ptr(const uint64& value) noexcept;
150 ptr<object> boxing_ptr(const xtd::ulong& value) noexcept;
151 ptr<object> boxing_ptr(xtd::byte& value) noexcept;
152 ptr<object> boxing_ptr(int16& value) noexcept;
153 ptr<object> boxing_ptr(int32& value) noexcept;
154 ptr<object> boxing_ptr(int64& value) noexcept;
155 ptr<object> boxing_ptr(slong& value) noexcept;
156 ptr<object> boxing_ptr(sbyte& value) noexcept;
157 ptr<object> boxing_ptr(uint16& value) noexcept;
158 ptr<object> boxing_ptr(uint32& value) noexcept;
159 ptr<object> boxing_ptr(uint64& value) noexcept;
160 ptr<object> boxing_ptr(xtd::ulong& value) noexcept;
161 ptr<object> boxing_ptr(const float& value) noexcept;
162 ptr<object> boxing_ptr(const double& value) noexcept;
163 ptr<object> boxing_ptr(const decimal& value) noexcept;
164 ptr<object> boxing_ptr(float& value) noexcept;
165 ptr<object> boxing_ptr(double& value) noexcept;
166 ptr<object> boxing_ptr(decimal& value) noexcept;
167 ptr<object> boxing_ptr(std::nullptr_t value) noexcept;
168
169 ptr<object> value_;
170 };
171
173 template<class type_t, class bool_t>
174 struct __is_enum_any_object__ {};
175
176 template<class type_t>
177 struct __is_enum_any_object__<type_t, std::true_type> {
178 bool operator()(const any_object& o) const {return is<enum_object<type_t>>(o.value());}
179 };
180
181 template<class type_t>
182 struct __is_enum_any_object__<type_t, std::false_type> {
183 bool operator()(const any_object& o) const {return false;}
184 };
185
186 template<class type_t, class bool_t>
187 struct __is_polymorphic_any_object__ {};
188
189 template<class type_t>
190 struct __is_polymorphic_any_object__<type_t, std::true_type> {
191 bool operator()(const any_object& o) const {return is<type_t>(o.value());}
192 };
193
194 template<class type_t>
195 struct __is_polymorphic_any_object__<type_t, std::false_type> {
196 bool operator()(const any_object& o) const {return __is_enum_any_object__<type_t, typename std::is_enum<type_t>::type> {}(o);}
197 };
198
199 template<class type_t>
200 requires (!std::is_null_pointer_v<type_t> && (!std::integral<type_t> || std::same_as<type_t, bool>))
201 bool is(any_object& o) {
202 if (!o.has_value()) return false;
203 if (is<box<type_t>>(o.value())) return true;
204 return __is_polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type > {}(o);
205 }
206
207 template<class type_t>
208 requires (!std::is_null_pointer_v<type_t> && (!std::integral<type_t> || std::same_as<type_t, bool>))
209 bool is(const any_object& o) {
210 if (!o.has_value()) return false;
211 if (is<box<type_t>>(o.value())) return true;
212 return __is_polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type > {}(o);
213 }
214
215 template<class type_t>
216 requires (!std::is_null_pointer_v<type_t> && std::integral<type_t> && !std::same_as<type_t, bool>)
217 bool is(any_object& o) {
218 if (!o.has_value()) return false;
219 if (xtd::is<xtd::box_integer<xtd::byte>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::byte>&>(o.value()).value);
220 if (xtd::is<xtd::box_integer<xtd::int16>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::int16>&>(o.value()).value);
221 if (xtd::is<xtd::box_integer<xtd::int32>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::int32>&>(o.value()).value);
222 if (xtd::is<xtd::box_integer<xtd::int64>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::int64>&>(o.value()).value);
223 if (xtd::is<xtd::box_integer<xtd::sbyte>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::sbyte>&>(o.value()).value);
224 if (xtd::is<xtd::box_integer<xtd::slong>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::slong>&>(o.value()).value);
225 if (xtd::is<xtd::box_integer<xtd::uint16>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::uint16>&>(o.value()).value);
226 if (xtd::is<xtd::box_integer<xtd::uint32>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::uint32>&>(o.value()).value);
227 if (xtd::is<xtd::box_integer<xtd::uint64>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::uint64>&>(o.value()).value);
228 if (xtd::is<xtd::box_integer<xtd::ulong>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::ulong>&>(o.value()).value);
229 return false;
230 }
231
232 template<class type_t>
233 requires (!std::is_null_pointer_v<type_t> && std::integral<type_t> && !std::same_as<type_t, bool>)
234 bool is(const any_object& o) {
235 if (!o.has_value()) return false;
236 if (xtd::is<xtd::box_integer<xtd::byte>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::byte>&>(o.value()).value);
237 if (xtd::is<xtd::box_integer<xtd::int16>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::int16>&>(o.value()).value);
238 if (xtd::is<xtd::box_integer<xtd::int32>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::int32>&>(o.value()).value);
239 if (xtd::is<xtd::box_integer<xtd::int64>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::int64>&>(o.value()).value);
240 if (xtd::is<xtd::box_integer<xtd::sbyte>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::sbyte>&>(o.value()).value);
241 if (xtd::is<xtd::box_integer<xtd::slong>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::slong>&>(o.value()).value);
242 if (xtd::is<xtd::box_integer<xtd::uint16>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::uint16>&>(o.value()).value);
243 if (xtd::is<xtd::box_integer<xtd::uint32>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::uint32>&>(o.value()).value);
244 if (xtd::is<xtd::box_integer<xtd::uint64>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::uint64>&>(o.value()).value);
245 if (xtd::is<xtd::box_integer<xtd::ulong>>(o.value())) return xtd::box_integer<type_t>::is_valid(static_cast<const xtd::box_integer<xtd::ulong>&>(o.value()).value);
246 return false;
247 }
248
249 template<class type_t>
250 requires std::is_null_pointer_v<type_t>
251 bool is(any_object& o) {
252 return !o.has_value();
253 }
254
255 template<class type_t>
256 requires std::is_null_pointer_v<type_t>
257 bool is(const any_object& o) {
258 return !o.has_value();
259 }
260
261 template<class type_t>
262 bool is(any_object* o) {
263 if (is<box<type_t>>(o->value())) return true;
264 return __is_polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type > {}(*o);
265 }
266
267 template<class type_t>
268 bool is(const any_object* o) {
269 if (is<box<type_t>>(o->value())) return true;
270 return __is_polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type > {}(*o);
271 }
273}
Contains xtd::boxing methods.
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:29
any_object(const type_t &value) noexcept
Initializes a new instance of the xtd::any_object class with specified value.
Definition any_object.hpp:47
any_object(type_t &value) noexcept
Initializes a new instance of the xtd::any_object class with specified value.
Definition any_object.hpp:43
string to_string() const noexcept override
Returns a xtd::string that represents the current object.
bool equals(const object &other) const noexcept override
Determines whether the specified object is equal to the current object.
void reset() noexcept
Reset the current object. Set the current object to null.
bool has_value() const noexcept
Gets a value indicating whether the current xtd::any_object object has a valid value of its underlyin...
any_object() noexcept=default
Initializes a new instance of the xtd::any_object class.
int32 compare_to(const any_object &other) const noexcept override
Compares the current instance with another object of the same type.
xtd::size get_hash_code() const noexcept override
Serves as a hash function for a particular type.
const object & value() const
Gets the value of the current xtd::any_object object if it has been assigned a valid underlying value...
static bool is_valid(std::signed_integral auto value) noexcept
Determines whether the specified signed integral value is within the range of type_t.
Definition box_integer.hpp:89
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition icomparable.hpp:22
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:23
object()=default
Create a new instance of the ultimate base class object.
Contains xtd::enum_object struct.
Contains xtd::collections::generic::helpers::comparer struct.
std::type_info type
Stores information about a type.
Definition type.hpp:23
std::int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
std::uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.hpp:23
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
char8_t char8
Represents a 8-bit unicode character.
Definition char8.hpp:26
__slong__ slong
Represents a 32-bit or 64-bit signed integer.
Definition slong.hpp:27
std::int8_t sbyte
Represents a 8-bit signed integer.
Definition sbyte.hpp:23
wchar_t wchar
Represents a wide character.
Definition wchar.hpp:24
__ulong__ ulong
Represents a 32-bit or 64-bit unsigned integer.
Definition ulong.hpp:27
long double decimal
Represents a decimal-precision floating-point number.
Definition decimal.hpp:23
std::uint16_t uint16
Represents a 16-bit unsigned integer.
Definition uint16.hpp:23
char16_t char16
Represents a 16-bit unicode character.
Definition char16.hpp:26
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
std::int16_t int16
Represents a 16-bit signed integer.
Definition int16.hpp:23
std::uint64_t uint64
Represents a 64-bit unsigned integer.
Definition uint64.hpp:23
std::uint8_t byte
Represents a 8-bit unsigned integer.
Definition byte.hpp:23
char32_t char32
Represents a 32-bit unicode character.
Definition char32.hpp:23
ptr< type_t > new_ptr(args_t &&... args)
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
bool is(xtd::any value)
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:484
@ other
The operating system is other.
Definition platform_id.hpp:60
@ o
The O key.
Definition console_key.hpp:116
Contains xtd::icomparable interface.
Contains xtd::iequatable interface.
Contains xtd::invalid_operation_exception exception.
Contains xtd::is method.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::object class.
Represents a boxed object.
Definition box.hpp:56