xtd 0.2.0
iformatable.cpp

Shows how to use xtd::iformatable interface.

#include <xtd/console>
#include <xtd/environment>
#include <xtd/iformatable>
using namespace xtd;
class foo : public object, public iformatable {
public:
explicit foo(int value) : value_ {value} {}
string to_string(const string& format, const std::locale& loc) const override {return string::format(string::format("{{:{}}}", format), value_);}
private:
int value_ = 0;
};
auto main() -> int {
auto f = foo {42};
console::out << "standard output :" << environment::new_line;
console::out << " " << f.to_string() << environment::new_line;
console::out << string::format(" {}", f) << environment::new_line;
console::out << string::format(" 0b{:b8}", f) << environment::new_line;
console::out << " 0b" << f.to_string("b8", std::locale {}) << environment::new_line;
console::write_line("write_line :");
console::write_line(f.to_string());
console::write_line(" 0b{:b8}", f);
console::write_line(f.to_string("b8", std::locale {}));
}
// This code produces the following output :
//
// standard output :
// 42
// 42
// 42
// 0b00101010
// 0b00101010
//
// write_line :
// 42
// 42
// 42
// 0b00101010
// 0b00101010
static void write(arg_t &&value)
Writes the text representation of the specified value to the standard output stream.
Definition console.hpp:462
static std::ostream out
Gets the standard output stream. A std::basic_ostream<char_t> that represents the standard output str...
Definition console.hpp:52
static void write_line()
Writes the current line terminator to the standard output stream using the specified format informati...
static xtd::string new_line() noexcept
Gets the newline string defined for this environment.
Provides functionality to format the value of an object into a string representation.
Definition iformatable.hpp:35
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
virtual xtd::string to_string() const noexcept
Returns a xtd::string that represents the current object.
xtd::string format(const xtd::string &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
Definition format.hpp:20
@ f
The F key.
Definition console_key.hpp:98
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
string to_string() const noexcept override
Returns the string representation of this xtd::read_only_span <type_t> object.
Definition read_only_span.hpp:375