2
0
mirror of https://github.com/boostorg/convert.git synced 2026-02-02 20:52:18 +00:00
Files
convert/doc/html/index.html
2014-02-21 10:30:42 +11:00

210 lines
13 KiB
HTML

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Chapter&#160;1.&#160;The Boost Convert Library 2.0</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.75.2">
<link rel="home" href="index.html" title="Chapter&#160;1.&#160;The Boost Convert Library 2.0">
<link rel="next" href="the_boost_convert_library/getting_started.html" title="Getting Started">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<div class="spirit-nav"><a accesskey="n" href="the_boost_convert_library/getting_started.html"><img src="images/next.png" alt="Next"></a></div>
<div class="chapter">
<div class="titlepage"><div>
<div><h2 class="title">
<a name="convert"></a>Chapter&#160;1.&#160;The Boost Convert Library 2.0</h2></div>
<div><div class="author"><h3 class="author">
<span class="firstname">Vladimir</span> <span class="surname">Batov</span>
</h3></div></div>
<div><p class="copyright">Copyright &#169; 2009-2014 Vladimir Batov</p></div>
<div><div class="legalnotice">
<a name="convert.legal"></a><p>
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at <a href="http://www.boost.org/LICENSE_1_0.txt" target="_top">http://www.boost.org/LICENSE_1_0.txt</a>)
</p>
</div></div>
</div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="section"><a href="index.html#the_boost_convert_library.introduction">Introduction</a></span></dt>
<dt><span class="section"><a href="the_boost_convert_library/getting_started.html">Getting Started</a></span></dt>
<dt><span class="section"><a href="the_boost_convert_library/getting_serious.html">Getting Serious</a></span></dt>
<dt><span class="section"><a href="the_boost_convert_library/convert_algorithms.html">Boost.Convert
with Standard Algorithms</a></span></dt>
<dt><span class="section"><a href="the_boost_convert_library/converters.html">Converters</a></span></dt>
<dt><span class="section"><a href="the_boost_convert_library/__boost__lexical_cast__based_converter.html"><span class="emphasis"><em>boost::lexical_cast</em></span>-Based
Converter</a></span></dt>
<dt><span class="section"><a href="the_boost_convert_library/__std__stringstream__based_converter.html"><span class="emphasis"><em>std::stringstream</em></span>-Based
Converter</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="the_boost_convert_library/__std__stringstream__based_converter.html#the_boost_convert_library.__std__stringstream__based_converter.formatting_support">Formatting
Support</a></span></dt>
<dt><span class="section"><a href="the_boost_convert_library/__std__stringstream__based_converter/locale_support.html">Locale
Support</a></span></dt>
<dt><span class="section"><a href="the_boost_convert_library/__std__stringstream__based_converter/integration_of_user_defined_types.html">Integration
of User-Defined Types</a></span></dt>
<dt><span class="section"><a href="the_boost_convert_library/__std__stringstream__based_converter/performance.html">Performance</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="the_boost_convert_library/type_requirements.html">Type Requirements</a></span></dt>
<dt><span class="section"><a href="the_boost_convert_library/the___default_constructible__type_requirement.html">The
<span class="emphasis"><em>Default Constructible</em></span> Type Requirement</a></span></dt>
<dt><span class="section"><a href="the_boost_convert_library/other_conversions.html">Other Conversions</a></span></dt>
<dt><span class="section"><a href="convert_reference.html">Convert Reference</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="convert_reference.html#header.boost.convert.api_hpp">Header &lt;boost/convert/api.hpp&gt;</a></span></dt>
<dd><dl></dl></dd>
<dt><span class="section"><a href="header/boost/convert/lexical_cast_converter_hpp.html">Header &lt;boost/convert/lexical_cast_converter.hpp&gt;</a></span></dt>
<dd><dl></dl></dd>
<dt><span class="section"><a href="header/boost/convert/safebool_hpp.html">Header &lt;boost/convert/safebool.hpp&gt;</a></span></dt>
<dd><dl></dl></dd>
<dt><span class="section"><a href="header/boost/convert/sstream_converter_hpp.html">Header &lt;boost/convert/sstream_converter.hpp&gt;</a></span></dt>
<dd><dl></dl></dd>
<dt><span class="section"><a href="header/boost/convert/string_sfinae_hpp.html">Header &lt;boost/convert/string_sfinae.hpp&gt;</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="the_boost_convert_library/motivation.html">Motivation</a></span></dt>
<dt><span class="section"><a href="the_boost_convert_library/references.html">References</a></span></dt>
<dt><span class="section"><a href="the_boost_convert_library/convert_acknowledgements.html">Acknowledgements</a></span></dt>
<dt><span class="section"><a href="index/s15.html">Index</a></span></dt>
</dl>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="the_boost_convert_library.introduction"></a><a class="link" href="index.html#the_boost_convert_library.introduction" title="Introduction">Introduction</a>
</h2></div></div></div>
<p>
<span class="bold"><strong>Please note that the described framework is not part
of the Boost library collection even though in this document it is referred
to as <span class="emphasis"><em>Boost.Convert</em></span>. This is done in preparation for potential
submission of the library to <span class="emphasis"><em>Boost</em></span>.</strong></span>
</p>
<p>
<span class="emphasis"><em>Boost.Convert</em></span> builds on the <span class="emphasis"><em>boost::lexical_cast</em></span>
design and experience and takes those conversion/transformation-related ideas
further
</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
to be applicable to a wider range of conversion-related deployment scenarios,
</li>
<li class="listitem">
to provide a more flexible, extensible and configurable type-conversion
framework.
</li>
</ul></div>
<p>
The <span class="emphasis"><em>boost::lexical_cast</em></span> framework essentially consists
of:
</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
an API facade to deploy the relevant functionality uniformly and
</li>
<li class="listitem">
the means to plug a particular type in to the <span class="emphasis"><em>boost::lexical_cast</em></span>
framework; namely, <span class="emphasis"><em>operator&gt;&gt;(std::istream&amp;)</em></span>
and <span class="emphasis"><em>operator&lt;&lt;(std::ostream&amp;)</em></span>).
</li>
</ul></div>
<p>
<span class="emphasis"><em>Boost.Convert</em></span> follows that successful design and also
consists of two components:
</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
the API facade to provide simple, minimal and uniform deployment interface;
</li>
<li class="listitem">
the extendible library of pluggable converters.
</li>
</ul></div>
<p>
The API facade implements new and familiar conversion/transformation-related
behavior such as:
</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
flexible throwing, delayed-throwing and non-throwing failed-conversion
behavior;
</li>
<li class="listitem">
support for the fallback value to be returned when conversion/transformation
fails;
</li>
<li class="listitem">
two types of the conversion-failure check - quick/simple and generic;
</li>
<li class="listitem">
means to deploy types that do no meet the <span class="emphasis"><em>Default Constructibility</em></span>
requirement;
</li>
<li class="listitem">
support for standard algorithms.
</li>
</ul></div>
<p>
The library of pluggable converters is independent of the <span class="emphasis"><em>Boost.Convert</em></span>
API facade and is designed to be extended over time. With the current proposal
two proof-of-the-concept converters are supplied -- string-to-type (and type-to-string)
<span class="emphasis"><em>boost::lexical_cast</em></span>-based and <span class="emphasis"><em>std::stringstream</em></span>-based
converters.
</p>
<p>
The <span class="emphasis"><em>boost::lexical_cast</em></span>-based converter demonstrates how
existing and independent conversion facilities might be incorporated in to
the <span class="emphasis"><em>Boost.Convert</em></span> framework.
</p>
<p>
The <span class="emphasis"><em>std::stringstream</em></span>-based converter draws on the standard
<span class="emphasis"><em>std::streams</em></span> functionality and provides:
</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem">
string-to-type and type-to-string conversions;
</li>
<li class="listitem">
formatting support based on standard manipulators (like <span class="emphasis"><em>std::hex</em></span>,
<span class="emphasis"><em>std::scientific</em></span>, etc.);
</li>
<li class="listitem">
support for different locales;
</li>
<li class="listitem">
<span class="emphasis"><em>char</em></span> and <span class="emphasis"><em>wchar_t</em></span> support.
</li>
</ul></div>
<p>
For example, the following snippet converts an array of integers from their
textual hexadecimal represention and assigns INT_MAX to those which fail to
convert:
</p>
<pre class="programlisting"><span class="identifier">boost</span><span class="special">::</span><span class="identifier">array</span><span class="special">&lt;</span><span class="keyword">char</span> <span class="keyword">const</span><span class="special">*,</span> <span class="number">5</span><span class="special">&gt;</span> <span class="identifier">strings</span> <span class="special">=</span> <span class="special">{{</span> <span class="string">"0XF"</span><span class="special">,</span> <span class="string">"0X10"</span><span class="special">,</span> <span class="string">"0X11"</span><span class="special">,</span> <span class="string">"0X12"</span><span class="special">,</span> <span class="string">"not int"</span> <span class="special">}};</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">vector</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;</span> <span class="identifier">integers</span><span class="special">;</span>
<span class="identifier">boost</span><span class="special">::</span><span class="identifier">cstringstream_converter</span> <span class="identifier">cnv</span><span class="special">;</span> <span class="comment">// stringstream-based char converter</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">transform</span><span class="special">(</span>
<span class="identifier">strings</span><span class="special">.</span><span class="identifier">begin</span><span class="special">(),</span>
<span class="identifier">strings</span><span class="special">.</span><span class="identifier">end</span><span class="special">(),</span>
<span class="identifier">std</span><span class="special">::</span><span class="identifier">back_inserter</span><span class="special">(</span><span class="identifier">integers</span><span class="special">),</span>
<span class="identifier">convert</span><span class="special">&lt;</span><span class="keyword">int</span><span class="special">&gt;::</span><span class="identifier">from</span><span class="special">&lt;</span><span class="identifier">string</span><span class="special">&gt;(</span><span class="identifier">ccnv</span><span class="special">(</span><span class="identifier">std</span><span class="special">::</span><span class="identifier">hex</span><span class="special">)).</span><span class="identifier">value_or</span><span class="special">(</span><span class="identifier">INT_MAX</span><span class="special">));</span>
</pre>
<p>
An interesting (yet to be explored) feature is that <span class="emphasis"><em>Boost.Convert</em></span>
is not limited to string-to-type and type-to-string conversions. In fact, the
<span class="emphasis"><em>Boost.Convert</em></span> API facade is type-agnostic. The plugged-in
converter ultimately dictates what types and conversions are supported. Consequently,
a wide range of conversion/transformation-related tasks can be addressed and
deployed <span class="emphasis"><em>uniformly</em></span> by plugging-in special-purpose converters.
For example, encryption by a string-to-string encrypting converter, etc.
</p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"><p><small>Last revised: February 20, 2014 at 23:29:17 GMT</small></p></td>
<td align="right"><div class="copyright-footer"></div></td>
</tr></table>
<hr>
<div class="spirit-nav"><a accesskey="n" href="the_boost_convert_library/getting_started.html"><img src="images/next.png" alt="Next"></a></div>
</body>
</html>