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 >

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/any_object>
#include <xtd/console>
#include <xtd/date_time>
using namespace xtd;
struct foo1 : public object {
explicit foo1(int v) : value(v) {}
int value = 0;
string to_string() const noexcept override {return string::format("{}", value);}
bool operator <(const foo1& f) const noexcept {return value < f.value;}
};
struct foo2 {
int value = 0;
bool operator ==(const foo2& f) const noexcept {return value == f.value;}
bool operator <(const foo2& f) const noexcept {return value < f.value;}
};
auto main() -> int {
auto a = any_object {};
a = "one";
console::write_line("a = {} => {} ({})", a, as<string>(a), typeof_(as<string>(a)));
a = date_time {1971, 1, 5};
console::write_line("a = {} => {} ({})", a, as<date_time>(a), typeof_(as<date_time>(a)));
a = 42;
console::write_line("a = {} => {} ({})", a, as<int32>(a), typeof_(as<int32>(a)));
a = .42;
console::write_line("a = {} => {} ({})", a, as<double>(a), typeof_(as<double>(a)));
a = foo1 {42};
console::write_line("a = {} => {} ({})", a, as<foo1>(a), typeof_(as<foo1>(a)));
a = foo2 {42};
console::write_line("a = {} => {} ({})", a, "NA", typeof_(as<foo2>(a)));
console::write_line("a = {} => {} ({})", a, as<day_of_week>(a), typeof_(as<day_of_week>(a)));
}
// This code produces the following output :
//
// a = one => one (xtd::string)
// a = Tue Jan 5 00:00:00 1971 => Tue Jan 5 00:00:00 1971 (xtd::date_time)
// a = 42 => 42 (int)
// a = 0.42 => 0.42 (double)
// a = 42 => 42 (foo1)
// a = foo2 => NA (foo2)
// a = wednesday => wednesday (xtd::day_of_week)
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.h:28
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
Represents an instant in time, typically expressed as a date and time of day.
Definition date_time.h:85
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
#define typeof_
Used to obtain the type object of a specified type or object.
Definition typeof.h:45
int32 as< int32 >(std::any value)
Casts a type into another type.
Definition __as_int32.h:36
double as< double >(std::any value)
Casts a type into another type.
Definition __as_double.h:36
string as< string >(std::any value)
Casts a type into another type.
Definition __as_string.h:36
std::string to_string(const value_t &value, const std::string &fmt, const std::locale &loc)
Convert a specified value into a string with specified format and locale.
Definition to_string.h:41
@ a
The A key.
@ f
The F key.
@ v
The V key.
@ wednesday
Indicates wednesday.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10
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.
Examples
any.cpp, any_info.cpp, ienumerable.cpp, ienumerator.cpp, and ilist.cpp.

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
 

Public Properties

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

Public Methods

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

Public Operators

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

Additional Inherited Members

- Public Member Functions inherited from xtd::object
 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::uptr< object_t > memberwise_clone () const
 Creates a shallow copy of the current object.
 
- Static Public Member Functions inherited from xtd::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/3]

xtd::any_object::any_object ( )
defaultnoexcept

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

◆ any_object() [2/3]

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/3]

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.

Member Function Documentation

◆ has_value()

bool xtd::any_object::has_value ( ) const
inlinenoexcept

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()

const object & xtd::any_object::value ( ) const
inline

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()

int32 xtd::any_object::compare_to ( const any_object obj) const
inlineoverridevirtualnoexcept

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]

bool xtd::any_object::equals ( const object obj) const
inlineoverridevirtualnoexcept

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.
Examples
The following code example compares the current instance with another object.
#include <xtd/console>
using namespace xtd;
auto main() -> int {
auto object1 = new_ptr<object>();
auto object2 = new_ptr<object>();
auto object3 = object2;
console::write_line(object1->equals(*object3));
console::write_line(*object1 == *object3);
object3 = object1;
console::write_line(object1->equals(*object3));
console::write_line(*object1 == *object3);
}
// This code produces the following output :
//
// false
// false
// true
// true

Reimplemented from xtd::object.

◆ equals() [2/2]

bool xtd::any_object::equals ( const any_object ) const
inlineoverridevirtualnoexcept

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()

xtd::size xtd::any_object::get_hash_code ( ) const
inlineoverridevirtualnoexcept

Serves as a hash function for a particular type.

Returns
size_t A hash code for the current object.

Reimplemented from xtd::object.

◆ to_string()

string xtd::any_object::to_string ( ) const
inlineoverridevirtualnoexcept

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

Returns
A string that represents the current object.
Examples
The following code example demonstrates what to_string returns.
#include <xtd/xtd>
using namespace xtd;
namespace examples {
namespace object_test {
class object1 : public object {
};
}
}
auto main() -> int {
ptr<object> obj1 = new_ptr<examples::object_test::object1>();
console::write_line(obj1->to_string());
ptr<object> obj2 = new_ptr<date_time>(1971, 1, 5, 23, 5, 0);
console::write_line(obj2->to_string());
ptr<object> obj3 = new_ptr<boolean_object>();
console::write_line(obj3->to_string());
}
// This code produces the following output :
//
// examples::object_test::object1
// Tue Jan 5 23:05:00 1971
// false
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.h:27

Reimplemented from xtd::object.

◆ reset()

void xtd::any_object::reset ( )
inlinenoexcept

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]

any_object & xtd::any_object::operator= ( const any_object other)
default

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]

any_object & xtd::any_object::operator= ( any_object &&  other)
inline

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: