xtd 0.2.0
Loading...
Searching...
No Matches
any_info.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;}
};
template<>
std::string xtd::to_string(const foo2& value, const std::string& fmt, const std::locale& loc) {return xtd::int32_object {value.value}.to_string(fmt, loc);}
template<typename type_t>
string get_any_info(const type_t& value) {
auto any_value = any_object (value);
return string::format("[value = {} ({}), any = {} ({})]", value, typeof_(value), any_value.value(), typeof_(any_value.value()));
}
auto main() -> int {
console::write_line(get_any_info("one"));
console::write_line(get_any_info(date_time {1971, 1, 5}));
console::write_line(get_any_info(42));
console::write_line(get_any_info(.42));
console::write_line(get_any_info(foo1 {42}));
console::write_line(get_any_info(foo2 {42}));
console::write_line(get_any_info(day_of_week::wednesday));
}
// This code produces the following output :
//
// [value = one (char [4]), any = one (xtd::string)]
// [value = Tue Jan 5 00:00:00 1971 (xtd::date_time), any = Tue Jan 5 00:00:00 1971 (xtd::date_time)]
// [value = 42 (int), any = 42 (xtd::box_integer<int>)]
// [value = 0.42 (double), any = 0.42 (xtd::box_floating_point<double>)]
// [value = 42 (foo1), any = 42 (foo1)]
// [value = 42 (foo2), any = 42 (xtd::box<foo2>)]
// [value = wednesday (xtd::day_of_week), any = wednesday (xtd::enum_object<xtd::day_of_week>)]
Represent a polymorphic wrapper capable of holding any type.
Definition any_object.h:28
Represents a boxed integer object.
Definition box_integer.h:52
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
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
@ f
The F key.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.h:10