xtd 0.2.0
Loading...
Searching...
No Matches
xtd::any_object Class Reference
Inheritance diagram for xtd::any_object:
xtd::object xtd::icomparable< any_object > xtd::iequatable< any_object > xtd::interface xtd::extensions::comparison_operators< any_object, icomparable< any_object > > xtd::interface xtd::extensions::equality_operators< any_object, iequatable< any_object > >

Definition

Represent a polymorphic wrapper capable of holding any type.

Header
#include <xtd/any_object>
Namespace
xtd
Library
xtd.core
Examples
The following code example illustrates the use of xtd::any_object class.
#include <xtd/xtd>
struct foo1 {
int value = 0;
};
auto operator <<(std::ostream& os, const foo1& f) noexcept -> std::ostream& {return os << f.value;}
struct foo2 {
int value = 0;
};
template<>
auto xtd::to_string(const foo2& value, const string& fmt, const std::locale& loc) -> string {return int32_object {value.value}.to_string(fmt, loc);}
struct foo3 : public istringable<foo3> {
explicit foo3(int v) : value {v} {}
auto to_string() const noexcept -> string override {return string::format("{}", value);}
int value = 0;
};
auto main() -> int {
auto a = any_object {};
a = "gammasoft";
if (is<string>(a)) console::write_line("a = {} => {} ({})", a, as<string>(a).to_title_case(), type_of(as<string>(a)));
a = date_time {1971, 1, 5};
if (is<date_time>(a)) console::write_line("a = {} => {} ({})", a, as<date_time>(a).to_short_date_string(), type_of(as<date_time>(a)));
a = 42;
if (is<int32>(a)) console::write_line("a = {} => {} ({})", a, as<int32>(a), type_of(as<int32>(a)));
a = .42;
if (is<double>(a)) console::write_line("a = {} => {} ({})", a, as<double>(a), type_of(as<double>(a)));
a = foo1 {42};
if (is<foo1>(a)) console::write_line("a = {} => {} ({})", a, as<foo1>(a), type_of(as<foo1>(a)));
a = foo2 {42};
if (is<foo2>(a)) console::write_line("a = {} => {} ({})", a, as<foo2>(a), type_of(as<foo2>(a)));
a = foo3 {42};
if (is<foo3>(a)) console::write_line("a = {} => {} ({})", a, as<foo3>(a), type_of(as<foo3>(a)));
a = day_of_week::wednesday;
if (is<day_of_week>(a)) console::write_line("a = {} => {} ({})", a, as<day_of_week>(a), type_of(as<day_of_week>(a)));
}
// This code produces the following output :
//
// a = gammasoft => Gammasoft (xtd::string)
// a = 1/5/1971 0:00:00 AM => 1/5/1971 (xtd::date_time)
// a = 42 => 42 (int)
// a = 0.42 => 0.42 (double)
// a = 42 => 42 (foo1)
// a = 42 => 42 (foo2)
// a = 42 => 42 (foo3)
// a = wednesday => wednesday (xtd::day_of_week)
auto value() const -> const object &
Gets the value of the current xtd::any_object object if it has been assigned a valid underlying value...
@ date_time
Write the date and time.
Definition trace_options.hpp:31
@ value
Represnets the constant operator precedence (42).
Definition operator_precedence.hpp:30
box_integer< int32 > int32_object
Represent a boxed int32.
Definition int32_object.hpp:29
@ a
The A key.
Definition console_key.hpp:88
@ f
The F key.
Definition console_key.hpp:98
@ v
The V key.
Definition console_key.hpp:130
string to_string() const noexcept override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:375
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
Remarks
TTo add a class, structure or any other type unknown to xtd, you need to implement the xtd::iequatable and xtd::icompoabale interfaces or overrride the == and < operetors.

Public Constructors

 any_object () noexcept=default
 Initializes a new instance of the xtd::any_object class.
template<typename type_t>
 any_object (type_t &&value) noexcept
 Initializes a new instance of the xtd::any_object class with specified value.
template<typename type_t>
 any_object (type_t &value) noexcept
 Initializes a new instance of the xtd::any_object class with specified value.
template<typename type_t>
 any_object (const type_t &value) noexcept
 Initializes a new instance of the xtd::any_object class with specified value.

Public Properties

auto has_value () const noexcept -> bool
 Gets a value indicating whether the current xtd::any_object object has a valid value of its underlying type.
auto value () const -> const object &
 Gets the value of the current xtd::any_object object if it has been assigned a valid underlying value.

Public Methods

auto compare_to (const any_object &other) const noexcept -> int32 override
 Compares the current instance with another object of the same type.
auto equals (const object &other) const noexcept -> bool override
 Determines whether the specified object is equal to the current object.
auto equals (const any_object &other) const noexcept -> bool override
 Indicates whether the current object is equal to another object of the same type.
auto get_hash_code () const noexcept -> xtd::usize override
 Serves as a hash function for a particular type.
auto to_string () const noexcept -> xtd::string override
 Returns a xtd::string that represents the current object.
auto reset () noexcept -> void
 Reset the current object. Set the current object to null.

Public Operators

