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.

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 wheth er 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.
 

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