xtd 0.2.0
Loading...
Searching...
No Matches
any_object.h
Go to the documentation of this file.
1
3#pragma once
5#include "boxing.h"
6#include "icomparable.h"
7#include "iequatable.h"
9#include "is.h"
10#include "object.h"
11
13namespace xtd {
28 class any_object : public object, icomparable<any_object>, iequatable<any_object> {
29 public:
31
34 any_object() noexcept = default;
37 template<typename type_t> // Can't be explicit by design.
38 any_object(type_t&& value) noexcept : value_(boxing_ptr(value)) {}
41 template<typename type_t> // Can't be explicit by design.
42 any_object(type_t& value) noexcept : value_(boxing_ptr(value)) {}
43 template<typename type_t> // Can't be explicit by design.
44 any_object(const type_t& value) noexcept : value_(boxing_ptr(value)) {}
46
48 any_object(any_object&&) noexcept = default;
49 any_object(any_object&) noexcept = default;
50 any_object(const any_object&) noexcept = default;
52
54
59 bool has_value() const noexcept {return value_ != null;}
60
64 const object& value() const {
65 if (value_ == null) throw invalid_operation_exception {csf_};
66 return *value_;
67 }
69
71
73 int32 compare_to(const any_object& other) const noexcept override {
74 if (!has_value() && other.has_value()) return -1;
75 if (has_value() && !other.has_value()) return 1;
76 return equals(other) ? 0 : xtd::collections::generic::helpers::comparer<ptr<object>> {}(value_, other.value_) ? -1 : 1;
77 }
78 bool equals(const object& other) const noexcept override {return dynamic_cast<const any_object*>(&other) && equals(static_cast<const any_object&>(other));}
79 bool equals(const any_object& other) const noexcept override {
80 if (!has_value() && !other.has_value()) return true;
81 if (has_value() != other.has_value()) return false;
82 return value_->equals(*other.value_);
83 }
84 xtd::size get_hash_code() const noexcept override {return has_value() ? value_->get_hash_code() : 0;}
85 string to_string() const noexcept override {return has_value() ? value_->to_string() : "(null)";}
86
89 void reset() noexcept {value_.reset();}
91
93
103 value_ = std::move(other.value_);
104 return *this;
105 };
107
108 private:
109 template<typename type_t>
110 inline ptr<object> boxing_ptr(const type_t& value) noexcept {return new_ptr<typename __box_enum_or_object__<type_t, typename std::is_enum<type_t>::type>::type>(value);}
111 template<typename type_t, typename ...args_t>
112 inline ptr<object> boxing_ptr(args_t&& ...args) noexcept {return new box<type_t>(args...);}
113 //inline ptr<object> boxing_ptr(const object& value) noexcept {return new value;}
114 inline ptr<object> boxing_ptr(const char* value) noexcept {return new_ptr<string>(value);}
115#if defined(__xtd__cpp_lib_char8_t)
116 inline ptr<object> boxing_ptr(const char8* value) noexcept {return new_ptr<string>(value);}
117#endif
118 inline ptr<object> boxing_ptr(const char16* value) noexcept {return new_ptr<string>(value);}
119 inline ptr<object> boxing_ptr(const char32* value) noexcept {return new_ptr<string>(value);}
120 inline ptr<object> boxing_ptr(const wchar* value) noexcept {return new_ptr<string>(value);}
121 inline ptr<object> boxing_ptr(const char& value) noexcept {return new_ptr<char_object>(value);}
122#if defined(__xtd__cpp_lib_char8_t)
123 inline ptr<object> boxing_ptr(const char8& value) noexcept {return new_ptr<char8_object>(value);}
124#endif
125 inline ptr<object> boxing_ptr(const char16& value) noexcept {return new_ptr<char16_object>(value);}
126 inline ptr<object> boxing_ptr(const char32& value) noexcept {return new_ptr<char32_object>(value);}
127 inline ptr<object> boxing_ptr(const wchar& value) noexcept {return new_ptr<wchar_object>(value);}
128 inline ptr<object> boxing_ptr(char& value) noexcept {return new_ptr<char_object>(value);}
129#if defined(__xtd__cpp_lib_char8_t)
130 inline ptr<object> boxing_ptr(char8& value) noexcept {return new_ptr<char8_object>(value);}
131#endif
132 inline ptr<object> boxing_ptr(char16& value) noexcept {return new_ptr<char16_object>(value);}
133 inline ptr<object> boxing_ptr(char32& value) noexcept {return new_ptr<char32_object>(value);}
134 inline ptr<object> boxing_ptr(wchar& value) noexcept {return new_ptr<wchar_object>(value);}
135 inline ptr<object> boxing_ptr(const xtd::byte& value) noexcept {return new_ptr<byte_object>(value);}
136 inline ptr<object> boxing_ptr(const int16& value) noexcept {return new_ptr<int16_object>(value);}
137 inline ptr<object> boxing_ptr(const int32& value) noexcept {return new_ptr<int32_object>(value);}
138 inline ptr<object> boxing_ptr(const int64& value) noexcept {return new_ptr<int64_object>(value);}
139 inline ptr<object> boxing_ptr(const slong& value) noexcept {return new_ptr<slong_object>(value);}
140 inline ptr<object> boxing_ptr(const sbyte& value) noexcept {return new_ptr<sbyte_object>(value);}
141 inline ptr<object> boxing_ptr(const uint16& value) noexcept {return new_ptr<uint16_object>(value);}
142 inline ptr<object> boxing_ptr(const uint32& value) noexcept {return new_ptr<uint32_object>(value);}
143 inline ptr<object> boxing_ptr(const uint64& value) noexcept {return new_ptr<uint64_object>(value);}
144 inline ptr<object> boxing_ptr(const xtd::ulong& value) noexcept {return new_ptr<ulong_object>(value);}
145 inline ptr<object> boxing_ptr(xtd::byte& value) noexcept {return new_ptr<byte_object>(value);}
146 inline ptr<object> boxing_ptr(int16& value) noexcept {return new_ptr<int16_object>(value);}
147 inline ptr<object> boxing_ptr(int32& value) noexcept {return new_ptr<int32_object>(value);}
148 inline ptr<object> boxing_ptr(int64& value) noexcept {return new_ptr<int64_object>(value);}
149 inline ptr<object> boxing_ptr(slong& value) noexcept {return new_ptr<slong_object>(value);}
150 inline ptr<object> boxing_ptr(sbyte& value) noexcept {return new_ptr<sbyte_object>(value);}
151 inline ptr<object> boxing_ptr(uint16& value) noexcept {return new_ptr<uint16_object>(value);}
152 inline ptr<object> boxing_ptr(uint32& value) noexcept {return new_ptr<uint32_object>(value);}
153 inline ptr<object> boxing_ptr(uint64& value) noexcept {return new_ptr<uint64_object>(value);}
154 inline ptr<object> boxing_ptr(xtd::ulong& value) noexcept {return new_ptr<ulong_object>(value);}
155 inline ptr<object> boxing_ptr(const float& value) noexcept {return new_ptr<single_object>(value);}
156 inline ptr<object> boxing_ptr(const double& value) noexcept {return new_ptr<double_object>(value);}
157 inline ptr<object> boxing_ptr(const decimal& value) noexcept {return new_ptr<decimal_object>(value);}
158 inline ptr<object> boxing_ptr(float& value) noexcept {return new_ptr<single_object>(value);}
159 inline ptr<object> boxing_ptr(double& value) noexcept {return new_ptr<double_object>(value);}
160 inline ptr<object> boxing_ptr(decimal& value) noexcept {return new_ptr<decimal_object>(value);}
161 inline ptr<object> boxing_ptr(nullptr_t value) noexcept {return ptr<object> {};}
162
163 ptr<object> value_;
164 };
165
167 template<typename type_t, typename bool_t>
168 struct __is_enum_any_object__ {};
169
170 template<typename type_t>
171 struct __is_enum_any_object__<type_t, std::true_type> {
172 bool operator()(const any_object& o) const {return is<enum_object<type_t>>(o.value());}
173 };
174
175 template<typename type_t>
176 struct __is_enum_any_object__<type_t, std::false_type> {
177 bool operator()(const any_object& o) const {return false;}
178 };
179
180 template<typename type_t, typename bool_t>
181 struct __is_polymorphic_any_object__ {};
182
183 template<typename type_t>
184 struct __is_polymorphic_any_object__<type_t, std::true_type> {
185 bool operator()(const any_object& o) const {return is<type_t>(o.value());}
186 };
187
188 template<typename type_t>
189 struct __is_polymorphic_any_object__<type_t, std::false_type> {
190 bool operator()(const any_object& o) const {return __is_enum_any_object__<type_t, typename std::is_enum<type_t>::type> {}(o);}
191 };
192
193 template<typename type_t>
194 bool is(any_object& o) {
195 if (is<box<type_t>>(o.value())) return true;
196 return __is_polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type> {}(o);
197 }
198
199 template<typename type_t>
200 bool is(const any_object& o) {
201 if (is<box<type_t>>(o.value())) return true;
202 return __is_polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type> {}(o);
203 }
204
205 template<typename type_t>
206 bool is(any_object* o) {
207 if (is<box<type_t>>(o->value())) return true;
208 return __is_polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type> {}(*o);
209 }
210
211 template<typename type_t>
212 bool is(const any_object* o) {
213 if (is<box<type_t>>(o->value())) return true;
214 return __is_polymorphic_any_object__<type_t, typename std::is_polymorphic<type_t>::type> {}(*o);
215 }
217}
Contains xtd::boxing methods.
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.h:28
any_object(type_t &value) noexcept
Initializes a new instance of the xtd::any_object class with specified value.
Definition any_object.h:42
string to_string() const noexcept override
Returns a xtd::string that represents the current object.
Definition any_object.h:85
bool equals(const any_object &other) const noexcept override
Indicates whether the current object is equal to another object of the same type.
Definition any_object.h:79
bool equals(const object &other) const noexcept override
Determines whether the specified object is equal to the current object.
Definition any_object.h:78
void reset() noexcept
Reset the current object. Set the current object to null.
Definition any_object.h:89
bool has_value() const noexcept
Gets a value indicating whether the current xtd::any_object object has a valid value of its underlyin...
Definition any_object.h:59
any_object() noexcept=default
Initializes a new instance of the xtd::any_object class.
any_object & operator=(const any_object &other)=default
Copy assignment operator. Replaces the contents with a copy of the contents of other.
int32 compare_to(const any_object &other) const noexcept override
Compares the current instance with another object of the same type.
Definition any_object.h:73
xtd::size get_hash_code() const noexcept override
Serves as a hash function for a particular type.
Definition any_object.h:84
const object & value() const
Gets the value of the current xtd::any_object object if it has been assigned a valid underlying value...
Definition any_object.h:64
Defines a generalized comparison method that a value type or class implements to create a type-specif...
Definition icomparable.h:21
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.h:22
The exception that is thrown when a method call is invalid for the object's current state.
Definition invalid_operation_exception.h:18
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
#define csf_
Provides information about the current stack frame.
Definition current_stack_frame.h:30
int16_t int16
Represents a 16-bit signed integer.
Definition int16.h:23
char8_t char8
Represents a 8-bit unicode character.
Definition char8.h:27
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.h:27
wchar_t wchar
Represents a wide character.
Definition wchar.h:24
int32_t int32
Represents a 32-bit signed integer.
Definition int32.h:23
long double decimal
Represents a decimal-precision floating-point number.
Definition decimal.h:23
int64_t int64
Represents a 64-bit signed integer.
Definition int64.h:23
__ulong__ ulong
Represents a 32-bit or 64-bit unsigned integer.
Definition ulong.h:27
int8_t sbyte
Represents a 8-bit signed integer.
Definition sbyte.h:23
char16_t char16
Represents a 16-bit unicode character.
Definition char16.h:26
uint32_t uint32
Represents a 32-bit unsigned integer.
Definition uint32.h:23
std::nullptr_t null
Represents a null pointer value.
size_t size
Represents a size of any object in bytes.
Definition size.h:23
char32_t char32
Represents a 32-bit unicode character.
Definition char32.h:26
uint64_t uint64
Represents a 64-bit unsigned integer.
Definition uint64.h:23
uint8_t byte
Represents a 8-bit unsigned integer.
Definition byte.h:23
uint16_t uint16
Represents a 16-bit unsigned integer.
Definition uint16.h:23
__slong__ slong
Represents a 32-bit or 64-bit signed integer.
Definition slong.h:27
std::type_info type
Stores information about a type.
Definition type.h:23
bool is(std::any value)
Checks if the result of an expression is compatible with a given type.
Definition is.h:365
@ other
The operating system is other.
@ o
The O key.
Contains xtd::collections::generic::helpers::comparer struct.
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 xtd_about_box.h:10
Contains xtd::object class.
Implements a function object for compare data.
Definition comparer.h:36