A class used to get the string representation of an object or of a complex type.
A single function is provided, ToString(), with multiple implementations. How an object is converted depends on its type. Pointers are dereferenced prior to conversion, and containers are recursed into. The output format resembles JSON, but does not fully meet the JSON specifications. The following table lists the various supported types and a sample output.
| Type | Output | Examples |
|---|---|---|
| bool | true or false | true, false |
| numeric | The string representation of the number, as provided by operator<<. | 0, 42.5, -123 |
| char, char32_t | The decimal representation of the character. | 0, 9, 255, 128513 |
| CYIString, std::string or const char* | The string enclosed by quotes. | "foo", "Hello world!", "", null |
| std::list, std::vector, std::deque, std::set, std::multiset | An 'array' representation of the container. Each element is converted using the ToString function. | [42, 12, 64], ["foo"], [] |
| std::map, std::multimap | A key-value representation of the container. Each key and value is converted using the ToString function. | {"foo" : 42, "bar" : 12}, {12 : "bar"}, {} |
| T*, std::shared_ptr, std::unique_ptr, std::weak_ptr | The dereferenced value of the pointer, or null if the pointer is null. | [42], *["foo"], null |
| void | The string representation of a void pointer, or null if the pointer is null. | *[void], null |
| CYIAny | The string representation of the object as returned by CYIAny::ToString, surrounded by angled brackets. | <42>, <"foo">, <(empty)> |
| any type that implements operator<< | The output of the operator<< when used with an std::ostringstream. | foobar, 12-51, >>myClass |
| any other type | Other types cannot be converted and display an error message. | (type has no operator<<) |
The ToString function of this class differs from CYIString::FromValue<T> in two important ways. First, the CYIString::FromValue<T> relies solely on the operator<< function of types (and thus does not recurse into containers and pointers). Second, the CYIString::FromValue<T> function will generate a compilation error if the type of its argument does not implement operator<<. The ToString function of this class is for use with objects of unknown types, and is used mostly for debugging.
Usage example:
#include <utility/YiObjectPrinter.h>
Static Public Member Functions | |
| template<typename TYPE > | |
| static CYIString | ToString (const TYPE &object) |
|
static |
Converts object to a CYIString object. See CYIObjectPrinter for details on how different types are converted.