EXT C++ Library - echo

The echo construct is introduced to convert values to strings. The conversion is defined for all built-in types and for some library classes. Output type can be std::string or std::wstring.


Interface:

template <typename C>
class ext::common_echo : public std::basic_string <C> {
public:
    common_echo (const char *, C invalid = '?');
    common_echo (const wchar_t *, C invalid = '?');
    common_echo (const std::basic_string <char> &, C invalid = '?');
    common_echo (const std::basic_string <wchar_t> &, C invalid = '?');

    template <typename T>
    common_echo (const T *);

    common_echo (bool, ext::echo_format = ext::default_format);

    common_echo (char);
    common_echo (wchar_t);

    common_echo (signed char, echo_format = default_format, unsigned int = 0, C=0);
    common_echo (signed short, echo_format = default_format, unsigned int = 0, C=0);
    common_echo (signed int, echo_format = default_format, unsigned int = 0, C=0);
    common_echo (signed long, echo_format = default_format, unsigned int = 0, C=0);
    common_echo (const signed long long &, echo_format = default_format,
                 unsigned int = 0, C = 0);

    common_echo (unsigned char, echo_format = default_format, unsigned int = 0, C=0);
    common_echo (unsigned short, echo_format = default_format, unsigned int = 0, C=0);
    common_echo (unsigned int, echo_format = default_format, unsigned int = 0, C=0);
    common_echo (unsigned long, echo_format = default_format, unsigned int = 0, C=0);
    common_echo (const unsigned long long &, echo_format = default_format,
                 unsigned int = 0, C = 0);

    common_echo (float, ext::echo_format = ext::default_format,
                 unsigned int = 0, unsigned int = 0, C = 0);
    common_echo (const double &, ext::echo_format = ext::default_format,
                 unsigned int = 0, unsigned int = 0, C = 0);
    common_echo (const long double &, ext::echo_format = ext::default_format,
                 unsigned int = 0, unsigned int = 0, C = 0);

    template <typename T>
    common_echo (const std::complex <T> &);

    common_echo (const std::type_info &);
    common_echo (const std::tm &);

    common_echo (const std::exception &);
};

typedef ext::common_echo <char>     ext::echo;
typedef ext::common_echo <wchar_t>  ext::wecho;

Details:

The ext::echo constructs from one parameter of any of supported type (and optional parameters that control the string format) and then behaves like std::basic_string.

Use ext::echo when converting to std::string, use ext::wecho when converting to std::wstring.

Following groups of input types are supported:

Strings

The second parameter invalid specifies a character which is used in place of any character that failed to be converted from input representation to output representation. This may happen for example for an international UNICODE character that is not contained in destination charset when converting from wchar_t to char.

Pointer

Boolean value

Single character

Signed or unsigned number

These conversions may take three following additional parameters:

Floating point numbers

These conversions may take four following additional parameters:

Complex number

Other library types

Examples:

#include <ext/c++>

int main () {
    std::printf ("%s\n", ext::echo ((void *) 0).c_str ());
    std::printf ("%s\n", ext::echo (-7).c_str ());
    std::printf ("%s\n", ext::echo (123, ext::hexadecimal_format).c_str ());
    std::printf ("%s\n", ext::echo (L"wide-char text").c_str ());

    std::time_t t = std::time (NULL);
    std::printf ("%s\n", ext::echo (*localtime (&t)).c_str ());

    return 0;
};

Remarks

The ext::echo and ext::wecho are implemented as typedefs for template ext::common_echo <T> where T is char or wchar_t respectively.

Note that to achieve full compatibility with std::string and std::wstring, the ext::common_echo is publicly derived from std::basic_string. Although this is bad practice in most cases, to avoid most common problems the ext::common_echo disallows its dynamic allocation.

The ext::common_echo does not introduce any public nor private data members to its std::basic_string base class and all conversions are computed in constructors.


[index]