xtd 0.2.0
Loading...
Searching...
No Matches
xtd::iformatable Class Referenceabstract
Inheritance diagram for xtd::iformatable:
xtd::interface xtd::box< type_t > xtd::date_time xtd::enum_object< enum_t > xtd::guid xtd::time_span xtd::box_integer< type_t > xtd::box_char< type_t > xtd::box_floating_point< type_t >

Definition

Provides functionality to format the value of an object into a string representation.

Namespace
xtd
Library
xtd.core
Remarks
The xtd::iformattable interface converts an object to its string representation based on a format string and a locale.
A format string typically defines the general appearance of an object. For example, the xtd framework supports the following:
* Standard format strings for formatting enumeration values (see [Enumeration Format Strings]()).
* Standard and custom format strings for formatting numeric values (see [Standard Numeric Format Strings]() and [Custom Numeric Format Strings]()).
* Standard and custom format strings for formatting date and time values (see [Standard Date and Time Format Strings]() and [Custom Date and Time Format Strings]()).
* Standard and custom format strings for formatting time intervals (see [Standard time_span Format Strings]() and [Custom TimeSpan Format Strings]()).
You can also define your own format strings to support formatting of your application-defined types.
The xtd::iformattable interface defines a single method, xtd::iformatable::to_string, that supplies formatting services for the implementing type. The xtd::iformatable::to_string method can be called directly. In addition, it is called automatically by the xtd::convert.to_string methods, and by methods that use the composite formatting feature in the xtd framework. Such methods include xtd::console::write_line, xtd::string::format, and xtd::text::string_builder::apend_format, among others. The ToString method is called for each format item in the method's format string.
The xtd::iformattable interface is implemented by the base data types.
Notes to Implementers
Classes that require more control over the formatting of strings than xtd::iformattable::to_string() provides should implement xtd::iformattable. A class that implements xtd::iformattable must support the "G" (general) format specifier. Besides the "G" specifier, the class can define the list of format specifiers that it supports. In addition, the class must be prepared to handle a format specifier that is null. For more information about formatting and formatting codes, see [Formatting Type]()s
Examples
The following example 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} {}
using object::to_string;
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 << " 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.h:462
static std::ostream out
Gets the standard output stream. A std::basic_ostream<char_t> that represents the standard output str...
Definition console.h: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.h:35
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.h:42
static basic_string format(const basic_string< char > &fmt, args_t &&... args)
Writes the text representation of the specified arguments list, to string using the specified format ...
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
Examples
format_class_with_specified_formating.cpp, iformatable.cpp, iformatable_vs_to_string.cpp, and sprintf_class_with_specified_formating.cpp.

Public Methods

virtual xtd::string to_string (const xtd::string &format, const std::locale &loc) const =0
 Formats the value of the current instance using the specified format.
 

Member Function Documentation

◆ to_string()

virtual xtd::string xtd::iformatable::to_string ( const xtd::string format,
const std::locale &  loc 
) const
pure virtual

Formats the value of the current instance using the specified format.

Parameters
formatThe format to use
-or-
A xtd::string::empty_string ("") to use the default format defined for the type of the xtd::iformattable implementation.
locAn std::locale object that contains locale information (see std::locale).
Returns
The value of the current instance in the specified format.

Implemented in xtd::date_time, xtd::guid, xtd::time_span, xtd::box< type_t >, and xtd::enum_object< enum_t >.


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