Chapter 41. String conversions

Index

Using x::value_string with custom classes
Convenience functions
#include <x/value_string.H>
#include <x/locale.H>

x::locale l;

std::string s;

short n;

// ...

s=x::value_string<short>::toString(n, l);

// ...

n=x::value_string<short>::fromString(s.begin(), s.end(), l);

The x::value_string defines the following functions:

std::string toString(value, locale)

toString() takes the instance of the template class parameter and converts it to a std::string using the locale given by the second parameter.

value fromString(string, locale)

fromString() is the opposite, taking a string parameter, and converting it to the value specified by the template class parameter.

For all natural integer and floating types, x::value_string uses standard library functions to effect the conversion. The template parameter may also be a std::string, which results in a fromString() and toString() that are effectively no-ops.

Using x::value_string with custom classes

The x::value_string template class works with x::hms and x::ymd::interval, x::uriimpl, and x::uuid classes. To implement x::value_string for any class, define several member templates, as described below. This is done for the purpose of converting an object into some human readable form. Object serialization template API would be more appropriate for saving and storing objects where readability is not a concern.

// ...

class Widget {

public:

    template<typename OutputIterator>
    OutputIterator toString(OutputIterator iter,
                            const x::const_locale &localeRef)
		const
    {
        //

        return iter;
    }


    template<typename InputIterator>
    static Widget fromString(InputIterator beg_iter,
                              InputIterator end_iter,
                              const const_locale &localeArg)
    {
        Widget w;

        //

        return w;
    }
};

// ...

Widget wInstance;
x::locale l;

std::string s=x::value_string<Winder>::toString(wInstance, l);

The toString() template methods takes an output iterator and a locale object (which they may ignore, if it's irrelevant for this class), writes the string representation of the object into the output iterator, and returns the new iterator value. fromString() must be a static method that takes a pair of iterators. The pair specifies the beginning and the end of a character or a character sequence. The third argument is a locale object. fromString() converts the character sequence to a new object instance, and return it.

fromString() should throw an exception it can't parse the string. Furthermore, the beginning and ending iterator sequence should define the complete parsable sequence, and an exception should get thrown if the object gets converted without consuming the entire input sequence (however, the methods can allow this, if they want to).

Convenience functions

#include <x/tostring.H>

x::locale l;

class Widget;

// ...

Widget w;

// ...

std::string s=x::tostring(w, l);

x::tostring() invokes x::value_string<class>::toString().