40 lines
1.8 KiB
Plaintext
40 lines
1.8 KiB
Plaintext
[/
|
|
Copyright (c) Vladimir Batov 2009-2014
|
|
Distributed under the Boost Software License, Version 1.0.
|
|
See copy at http://www.boost.org/LICENSE_1_0.txt.
|
|
]
|
|
|
|
[section Converters]
|
|
|
|
[import ../test/callable.cpp]
|
|
|
|
`boost::convert()` API plays its role by providing a ['uniform interface] and ensuring ['consistent behavior]. However, it is the respective converter which does the hard work of turning custom-types into strings and strings into custom-types, etc., i.e. the actual type conversion\/transformation.
|
|
|
|
For a converter to be plugged in to the ['Boost.Convert] framework it needs to be a ['callable] with the following signature:
|
|
|
|
template<typename TypeOut, typename TypeIn>
|
|
void operator()(TypeIn const& value_in, boost::optional<TypeOut>& result_out) const;
|
|
|
|
if that is a general-purpose converter capable of handling many types (like string-to-type and type-to-string conversions). Alternatively, a purpose-built custom converter might only care to provide
|
|
|
|
void operator()(TypeIn const&, boost::optional<TypeOut>&) const;
|
|
|
|
if its sole purpose is to handle one particular conversion\/transformation of ['TypeIn] to ['TypeOut]. For example, a converter from the operating-system-specific MBCS string format to the UCS-2 or UCS-4 (depending on `wchar_t` size) might be one such example:
|
|
|
|
void operator()(std::string const&, boost::optional<std::wstring>&) const;
|
|
|
|
Alternatively again, an ad-hoc in-place ['callable] might be provided as a converter. For example,
|
|
|
|
[getting_started_using]
|
|
[callable_example3]
|
|
|
|
or an old-fashioned function:
|
|
|
|
[callable_example1]
|
|
[callable_example2]
|
|
|
|
With regard to converters the ['Boost.Convert] framework has been designed with the following requirements in mind:
|
|
|
|
[note Converters shall be independent from and must not rely on the ['Boost.Convert] infrastructure.]
|
|
|