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

Show how to use format xtd::format class with std::any.

#include <xtd/console>
#include <xtd/environment>
#include <xtd/register_any_stringer>
#include <xtd/string>
#include <xtd/unregister_any_stringer>
using namespace xtd;
class character {
public:
character(const string& name, const string& rank) noexcept : name_(name), rank_(rank) {}
const string& name() const noexcept {return name_;}
const string& rank() const noexcept {return rank_;}
string to_string() const noexcept {return name_ + " (" + rank_ + ")";}
private:
string name_;
string rank_;
};
auto main() -> int {
auto value = std::make_any<int>(42);
console::out << string::format("{}", value) << environment::new_line;
value = std::make_any<string>("Star Trek: The Next Generation");
console::out << string::format("{}", value) << environment::new_line;
value = std::make_any<character>("Jean-Luc Picard", "Captain");
console::out << "Before register_any_stringer : " << string::format("{}", value) << environment::new_line;
register_any_stringer<character>([](auto value) {return value.to_string();});
console::out << "After register_any_stringer : " << string::format("{}", value) << environment::new_line;
unregister_any_stringer<character>();
console::out << "After unregister_any_stringer : " << string::format("{}", value) << environment::new_line;
}
// This code produces the following output :
//
// 42
// Star Trek: The Next Generation
// Before register_any_stringer : (unregistered)
// After register_any_stringer : Jean-Luc Picard (Captain)
// After unregister_any_stringer : (unregistered)
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10