auto operator= (const any_object &other) noexcept -> any_object &=default
 Copy assignment operator. Replaces the contents with a copy of the contents of other.
auto operator= (any_object &&other) noexcept -> any_object &
 Move assignment operator. Replaces the contents with a copy of the contents of other.

Additional Inherited Members

 object ()=default
 Create a new instance of the ultimate base class object.
virtual type_object get_type () const noexcept
 Gets the type of the current instance.
template<typename object_t>
xtd::unique_ptr_object< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
template<typename object_a_t, typename object_b_t>
static bool equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are considered equal.
template<typename object_a_t, typename object_b_t>
static bool reference_equals (const object_a_t &object_a, const object_b_t &object_b) noexcept
 Determines whether the specified object instances are the same instance.

Constructor & Destructor Documentation

◆ any_object() [1/4]

xtd::any_object::any_object ( )
defaultnoexcept

Initializes a new instance of the xtd::any_object class.

◆ any_object() [2/4]

template<typename type_t>
xtd::any_object::any_object ( type_t && value)
inlinenoexcept

Initializes a new instance of the xtd::any_object class with specified value.

Parameters
valueThe value to initialize the contained value with.

◆ any_object() [3/4]

template<typename type_t>
xtd::any_object::any_object ( type_t & value)
inlinenoexcept

Initializes a new instance of the xtd::any_object class with specified value.

Parameters
valueThe value to initialize the contained value with.

◆ any_object() [4/4]

template<typename type_t>
xtd::any_object::any_object ( const type_t & value)
inlinenoexcept

Initializes a new instance of the xtd::any_object class with specified value.

Parameters
valueThe value to initialize the contained value with.

Member Function Documentation

◆ has_value()

auto xtd::any_object::has_value ( ) const -> bool
nodiscardnoexcept

Gets a value indicating whether the current xtd::any_object object has a valid value of its underlying type.

Returns
true if the current xtd::any_object object has a value; false if the current xtd::any_object object has no value.
Remarks
If the xtd::any_object::has_value property is true, the value of the current xtd::any_object object can be accessed with the xtd::any_object::value property. Otherwise, attempting to access its value throws an xtd::invalid_operation_exception exception.

◆ value()

auto xtd::any_object::value ( ) const -> const object &
nodiscard

Gets the value of the current xtd::any_object object if it has been assigned a valid underlying value.

Returns
The value of the current xtd::any_object object if the xtd::any_object::has_value property is true. An exception is thrown if the xtd::any_object::has_value property is false.
Exceptions
xtd::invalid_operation_exceptionThe xtd::any_object::has_value property is `false`.

◆ compare_to()

auto xtd::any_object::compare_to ( const any_object & other) const -> int32
nodiscardoverridevirtualnoexcept

Compares the current instance with another object of the same type.

Parameters
objAn object to compare with this instance.
Returns
A 32-bit signed integer that indicates the relative order of the objects being compared. The return value has these meanings:
Value Condition
Less than zero This instance is less than obj.
Zero This instance is equal to obj.
Greater than zero This instance is greater than obj.

Implements xtd::icomparable< any_object >.

◆ equals() [1/2]

auto xtd::any_object::equals ( const object & other) const -> bool
nodiscardoverridevirtualnoexcept

Determines whether the specified object is equal to the current object.

Parameters
objThe object to compare with the current object.
Returns
true if the specified object is equal to the current object. otherwise, false.

Reimplemented from xtd::object.

◆ equals() [2/2]

auto xtd::any_object::equals ( const any_object & other) const -> bool
nodiscardoverridevirtualnoexcept

Indicates whether the current object is equal to another object of the same type.

Parameters
objAn object to compare with this object.
Returns
true if the current object is equal to the other parameter; otherwise, false.

Implements xtd::iequatable< any_object >.

◆ get_hash_code()

auto xtd::any_object::get_hash_code ( ) const -> xtd::usize
nodiscardoverridevirtualnoexcept

Serves as a hash function for a particular type.

Returns
A hash code for the current object.

Reimplemented from xtd::object.

◆ to_string()

auto xtd::any_object::to_string ( ) const -> xtd::string
nodiscardoverridevirtualnoexcept

Returns a xtd::string that represents the current object.

Returns
A string that represents the current object.

Reimplemented from xtd::object.

Examples
generic_ienumerable2.cpp, generic_ienumerable3.cpp, and generic_ienumerator.cpp.

◆ reset()

auto xtd::any_object::reset ( ) -> void
noexcept

Reset the current object. Set the current object to null.

Remarks
If xtd::any_object::has_values is true, destroys the contained object; otherwise does nothing.

◆ operator=() [1/2]

auto xtd::any_object::operator= ( const any_object & other) -> any_object &=default
defaultnoexcept

Copy assignment operator. Replaces the contents with a copy of the contents of other.

Parameters
otherAnother polymorphic wrapper.
Returns
This current instance.

◆ operator=() [2/2]

auto xtd::any_object::operator= ( any_object && other) -> any_object &
noexcept

Move assignment operator. Replaces the contents with a copy of the contents of other.

Parameters
otherAnother polymorphic wrapper.
Returns
This current instance.

The documentation for this class was generated from the following file: