xtd 0.2.0
Loading...
Searching...
No Matches
any.cpp

Shows how to use 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)));
a = day_of_week::wednesday;
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
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
@ a
The A key.
@ f
The F key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10