xtd 1.0.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 "character.hpp"
7#include "enum_object.hpp"
8#include "icomparable.hpp"
9#include "iequatable.hpp"
10#include "integer.hpp"
11#include "is.hpp"
12#include "object.hpp"
13
15namespace xtd {
30 class any_object : public object, public icomparable<any_object>, public iequatable<any_object> {
31 public:
33
36 any_object() noexcept = default;
39 template<typename type_t> // Can't be explicit by design.
40 any_object(type_t&& value) noexcept : value_(boxing_ptr(std::forward<type_t>(value))) {}
43 template<typename type_t> // Can't be explicit by design.
44 any_object(type_t& value) noexcept : value_(boxing_ptr(value)) {}
47 template<typename type_t> // Can't be explicit by design.
48 any_object(const type_t& value) noexcept : value_(boxing_ptr(value)) {}
51 template<xtd::character type_t> // Can't be explicit by design.
52 any_object(const type_t* value) noexcept : value_(boxing_ptr(value)) {}
55 template<xtd::character type_t> // Can't be explicit by design.
56 any_object(type_t* value) noexcept : value_(boxing_ptr(value)) {}
58
60 any_object(any_object&&) noexcept = default;
61 any_object(any_object&) noexcept = default;
62 any_object(const any_object&) noexcept = default;
64
66
71 [[nodiscard]] auto has_value() const noexcept -> bool;
72
76 [[nodiscard]] auto value() const -> const object&;
78
80
92 [[nodiscard]] auto compare_to(const any_object& other) const noexcept -> int32 override;
93
97 [[nodiscard]] auto equals(const object& other) const noexcept -> bool override;
101 [[nodiscard]] auto equals(const any_object& other) const noexcept -> bool override;
102
105 [[nodiscard]] auto get_hash_code() const noexcept -> xtd::usize override;
106
109 [[nodiscard]] auto to_string() const noexcept -> xtd::string override;
110
113 auto reset() noexcept -> void;
115
117
122 auto operator =(const any_object& other) noexcept -> any_object& = default;
126 auto operator =(any_object&& other) noexcept -> any_object&;
128
129 private:
130 template<typename type_t>
131 [[nodiscard]] auto boxing_ptr(const type_t& value) noexcept -> ptr<object> {return new_ptr<typename __box_enum_or_object__<type_t, typename std::is_enum<type_t>::type>::type>(value);}
132 template<typename type_t, typename ...args_t>
133 [[nodiscard]] auto boxing_ptr(args_t&& ...args) noexcept -> ptr<object> {return new box<type_t>(args...);}
134 //[[nodiscard]] auto boxing_ptr(const object& value) noexcept -> ptr<object>;
135 [[nodiscard]] auto boxing_ptr(const char* value) noexcept -> ptr<object>;
136 [[nodiscard]] auto boxing_ptr(const char8* value) noexcept -> ptr<object>;
137 [[nodiscard]] auto boxing_ptr(const char16* value) noexcept -> ptr<object>;
138 [[nodiscard]] auto boxing_ptr(const char32* value) noexcept -> ptr<object>;
139 [[nodiscard]] auto boxing_ptr(const wchar* value) noexcept -> ptr<object>;
140 [[nodiscard]] auto boxing_ptr(const char& value) noexcept -> ptr<object>;
141 [[nodiscard]] auto boxing_ptr(const char8& value) noexcept -> ptr<object>;
142 [[nodiscard]] auto boxing_ptr(const char16& value) noexcept -> ptr<object>;
143 [[nodiscard]] auto boxing_ptr(const char32& value) noexcept -> ptr<object>;
144 [[nodiscard]] auto boxing_ptr(const wchar& value) noexcept -> ptr<object>;
145 [[nodiscard]] auto boxing_ptr(char& value) noexcept -> ptr<object>;
146 [[nodiscard]] auto boxing_ptr(char8& value) noexcept -> ptr<object>;
147 [[nodiscard]] auto boxing_ptr(char16& value) noexcept -> ptr<object>;
148 [[nodiscard]] auto boxing_ptr(char32& value) noexcept -> ptr<object>;
149 [[nodiscard]] auto boxing_ptr(wchar& value) noexcept -> ptr<object>;
150 [[nodiscard]] auto boxing_ptr(const xtd::byte& value) noexcept -> ptr<object>;
151 [[nodiscard]] auto boxing_ptr(const int16& value) noexcept -> ptr<object>;
152 [[nodiscard]] auto boxing_ptr(const int32& value) noexcept -> ptr<object>;
153 [[nodiscard]] auto boxing_ptr(const int64& value) noexcept -> ptr<object>;
154 [[nodiscard]] auto boxing_ptr(const slong& value) noexcept -> ptr<object>;
155 [[nodiscard]] auto boxing_ptr(const sbyte& value) noexcept -> ptr<object>;
156 [[nodiscard]] auto boxing_ptr(const uint16& value) noexcept -> ptr<object>;
157 [[nodiscard]] auto boxing_ptr(const uint32& value) noexcept -> ptr<object>;
158 [[nodiscard]] auto boxing_ptr(const uint64& value) noexcept -> ptr<object>;
159 [[nodiscard]] auto boxing_ptr(const xtd::ulong& value) noexcept -> ptr<object>;
160 [[nodiscard]] auto boxing_ptr(xtd::byte& value) noexcept -> ptr<object>;
161 [[nodiscard]] auto boxing_ptr(int16& value) noexcept -> ptr<object>;
162 [[nodiscard]] auto boxing_ptr(int32& value) noexcept -> ptr<object>;
163 [[nodiscard]] auto boxing_ptr(int64& value) noexcept -> ptr<object>;
164 [[nodiscard]] auto boxing_ptr(slong& value) noexcept -> ptr<object>;
165 [[nodiscard]] auto boxing_ptr(sbyte& value) noexcept -> ptr<object>;
166 [[nodiscard]] auto boxing_ptr(uint16& value) noexcept -> ptr<object>;
167 [[nodiscard]] auto boxing_ptr(uint32& value) noexcept -> ptr<object>;
168 [[nodiscard]] auto boxing_ptr(uint64& value) noexcept -> ptr<object>;
169 [[nodiscard]] auto boxing_ptr(xtd::ulong& value) noexcept -> ptr<object>;
170 [[nodiscard]] auto boxing_ptr(const float& value) noexcept -> ptr<object>;
171 [[nodiscard]] auto boxing_ptr(const double& value) noexcept -> ptr<object>;
172 [[nodiscard]] auto boxing_ptr(const decimal& value) noexcept -> ptr<object>;
173 [[nodiscard]] auto boxing_ptr(float& value) noexcept -> ptr<object>;
174 [[nodiscard]] auto boxing_ptr(double& value) noexcept -> ptr<object>;
175 [[nodiscard]] auto boxing_ptr(decimal& value) noexcept -> ptr<object>;
176 [[nodiscard]] auto boxing_ptr(std::nullptr_t value) noexcept -> ptr<object>;
177
178 ptr<object> value_;
179 };
180
182 template<typename type_t, typename bool_t>
183 struct __is_enum_any_object__ {};
184
185 template<typename type_t>
186 struct __is_enum_any_object__<type_t, std::true_type> {
187 [[nodiscard]] auto operator()(const any_object& o) const -> bool {return is<enum_object<type_t>>(o.value());}
188 };
189
190 template<typename type_t>
191 struct __is_enum_any_object__<type_t, std::false_type> {
192 [[nodiscard]] auto operator()(const any_object& o) const -> bool {return false;}
193 };
194
195 template<typename type_t, typename bool_t>
196 struct __is_polymorphic_any_object__ {};
197
198 template<typename type_t>
199 struct __is_polymorphic_any_object__<type_t, std::true_type> {
200 [[nodiscard]] auto operator()(const any_object& o) const -> bool {return is<type_t>(o.value());}
201 };
202
203 template<typename type_t>
204 struct __is_polymorphic_any_object__<type_t, std::false_type> {
205 [[nodiscard]] auto operator()(const any_object& o) const -> bool {return __is_enum_any_object__<type_t, typename std::is_enum<type_t>::type> {}(o);}
206 };
207
208 template<typename type_t>
209 requires (!std::is_null_pointer_v<type_t> /*&& !xtd::integer<type_t>*/)
210 [[nodiscard]] auto is(any_object& o) -> bool {
211 if (!o.has_value()) return false;
212 if (is<box<type_t>>(o.value())) return true;
213 return __is_polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type > {}(o);
214 }
215
216 template<typename type_t>
217 requires (!std::is_null_pointer_v<type_t> /*&& !xtd::integer<type_t>*/)
218 [[nodiscard]] auto is(const any_object& o) -> bool {
219 if (!o.has_value()) return false;
220 if (is<box<type_t>>(o.value())) return true;
221 return __is_polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type > {}(o);
222 }
223 /*
224 template<typename type_t>
225 requires (!std::is_null_pointer_v<type_t> && xtd::integer<type_t>)
226 [[nodiscard]] auto is(any_object& o) -> bool {
227 if (!o.has_value()) return false;
228 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);
229 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);
230 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);
231 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);
232 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);
233 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);
234 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);
235 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);
236 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);
237 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);
238 return false;
239 }
240
241 template<typename type_t>
242 requires (!std::is_null_pointer_v<type_t> && xtd::integer<type_t>)
243 [[nodiscard]] auto is(const any_object& o) -> bool {
244 if (!o.has_value()) return false;
245 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);
246 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);
247 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);
248 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);
249 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);
250 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);
251 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);
252 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);
253 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);
254 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);
255 return false;
256 }*/
257
258 template<typename type_t>
259 requires std::is_null_pointer_v<type_t>
260 [[nodiscard]] auto is(any_object& o) -> bool {
261 return !o.has_value();
262 }
263
264 template<typename type_t>
265 requires std::is_null_pointer_v<type_t>
266 [[nodiscard]] auto is(const any_object& o) -> bool {
267 return !o.has_value();
268 }
269
270 template<typename type_t>
271 [[nodiscard]] auto is(any_object* o) -> bool {
272 if (is<box<type_t>>(o->value())) return true;
273 return __is_polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type > {}(*o);
274 }
275
276 template<typename type_t>
277 [[nodiscard]] auto is(const any_object* o) -> bool {
278 if (is<box<type_t>>(o->value())) return true;
279 return __is_polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type > {}(*o);
280 }
282}
Contains xtd::boxing methods.
Contains xtd::character concept.
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.hpp:30
any_object(const type_t &value) noexcept
Initializes a new instance of the xtd::any_object class with specified value.
Definition any_object.hpp:48
any_object(type_t &value) noexcept
Initializes a new instance of the xtd::any_object class with specified value.
Definition any_object.hpp:44
auto get_hash_code() const noexcept -> xtd::usize override
Serves as a hash function for a particular type.
auto equals(const object &other) const noexcept -> bool override
Determines whether the specified object is equal to the current object.
auto compare_to(const any_object &other) const noexcept -> int32 override
Compares the current instance with another object of the same type.
any_object(type_t *value) noexcept
Initializes a new instance of the xtd::any_object class with specified value.
Definition any_object.hpp:56
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
auto value() const -> const object &
Gets the value of the current xtd::any_object object if it has been assigned a valid underlying value...
any_object() noexcept=default
Initializes a new instance of the xtd::any_object class.
auto has_value() const noexcept -> bool
Gets a value indicating whether the current xtd::any_object object has a valid value of its underlyin...
auto reset() noexcept -> void
Reset the current object. Set the current object to null.
any_object(const type_t *value) noexcept
Initializes a new instance of the xtd::any_object class with specified value.
Definition any_object.hpp:52
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
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:25
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::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
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
auto is(xtd::any value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:485
auto new_ptr(args_t &&... args) -> xtd::ptr< type_t >
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
@ o
The O key.
Definition console_key.hpp:116
Contains xtd::icomparable interface.
Contains xtd::iequatable interface.
Contains xtd::integer concept.
Contains xtd::is method.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
auto operator()(size_type index) const -> const_reference
Gets the element at the specified zero-based index.
Definition read_only_span.hpp:401
Contains xtd::object class.
Represents a boxed object.
Definition box.hpp:55
box()=default
Initialize a new xtd::box object.