initial commit to Git for GitHub

This commit is contained in:
Robert Ramey
2012-07-13 09:11:40 -08:00
commit 69c3e12e16
86 changed files with 9582 additions and 0 deletions

View File

@@ -0,0 +1,4 @@
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:import href="http://www.boost.org/tools/boostbook/xsl/docbook.xsl"/>
</xsl:stylesheet>

View File

@@ -0,0 +1,60 @@
<?xml version='1.0'?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0">
<xsl:import href="http://www.boost.org/tools/boostbook/xsl/html.xsl"/>
<!-- The root of the Boost directory - not used here! -->
<!-- <xsl:param name="boost.root" select="'file:///c:/BoostRelease/'"/> -->
<!-- use boost directorys for css stylesheets and images, etc - again not used here -->
<!-- <xsl:param name="boost.defaults" select="'Boost'"/> -->
<!-- use stylesheet inside of the html directory -->
<xsl:param name="html.stylesheet" select="'boostbook.css'" />
<!-- use the images inside of the html directory -->
<xsl:param name="boost.graphics.root" select="'images/'" />
<!-- enable spirit type nav bar which is the current boost design -->
<xsl:param name="nav.layout">horizontal</xsl:param>
<!-- directory "libraries" to boost page -->
<xsl:param name="boost.libraries">http://www.boost.org/doc/libs</xsl:param>
<xsl:param name="boost.home_link">file:///index.html</xsl:param>
<xsl:param name="chunk.section.depth" select="2"/>
<!-- remove "Chapter 1" from first page -->
<xsl:param name="chapter.autolabel" select="0"/>
<!-- don't make first sections special - leave TOC in different file -->
<xsl:param name="chunk.first.sections" select="1"></xsl:param>
<!-- leave the html files in the directory ../html -->
<xsl:param name="base.dir" select="'../html/'"/>
<!-- substitute the pre-boost logo for the boost one -->
<xsl:param name = "boost.image.src">pre-boost.jpg</xsl:param>
<xsl:param name = "boost.image.alt">pre-boost</xsl:param>
<xsl:param name = "boost.image.w">30%</xsl:param>
<xsl:param name = "boost.image.h">30%</xsl:param>
<!--
BoostBook takes a section node id like safe_numeric.safe_cast
and renders it as safe_numeric/safe_cast. Presumably they do this
so they can make a huge "book" with all the libraries in subdirectories.
But we want something different. To my mind, this should have been
done using the library "directory" attribute. But of course that
doesn't matter now. We'll just re-hack the path to eliminate
the "safe_numeric/" from the above example.
-->
<xsl:template match="*" mode="recursive-chunk-filename">
<xsl:variable name="their">
<xsl:apply-imports mode="recursive-chunk-filename" select="."/>
</xsl:variable>
<xsl:choose>
<xsl:when test="contains($their, '/')">
<xsl:value-of select="substring-after($their, '/')" />
</xsl:when>
<xsl:otherwise>
<xsl:value-of select="$their"/>
</xsl:otherwise>
</xsl:choose>
</xsl:template>
</xsl:stylesheet>

View File

@@ -0,0 +1,13 @@
if x == x%BOOST_ROOT% goto abort
xsltproc --nonet --xinclude bb2db.xsl safe_numerics.xml | xsltproc --nonet db2html.xsl -
xcopy /i /q /y *.jpg ..\html
if not exist ..\html md ..\html
xcopy /i /q /y %BOOST_ROOT%\doc\src\boostbook.css ..\html
if not exist ..\html md ..\html\images
xcopy /i /q /y /s %BOOST_ROOT%\doc\html\images ..\html\images
exit
:abort
echo BOOST_ROOT not set
exit

View File

@@ -0,0 +1,9 @@
if test x = x$BOOST_ROOT
then
echo BOOST_ROOT not set
exit 1
fi
xsltproc --nonet --xinclude bb2db.xsl safe_numerics.xml | xsltproc --nonet db2html.xsl -
cp pre-boost.jpg ../html
cp $BOOST_ROOT/doc/src/boostbook.css ../html
cp -R $BOOST_ROOT/doc/html/images ../html

View File

@@ -0,0 +1,460 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
<section id="safe_numerics.numeric">
<title>Numeric&lt;T&gt;</title>
<section>
<title>Description</title>
<para>A type is Numeric if it has the properties of a number.</para>
<para>More specifically, a type T is Numeric if there exists
specialization of <code>std::numeric_limits&lt;T&gt;</code>. See the
documentation for standard library class numeric_limits. The standard
library includes such specializations for all the primitive numeric types.
Note that this concept is distinct from the C++ standard library type
traits <code>is_integral</code> and <code>is_arithmetic</code>. These
latter fullfill the requirement of the concept Numeric. But there are
types T which fullfill this concept for which
<code>is_arithmetic&lt;T&gt;::value == false</code>. For example see
<code>safe_signed_integer&lt;int&gt;</code>.</para>
</section>
<section>
<title>Notation</title>
<table colsep="0" frame="all" rowsep="0">
<title>Notation</title>
<tgroup cols="2" colsep="1" rowsep="1">
<colspec align="left"/>
<colspec align="left" colwidth="3*"/>
<tbody>
<row>
<entry><code>T, U, V</code></entry>
<entry>A type that is a model of the Numeric</entry>
</row>
<row>
<entry><code>t, u, v</code></entry>
<entry>An object of type modeling Numeric</entry>
</row>
</tbody>
</tgroup>
</table>
</section>
<section>
<title>Associated Types</title>
<table>
<title>Associated Types</title>
<tgroup cols="2">
<colspec align="left"/>
<colspec align="left" colwidth="3*"/>
<tbody>
<row>
<entry><code>std::numeric_limits&lt;T&gt;</code></entry>
<entry>The numeric_limits class template provides a C++ program
with information about various properties of the implementations
representation of the arithmetic types. See C++ standard
18.3.2.2.</entry>
</row>
</tbody>
</tgroup>
</table>
</section>
<section>
<title>Valid Expressions</title>
<para>In addition to the expressions defined in <ulink
url="http://www.sgi.com/tech/stl/Assignable.html">Assignable</ulink> the
following expressions must be valid.</para>
<para>Any operations which result in integers which cannot be represented
as some Numeric type will throw an exception.<table>
<title>General</title>
<tgroup cols="2">
<colspec align="left"/>
<colspec align="left"/>
<thead>
<row>
<entry align="left">Expression</entry>
<entry>Return Value</entry>
</row>
</thead>
<tbody>
<row>
<entry><code>std::numeric_limits&lt;T&gt;.is_bounded
</code></entry>
<entry>true</entry>
</row>
<row>
<entry><code>std::numeric_limits&lt;T&gt;.is_integer</code></entry>
<entry>true</entry>
</row>
<row>
<entry><code>std::numeric_limits&lt;T&gt;.is_specialized
</code></entry>
<entry>true</entry>
</row>
</tbody>
</tgroup>
</table></para>
<table>
<title>Unary Operators</title>
<tgroup cols="3">
<colspec align="left"/>
<colspec align="left"/>
<colspec align="left" colwidth="3*"/>
<thead>
<row>
<entry align="left">Expression</entry>
<entry>Return Type</entry>
<entry>Semantics</entry>
</row>
</thead>
<tbody>
<row>
<entry><code>-t</code></entry>
<entry>T</entry>
<entry>Invert sign</entry>
</row>
<row>
<entry><code>+t</code></entry>
<entry>T</entry>
<entry>unary plus - a no op</entry>
</row>
<row>
<entry><code>t--</code></entry>
<entry>T</entry>
<entry>post decrement</entry>
</row>
<row>
<entry><code>t++</code></entry>
<entry>T</entry>
<entry>post increment</entry>
</row>
<row>
<entry><code>--t</code></entry>
<entry>T</entry>
<entry>predecrement</entry>
</row>
<row>
<entry><code>++t</code></entry>
<entry>T</entry>
<entry>preincrement</entry>
</row>
<row>
<entry><code>~</code></entry>
<entry>T</entry>
<entry>complement</entry>
</row>
</tbody>
</tgroup>
</table>
<table>
<title>Binary Operators</title>
<tgroup cols="3">
<colspec align="left"/>
<colspec align="left"/>
<colspec align="left" colwidth="3*"/>
<thead>
<row>
<entry align="left">Expression</entry>
<entry>Return Type</entry>
<entry>Semantics</entry>
</row>
</thead>
<tbody>
<row>
<entry><code>t - u</code></entry>
<entry>V</entry>
<entry>Invert sign</entry>
</row>
<row>
<entry><code>t + u</code></entry>
<entry>V</entry>
<entry>unary plus - a no op</entry>
</row>
<row>
<entry><code>t * u</code></entry>
<entry>V</entry>
<entry>multiply t by u</entry>
</row>
<row>
<entry><code>t / u</code></entry>
<entry>T</entry>
<entry>divide t by u</entry>
</row>
<row>
<entry><code>t % u</code></entry>
<entry>T</entry>
<entry>t modulus u</entry>
</row>
<row>
<entry><code>t &lt;&lt; u</code></entry>
<entry>T</entry>
<entry>shift t left u bits</entry>
</row>
<row>
<entry><code>t &gt;&gt; u</code></entry>
<entry>T</entry>
<entry>shift t right by ubits</entry>
</row>
<row>
<entry><code>t &lt; u</code></entry>
<entry>bool</entry>
<entry>true if t less than u, false otherwise</entry>
</row>
<row>
<entry><code>t &lt;= u</code></entry>
<entry>bool</entry>
<entry>true if t less than or equal to u, false otherwise</entry>
</row>
<row>
<entry><code>t &gt; u</code></entry>
<entry>bool</entry>
<entry>true if t greater than u, false otherwise</entry>
</row>
<row>
<entry><code>t &gt;= u</code></entry>
<entry>bool</entry>
<entry>true if t greathan or equal to u, false otherwise</entry>
</row>
<row>
<entry><code>t == u</code></entry>
<entry>bool</entry>
<entry>true if t equal to u, false otherwise</entry>
</row>
<row>
<entry><code>t != u</code></entry>
<entry>bool</entry>
<entry>true if t not equal to u, false otherwise</entry>
</row>
<row>
<entry><code>t &amp; u</code></entry>
<entry>bool</entry>
<entry>and of t and u padded out max # bits in t, u</entry>
</row>
<row>
<entry><code>t | u</code></entry>
<entry>bool</entry>
<entry>or of t and u padded out max # bits in t, u</entry>
</row>
<row>
<entry><code>t ^ u</code></entry>
<entry>V</entry>
<entry>exclusive or of t and u padded out max # bits in t,
u</entry>
</row>
<row>
<entry><code>t = u</code></entry>
<entry><code>T</code></entry>
<entry>assign value of u to t</entry>
</row>
<row>
<entry><code>t += u</code></entry>
<entry><code>T</code></entry>
<entry>add u to t and assign to t</entry>
</row>
<row>
<entry><code>t -= u</code></entry>
<entry><code>T</code></entry>
<entry>subtract u from t and assign to t</entry>
</row>
<row>
<entry><code>t *= u</code></entry>
<entry><code>T</code></entry>
<entry>multiply t by u and assign to t</entry>
</row>
<row>
<entry><code>t /= u</code></entry>
<entry><code>T</code></entry>
<entry>divide t by u and assign to t</entry>
</row>
<row>
<entry><code>t &amp;= u</code></entry>
<entry><code>T</code></entry>
<entry>and t with u and assign to t</entry>
</row>
<row>
<entry><code>t &lt;&lt;= u</code></entry>
<entry>T</entry>
<entry>left shift the value of t by u bits</entry>
</row>
<row>
<entry><code>t &gt;&gt;= u</code></entry>
<entry>T</entry>
<entry>right shift the value of t by u bits</entry>
</row>
<row>
<entry><code>t &amp;= u</code></entry>
<entry>T</entry>
<entry>and the value of t with u and assign to t</entry>
</row>
<row>
<entry><code>t |= u</code></entry>
<entry>T</entry>
<entry>or the value of t with u and assign to t</entry>
</row>
<row>
<entry><code>t ^= u</code></entry>
<entry>T</entry>
<entry>exclusive or the value of t with u and assign to t</entry>
</row>
</tbody>
</tgroup>
</table>
</section>
<section>
<title>Header</title>
<para><ulink url="../../include/concept/numeric.hpp"><code>#include
&lt;safe_numerics/include/concepts/numeric.hpp&gt; </code></ulink></para>
</section>
<section>
<title>Models</title>
<para><code>int, safe_signed_integer&lt;int&gt;,
safe_signed_range&lt;int&gt;, etc.</code></para>
</section>
</section>

View File

@@ -0,0 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
<section id="safe_numerics.rationale.overflow">
<title id="safe_numerics.functions.overflow">overflow</title>
<section>
<title>Synopsis</title>
<para>This function is invoked by the library whenever it is not possible
to produce a result for an arithmetic operation.</para>
<para><programlisting>void overflow(char const * const msg);</programlisting></para>
</section>
<section>
<title>Description</title>
<para>If evironment supports C++ exceptions, this function throws the
exception .</para>
<para>If the environment does not support C++ exceptions, the user should
implement this function and expect it to be called when appropriate.
Otherwise, function is implemented by the library so that it throws the
standard library exception <code>std::out_of_range(msg)</code>.</para>
<para><filename>boost/config.hpp </filename>defines BOOST_NO_EXCEPTIONS
when the environment doesn't support exceptions. It is by checking for the
definition of this macro that the system determines whether or not
exceptions are supported.</para>
</section>
<section>
<title>Header</title>
<para><ulink url="../include/safe_numerics/overflow"><code>#include
&lt;boost/safe_numerics/overflow.hpp&gt; </code></ulink></para>
</section>
<section>
<title>Example of use</title>
<programlisting>#include &lt;cstdio&gt;
void overflow(char const * const msg){
std::fputs("safe_numerics overflow error:, std::stderr);
std::fputs(msg, std::stderr);
std::fputc('\n', std::stderr);
}</programlisting>
</section>
<section>
<title>See Also</title>
<para>See <link
linkend="safe_numerics.rationale.overflow">rationale</link> for more
information on this function</para>
</section>
</section>

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

View File

@@ -0,0 +1,147 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
<section id="safe_numerics.safe">
<title>safe&lt;T&gt;</title>
<section>
<title>Description</title>
<para>A <code>safe&lt;T&gt;</code> can be used anywhere a type T is used.
When T is used in operation which overflows, a exception is thrown</para>
</section>
<section>
<title>Template Parameters</title>
<para/>
<informaltable>
<tgroup cols="3">
<colspec align="left"/>
<colspec align="left" colwidth="3*"/>
<colspec align="left" colwidth="3*"/>
<thead>
<row>
<entry align="left">Parameter</entry>
<entry align="left">Type Requirements</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry><code>T</code></entry>
<entry><link
linkend="safe_numerics.numeric">Numeric</link></entry>
<entry><para>The underlying integer type</para></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
<section>
<title>Model of</title>
<para><link linkend="safe_numerics.numeric">Numeric</link></para>
</section>
<section>
<title>Derived From</title>
<para><code><link
linkend="safe_numerics.safe_signed_range">safe_signed_range&lt;T&gt;</link></code>
(if std::numeric_limits&lt;T&gt;::is_signed == ftrue)</para>
<para><code><link
linkend="safe_numerics.safe_unsigned_range">safe_unsigned_range&lt;T&gt;</link></code>
(if std::numeric_limits&lt;T&gt;::is_signed == ftrue)</para>
</section>
<section>
<title>Notation</title>
<informaltable>
<tgroup cols="2">
<colspec align="left"/>
<colspec align="left" colwidth="6*"/>
<thead>
<row>
<entry align="left">Symbol</entry>
<entry align="left">Description</entry>
</row>
</thead>
<tbody>
<row>
<entry><code>T</code></entry>
<entry>A type that models the <link
linkend="safe_numerics.numeric">Numeric</link> concept</entry>
</row>
<row>
<entry>t</entry>
<entry>An object of type <code>T</code></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
<section>
<title>Header</title>
<para><filename><ulink url="../../include/safe_integer.hpp">#include
&lt;boost/safe_numerics/safe_integer.hpp&gt;</ulink></filename></para>
</section>
<section>
<title>Example of use</title>
<para>The following program will emit an error message on a machine where
int is only 16 bits but run without problem on a machine where int is 32
bits.</para>
<para><programlisting>#include &lt;boost/numeric/safe.hpp&gt;
#include &lt;iostream&gt;
void f(){
safe&lt;int&gt; i = 400;
safe&lt;int&gt; j;
try {
j = i * i;
}
catch(...){
std::cout &lt;&lt; "overflow error" &lt;&lt; std::endl;
}
}
</programlisting></para>
</section>
<section>
<title>Notes</title>
<para>Footnotes (if any) that are referred to by other parts of the
page.</para>
</section>
<section>
<title>See Also</title>
<para>Footnotes (if any) that are referred to by other parts of the
page.</para>
</section>
</section>

View File

@@ -0,0 +1,99 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
<section id="safe_numerics.safe_cast">
<title>safe_cast</title>
<section>
<title>Synopsis</title>
<programlisting>template&lt;class T, class U&gt;
T safe_cast(const U &amp; u);</programlisting>
</section>
<section>
<title>Description</title>
<para>Converts one <ulink url="numeric_concept.xml">Numeric</ulink> type
to another. Throws an <code>std::out_of_range</code> exception if such a
conversion is not possible without changing the value.</para>
</section>
<section>
<title>Type requirements</title>
<informaltable>
<tgroup cols="2">
<colspec align="left"/>
<colspec align="left" colwidth="3*"/>
<thead>
<row>
<entry align="left">Type</entry>
<entry align="left">Requirements</entry>
</row>
</thead>
<tbody>
<row>
<entry><code>T</code></entry>
<entry><ulink url="numeric_concept.xml">Numeric</ulink></entry>
</row>
<row>
<entry><code>U </code></entry>
<entry><ulink url="numeric_concept.xml">Numeric</ulink></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
<section>
<title>Preconditions</title>
<para>The value of u can be represented by the type <code>T</code>. If
this is not true, an <code>std::out_of_range</code> exception will be
thrown.</para>
</section>
<section>
<title>Complexity</title>
<para>[Example:]</para>
<para>O(N log(N)) comparisons (both average and worst-case), where N is
last - first. [2]</para>
</section>
<section>
<title>Header</title>
<para><filename><ulink url="../../include/safe_cast.hpp">#include
&lt;boost/numeric/safe_cast.hpp&gt; </ulink></filename></para>
</section>
<section>
<title>Example of use</title>
<para>[A code fragment that illustrates how to use the function.]</para>
<programlisting>#include &lt;boost/numeric/safe_cast.hpp&gt;
#include &lt;boost/numeric/safe_integer.hpp&gt;
void f(){
safe_integer&lt;char&gt; i;
unsigned char j;
i = 1;
j = safe_cast&lt;unsigned char&gt;(i); // ok
i = -1;
j = safe_cast&lt;unsigned char&gt;(i); // throws std::out_of_range exception
i = 1024;
j = safe_cast&lt;unsigned char&gt;(i); // throws std::out_of_range exception
}</programlisting>
</section>
</section>

View File

@@ -0,0 +1,94 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
<section id="safe_numerics.safe_compare">
<title>safe_compare</title>
<section>
<title>Synopsis</title>
<para>safe_compare is really two functions:.</para>
<programlisting>template&lt;class T, class U&gt;
bool safe_compare::less_than(const T &amp; lhs, const U &amp; rhs);
template&lt;class T, class U&gt;
bool safe_compare::greater_than(const T &amp; lhs, const U &amp; rhs);</programlisting>
</section>
<section>
<title>Description</title>
<para>With normal comparison operators, comparison of unsigned types to
signed types will be done by converting the unsigned type to a signed type
before comparing. Unfortunately this is not always possible. Most C++
compilers will emit an warning message when this is possible but won't
check that an error is made in the conversion. This function guarentees a
correct result regardless of the types of the arguments.</para>
</section>
<section>
<title>Type requirements</title>
<para>[This section lists the requirements that must be satisfied by the
function's template parameters. If the function has no template
parameters, this section can be skipped. Example:]</para>
<informaltable>
<tgroup cols="2">
<colspec align="left"/>
<colspec align="left" colwidth="3*"/>
<thead>
<row>
<entry align="left">Type</entry>
<entry align="left">Requirements</entry>
</row>
</thead>
<tbody>
<row>
<entry><code>T</code></entry>
<entry><link
linkend="safe_numerics.numeric">Numeric</link></entry>
</row>
<row>
<entry><code>U </code></entry>
<entry><link
linkend="safe_numerics.numeric">Numeric</link></entry>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
<section>
<title>Header</title>
<para>[A link to the source code where the function header is
defined.]</para>
<para><ulink url="../../include/safe_compare.hpp"><filename>#include
&lt;boost/numeric/safe_compare.hpp&gt; </filename></ulink></para>
</section>
<section>
<title>Example of use</title>
<programlisting>#include &lt;boost/numeric/safe_compare.hpp&gt;
void f(){
int i = -1;
unsigned char i = 0x129;
unsigned int j = 1;
bool result;
result = j &lt; i; // incorrect result
result = safe_compare::less_than(j &lt; i); // correct result
}</programlisting>
</section>
</section>

View File

@@ -0,0 +1,251 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
<library dirname="safe_numerics" id="safe_numerics" last-revision="$Date"
name="Safe Numerics">
<title>Safe Numerics</title>
<libraryinfo>
<author>
<firstname>Robert</firstname>
<surname>Ramey</surname>
</author>
<copyright>
<year>2012</year>
<holder>Robert Ramey</holder>
</copyright>
<legalnotice>
<para><ulink url="???">Subject to Boost Software License</ulink></para>
</legalnotice>
<librarypurpose>Safe integer operations</librarypurpose>
<librarycategory name="???">Numerics</librarycategory>
</libraryinfo>
<section id="safe_numerics.introduction">
<title>Introduction</title>
<para>Arithmetic operations in C++ are NOT guarenteed to yield a correct
mathematical result. This feature is inherited from the early days of C.
The behavior of <code>int</code>, <code>unsigned int</code> and others
were designed to map closely to the underlying hardware. Computer hardware
implements these types as a fixed number of bits. When the result of
arithmetic operations exceeds this number of bits, the result is undefined
and usually not what the programmer intended. It is incumbent up the C++
programmer to guarentee that this behavior does not result in incorrect
behavior of the program. This library implements special versions of these
data types which behave exactly like the original ones EXCEPT that the
results of these operations are checked to be sure that an exception will
be thrown anytime an attempt is made to store the result of an undefined
operation.</para>
<para>Additionally, we define data types <code>safe_signed_range&lt;MIN,
MAX&gt;</code> and <code>safe_unsigned_range&lt;MIN, MAX&gt;</code> which
will throw an exception if an attempt is made to store a result which is
outside the closed range [MIN, MAX]</para>
</section>
<xi:include href="tutorial.xml" xpointer="element(/1)"
xmlns:xi="http://www.w3.org/2001/XInclude"/>
<section id="safe_numerics.notes">
<title>Notes</title>
<para>This library really an re-implementation the facilities provided by
<ulink url="http://safeint.codeplex.com">David LeBlanc's SafeInt
Library</ulink> using <ulink url="www.boost.org">Boost</ulink>. I found
this library very well done in every way. My main usage was to run unit
tests for my embedded systems projects on my PC. Still, I had a few
issues.</para>
<itemizedlist>
<listitem>
<para>I was a lot of code in one header - 6400 lines. Very unwieldy to
understand and modify.</para>
</listitem>
<listitem>
<para>I couldn't find separate documentation other than that in the
header file.</para>
</listitem>
<listitem>
<para>I didn't use <ulink url="www.boost.org">Boost</ulink>
conventions for naming.</para>
</listitem>
<listitem>
<para>I required porting to different compilers.</para>
</listitem>
<listitem>
<para>It had a very long license associated with it.</para>
</listitem>
<listitem>
<para>The package I downloaded didn't have a test suite</para>
</listitem>
<listitem>
<para>I believe the original <ulink
url="http://safeint.codeplex.com/releases/view/73792">SafeInt</ulink>
library is not easily found. MSVC 10 has this built in so I seems that
they've decided to make it less attractive to use on other
systems.</para>
</listitem>
</itemizedlist>
<para>This version addresses these issues. It exploits <ulink
url="www.boost.org">Boost</ulink> facilities such as template
metaprogramming to reduce the number of lines of source code to
approximately 1500. It exploits the Boost Preprocessor Library to generate
exhaustive tests.</para>
<para>All concepts, types and functions documented are declared in the
namespace <code>boost::numeric</code>.</para>
</section>
<section id="safe_numerics.concepts">
<title>Concepts</title>
<xi:include href="numeric_concept.xml" xpointer="element(/1)"
xmlns:xi="http://www.w3.org/2001/XInclude"/>
</section>
<section id="safe_numerics.types">
<title>Types</title>
<xi:include href="safe_unsigned_range.xml" xpointer="element(/1)"
xmlns:xi="http://www.w3.org/2001/XInclude"/>
<xi:include href="safe_signed_range.xml" xpointer="element(/1)"
xmlns:xi="http://www.w3.org/2001/XInclude"/>
<xi:include href="safe.xml" xpointer="element(/1)"
xmlns:xi="http://www.w3.org/2001/XInclude"/>
</section>
<section id="safe_numerics.functions">
<title>Functions</title>
<xi:include href="safe_cast.xml" xpointer="element(/1)"
xmlns:xi="http://www.w3.org/2001/XInclude"/>
<xi:include href="safe_compare.xml" xpointer="element(/1)"
xmlns:xi="http://www.w3.org/2001/XInclude"/>
<xi:include href="overflow.xml" xpointer="element(/1)"
xmlns:xi="http://www.w3.org/2001/XInclude"/>
</section>
<section id="safe_numerics.rationale">
<title>Rationale</title>
<qandaset defaultlabel="number">
<qandaentry>
<question>
<para>Why does a binary operation on two
<code>safe&lt;int&gt;</code> values not necessarily return another
<code>safe</code> type ?</para>
</question>
<answer>
<para>There are a number of</para>
<itemizedlist>
<listitem>
<para>it was hard to implement.</para>
</listitem>
<listitem>
<para>it doesn't really seem necessary. We always do SOMETHING
with result of the operation. This will result in an assignment
or a conversion to some other type where the result will be
validated.</para>
</listitem>
</itemizedlist>
</answer>
</qandaentry>
<qandaentry>
<question>
<para>Why is there no policy driven design for handling
overflows</para>
<para>The question was - to which type does one apply it to?
Consider the following example:</para>
<para><programlisting>safe&lt;int, overflow_policy_1&gt; t1 = 2;
safe&lt;int, overflow_policy_2&gt; t2 = 4;
unsigned int x = t1 - t2; // which policy should be invoked?</programlisting></para>
</question>
</qandaentry>
<qandaentry>
<question>
<para>Why is Boost.Convert not used.</para>
<para>I couldn't figure out how to use it from the
documentation.</para>
</question>
</qandaentry>
</qandaset>
</section>
<section id="safe_numerics.change_log">
<title>Change Log</title>
<para>This is the initial version.</para>
</section>
<section id="safe_numerics.acknowledgements">
<title>Acknowledgements</title>
<para><ulink url="http://safeint.codeplex.com/releases/view/73792">David
LeBlanc's</ulink> is the author of <ulink
url="http://safeint.codeplex.com/releases/view/73792">SafeInt3</ulink>
library which motivated this work</para>
</section>
<section id="safe_numerics.references">
<title>References</title>
<orderedlist>
<listitem>
<para>David LeBlanc, <ulink url="http://safeint.codeplex.com">Codeplex
SafeInt Page</ulink></para>
</listitem>
<listitem>
<para>Daniel Plakosh, <ulink
url="https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/312-BSI.html">Safe
Integer Operations</ulink></para>
</listitem>
<listitem>
<para>Omer Katz, <ulink
url="http://boost.2283326.n4.nabble.com/SafeInt-code-proposal-td2663669.html">SafeInt
code proposal</ulink>, <ulink
url="https://svn.boost.org/trac/boost/wiki/LibrariesUnderConstruction">Boost.SafeInt</ulink></para>
</listitem>
<listitem>
<para>Software Engineering Institute, Carnegie Mellon, <ulink
url="https://www.securecoding.cert.org/confluence/display/seccode/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow?showComments=false">INT32-C.
Ensure that operations on signed integers do not result in
overflow</ulink></para>
</listitem>
<listitem>
<para>Will Dietz, Peng Li,y John Regehr,y and Vikram Adve, <ulink
url="http://www.cs.utah.edu/~regehr/papers/overflow12.pdf">Understanding
Integer Overflow in C/C++</ulink></para>
</listitem>
</orderedlist>
</section>
</library>

View File

@@ -0,0 +1,96 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
<section id="safe_numerics.safe_signed_range">
<title>safe_signed_range&lt;MIN, MAX&gt;</title>
<section>
<title>Description</title>
<para>This type holds a integer in the range [MIN, MAX]. It will throw a
<code>std::out_of_range</code> exception for operation which would result
in assigning an integer value outside of this range.</para>
</section>
<section>
<title>Template Parameters</title>
<informaltable>
<tgroup cols="3">
<colspec align="left"/>
<colspec align="left" colwidth="3*"/>
<colspec align="left" colwidth="3*"/>
<thead>
<row>
<entry align="left">Parameter</entry>
<entry align="left">Requirements</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry><code>MIN</code></entry>
<entry>must be an integer literal</entry>
<entry>The minimum integer value that this type may hold</entry>
</row>
<row>
<entry><code>MAX</code></entry>
<entry>must be an integer literal</entry>
<entry>The maximum integer value that this type may hold</entry>
</row>
<row>
<entry><code/></entry>
<entry>MIN &lt; MAX</entry>
<entry/>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
<section>
<title>Model of</title>
<para><link linkend="safe_numerics.numeric">Numeric</link></para>
</section>
<section>
<title>Header</title>
<para><filename><ulink url="../../include/safe_range.hpp">#include
&lt;boost/safe_numerics/safe_range.hpp&gt;</ulink></filename></para>
</section>
<section>
<title>Example of use</title>
<programlisting>#include &lt;safe/numeric/safe_range.hpp&gt;
void f(){
boost::numeric::safe_signed_range&lt;7, 24&gt; i;
i = 0; // error
i = 9; // ok
i *= 9; // throws overflow exception
}</programlisting>
</section>
<section>
<title>See Also</title>
<para><code>std::out_of_range</code></para>
</section>
</section>

View File

@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
"http://www.boost.org/tools/boostbook/dtd/boostbook.dtd">
<section id="safe_numerics.safe_unsigned_range">
<title>safe_unsigned_range&lt;MIN, MAX&gt;</title>
<section>
<title>Description</title>
<para>This type holds a integer in the range [MIN, MAX]. It will throw a
<code>std::out_of_range</code> exception for any operation which would
result in assigning an integer value outside of this range.</para>
</section>
<section>
<title>Template Parameters</title>
<informaltable>
<tgroup cols="3">
<colspec align="left"/>
<colspec align="left" colwidth="3*"/>
<colspec align="left" colwidth="3*"/>
<thead>
<row>
<entry align="left">Parameter</entry>
<entry align="left">Requirements</entry>
<entry>Description</entry>
</row>
</thead>
<tbody>
<row>
<entry><code>MIN</code></entry>
<entry>must be a positive integer literal</entry>
<entry>The minimum integer value that this type may hold</entry>
</row>
<row>
<entry><code>MAX</code></entry>
<entry>must be a positive integer literal</entry>
<entry>The maximum integer value that this type may hold</entry>
</row>
<row>
<entry><code/></entry>
<entry>MIN &lt; MAX</entry>
<entry/>
</row>
</tbody>
</tgroup>
</informaltable>
</section>
<section>
<title>Model of</title>
<para><link linkend="safe_numerics.numeric">Numeric</link></para>
</section>
<section>
<title>Header</title>
<para><filename><ulink url="../../include/safe_range.hpp">#include
&lt;safe/numeric/safe_range.hpp&gt;</ulink></filename></para>
</section>
<section>
<title>Example of use</title>
<programlisting>#include &lt;safe/numeric/safe_range.hpp&gt;
void f(){
boost::numeric::safe_unsigned_range&lt;7, 24&gt; i;
i = 0; // throws out_of_range exception
i = 9; // ok
i *= 9; // throws out_of_range exception
i = -1; // throws out_of_range exception
}</programlisting>
</section>
<section>
<title>See Also</title>
<para><code>std::out_of_range</code></para>
</section>
</section>

View File

@@ -0,0 +1,74 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE section PUBLIC "-//Boost//DTD BoostBook XML V1.1//EN"
"http://www.oasis-open.org/docbook/xml/4.2/docbookx.dtd">
<section id="safe_numerics.tutorial">
<title>Tutorial</title>
<section id="safe_numerics.tutorial.1">
<title>Problem:arithmetic operations can yield in correct results.</title>
<para>When some operation results in a result which exceeds the capacity
of a data variable to hold it, the result is undefined. This is called
"overflow". Since word size can differ between machines, code which
produces correct results in one set of circumstances may fail when
re-compiled on a machine with different hardware. When this occurs, Most
C++ compilers will continue to execute with no indication that the results
are wrong. It is the programmer's responsabiity to ensure such undefined
behavior is avoided.</para>
<para>This program demonstrates this problem. The solution is to replace
instances of <code>char</code> type with <code>safe&lt;char&gt;</code>
type.</para>
<programlisting><xi:include href="../../examples/example1.cpp"
parse="text" xmlns:xi="http://www.w3.org/2001/XInclude"/></programlisting>
</section>
<section id="safe_numerics.tutorial.2">
<title>Problem:Undetected overflow</title>
<para>A variation of the above is when a value is incremented/decremented
beyond it's domain. This is a common problem with for loops.</para>
<programlisting><xi:include href="../../examples/example3.cpp"
parse="text" xmlns:xi="http://www.w3.org/2001/XInclude"/></programlisting>
</section>
<section id="safe_numerics.tutorial.3">
<title>Problem:Implicit conversions change data values</title>
<para>A simple assign or arithment expression will generally convert all
the terms to the same type. Sometimes this can silently change values. For
example, when a signed data variable contains a negative type, assigning
to a unsigned type will be permitted by any C/C++ compiler but will be
treated as large unsigned value. Most modern compilers will emit a compile
time warning when this conversion is performed. The user may then decide
to change some data types or apply a <code>static_cast</code>. This is
less than satisfactory for two reasons:</para>
<para><itemizedlist>
<listitem>
<para>It may be unwield to change all the types to signed or
unsigned.</para>
</listitem>
<listitem>
<para>Litering one's program with <code>static_cast</code><code>
</code>makes it more difficult to read.</para>
</listitem>
<listitem>
<para>We may believe that our signed type will never contain a
negative value. If we use a <code>static_cast</code> to suppress the
warning, we'll fail to detect a program error when it is commited.
This is aways a risk with casts.</para>
</listitem>
</itemizedlist></para>
<para>This solution is the same as the above, Just replace instances of
the <code>int </code>with <code>safe&lt;int&gt;</code>.</para>
<programlisting><xi:include href="../../examples/example2.cpp"
parse="text" xmlns:xi="http://www.w3.org/2001/XInclude"/></programlisting>
</section>
</section>

View File

@@ -0,0 +1,44 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Acknowledgements</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="index.html" title="Safe Numerics">
<link rel="prev" href="change_log.html" title="Change Log">
<link rel="next" href="references.html" title="References">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="change_log.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="references.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="safe_numerics.acknowledgements"></a>Acknowledgements</h2></div></div></div>
<p><a href="http://safeint.codeplex.com/releases/view/73792" target="_top">David
LeBlanc's</a> is the author of <a href="http://safeint.codeplex.com/releases/view/73792" target="_top">SafeInt3</a>
library which motivated this work</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="change_log.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="references.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,601 @@
/*=============================================================================
Copyright (c) 2004 Joel de Guzman
http://spirit.sourceforge.net/
Distributed under the Boost Software License, Version 1.0. (See accompany-
ing file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
/*=============================================================================
Body defaults
=============================================================================*/
body
{
margin: 1em;
font-family: sans-serif;
}
/*=============================================================================
Paragraphs
=============================================================================*/
p
{
text-align: left;
font-size: 10pt;
line-height: 1.15;
}
/*=============================================================================
Program listings
=============================================================================*/
/* Code on paragraphs */
p tt.computeroutput
{
font-size: 9pt;
}
pre.synopsis
{
font-size: 9pt;
margin: 1pc 4% 0pc 4%;
padding: 0.5pc 0.5pc 0.5pc 0.5pc;
}
.programlisting,
.screen
{
font-size: 9pt;
display: block;
margin: 1pc 4% 0pc 4%;
padding: 0.5pc 0.5pc 0.5pc 0.5pc;
}
/* Program listings in tables don't get borders */
td .programlisting,
td .screen
{
margin: 0pc 0pc 0pc 0pc;
padding: 0pc 0pc 0pc 0pc;
}
/*=============================================================================
Headings
=============================================================================*/
h1, h2, h3, h4, h5, h6
{
text-align: left;
margin: 1em 0em 0.5em 0em;
font-weight: bold;
}
h1 { font: 140% }
h2 { font: bold 140% }
h3 { font: bold 130% }
h4 { font: bold 120% }
h5 { font: italic 110% }
h6 { font: italic 100% }
/* Top page titles */
title,
h1.title,
h2.title
h3.title,
h4.title,
h5.title,
h6.title,
.refentrytitle
{
font-weight: bold;
margin-bottom: 1pc;
}
h1.title { font-size: 140% }
h2.title { font-size: 140% }
h3.title { font-size: 130% }
h4.title { font-size: 120% }
h5.title { font-size: 110% }
h6.title { font-size: 100% }
.section h1
{
margin: 0em 0em 0.5em 0em;
font-size: 140%;
}
.section h2 { font-size: 140% }
.section h3 { font-size: 130% }
.section h4 { font-size: 120% }
.section h5 { font-size: 110% }
.section h6 { font-size: 100% }
/* Code on titles */
h1 tt.computeroutput { font-size: 140% }
h2 tt.computeroutput { font-size: 140% }
h3 tt.computeroutput { font-size: 130% }
h4 tt.computeroutput { font-size: 130% }
h5 tt.computeroutput { font-size: 130% }
h6 tt.computeroutput { font-size: 130% }
/*=============================================================================
Author
=============================================================================*/
h3.author
{
font-size: 100%
}
/*=============================================================================
Lists
=============================================================================*/
li
{
font-size: 10pt;
line-height: 1.3;
}
/* Unordered lists */
ul
{
text-align: left;
}
/* Ordered lists */
ol
{
text-align: left;
}
/*=============================================================================
Links
=============================================================================*/
a
{
text-decoration: none; /* no underline */
}
a:hover
{
text-decoration: underline;
}
/*=============================================================================
Spirit style navigation
=============================================================================*/
.spirit-nav
{
text-align: right;
}
.spirit-nav a
{
color: white;
padding-left: 0.5em;
}
.spirit-nav img
{
border-width: 0px;
}
/*=============================================================================
Copyright footer
=============================================================================*/
.copyright-footer
{
text-align: right;
font-size: 70%;
}
.copyright-footer p
{
text-align: right;
font-size: 80%;
}
/*=============================================================================
Table of contents
=============================================================================*/
.toc
{
margin: 1pc 4% 0pc 4%;
padding: 0.1pc 1pc 0.1pc 1pc;
font-size: 80%;
line-height: 1.15;
}
.boost-toc
{
float: right;
padding: 0.5pc;
}
/* Code on toc */
.toc .computeroutput { font-size: 120% }
/*=============================================================================
Tables
=============================================================================*/
.table-title,
div.table p.title
{
margin-left: 4%;
padding-right: 0.5em;
padding-left: 0.5em;
}
.informaltable table,
.table table
{
width: 92%;
margin-left: 4%;
margin-right: 4%;
}
div.informaltable table,
div.table table
{
padding: 4px;
}
/* Table Cells */
div.informaltable table tr td,
div.table table tr td
{
padding: 0.5em;
text-align: left;
font-size: 9pt;
}
div.informaltable table tr th,
div.table table tr th
{
padding: 0.5em 0.5em 0.5em 0.5em;
border: 1pt solid white;
font-size: 80%;
}
table.simplelist
{
width: auto !important;
margin: 0em !important;
padding: 0em !important;
border: none !important;
}
table.simplelist td
{
margin: 0em !important;
padding: 0em !important;
text-align: left !important;
font-size: 9pt !important;
border: none !important;
}
/*=============================================================================
Blurbs
=============================================================================*/
div.note,
div.tip,
div.important,
div.caution,
div.warning,
p.blurb
{
font-size: 9pt; /* A little bit smaller than the main text */
line-height: 1.2;
display: block;
margin: 1pc 4% 0pc 4%;
padding: 0.5pc 0.5pc 0.5pc 0.5pc;
}
p.blurb img
{
padding: 1pt;
}
/*=============================================================================
Variable Lists
=============================================================================*/
div.variablelist
{
margin: 1em 0;
}
/* Make the terms in definition lists bold */
div.variablelist dl dt,
span.term
{
font-weight: bold;
font-size: 10pt;
}
div.variablelist table tbody tr td
{
text-align: left;
vertical-align: top;
padding: 0em 2em 0em 0em;
font-size: 10pt;
margin: 0em 0em 0.5em 0em;
line-height: 1;
}
div.variablelist dl dt
{
margin-bottom: 0.2em;
}
div.variablelist dl dd
{
margin: 0em 0em 0.5em 2em;
font-size: 10pt;
}
div.variablelist table tbody tr td p,
div.variablelist dl dd p
{
margin: 0em 0em 0.5em 0em;
line-height: 1;
}
/*=============================================================================
Misc
=============================================================================*/
/* Title of books and articles in bibliographies */
span.title
{
font-style: italic;
}
span.underline
{
text-decoration: underline;
}
span.strikethrough
{
text-decoration: line-through;
}
/* Copyright, Legal Notice */
div div.legalnotice p
{
text-align: left
}
/*=============================================================================
Colors
=============================================================================*/
@media screen
{
body {
background-color: #FFFFFF;
color: #000000;
}
/* Syntax Highlighting */
.keyword { color: #0000AA; }
.identifier { color: #000000; }
.special { color: #707070; }
.preprocessor { color: #402080; }
.char { color: teal; }
.comment { color: #800000; }
.string { color: teal; }
.number { color: teal; }
.white_bkd { background-color: #FFFFFF; }
.dk_grey_bkd { background-color: #999999; }
/* Links */
a, a .keyword, a .identifier, a .special, a .preprocessor
a .char, a .comment, a .string, a .number
{
color: #005a9c;
}
a:visited, a:visited .keyword, a:visited .identifier,
a:visited .special, a:visited .preprocessor a:visited .char,
a:visited .comment, a:visited .string, a:visited .number
{
color: #9c5a9c;
}
h1 a, h2 a, h3 a, h4 a, h5 a, h6 a,
h1 a:hover, h2 a:hover, h3 a:hover, h4 a:hover, h5 a:hover, h6 a:hover,
h1 a:visited, h2 a:visited, h3 a:visited, h4 a:visited, h5 a:visited, h6 a:visited
{
text-decoration: none; /* no underline */
color: #000000;
}
/* Copyright, Legal Notice */
.copyright
{
color: #666666;
font-size: small;
}
div div.legalnotice p
{
color: #666666;
}
/* Program listing */
pre.synopsis
{
border: 1px solid #DCDCDC;
}
.programlisting,
.screen
{
border: 1px solid #DCDCDC;
}
td .programlisting,
td .screen
{
border: 0px solid #DCDCDC;
}
/* Blurbs */
div.note,
div.tip,
div.important,
div.caution,
div.warning,
p.blurb
{
border: 1px solid #DCDCDC;
}
/* Table of contents */
.toc
{
border: 1px solid #DCDCDC;
}
/* Tables */
div.informaltable table tr td,
div.table table tr td
{
border: 1px solid #DCDCDC;
}
div.informaltable table tr th,
div.table table tr th
{
background-color: #F0F0F0;
border: 1px solid #DCDCDC;
}
.copyright-footer
{
color: #8F8F8F;
}
/* Misc */
span.highlight
{
color: #00A000;
}
}
@media print
{
/* Links */
a
{
color: black;
}
a:visited
{
color: black;
}
.spirit-nav
{
display: none;
}
/* Program listing */
pre.synopsis
{
border: 1px solid gray;
}
.programlisting,
.screen
{
border: 1px solid gray;
}
td .programlisting,
td .screen
{
border: 0px solid #DCDCDC;
}
/* Table of contents */
.toc
{
border: 1px solid gray;
}
.informaltable table,
.table table
{
border: 1px solid gray;
border-collapse: collapse;
}
/* Tables */
div.informaltable table tr td,
div.table table tr td
{
border: 1px solid gray;
}
div.informaltable table tr th,
div.table table tr th
{
border: 1px solid gray;
}
table.simplelist tr td
{
border: none !important;
}
/* Misc */
span.highlight
{
font-weight: bold;
}
}
/*=============================================================================
Images
=============================================================================*/
span.inlinemediaobject img
{
vertical-align: middle;
}
/*==============================================================================
Super and Subscript: style so that line spacing isn't effected, see
http://www.adobe.com/cfusion/communityengine/index.cfm?event=showdetails&productId=1&postId=5341
==============================================================================*/
sup,
sub {
height: 0;
line-height: 1;
vertical-align: baseline;
_vertical-align: bottom;
position: relative;
}
sup {
bottom: 1ex;
}
sub {
top: .5ex;
}

View File

@@ -0,0 +1,42 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Change Log</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="index.html" title="Safe Numerics">
<link rel="prev" href="rationale.html" title="Rationale">
<link rel="next" href="acknowledgements.html" title="Acknowledgements">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="rationale.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="acknowledgements.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="safe_numerics.change_log"></a>Change Log</h2></div></div></div>
<p>This is the initial version.</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="rationale.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="acknowledgements.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,42 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Concepts</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="index.html" title="Safe Numerics">
<link rel="prev" href="notes.html" title="Notes">
<link rel="next" href="numeric.html" title="Numeric&lt;T&gt;">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="notes.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="numeric.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="safe_numerics.concepts"></a>Concepts</h2></div></div></div>
<div class="toc"><dl><dt><span class="section"><a href="numeric.html">Numeric&lt;T&gt;</a></span></dt></dl></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="notes.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="numeric.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,46 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Functions</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="index.html" title="Safe Numerics">
<link rel="prev" href="safe.html" title="safe&lt;T&gt;">
<link rel="next" href="safe_cast.html" title="safe_cast">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="safe.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="safe_cast.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="safe_numerics.functions"></a>Functions</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe_cast.html">safe_cast</a></span></dt>
<dt><span class="section"><a href="safe_compare.html">safe_compare</a></span></dt>
<dt><span class="section"><a href="rationale/overflow.html">overflow</a></span></dt>
</dl></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="safe.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="safe_cast.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,74 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Safe Numerics</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="next" href="introduction.html" title="Introduction">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav"><a accesskey="n" href="introduction.html"><img src="images/next.png" alt="Next"></a></div>
<div class="chapter">
<div class="titlepage"><div>
<div><h2 class="title">
<a name="safe_numerics"></a>Safe Numerics</h2></div>
<div><div class="author"><h3 class="author">
<span class="firstname">Robert</span> <span class="surname">Ramey</span>
</h3></div></div>
<div><p class="copyright">Copyright &#169; 2012 Robert Ramey</p></div>
<div><div class="legalnotice">
<a name="id753358"></a><p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></div>
</div></div>
<div class="toc">
<p><b>Table of Contents</b></p>
<dl>
<dt><span class="section"><a href="introduction.html">Introduction</a></span></dt>
<dt><span class="section"><a href="tutorial.html">Tutorial</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="tutorial/1.html">Problem:arithmetic operations can yield in correct results.</a></span></dt>
<dt><span class="section"><a href="tutorial/2.html">Problem:Undetected overflow</a></span></dt>
<dt><span class="section"><a href="tutorial/3.html">Problem:Implicit conversions change data values</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="notes.html">Notes</a></span></dt>
<dt><span class="section"><a href="concepts.html">Concepts</a></span></dt>
<dd><dl><dt><span class="section"><a href="numeric.html">Numeric&lt;T&gt;</a></span></dt></dl></dd>
<dt><span class="section"><a href="types.html">Types</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="safe_unsigned_range.html">safe_unsigned_range&lt;MIN, MAX&gt;</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html">safe_signed_range&lt;MIN, MAX&gt;</a></span></dt>
<dt><span class="section"><a href="safe.html">safe&lt;T&gt;</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="functions.html">Functions</a></span></dt>
<dd><dl>
<dt><span class="section"><a href="safe_cast.html">safe_cast</a></span></dt>
<dt><span class="section"><a href="safe_compare.html">safe_compare</a></span></dt>
<dt><span class="section"><a href="rationale/overflow.html">overflow</a></span></dt>
</dl></dd>
<dt><span class="section"><a href="rationale.html">Rationale</a></span></dt>
<dt><span class="section"><a href="change_log.html">Change Log</a></span></dt>
<dt><span class="section"><a href="acknowledgements.html">Acknowledgements</a></span></dt>
<dt><span class="section"><a href="references.html">References</a></span></dt>
</dl>
</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: , at </small></p></td>
<td align="right"><div class="copyright-footer"></div></td>
</tr></table>
<hr>
<div class="spirit-nav"><a accesskey="n" href="introduction.html"><img src="images/next.png" alt="Next"></a></div>
</body>
</html>

View File

@@ -0,0 +1,47 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Introduction</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="index.html" title="Safe Numerics">
<link rel="prev" href="index.html" title="Safe Numerics">
<link rel="next" href="tutorial.html" title="Tutorial">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="index.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="tutorial.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="safe_numerics.introduction"></a>Introduction</h2></div></div></div>
<p>Arithmetic operations in C++ are NOT guarenteed to yield the correct
mathematical result. The feature is inherited from the early days of C the
behavior of int, unsigned int and others were designed to map closely to
the underlying hardware. The result is that one cannot know when
the</p>
<p></p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="index.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="tutorial.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,66 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Notes</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="index.html" title="Safe Numerics">
<link rel="prev" href="tutorial/3.html" title="Problem:Implicit conversions change data values">
<link rel="next" href="concepts.html" title="Concepts">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="tutorial/3.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="concepts.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="safe_numerics.notes"></a>Notes</h2></div></div></div>
<p>This library really an re-implementation the facilities provided by
<a href="http://safeint.codeplex.com" target="_top">David LeBlanc's SafeInt
Library</a> using <a href="www.boost.org" target="_top">Boost</a>. I found
this library very well done in every way. My main usage was to run unit
tests for my embedded systems projects on my PC. Still, I had a few
issues.</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>I was a lot of code in one header - 6400 lines. Very unwieldy to
understand and modify.</p></li>
<li class="listitem"><p>I couldn't find separate documentation other than that in the
header file.</p></li>
<li class="listitem"><p>I didn't use <a href="www.boost.org" target="_top">Boost</a>
conventions for naming.</p></li>
<li class="listitem"><p>I required porting to different compilers.</p></li>
<li class="listitem"><p>It had a very long license associated with it.</p></li>
<li class="listitem"><p>I believe the original <a href="http://safeint.codeplex.com/releases/view/73792" target="_top">SafeInt</a>
library is not easily found. MSVC 10 has this built in so I seems that
they've decided to make it less attractive to use on other
systems.</p></li>
</ul></div>
<p>This version addresses these issues. It exploits <a href="www.boost.org" target="_top">Boost</a> facilities such as template
metaprogramming to reduce the number of lines of source code. It exploits
the Boost Preprocessor Library to generate exhaustive tests.</p>
<p>All concepts, types and functions documented are declared in the
namespace <code class="computeroutput">boost::numeric</code>.</p>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="tutorial/3.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="concepts.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,382 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Numeric&lt;T&gt;</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="concepts.html" title="Concepts">
<link rel="prev" href="concepts.html" title="Concepts">
<link rel="next" href="types.html" title="Types">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="concepts.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="concepts.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="types.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.numeric"></a>Numeric&lt;T&gt;</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="numeric.html#id742223">Description</a></span></dt>
<dt><span class="section"><a href="numeric.html#id753040">Notation</a></span></dt>
<dt><span class="section"><a href="numeric.html#id753122">Associated Types</a></span></dt>
<dt><span class="section"><a href="numeric.html#id753183">Valid Expressions</a></span></dt>
<dt><span class="section"><a href="numeric.html#id698056">Header</a></span></dt>
<dt><span class="section"><a href="numeric.html#id698078">Models</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id742223"></a>Description</h4></div></div></div>
<p>A type is Numeric if it has the properties of a number.</p>
<p>More specifically, a type T is Numeric if there exists
specialization of std::numeric_limits&lt;T&gt;. See the documentation for
standard library class numeric_limits. The standard library includes such
specializations for all the primitive numeric types. Note that this
concept is distinct from the C++ standard library type traits
<code class="computeroutput">is_integral</code> and <code class="computeroutput">is_arithmetic</code>. These latter
fullfill the requirement of the concept Numeric. But there are types T
which fullfill this concept for which <code class="computeroutput">is_arithmetic&lt;T&gt;::value
== false</code>. For example see
<code class="computeroutput">safe_signed_integer&lt;int&gt;</code>.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753040"></a>Notation</h4></div></div></div>
<p>[The next two sections, associated types and valid expressions,
present expressions involving types that model the concept being defined.
This section defines the meaning of the variables and identifiers used in
those expressions.]</p>
<div class="table">
<a name="id753049"></a><p class="title"><b>Table&#160;1.&#160;</b></p>
<div class="table-contents"><table class="table">
<colgroup>
<col align="left">
<col align="left">
</colgroup>
<tbody>
<tr>
<td align="left">
<code class="computeroutput">T, U, V</code>
</td>
<td align="left">A type that is a model of the Numeric</td>
</tr>
<tr>
<td align="left">
<code class="computeroutput">t, u, v</code>
</td>
<td align="left">An object of type modeling Numeric</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break">
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753122"></a>Associated Types</h4></div></div></div>
<p>[A concept is a set of requirements on some type. Frequently,
however, some of those requirements involve some other type. For example,
one of the Unary Function requirements is that a Unary Function must have
an argument type; if F is a type that models Unary Function and f is an
object of type F, then, in the expression f(x), x must be of F's argument
type. If a concept does not have any such associated types this section
can be eliminated.]</p>
<div class="table">
<a name="id753132"></a><p class="title"><b>Table&#160;2.&#160;</b></p>
<div class="table-contents"><table class="table">
<colgroup>
<col align="left">
<col align="left">
</colgroup>
<tbody><tr>
<td align="left">
<code class="computeroutput">std::numeric_limits&lt;T&gt;</code>
</td>
<td align="left">The numeric_limits class template provides a C++ program
with information about various properties of the implementation&#8217;s
representation of the arithmetic types. See C++ standard
18.3.2.2.</td>
</tr></tbody>
</table></div>
</div>
<br class="table-break">
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753183"></a>Valid Expressions</h4></div></div></div>
<p>In addition to the expressions defined in <a href="http://www.sgi.com/tech/stl/Assignable.html" target="_top">Assignable</a> the
following expressions must be valid.</p>
<p>Any operations which result in integers which cannot be represented
as some Numeric type will throw an exception.</p>
<div class="table">
<a name="id753206"></a><p class="title"><b>Table&#160;3.&#160;General</b></p>
<div class="table-contents"><table class="table" summary="General">
<colgroup>
<col align="left">
<col align="left">
</colgroup>
<thead><tr>
<th align="left">Expression</th>
<th align="left">Return Value</th>
</tr></thead>
<tbody>
<tr>
<td align="left"><code class="computeroutput">std::numeric_limits&lt;T&gt;.is_bounded
</code></td>
<td align="left">true</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">std::numeric_limits&lt;T&gt;.is_integer</code></td>
<td align="left">true</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">std::numeric_limits&lt;T&gt;.is_specialized
</code></td>
<td align="left">true</td>
</tr>
</tbody>
</table></div>
</div>
<p><br class="table-break"></p>
<div class="table">
<a name="id699671"></a><p class="title"><b>Table&#160;4.&#160;Unary Operators</b></p>
<div class="table-contents"><table class="table" summary="Unary Operators">
<colgroup>
<col align="left">
<col align="left">
<col align="left">
</colgroup>
<thead><tr>
<th align="left">Expression</th>
<th align="left">Return Type</th>
<th align="left">Semantics</th>
</tr></thead>
<tbody>
<tr>
<td align="left"><code class="computeroutput">-t</code></td>
<td align="left">T</td>
<td align="left">Invert sign</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">+t</code></td>
<td align="left">T</td>
<td align="left">unary plus - a no op</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t--</code></td>
<td align="left">T</td>
<td align="left">post decrement</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t++</code></td>
<td align="left">T</td>
<td align="left">post increment</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">--t</code></td>
<td align="left">T</td>
<td align="left">predecrement</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">++t</code></td>
<td align="left">T</td>
<td align="left">preincrement</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">~</code></td>
<td align="left">T</td>
<td align="left">complement</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break"><div class="table">
<a name="id699857"></a><p class="title"><b>Table&#160;5.&#160;Binary Operators</b></p>
<div class="table-contents"><table class="table" summary="Binary Operators">
<colgroup>
<col align="left">
<col align="left">
<col align="left">
</colgroup>
<thead><tr>
<th align="left">Expression</th>
<th align="left">Return Type</th>
<th align="left">Semantics</th>
</tr></thead>
<tbody>
<tr>
<td align="left"><code class="computeroutput">t - u</code></td>
<td align="left">V</td>
<td align="left">Invert sign</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t + u</code></td>
<td align="left">V</td>
<td align="left">unary plus - a no op</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t * u</code></td>
<td align="left">V</td>
<td align="left">multiply t by u</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t / u</code></td>
<td align="left">T</td>
<td align="left">divide t by u</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t % u</code></td>
<td align="left">T</td>
<td align="left">t modulus u</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &lt;&lt; u</code></td>
<td align="left">T</td>
<td align="left">shift t left u bits</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &gt;&gt; u</code></td>
<td align="left">T</td>
<td align="left">shift t right by ubits</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &lt; u</code></td>
<td align="left">bool</td>
<td align="left">true if t less than u, false otherwise</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &lt;= u</code></td>
<td align="left">bool</td>
<td align="left">true if t less than or equal to u, false otherwise</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &gt; u</code></td>
<td align="left">bool</td>
<td align="left">true if t greater than u, false otherwise</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &gt;= u</code></td>
<td align="left">bool</td>
<td align="left">true if t greathan or equal to u, false otherwise</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t == u</code></td>
<td align="left">bool</td>
<td align="left">true if t equal to u, false otherwise</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t != u</code></td>
<td align="left">bool</td>
<td align="left">true if t not equal to u, false otherwise</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &amp; u</code></td>
<td align="left">bool</td>
<td align="left">and of t and u padded out max # bits in t, u</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t | u</code></td>
<td align="left">bool</td>
<td align="left">or of t and u padded out max # bits in t, u</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t ^ u</code></td>
<td align="left">V</td>
<td align="left">exclusive or of t and u padded out max # bits in t,
u</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t = u</code></td>
<td align="left"><code class="computeroutput">T</code></td>
<td align="left">assign value of u to t</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t += u</code></td>
<td align="left"><code class="computeroutput">T</code></td>
<td align="left">add u to t and assign to t</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t -= u</code></td>
<td align="left"><code class="computeroutput">T</code></td>
<td align="left">subtract u from t and assign to t</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t *= u</code></td>
<td align="left"><code class="computeroutput">T</code></td>
<td align="left">multiply t by u and assign to t</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t /= u</code></td>
<td align="left"><code class="computeroutput">T</code></td>
<td align="left">divide t by u and assign to t</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &amp;= u</code></td>
<td align="left"><code class="computeroutput">T</code></td>
<td align="left">and t with u and assign to t</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &lt;&lt;= u</code></td>
<td align="left">T</td>
<td align="left">left shift the value of t by u bits</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &gt;&gt;= u</code></td>
<td align="left">T</td>
<td align="left">right shift the value of t by u bits</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t &amp;= u</code></td>
<td align="left">T</td>
<td align="left">and the value of t with u and assign to t</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t |= u</code></td>
<td align="left">T</td>
<td align="left">or the value of t with u and assign to t</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">t ^= u</code></td>
<td align="left">T</td>
<td align="left">exclusive or the value of t with u and assign to t</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break">
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id698056"></a>Header</h4></div></div></div>
<p><a href="../../include/concept/numeric.hpp" target="_top"><code class="computeroutput">#include
&lt;safe_numerics/include/concepts/numeric.hpp&gt; </code></a></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id698078"></a>Models</h4></div></div></div>
<p><code class="computeroutput">Numeric&lt;char&gt;, Numeric&lt;float&gt;, Numeric&lt;unsigned
int&gt;, safe_signed_integer&lt;int&gt; etc.</code></p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="concepts.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="concepts.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="types.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

Binary file not shown.

After

Width:  |  Height:  |  Size: 9.4 KiB

View File

@@ -0,0 +1,96 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Rationale</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="index.html" title="Safe Numerics">
<link rel="prev" href="rationale/overflow.html" title="overflow">
<link rel="next" href="change_log.html" title="Change Log">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="rationale/overflow.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="change_log.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="safe_numerics.rationale"></a>Rationale</h2></div></div></div>
<div class="qandaset">
<a name="id754744"></a><dl>
<dt>1. <a href="rationale.html#id754748">Why does an operation on two safe&lt;int&gt;
values not necessarily return another safe type
?</a>
</dt>
<dt>2. <a href="rationale.html#id754811">Why is there no policy driven design for handling
overflows</a>
</dt>
<dt>3. <a href="rationale.html#id754823">Why is Boost.Convert not used.</a>
</dt>
</dl>
<table border="0" width="100%" summary="Q and A Set">
<col align="left" width="1%">
<col>
<tbody>
<tr class="question">
<td align="left" valign="top">
<a name="id754748"></a><a name="id754750"></a><p><b>1.</b></p>
</td>
<td align="left" valign="top"><p>Why does an operation on two <code class="computeroutput">safe&lt;int&gt;</code>
values not necessarily return another <code class="computeroutput">safe</code> type
?</p></td>
</tr>
<tr class="answer">
<td align="left" valign="top"></td>
<td align="left" valign="top">
<p>There are a number of</p>
<div class="itemizedlist"><ul class="itemizedlist" type="disc">
<li class="listitem"><p>it was hard to implement.</p></li>
<li class="listitem"><p>it doesn't really seem necessary. We always do SOMETHING
with result of the operation. This will result in an assignment
or a conversion to some other type where the result will be
validated.</p></li>
<li class="listitem"><p></p></li>
<li class="listitem"><p></p></li>
</ul></div>
</td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id754811"></a><a name="id754814"></a><p><b>2.</b></p>
</td>
<td align="left" valign="top"><p>Why is there no policy driven design for handling
overflows</p></td>
</tr>
<tr class="question">
<td align="left" valign="top">
<a name="id754823"></a><a name="id754826"></a><p><b>3.</b></p>
</td>
<td align="left" valign="top"><p>Why is Boost.Convert not used.</p></td>
</tr>
</tbody>
</table>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="rationale/overflow.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="change_log.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,92 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>overflow</title>
<link rel="stylesheet" href="../boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="../index.html" title="Safe Numerics">
<link rel="up" href="../functions.html" title="Functions">
<link rel="prev" href="../safe_compare.html" title="safe_compare">
<link rel="next" href="../rationale.html" title="Rationale">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="../pre-boost.jpg"></td>
<td align="center"><a href="../../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../safe_compare.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../functions.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="../rationale.html"><img src="../images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.rationale.overflow"></a>overflow</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="../overflow.html#id754630">Synopsis</a></span></dt>
<dt><span class="section"><a href="../overflow.html#id754648">Description</a></span></dt>
<dt><span class="section"><a href="../overflow.html#id754678">Header</a></span></dt>
<dt><span class="section"><a href="../overflow.html#id754700">Example of use</a></span></dt>
<dt><span class="section"><a href="../overflow.html#id754713">See Also</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754630"></a>Synopsis</h4></div></div></div>
<p>This function is invoked by the library whenever it is not possible
to produce a result for an arithmetic operation.</p>
<pre class="programlisting">void overflow(char const * const msg);</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754648"></a>Description</h4></div></div></div>
<p>If evironment supports C++ exceptions, this function throws the
exception .</p>
<p>If the environment does not support C++ exceptions, the user should
implement this function and expect it to be called when appropriate.
Otherwise, function is implemented by the library so that it throws the
standard library exception <code class="computeroutput">std::out_of_range(msg)</code>.</p>
<p><code class="filename">boost/config.hpp </code>defines BOOST_NO_EXCEPTIONS
when the environment doesn't support exceptions. It is by checking for the
definition of this macro that the system determines whether or not
exceptions are supported.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754678"></a>Header</h4></div></div></div>
<p><a href="../include/safe_numerics/overflow" target="_top"><code class="computeroutput">#include
&lt;boost/safe_numerics/overflow.hpp&gt; </code></a></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754700"></a>Example of use</h4></div></div></div>
<pre class="programlisting">#include &lt;cstdio&gt;
void overflow(char const * const msg){
std::fputs("safe_numerics overflow error:, std::stderr);
std::fputs(msg, std::stderr);
std::fputc('\n', std::stderr);
}</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754713"></a>See Also</h4></div></div></div>
<p>See <a class="link" href="rationale/overflow.html" title="overflow">rationale</a> for more
information on this function</p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="../safe_compare.html"><img src="../images/prev.png" alt="Prev"></a><a accesskey="u" href="../functions.html"><img src="../images/up.png" alt="Up"></a><a accesskey="h" href="../index.html"><img src="../images/home.png" alt="Home"></a><a accesskey="n" href="../rationale.html"><img src="../images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,54 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>References</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="index.html" title="Safe Numerics">
<link rel="prev" href="acknowledgements.html" title="Acknowledgements">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="acknowledgements.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="safe_numerics.references"></a>References</h2></div></div></div>
<p></p>
<div class="orderedlist"><ol class="orderedlist" type="1">
<li class="listitem"><p>David LeBlanc, <a href="http://safeint.codeplex.com" target="_top">Codeplex
SafeInt Page</a></p></li>
<li class="listitem"><p>Daniel Plakosh, <a href="https://buildsecurityin.us-cert.gov/bsi/articles/knowledge/coding/312-BSI.html" target="_top">Safe
Integer Operations</a></p></li>
<li class="listitem"><p>Omer Katz, <a href="http://boost.2283326.n4.nabble.com/SafeInt-code-proposal-td2663669.html" target="_top">SafeInt
code proposal</a>, <a href="https://svn.boost.org/trac/boost/wiki/LibrariesUnderConstruction" target="_top">Boost.SafeInt</a></p></li>
<li class="listitem"><p>Software Engineering Institute, Carnegie Mellon, <a href="https://www.securecoding.cert.org/confluence/display/seccode/INT32-C.+Ensure+that+operations+on+signed+integers+do+not+result+in+overflow?showComments=false" target="_top">INT32-C.
Ensure that operations on signed integers do not result in
overflow</a></p></li>
<li class="listitem"><p>Will Dietz, Peng Li,y John Regehr,y and Vikram Adve, <a href="http://www.cs.utah.edu/~regehr/papers/overflow12.pdf" target="_top">Understanding
Integer Overflow in C/C++</a></p></li>
</ol></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="acknowledgements.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,160 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>safe&lt;T&gt;</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="types.html" title="Types">
<link rel="prev" href="safe_signed_range.html" title="safe_signed_range&lt;MIN, MAX&gt;">
<link rel="next" href="functions.html" title="Functions">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="safe_signed_range.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="types.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="functions.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.safe"></a>safe&lt;T&gt;</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe.html#id753878">Description</a></span></dt>
<dt><span class="section"><a href="safe.html#id753896">Template Parameters</a></span></dt>
<dt><span class="section"><a href="safe.html#id753982">Model of</a></span></dt>
<dt><span class="section"><a href="safe.html#id753996">Derived From</a></span></dt>
<dt><span class="section"><a href="safe.html#id754035">Notation</a></span></dt>
<dt><span class="section"><a href="safe.html#id754113">Header</a></span></dt>
<dt><span class="section"><a href="safe.html#id754133">Example of use</a></span></dt>
<dt><span class="section"><a href="safe.html#id754160">Notes</a></span></dt>
<dt><span class="section"><a href="safe.html#id754172">See Also</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753878"></a>Description</h4></div></div></div>
<p>A <code class="computeroutput">safe&lt;T&gt;</code> can be used anywhere a type T is used.
When T is used in operation which overflows, a exception is thrown</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753896"></a>Template Parameters</h4></div></div></div>
<p></p>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
<col align="left">
<col align="left">
</colgroup>
<thead><tr>
<th align="left">Parameter</th>
<th align="left">Type Requirements</th>
<th align="left">Description</th>
</tr></thead>
<tbody><tr>
<td align="left"><code class="computeroutput">T</code></td>
<td align="left"><a href="numeric_concept.xml" target="_top">Numeric</a></td>
<td align="left"><p>The underlying integer type</p></td>
</tr></tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753982"></a>Model of</h4></div></div></div>
<p><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753996"></a>Derived From</h4></div></div></div>
<p><code class="computeroutput"><a class="link" href="safe_signed_range.html" title="safe_signed_range&lt;MIN, MAX&gt;">safe_signed_range&lt;T&gt;</a></code>
(if std::numeric_limits&lt;T&gt;::is_signed == ftrue)</p>
<p><code class="computeroutput"><a class="link" href="safe_unsigned_range.html" title="safe_unsigned_range&lt;MIN, MAX&gt;">safe_unsigned_range&lt;T&gt;</a></code>
(if std::numeric_limits&lt;T&gt;::is_signed == ftrue)</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754035"></a>Notation</h4></div></div></div>
<div class="table">
<a name="id754041"></a><p class="title"><b>Table&#160;6.&#160;</b></p>
<div class="table-contents"><table class="table">
<colgroup>
<col align="left">
<col align="left">
</colgroup>
<tbody>
<tr>
<td align="left">
<code class="computeroutput">T</code>
</td>
<td align="left">A type that is a model of the Numeric</td>
</tr>
<tr>
<td align="left">
<code class="computeroutput">t</code>
</td>
<td align="left">An object of type modeling Numeric</td>
</tr>
</tbody>
</table></div>
</div>
<br class="table-break">
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754113"></a>Header</h4></div></div></div>
<p><code class="filename"><a href="../../include/safe_integer.hpp" target="_top">#include
&lt;boost/safe_numerics/safe_integer.hpp&gt;</a></code></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754133"></a>Example of use</h4></div></div></div>
<p>The following program will emit an error message on a machine where
int is only 16 bits but run without problem on a machine where int is 32
bits.</p>
<pre class="programlisting">#include &lt;boost/numeric/safe.hpp&gt;
#include &lt;iostream&gt;
void f(){
safe&lt;int&gt; i = 400;
safe&lt;int&gt; j;
try {
j = i * i;
}
catch(...){
std::cout &lt;&lt; "overflow error" &lt;&lt; std::endl;
}
}
</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754160"></a>Notes</h4></div></div></div>
<p>Footnotes (if any) that are referred to by other parts of the
page.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754172"></a>See Also</h4></div></div></div>
<p>Footnotes (if any) that are referred to by other parts of the
page.</p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="safe_signed_range.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="types.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="functions.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,125 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>safe_cast</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="functions.html" title="Functions">
<link rel="prev" href="functions.html" title="Functions">
<link rel="next" href="safe_compare.html" title="safe_compare">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="functions.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="functions.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="safe_compare.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.safe_cast"></a>safe_cast</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe_cast.html#id754202">Synopsis</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#id754218">Description</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#id754243">Type requirements</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#id754329">Preconditions</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#id754355">Complexity</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#id754371">Header</a></span></dt>
<dt><span class="section"><a href="safe_cast.html#id754391">Example of use</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754202"></a>Synopsis</h4></div></div></div>
<pre class="programlisting">template&lt;class T, class U&gt;
T safe_cast(const U &amp; u);</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754218"></a>Description</h4></div></div></div>
<p>Converts one <a href="numeric_concept.xml" target="_top">Numeric</a> type
to another. Throws an <code class="computeroutput">std::out_of_range</code> exception if such a
conversion is not possible without changing the value.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754243"></a>Type requirements</h4></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
<col align="left">
</colgroup>
<thead><tr>
<th align="left">Type</th>
<th align="left">Requirements</th>
</tr></thead>
<tbody>
<tr>
<td align="left"><code class="computeroutput">T</code></td>
<td align="left"><a href="numeric_concept.xml" target="_top">Numeric</a></td>
</tr>
<tr>
<td align="left"><code class="computeroutput">U </code></td>
<td align="left"><a href="numeric_concept.xml" target="_top">Numeric</a></td>
</tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754329"></a>Preconditions</h4></div></div></div>
<p>The value of u can be represented by the type <code class="computeroutput">T</code>. If
this is not true, an <code class="computeroutput">std::out_of_range</code> exception will be
thrown.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754355"></a>Complexity</h4></div></div></div>
<p>[Example:]</p>
<p>O(N log(N)) comparisons (both average and worst-case), where N is
last - first. [2]</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754371"></a>Header</h4></div></div></div>
<p><code class="filename"><a href="../../include/safe_cast.hpp" target="_top">#include
&lt;boost/numeric/safe_cast.hpp&gt; </a></code></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754391"></a>Example of use</h4></div></div></div>
<p>[A code fragment that illustrates how to use the function.]</p>
<pre class="programlisting">#include &lt;boost/numeric/safe_cast.hpp&gt;
#include &lt;boost/numeric/safe_integer.hpp&gt;
void f(){
safe_integer&lt;char&gt; i;
unsigned char j;
i = 1;
j = safe_cast&lt;unsigned char&gt;(i); // ok
i = -1;
j = safe_cast&lt;unsigned char&gt;(i); // throws std::out_of_range exception
i = 1024;
j = safe_cast&lt;unsigned char&gt;(i); // throws std::out_of_range exception
}</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="functions.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="functions.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="safe_compare.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,117 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>safe_compare</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="functions.html" title="Functions">
<link rel="prev" href="safe_cast.html" title="safe_cast">
<link rel="next" href="rationale/overflow.html" title="overflow">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="safe_cast.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="functions.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="rationale/overflow.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.safe_compare"></a>safe_compare</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe_compare.html#id754426">Synopsis</a></span></dt>
<dt><span class="section"><a href="safe_compare.html#id754449">Description</a></span></dt>
<dt><span class="section"><a href="safe_compare.html#id754474">Type requirements</a></span></dt>
<dt><span class="section"><a href="safe_compare.html#id754568">Header</a></span></dt>
<dt><span class="section"><a href="safe_compare.html#id754593">Example of use</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754426"></a>Synopsis</h4></div></div></div>
<p>safe_compare is really two functions:.</p>
<pre class="programlisting">template&lt;class T, class U&gt;
bool safe_compare::less_than(const T &amp; lhs, const U &amp; rhs);
template&lt;class T, class U&gt;
bool safe_compare::greater_than(const T &amp; lhs, const U &amp; rhs);</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754449"></a>Description</h4></div></div></div>
<p>With normal comparison operators, comparison of unsigned types to
signed types will be done by converting the unsigned type to a signed type
before comparing. Unfortunately this is not always possible. Most C++
compilers will emit an warning message when this is possible but won't
check that an error is made in the conversion. This function guarentees a
correct result regardless of the types of the arguments.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754474"></a>Type requirements</h4></div></div></div>
<p>[This section lists the requirements that must be satisfied by the
function's template parameters. If the function has no template
parameters, this section can be skipped. Example:]</p>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
<col align="left">
</colgroup>
<thead><tr>
<th align="left">Type</th>
<th align="left">Requirements</th>
</tr></thead>
<tbody>
<tr>
<td align="left"><code class="computeroutput">T</code></td>
<td align="left"><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a></td>
</tr>
<tr>
<td align="left"><code class="computeroutput">U </code></td>
<td align="left"><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a></td>
</tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754568"></a>Header</h4></div></div></div>
<p>[A link to the source code where the function header is
defined.]</p>
<p><a href="../../include/safe_compare.hpp" target="_top"><code class="filename">#include
&lt;boost/numeric/safe_compare.hpp&gt; </code></a></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id754593"></a>Example of use</h4></div></div></div>
<pre class="programlisting">#include &lt;boost/numeric/safe_compare.hpp&gt;
void f(){
int i = -1;
unsigned char i = 0x129;
unsigned int j = 1;
bool result;
result = j &lt; i; // incorrect result
result = safe_compare::less_than(j &lt; i); // correct result
}</pre>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="safe_cast.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="functions.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="rationale/overflow.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,117 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>safe_signed_range&lt;MIN, MAX&gt;</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="types.html" title="Types">
<link rel="prev" href="safe_unsigned_range.html" title="safe_unsigned_range&lt;MIN, MAX&gt;">
<link rel="next" href="safe.html" title="safe&lt;T&gt;">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="safe_unsigned_range.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="types.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="safe.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.safe_signed_range"></a>safe_signed_range&lt;MIN, MAX&gt;</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe_signed_range.html#id753667">Description</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#id753683">Template Parameters</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#id753800">Model of</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#id753814">Header</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#id753834">Example of use</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html#id753854">See Also</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753667"></a>Description</h4></div></div></div>
<p>This type holds a integer in the range [MIN, MAX]. It will throw a
<code class="computeroutput">std::out_of_range</code> exception for operation which would result
in assigning an integer value outside of this range.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753683"></a>Template Parameters</h4></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
<col align="left">
<col align="left">
</colgroup>
<thead><tr>
<th align="left">Parameter</th>
<th align="left">Requirements</th>
<th align="left">Description</th>
</tr></thead>
<tbody>
<tr>
<td align="left"><code class="computeroutput">MIN</code></td>
<td align="left">must be an integer literal</td>
<td align="left">The minimum integer value that this type may hold</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">MAX</code></td>
<td align="left">must be an integer literal</td>
<td align="left">The maximum integer value that this type may hold</td>
</tr>
<tr>
<td align="left"><code class="computeroutput"></code></td>
<td align="left">MIN &lt; MAX</td>
<td align="left">&#160;</td>
</tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753800"></a>Model of</h4></div></div></div>
<p><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753814"></a>Header</h4></div></div></div>
<p><code class="filename"><a href="../../include/safe_range.hpp" target="_top">#include
&lt;boost/safe_numerics/safe_range.hpp&gt;</a></code></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753834"></a>Example of use</h4></div></div></div>
<pre class="programlisting">#include &lt;safe/numeric/safe_range.hpp&gt;
void f(){
boost::numeric::safe_signed_integer&lt;7, 24&gt; i;
i = 0; // error
i = 9; // ok
i *= 9; // throws overflow exception
}</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753854"></a>See Also</h4></div></div></div>
<p><code class="computeroutput">std::out_of_range</code></p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="safe_unsigned_range.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="types.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="safe.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,118 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>safe_unsigned_range&lt;MIN, MAX&gt;</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="types.html" title="Types">
<link rel="prev" href="types.html" title="Types">
<link rel="next" href="safe_signed_range.html" title="safe_signed_range&lt;MIN, MAX&gt;">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="types.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="types.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="safe_signed_range.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h3 class="title">
<a name="safe_numerics.safe_unsigned_range"></a>safe_unsigned_range&lt;MIN, MAX&gt;</h3></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe_unsigned_range.html#id698117">Description</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#id698134">Template Parameters</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#id753478">Model of</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#id753595">Header</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#id753615">Example of use</a></span></dt>
<dt><span class="section"><a href="safe_unsigned_range.html#id753640">See Also</a></span></dt>
</dl></div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id698117"></a>Description</h4></div></div></div>
<p>This type holds a integer in the range [MIN, MAX]. It will throw a
<code class="computeroutput">std::out_of_range</code> exception for operation which would result
in assigning an integer value outside of this range.</p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id698134"></a>Template Parameters</h4></div></div></div>
<div class="informaltable"><table class="table">
<colgroup>
<col align="left">
<col align="left">
<col align="left">
</colgroup>
<thead><tr>
<th align="left">Parameter</th>
<th align="left">Requirements</th>
<th align="left">Description</th>
</tr></thead>
<tbody>
<tr>
<td align="left"><code class="computeroutput">MIN</code></td>
<td align="left">must be a positive integer literal</td>
<td align="left">The minimum integer value that this type may hold</td>
</tr>
<tr>
<td align="left"><code class="computeroutput">MAX</code></td>
<td align="left">must be a positive integer literal</td>
<td align="left">The maximum integer value that this type may hold</td>
</tr>
<tr>
<td align="left"><code class="computeroutput"></code></td>
<td align="left">MIN &lt; MAX</td>
<td align="left">&#160;</td>
</tr>
</tbody>
</table></div>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753478"></a>Model of</h4></div></div></div>
<p><a class="link" href="numeric.html" title="Numeric&lt;T&gt;">Numeric</a></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753595"></a>Header</h4></div></div></div>
<p><code class="filename"><a href="../../include/safe_range.hpp" target="_top">#include
&lt;safe/numeric/safe_range.hpp&gt;</a></code></p>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753615"></a>Example of use</h4></div></div></div>
<pre class="programlisting">#include &lt;safe/numeric/safe_range.hpp&gt;
void f(){
boost::numeric::safe_unsigned_integer&lt;7, 24&gt; i;
i = 0; // error
i = 9; // ok
i *= 9; // throws out_of_range exception
i = -1; // throws out_of_range exception
}</pre>
</div>
<div class="section">
<div class="titlepage"><div><div><h4 class="title">
<a name="id753640"></a>See Also</h4></div></div></div>
<p><code class="computeroutput">std::out_of_range</code></p>
</div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="types.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="types.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="safe_signed_range.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,46 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Tutorial</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="index.html" title="Safe Numerics">
<link rel="prev" href="introduction.html" title="Introduction">
<link rel="next" href="tutorial/1.html" title="Problem:arithmetic operations can yield in correct results.">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="introduction.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="tutorial/1.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="safe_numerics.tutorial"></a>Tutorial</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="tutorial/1.html">Problem:arithmetic operations can yield in correct results.</a></span></dt>
<dt><span class="section"><a href="tutorial/2.html">Problem:Undetected overflow</a></span></dt>
<dt><span class="section"><a href="tutorial/3.html">Problem:Implicit conversions change data values</a></span></dt>
</dl></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="introduction.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="tutorial/1.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,46 @@
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=US-ASCII">
<title>Types</title>
<link rel="stylesheet" href="boostbook.css" type="text/css">
<meta name="generator" content="DocBook XSL Stylesheets V1.76.1">
<link rel="home" href="index.html" title="Safe Numerics">
<link rel="up" href="index.html" title="Safe Numerics">
<link rel="prev" href="numeric.html" title="Numeric&lt;T&gt;">
<link rel="next" href="safe_unsigned_range.html" title="safe_unsigned_range&lt;MIN, MAX&gt;">
</head>
<body bgcolor="white" text="black" link="#0000FF" vlink="#840084" alink="#0000FF">
<table cellpadding="2" width="100%"><tr>
<td valign="top"><img alt="pre-boost" width="30%" height="30%" src="pre-boost.jpg"></td>
<td align="center"><a href="../../index.html">Home</a></td>
<td align="center"><a href="http://www.boost.org/doc/libs">Libraries</a></td>
<td align="center"><a href="http://www.boost.org/users/people.html">People</a></td>
<td align="center"><a href="http://www.boost.org/users/faq.html">FAQ</a></td>
<td align="center"><a href="../../more/index.htm">More</a></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="numeric.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="safe_unsigned_range.html"><img src="images/next.png" alt="Next"></a>
</div>
<div class="section">
<div class="titlepage"><div><div><h2 class="title" style="clear: both">
<a name="safe_numerics.types"></a>Types</h2></div></div></div>
<div class="toc"><dl>
<dt><span class="section"><a href="safe_unsigned_range.html">safe_unsigned_range&lt;MIN, MAX&gt;</a></span></dt>
<dt><span class="section"><a href="safe_signed_range.html">safe_signed_range&lt;MIN, MAX&gt;</a></span></dt>
<dt><span class="section"><a href="safe.html">safe&lt;T&gt;</a></span></dt>
</dl></div>
</div>
<table xmlns:rev="http://www.cs.rpi.edu/~gregod/boost/tools/doc/revision" width="100%"><tr>
<td align="left"></td>
<td align="right"><div class="copyright-footer">Copyright &#169; 2012 Robert Ramey<p>
<a href="???" target="_top">Subject to Boost Software License</a>
</p>
</div></td>
</tr></table>
<hr>
<div class="spirit-nav">
<a accesskey="p" href="numeric.html"><img src="images/prev.png" alt="Prev"></a><a accesskey="u" href="index.html"><img src="images/up.png" alt="Up"></a><a accesskey="h" href="index.html"><img src="images/home.png" alt="Home"></a><a accesskey="n" href="safe_unsigned_range.html"><img src="images/next.png" alt="Next"></a>
</div>
</body>
</html>

View File

@@ -0,0 +1,35 @@
void example1(){
// problem: undetected erroneous expression evaluation
try{
char x = 127;
char y = 2;
char z;
// this produces an invalid result !
z = x + y;
// it is the wrong result !!!
assert(z != 129);
// but assert fails to detect it since C++ implicitly
// converts variables to int before evaluating he expression!
assert(z != x + y);
std::cout << static_cast<int>(z) << " != " << x + y;
detected_msg(false);
}
catch(...){
assert(false); // never arrive here
}
// solution: replace char with safe<char>
try{
using namespace boost::numeric;
safe<char> x = 127;
safe<char> y = 2;
safe<char> z;
// rather than producing and invalid result an exception is thrown
z = x + y;
assert(false); // never arrive here
}
catch(std::range_error & e){
// which can catch here
std::cout << e.what();
detected_msg(true);
}
}

View File

@@ -0,0 +1,25 @@
void example2(){
// problem: undetected overflow in data type
try{
int x = INT_MAX;
// the following silently produces an incorrect result
++x;
//std::cout << x << " != " << -1;
detected_msg(false);
}
catch(...){
assert(false); // never arrive here
}
// solution: replace int with safe<int>
try{
using namespace boost::numeric;
safe<int> x = INT_MAX;
// throws exception when result is past maximum possible
++x;
assert(false); // never arrive here
}
catch(std::range_error & e){
std::cout << e.what();
detected_msg(true);
}
}

View File

@@ -0,0 +1,25 @@
void example3(){
// problem: undetected overflow in data type
try{
int x = INT_MAX;
// the following silently produces an incorrect result
++x;
//std::cout << x << " != " << -1;
detected_msg(false);
}
catch(...){
assert(false); // never arrive here
}
// solution: replace int with safe<int>
try{
using namespace boost::numeric;
safe<int> x = INT_MAX;
// throws exception when result is past maximum possible
++x;
assert(false); // never arrive here
}
catch(std::range_error & e){
std::cout << e.what();
detected_msg(true);
}
}

View File

@@ -0,0 +1,31 @@
#include "../include/safe_compare.hpp"
void example4(){
// problem: undetected underflow in data type
try{
unsigned int x = 0;
// the following silently produces an incorrect result
--x;
// because C implicitly converts mis-matched arguments to int
// the following assert suggests the result is correct
assert(boost::numeric::safe_compare::equal(x, -1));
// even though it's not !!!
std::cout << x << " != " << -1;
detected_msg(false);
}
catch(...){
assert(false); // never arrive here
}
// solution: replace unsigned int with safe<unsigned int>
try{
using namespace boost::numeric;
safe<unsigned int> x = 0;
// decrement unsigned to less than zero throws exception
--x;
assert(false); // never arrive here
}
catch(std::range_error & e){
std::cout << e.what();
detected_msg(true);
}
}

View File

@@ -0,0 +1,22 @@
#include <cassert>
#include <stdexcept>
#include <iostream>
#include "../include/safe_integer.hpp"
#include "../include/safe_compare.hpp"
void detected_msg(bool detected){
std::cout << (detected ? ": error detected!" : ": error NOT detected!: ") << std::endl;
}
#include "example1.cpp"
#include "example2.cpp"
#include "example3.cpp"
#include "example4.cpp"
int main(int argc, const char * argv[]){
example1();
example2();
example3();
example4();
}

View File

@@ -0,0 +1,28 @@
#ifndef BOOST_NUMERIC_CONCEPT_NUMERIC_HPP
#define BOOST_NUMERIC_CONCEPT_NUMERIC_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/limits.hpp>
#include <boost/static_assert.hpp>
template<class T>
struct Numeric {
// if your program traps here, you need to create a
// std::numeric_limits class for your type T. see
// see C++ standard 18.3.2.2
BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_specialized == true);
BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_bounded == true);
BOOST_STATIC_ASSERT(std::numeric_limits<T>::is_integer == true);
};
#endif // BOOST_NUMERIC_CONCEPT_NUMERIC_HPP

View File

@@ -0,0 +1,356 @@
#ifndef BOOST_NUMERIC_NUMERIC_HPP
#define BOOST_NUMERIC_NUMERIC_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/config.hpp>
#include <boost/limits.hpp>
#include <boost/integer.hpp>
#include <boost/mpl/integral_c.hpp>
#include <boost/mpl/plus.hpp>
#include <boost/mpl/min_max.hpp>
#include <boost/mpl/or.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/eval_if.hpp>
#include "concept/numeric.hpp"
#include "boost/concept/assert.hpp"
namespace boost {
namespace numeric {
namespace detail {
template<boost::intmax_t MAX, int BASE>
struct log;
template<int BASE>
struct log<0, BASE> : public
boost::mpl::integral_c<
boost::intmax_t ,
0
>
{};
template<int BASE>
struct log<-1, BASE> : public
boost::mpl::integral_c<
boost::intmax_t ,
0
>
{};
template<boost::intmax_t MAX, int BASE>
struct log : public
boost::mpl::integral_c<
boost::intmax_t ,
(1 + log<MAX / BASE, BASE>::value )
>
{};
template<boost::uintmax_t MAX, int BASE>
struct ulog;
template<int BASE>
struct ulog<0, BASE> : public
boost::mpl::integral_c<
boost::uintmax_t ,
0
>
{};
template<boost::uintmax_t MAX, int BASE>
struct ulog : public
boost::mpl::integral_c<
boost::uintmax_t ,
(1 + ulog<MAX / BASE, BASE>::value)
>
{};
} //detail
template<boost::intmax_t MIN, boost::intmax_t MAX>
class safe_signed_range;
template<boost::uintmax_t MIN, boost::uintmax_t MAX>
class safe_unsigned_range;
template<class Stored, class Derived>
class safe_range_base;
} // numeric
} // boost
namespace std {
template<boost::intmax_t MIN, boost::intmax_t MAX>
class numeric_limits< boost::numeric::safe_signed_range<MIN, MAX> > : public
numeric_limits<int>
{
typedef boost::numeric::safe_signed_range<MIN, MAX> T;
public:
BOOST_STATIC_CONSTEXPR T min() BOOST_NOEXCEPT { return T(MIN); }
BOOST_STATIC_CONSTEXPR T max() BOOST_NOEXCEPT { return T(MAX); }
BOOST_STATIC_CONSTEXPR T lowest() BOOST_NOEXCEPT { return T(MIN); }
BOOST_STATIC_CONSTANT(int,digits = (boost::numeric::detail::log<MAX, 2>::value)); // in base 2
BOOST_STATIC_CONSTANT(int,digits10 = (boost::numeric::detail::log<MAX, 10>::value)); // in base 10
BOOST_STATIC_CONSTANT(int,max_digits10 = digits10);
};
template<boost::uintmax_t MIN, boost::uintmax_t MAX>
class numeric_limits< boost::numeric::safe_unsigned_range<MIN, MAX> > : public
numeric_limits<unsigned int>
{
typedef boost::numeric::safe_unsigned_range<MIN, MAX> T;
public:
BOOST_STATIC_CONSTEXPR T min() BOOST_NOEXCEPT { return T(MIN); }
BOOST_STATIC_CONSTEXPR T max() BOOST_NOEXCEPT { return T(MAX); }
BOOST_STATIC_CONSTEXPR T lowest() BOOST_NOEXCEPT { return T(MIN); }
BOOST_STATIC_CONSTEXPR int digits = boost::numeric::detail::ulog<MAX, 2>::value; // in base 2
BOOST_STATIC_CONSTEXPR int digits10 = boost::numeric::detail::ulog<MAX, 10>::value; // in base 10
BOOST_STATIC_CONSTEXPR int max_digits10 = digits10;
};
template<class Stored, class Derived>
class numeric_limits< boost::numeric::safe_range_base<Stored, Derived> > : public
numeric_limits<Stored>
{};
} // std
namespace boost {
namespace numeric {
// can't use is_signed type traits as these are defined ONLY for C++
// primitive types. They are are not (by design) implemented in terms
// of numeric_limits so they aren't helpful to us. We leave then in the
// boost/numeric namespace to distinguish them form the "official" type
// traits !
template<class T>
struct is_signed : public
boost::mpl::integral_c<
bool,
std::numeric_limits<T>::is_signed
>
{
BOOST_CONCEPT_ASSERT((Numeric<T>));
};
template<class T>
struct is_unsigned : public
boost::mpl::integral_c<
bool,
! std::numeric_limits<T>::is_signed
>
{
BOOST_CONCEPT_ASSERT((Numeric<T>));
};
// return the number of bits in a type excluding any sign
template<class T>
struct digits : public
boost::mpl::integral_c<int, std::numeric_limits<T>::digits>
{
BOOST_CONCEPT_ASSERT((Numeric<T>));
};
// return maximum bits in types T and U
template<class T, class U>
struct max_digits : public
boost::mpl::max<
digits<T>,
digits<U>
>::type
{};
// return the number of bits in a type
template<class T>
struct bits : public
boost::mpl::plus<
typename boost::mpl::integral_c<int, std::numeric_limits<T>::digits>,
typename boost::mpl::integral_c<int, std::numeric_limits<T>::is_signed>
>
{};
// return maximum bits in types T and U
template<class T, class U>
struct max_bits : public
boost::mpl::max<
bits<T>,
bits<U>
>::type
{};
// return # of bit in the result of an addition of two types
template<class T, class U>
struct addition_result_bits : public
boost::mpl::plus<
max_digits<T, U>,
// if either is signed
typename boost::mpl::eval_if<
typename boost::mpl::or_<
is_signed<T>,
is_signed<U>
>,
// add one guard bit and one sign bit
boost::mpl::integral_c<int, 2>,
// add one guard bit to detect overflow
boost::mpl::integral_c<int, 1>
>::type
>
{};
// return smallest type that can hold the sum of types T and U
template<class T, class U>
struct addition_result_type {
typedef typename boost::mpl::if_<
typename boost::mpl::or_<
is_signed<T>,
is_signed<U>
>,
typename boost::int_t<
boost::mpl::min<
bits<boost::intmax_t>,
addition_result_bits<T, U>
>::type::value
>::least,
typename boost::uint_t<
boost::mpl::min<
bits<boost::uintmax_t>,
addition_result_bits<T, U>
>::type::value
>::least
>::type type;
};
// return # of bit in the result of an subtraction of two types
template<class T, class U>
struct subtraction_result_bits : public
boost::mpl::plus<
max_digits<T, U>,
// add one guard bit and one sign bit
boost::mpl::integral_c<int, 2>
>
{};
// return smallest type that can hold the difference of types T and U
template<class T, class U>
struct subtraction_result_type {
typedef typename boost::int_t<
boost::mpl::min<
bits<boost::intmax_t>,
subtraction_result_bits<T, U>
>::type::value
>::least type;
};
// return # of bits in the result of an product of two types
template<class T, class U>
struct multiply_result_bits : public
boost::mpl::plus<
digits<T>,
digits<U>,
// if either is signed
typename boost::mpl::eval_if<
typename boost::mpl::or_<
is_signed<T>,
is_signed<U>
>,
// add one sign bit
boost::mpl::integral_c<int, 1>,
boost::mpl::integral_c<int, 0>
>::type
>
{};
// return smallest type that can hold the product of types T and U
template<class T, class U>
struct multiply_result_type {
typedef typename boost::mpl::if_<
typename boost::mpl::or_<
is_signed<T>,
is_signed<U>
>,
typename boost::int_t<
boost::mpl::min<
bits<boost::intmax_t>,
multiply_result_bits<T, U>
>::type::value
>::least,
typename boost::uint_t<
boost::mpl::min<
bits<boost::uintmax_t>,
multiply_result_bits<T, U>
>::type::value
>::least
>::type type;
};
// return # of bits in the result of an quotient of two types
template<class T, class U>
struct division_result_bits : public
boost::mpl::plus<
digits<T>,
// if either is signed
typename boost::mpl::eval_if<
typename boost::mpl::or_<
is_signed<T>,
is_signed<U>
>,
// add one sign bit
boost::mpl::integral_c<int, 1>,
boost::mpl::integral_c<int, 0>
>::type
>
{};
// return smallest type that can hold the quotient of types T and U
template<class T, class U>
struct division_result_type {
typedef typename boost::mpl::if_<
typename boost::mpl::or_<
is_signed<T>,
is_signed<U>
>,
typename boost::int_t<
boost::mpl::min<
bits<boost::intmax_t>,
division_result_bits<T, U>
>::type::value
>::least,
typename boost::uint_t<
boost::mpl::min<
bits<boost::uintmax_t>,
division_result_bits<T, U>
>::type::value
>::least
>::type type;
};
// return the type which results from usage of one the
// operators &, |, ^, <<, >>
template<class T, class U>
struct logical_result_type {
typedef typename boost::mpl::if_<
typename boost::mpl::or_<
is_signed<T>,
is_signed<U>
>,
typename boost::int_t<
max_bits<T, U>::value
>::least,
typename boost::uint_t<
max_bits<T, U>::value
>::least
>::type type;
};
} // numeric
} // boost
#endif // BOOST_NUMERIC_NUMERIC_HPP

View File

@@ -0,0 +1,32 @@
#ifndef BOOST_NUMERIC_SAFE_OVERFLOW_HPP
#define BOOST_NUMERIC_SAFE_OVERFLOW_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <stdexcept>
#include <boost/config.hpp>
namespace boost {
namespace numeric {
#ifndef BOOST_NO_EXCEPTIONS
inline void overflow(char const * const msg) {
throw std::range_error(msg);
}
#else
void overflow(char const * const msg);
#endif
} // namespace numeric
} // namespace boost
#endif // BOOST_NUMERIC_SAFE_OVERFLOW_HPP

View File

@@ -0,0 +1,112 @@
#ifndef BOOST_NUMERIC_SAFE_CAST_HPP
#define BOOST_NUMERIC_SAFE_CAST_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/limits.hpp>
#include <boost/integer.hpp>
#include <boost/mpl/min_max.hpp>
#include <boost/mpl/plus.hpp>
#include "numeric.hpp"
#include "overflow.hpp"
namespace boost {
namespace numeric {
namespace detail {
// simple case when signess are the same.
template<bool TS, bool US>
struct safe_cast {
template<class T, class U>
inline static T invoke(const U & u){
if(u > std::numeric_limits<T>::max())
overflow("safe range overflow");
if(u < std::numeric_limits<T>::min())
overflow("safe range underflow");
return static_cast<T>(u);
}
};
// T signed <- U unsigned
template<>
struct safe_cast<true, false> {
template<class T>
struct sbits : public
boost::mpl::min<
typename boost::mpl::integral_c<
int,
std::numeric_limits<boost::intmax_t>::digits
>,
typename boost::mpl::plus<
typename boost::mpl::integral_c<
int,
std::numeric_limits<T>::digits
>,
typename boost::mpl::integral_c<int, 1>
>
>::type
{};
template<class T, class U>
inline static T invoke(const U & u){
// figure # of bits in U
// if that # == maximum supported
if(bits<U>::value == bits<boost::uintmax_t>::value)
// just cast to signed and
// choke if it's negative
if(static_cast<boost::intmax_t>(u) < 0)
overflow("safe range overflow");
typedef typename boost::int_t<
sbits<U>::value
>::fast signed_u_type;
return safe_cast<true, true>::invoke<T>(
static_cast<signed_u_type>(u)
);
}
};
// T unsigned <- U signed
template<>
struct safe_cast<false, true> {
template<class T, class U>
inline static T invoke(const U & u){
if(u < 0)
overflow("safe range underflow");
typedef typename boost::uint_t<
boost::mpl::integral_c<
int,
std::numeric_limits<U>::digits
>::value
>::fast unsigned_u_type;
return safe_cast<false, false>::invoke<T>(
static_cast<const unsigned_u_type &>(u)
);
}
};
} // detail
template<class T, class U>
T safe_cast(const U & u) {
T t = detail::safe_cast<
boost::numeric::is_signed<T>::value,
boost::numeric::is_signed<U>::value
>::template invoke<T>(u);
return t;
}
} // numeric
} // boost
#endif // BOOST_NUMERIC_SAFE_CAST_HPP

View File

@@ -0,0 +1,155 @@
#ifndef BOOST_NUMERIC_SAFE_COMPARE_HPP
#define BOOST_NUMERIC_SAFE_COMPARE_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/limits.hpp>
#include <boost/integer.hpp>
#include <boost/mpl/if.hpp>
#include <boost/mpl/identity.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/type_traits/make_unsigned.hpp>
#include "numeric.hpp"
namespace boost {
namespace numeric {
namespace safe_compare {
namespace detail {
// note make_unsigned needs more research. We use the one
// from boost type_traits. BUT boost type_traits only handles
// C primitives. What we really want is something which works
// for any type T such that std::numeric_limits<T>::is_signed is true.
template<typename T>
struct make_unsigned {
typedef typename boost::mpl::if_<
boost::is_integral<T>,
typename boost::make_unsigned<T>,
typename boost::mpl::identity<T>
>::type::type type;
};
// both arguments unsigned or signed
template<bool TS, bool US>
struct less_than {
template<class T, class U>
static bool invoke(const T & t, const U & u){
return t < u;
}
};
// T unsigned, U signed
template<>
struct less_than<false, true> {
template<class T, class U>
static bool invoke(const T & t, const U & u){
if(u < 0)
return false;
return less_than<false, false>::invoke(
t,
static_cast<const typename make_unsigned<U>::type &>(u)
);
}
};
// T signed, U unsigned
template<>
struct less_than<true, false> {
template<class T, class U>
static bool invoke(const T & t, const U & u){
if(t < 0)
return true;
return less_than<false, false>::invoke(
static_cast<const typename make_unsigned<T>::type &>(t),
u
);
}
};
} // detail
template<class T, class U>
bool less_than(const T & lhs, const U & rhs) {
return detail::less_than<
boost::numeric::is_signed<T>::value,
boost::numeric::is_signed<U>::value
>::template invoke(lhs, rhs);
}
template<class T, class U>
bool greater_than_equal(const T & lhs, const U & rhs) {
return less_than(rhs, lhs);
}
namespace detail {
// both arguments unsigned or signed
template<bool TS, bool US>
struct greater_than {
template<class T, class U>
static bool invoke(const T & t, const U & u){
return t > u;
}
};
// T unsigned, U signed
template<>
struct greater_than<false, true> {
template<class T, class U>
static bool invoke(const T & t, const U & u){
if(u < 0)
return true;
return greater_than<false, false>::invoke(
t,
static_cast<const typename make_unsigned<U>::type &>(u)
);
}
};
// T signed, U unsigned
template<>
struct greater_than<true, false> {
template<class T, class U>
static bool invoke(const T & t, const U & u){
if(t < 0)
return false;
return greater_than<false, false>::invoke(
static_cast<const typename make_unsigned<T>::type &>(t),
u
);
}
};
} // detail
template<class T, class U>
bool greater_than(const T & lhs, const U & rhs) {
return detail::greater_than<
boost::numeric::is_signed<T>::value,
boost::numeric::is_signed<U>::value
>::template invoke(lhs, rhs);
}
template<class T, class U>
bool less_than_equal(const T & lhs, const U & rhs) {
return greater_than(rhs, lhs);
}
template<class T, class U>
bool equal(const T & lhs, const U & rhs) {
return ! less_than(lhs, rhs) && ! greater_than(lhs, rhs);
}
template<class T, class U>
bool not_equal(const T & lhs, const U & rhs) {
return ! equal(lhs, rhs);
}
} // safe_compare
} // numeric
} // boost
#endif // BOOST_NUMERIC_SAFE_COMPARE_HPP

View File

@@ -0,0 +1,81 @@
#ifndef BOOST_NUMERIC_SAFE_INTEGER_HPP
#define BOOST_NUMERIC_SAFE_INTEGER_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <boost/limits.hpp>
#include <boost/mpl/if.hpp>
#include "safe_range.hpp"
namespace boost {
namespace numeric {
namespace detail{
template<class T>
struct safe_integer_base {
typedef typename boost::mpl::if_c<
std::numeric_limits<T>::is_signed,
safe_signed_range<
boost::integer_traits<T>::const_min,
boost::integer_traits<T>::const_max
>,
safe_unsigned_range<
boost::integer_traits<T>::const_min,
boost::integer_traits<T>::const_max
>
>::type type;
};
} // detail
template<class T>
struct safe : public detail::safe_integer_base<T>::type {
typedef typename detail::safe_integer_base<T>::type base_type;
// verify that std::numeric_limits has been specialized for this type
BOOST_STATIC_ASSERT_MSG(
std::numeric_limits<T>::is_specialized,
"std::numeric_limits<T> has not been specialized for this type"
);
// verify that T is an integer type
BOOST_STATIC_ASSERT_MSG(
std::numeric_limits<T>::is_integer,
"T is not an integer type"
);
safe(){}
template<class U>
safe(const U & u) :
detail::safe_integer_base<T>::type(u)
{}
};
} // numeric
} // boost
#include <boost/config.hpp> // BOOST_NOEXCEPT
namespace std {
template<class T>
class numeric_limits< boost::numeric::safe<T> > : public
numeric_limits<T>
{
typedef boost::numeric::safe<T> SI;
public:
BOOST_STATIC_CONSTEXPR SI min() BOOST_NOEXCEPT { return numeric_limits<T>::min(); }
BOOST_STATIC_CONSTEXPR SI max() BOOST_NOEXCEPT { return numeric_limits<T>::max(); }
BOOST_STATIC_CONSTEXPR SI lowest() BOOST_NOEXCEPT { return numeric_limits<T>::min(); }
};
} // std
#endif // BOOST_NUMERIC_SAFE_INTEGER_HPP

View File

@@ -0,0 +1,795 @@
#ifndef BOOST_NUMERIC_SAFE_RANGE_HPP
#define BOOST_NUMERIC_SAFE_RANGE_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//#include <algorithm>
#include "numeric.hpp"
#include "safe_compare.hpp"
#include "safe_cast.hpp"
#include "overflow.hpp"
#include <boost/limits.hpp>
#include <boost/concept_check.hpp>
#include <boost/integer.hpp>
#include <boost/type_traits/is_convertible.hpp>
#include <boost/type_traits/is_same.hpp>
#include <boost/type_traits/is_integral.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/mpl/min_max.hpp>
#include <boost/mpl/less_equal.hpp>
#include <boost/mpl/greater.hpp>
#include <boost/mpl/integral_c.hpp>
#include <ostream>
#include <istream>
#include <stdexcept>
namespace boost {
namespace numeric {
namespace detail {
template<bool TS, bool US>
struct check_addition_overflow{};
// both arguments unsigned
template<>
struct check_addition_overflow<false, false> {
template<class T, class U>
static typename addition_result_type<T, U>::type
add(const T & t, const U & u){
typedef typename addition_result_type<T, U>::type result_type;
result_type tmp;
tmp = static_cast<result_type>(t) + static_cast<result_type>(u);
if(safe_compare::less_than(tmp, t))
overflow("safe range addition out of range");
return tmp;
}
};
// T unsigned, U signed
template<>
struct check_addition_overflow<false, true> {
template<class T, class U>
static typename addition_result_type<T, U>::type
add(const T & t, const U & u){
if(u > 0)
return check_addition_overflow<false, false>::add(t, u);
return t + u;
}
};
// T signed, U unsigned
template<>
struct check_addition_overflow<true, false> {
template<class T, class U>
static typename addition_result_type<T, U>::type
add(const T & t, const U & u){
if(t > 0)
return check_addition_overflow<false, false>::add(t, u);
return t + u;
}
};
// both arguments signed
template<>
struct check_addition_overflow<true, true> {
template<class T, class U>
static typename addition_result_type<T, U>::type
add(const T & t, const U & u){
if(t > 0)
check_addition_overflow<false, true>::add(t, u);
if(u < 0){
typedef typename addition_result_type<T, U>::type result_type;
result_type tmp;
tmp = static_cast<result_type>(t) + static_cast<result_type>(u);
if(safe_compare::greater_than(tmp, t))
overflow("safe range addition out of range");
return tmp;
}
return t + u;
}
};
template<bool TS, bool US>
struct check_multiplication_overflow;
// both arguments unsigned
template<>
struct check_multiplication_overflow<false, false> {
template<class T, class U>
static void invoke(const T & t, const U & u){
char const * const msg = "safe range multiplication out of range";
typedef boost::uintmax_t accumulator_type;
const int temp_bits = bits<accumulator_type>::value / 2;
typedef typename boost::uint_t<temp_bits>::least temp_type;
temp_type a = (static_cast<accumulator_type>(t) >> temp_bits);
temp_type c = (static_cast<accumulator_type>(u) >> temp_bits);
if(0 != a && 0 != c)
overflow(msg);
temp_type b = static_cast<temp_type>(t);
if((static_cast<accumulator_type>(b) * static_cast<accumulator_type>(c) >> temp_bits) > 0)
overflow(msg);
temp_type d = static_cast<const temp_type>(u);
if(0 != (static_cast<accumulator_type>(a) * static_cast<accumulator_type>(d) >> temp_bits))
overflow(msg);
}
};
// T unsigned, U signed
template<>
struct check_multiplication_overflow<false, true> {
template<class T, class U>
static void invoke(const T & t, const U & u){
check_multiplication_overflow<false, false>::invoke(
t,
u < 0 ? -u : u
);
}
};
// T signed, U unsigned
template<>
struct check_multiplication_overflow<true, false> {
template<class T, class U>
static void invoke(const T & t, const U & u){
check_multiplication_overflow<false, false>::invoke(
t < 0 ? -t : t,
u
);
}
};
// both arguments signed, signed
template<>
struct check_multiplication_overflow<true, true> {
template<class T, class U>
static void invoke(const T & t, const U & u){
check_multiplication_overflow<false, false>::invoke(
t < 0 ? -t : t,
u < 0 ? -u : u
);
}
};
} // detail
template<
class Stored,
class Derived
>
class safe_range_base {
Derived &
derived() {
return static_cast<Derived &>(*this);
}
const Derived &
derived() const {
return static_cast<const Derived &>(*this);
}
template<class T>
Stored validate(const T & t) const {
return derived().validate(t);
}
Stored m_t;
protected:
////////////////////////////////////////////////////////////
// constructors
// default constructor
safe_range_base() {}
// copy constructor
safe_range_base(const safe_range_base & t) :
m_t(t.m_t)
{}
template<class T>
safe_range_base(const T & t)
{
// verify that this is convertible to the storable type
BOOST_STATIC_ASSERT(( boost::is_convertible<T, Stored>::value ));
validate(t);
m_t = static_cast<const Stored &>(t);
}
typedef Stored stored_type;
public:
template<class T>
Derived & operator=(const T & rhs){
m_t = validate(rhs);
return derived();
}
template<class T>
Derived & operator+=(const T & rhs){
*this = *this + rhs;
return derived();
}
template<class T>
Derived & operator-=(const T & rhs){
*this = *this - rhs;
return derived();
}
template<class T>
Derived & operator*=(const T & rhs){
*this = *this * rhs;
return derived();
}
template<class T>
Derived & operator/=(const T & rhs){
*this = *this / rhs;
return derived();
}
template<class T>
Derived & operator%=(const T & rhs){
*this = *this % rhs;
return derived();
}
template<class T>
Derived & operator|=(const T & rhs){
*this = *this | rhs;
return derived();
}
template<class T>
Derived & operator&=(const T & rhs){
*this = *this & rhs;
return derived();
}
template<class T>
Derived & operator^=(const T & rhs){
*this = *this * rhs;
return derived();
}
template<class T>
Derived & operator>>=(const T & rhs){
*this = *this >> rhs;
return derived();
}
template<class T>
Derived & operator<<=(const T & rhs){
*this = *this << rhs;
return derived();
}
Derived operator++(){
*this = *this + 1;
return derived();
}
Derived operator--(){
*this = *this - 1;
return derived();
}
Derived operator++(int){
Derived rvalue = *this;
m_t = validate(*this + 1);
return rvalue;
}
Derived & operator--(int){
Derived rvalue = *this;
m_t = validate(*this - 1);
return rvalue;
}
Derived operator-() const {
return validate(- m_t);
}
Derived operator~() const {
return validate(~m_t);
}
/////////////////////////////////////////////////////////////////
// comparison operators
template<class U>
bool operator<(const U & rhs) const {
return safe_compare::less_than(m_t, rhs);
}
template<class U>
bool operator>(const U & rhs) const {
return safe_compare::greater_than(m_t, rhs);
}
template<class U>
bool operator==(const U & rhs) const {
return safe_compare::equal(m_t, rhs);
}
template<class U>
bool inline operator!=(const U & rhs) const {
return ! safe_compare::equal(m_t,rhs);
}
template<class U>
bool inline operator>=(const U & rhs) const {
return ! safe_compare::less_than(m_t, rhs);
}
template<class U>
bool inline operator<=(const U & rhs) const {
return ! safe_compare::greater_than(m_t, rhs);
}
/////////////////////////////////////////////////////////////////
// addition
// case 1 - no overflow possible
template<class U>
typename boost::enable_if<
typename boost::mpl::less_equal<
addition_result_bits<Stored, U>,
// note presumption that size(boost::uintmax) == size(boost::intmax)
bits<boost::uintmax_t>
>,
typename addition_result_type<Stored, U>::type
>::type
inline operator+(const U & rhs) const {
typedef typename addition_result_type<Stored, U>::type result_type;
return static_cast<result_type>(m_t) + static_cast<result_type>(rhs);
}
// case 2 - overflow possible - must be checked at run time
template<class U>
typename boost::enable_if<
typename boost::mpl::greater<
addition_result_bits<Stored, U>,
// note presumption that size(boost::uintmax) == size(boost::intmax)
bits<boost::uintmax_t>
>,
typename addition_result_type<Stored, U>::type
>::type
inline operator+(const U & rhs) const {
return detail::check_addition_overflow<
boost::numeric::is_signed<Stored>::value,
boost::numeric::is_signed<U>::value
>::add(m_t, rhs);
}
/////////////////////////////////////////////////////////////////
// subtraction
template<class U>
typename boost::enable_if<
typename boost::mpl::less_equal<
subtraction_result_bits<Stored, U>,
bits<boost::intmax_t>
>,
typename subtraction_result_type<Stored, U>::type
>::type
inline operator-(const U & rhs) const {
typedef typename subtraction_result_type<Stored, U>::type result_type;
return static_cast<result_type>(m_t) - static_cast<result_type>(rhs);
}
template<class U>
typename boost::enable_if<
typename boost::mpl::greater<
subtraction_result_bits<Stored, U>,
bits<boost::intmax_t>
>,
typename subtraction_result_type<Stored, U>::type
>::type
inline operator-(const U & rhs) const {
typedef typename subtraction_result_type<Stored, U>::type result_type;
result_type tmp;
tmp = static_cast<result_type>(m_t) - static_cast<result_type>(rhs);
if(tmp > static_cast<result_type>(m_t))
overflow("safe range subtraction out of range");
return tmp;
}
/////////////////////////////////////////////////////////////////
// multiplication
template<class U>
typename boost::enable_if<
typename boost::mpl::less_equal<
multiply_result_bits<Stored, U>,
// note presumption that size(boost::uintmax) == size(boost::intmax)
bits<boost::uintmax_t>
>,
typename multiply_result_type<Stored, U>::type
>::type
inline operator*(const U & rhs) const {
typedef typename multiply_result_type<Stored, U>::type result_type;
return static_cast<result_type>(m_t) * static_cast<result_type>(rhs);
}
// implement multiply larger numbers. This is
// intended to function for all combinations of
// signed/unsigned types when the product exceeds
// the maximum integer size
template<class U>
typename boost::enable_if<
typename boost::mpl::greater<
multiply_result_bits<Stored, U>,
bits<boost::uintmax_t>
>,
boost::uintmax_t
>::type
inline operator*(const U & rhs) const {
detail::check_multiplication_overflow<
boost::numeric::is_signed<Stored>::value,
boost::numeric::is_signed<U>::value
>::invoke(m_t, rhs);
return static_cast<boost::uintmax_t>(m_t) * static_cast<boost::uintmax_t>(rhs);
}
/////////////////////////////////////////////////////////////////
// division
// simple case - default rules work
template<class U>
typename boost::enable_if<
typename boost::mpl::less_equal<
division_result_bits<Stored, U>,
bits<boost::uintmax_t>
>,
typename division_result_type<Stored, U>::type
>::type
inline operator/(const U & rhs) const {
if(0 == rhs)
throw std::domain_error("Divide by zero");
return safe_cast<typename division_result_type<Stored, U>::type>(m_t / rhs);
}
// special case - possible overflow
template<class U>
typename boost::enable_if<
typename boost::mpl::greater<
division_result_bits<Stored, U>,
bits<boost::uintmax_t>
>,
typename division_result_type<Stored, U>::type
>::type
inline operator/(const U & rhs) const {
if(0 == rhs)
throw std::domain_error("Divide by zero");
return safe_cast<typename division_result_type<Stored, U>::type>(m_t / rhs);
}
/////////////////////////////////////////////////////////////////
// modulus
template<class U>
typename division_result_type<Stored, U>::type
inline operator%(const U & rhs) const {
if(0 == rhs)
throw std::domain_error("Divide by zero");
return safe_cast<typename division_result_type<Stored, U>::type>(m_t % rhs);
}
/////////////////////////////////////////////////////////////////
// logical operators
template<class U>
typename logical_result_type<Stored, U>::type
inline operator|(const U & rhs) const {
typedef typename logical_result_type<Stored, U>::type result_type;
return static_cast<result_type>(m_t) | static_cast<result_type>(rhs);
}
template<class U>
typename logical_result_type<Stored, U>::type
inline operator&(const U & rhs) const {
typedef typename logical_result_type<Stored, U>::type result_type;
return static_cast<result_type>(m_t) & static_cast<result_type>(rhs);
}
template<class U>
typename logical_result_type<Stored, U>::type
inline operator^(const U & rhs) const {
typedef typename logical_result_type<Stored, U>::type result_type;
return static_cast<result_type>(m_t) ^ static_cast<result_type>(rhs);
}
template<class U>
Stored inline operator>>(const U & rhs) const {
typedef typename logical_result_type<Stored, U>::type result_type;
return static_cast<result_type>(m_t) >> static_cast<result_type>(rhs);
}
template<class U>
Stored inline operator<<(const U & rhs) const {
typedef typename logical_result_type<Stored, U>::type result_type;
return static_cast<result_type>(m_t) << static_cast<result_type>(rhs);
}
/////////////////////////////////////////////////////////////////
// casting operators for intrinsic integers
operator stored_type () const {
return m_t;
}
};
/////////////////////////////////////////////////////////////////
// Note: the following global operators will be only found via
// argument dependent lookup. So they won't conflict any
// other global operators for types in namespaces other than
// boost::numeric
// These should catch things like U < safe_range_base<...> and implement them
// as safe_range_base<...> >= U which should be handled above.
/////////////////////////////////////////////////////////////////
// binary operators
// comparison operators
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
bool
>::type
operator<(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs >= lhs;
}
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
bool
>::type
inline operator>(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs <= lhs;
}
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
bool
>::type
inline operator==(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs == lhs;
}
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
bool
>::type
inline operator!=(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs != rhs;
}
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
bool
>::type
inline operator>=(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs < lhs;
}
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
bool
>::type
inline operator<=(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs > lhs;
}
// addition
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
typename addition_result_type<T, Stored>::type
>::type
inline operator+(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs + lhs;
}
// subtraction
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
typename subtraction_result_type<T, Stored>::type
>::type
inline operator-(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
typename subtraction_result_type<T, Stored>::type tmp;
tmp = rhs - lhs;
return - tmp;
}
// multiplication
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
typename multiply_result_type<T, Stored>::type
>::type
inline operator*(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs * lhs;
}
// division
// special case - possible overflow
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
typename division_result_type<T, Stored>::type
>::type
inline operator/(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
if(safe_compare::equal(0, rhs))
throw std::domain_error("Divide by zero");
return static_cast<
typename division_result_type<T, Stored>::type
>(lhs / static_cast<const Stored &>(rhs));
}
// modulus
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
typename division_result_type<T, Stored>::type
>::type
inline operator%(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
if(safe_compare::equal(0, rhs))
throw std::domain_error("Divide by zero");
return static_cast<
typename division_result_type<T, Stored>::type
>(lhs % static_cast<const Stored &>(rhs));
}
// logical operators
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
typename multiply_result_type<T, Stored>::type
>::type
inline operator|(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs | lhs;
}
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
typename logical_result_type<T, Stored>::type
>::type
inline operator&(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs & lhs;
}
template<class T, class Stored, class Derived>
typename boost::enable_if<
boost::is_integral<T>,
typename logical_result_type<T, Stored>::type
>::type
inline operator^(const T & lhs, const safe_range_base<Stored, Derived> & rhs) {
return rhs ^ lhs;
}
/////////////////////////////////////////////////////////////////
// higher level types implemented in terms of safe_range_base
namespace detail {
template<
boost::intmax_t MIN,
boost::intmax_t MAX
>
struct signed_stored_type {
// double check that MIN < MAX
typedef typename boost::int_t<
boost::mpl::max<
typename boost::numeric::detail::log<MIN, 2>,
typename boost::numeric::detail::log<MAX, 2>
>::type::value
>::least type;
};
template<
boost::uintmax_t MIN,
boost::uintmax_t MAX
>
struct unsigned_stored_type {
// calculate max(abs(MIN, MAX))
typedef typename boost::uint_t<
boost::mpl::max<
typename boost::numeric::detail::ulog<MIN, 2>,
typename boost::numeric::detail::ulog<MAX, 2>
>::type::value
>::least type;
};
} // detail
/////////////////////////////////////////////////////////////////
// safe_signed_range
template<
boost::intmax_t MIN,
boost::intmax_t MAX
>
class safe_signed_range : public
safe_range_base<
typename detail::signed_stored_type<MIN, MAX>::type,
safe_signed_range<MIN, MAX>
>
{
BOOST_STATIC_ASSERT_MSG(
MIN < MAX,
"minimum must be less than maximum"
);
public:
typedef typename boost::numeric::safe_range_base<
typename detail::signed_stored_type<MIN, MAX>::type,
safe_signed_range<MIN, MAX>
> base;
typedef typename detail::signed_stored_type<MIN, MAX>::type stored_type;
template<class T>
stored_type validate(const T & t) const {
const boost::intmax_t tx = t;
if(MAX < tx
|| MIN > tx
)
overflow("safe range out of range");
return static_cast<stored_type>(t);
}
safe_signed_range(){}
template<class T>
safe_signed_range(const T & t) :
base(t)
{}
};
template<
boost::intmax_t MIN,
boost::intmax_t MAX
>
std::ostream & operator<<(std::ostream & os, const safe_signed_range<MIN, MAX> & t){
return os << static_cast<const typename safe_signed_range<MIN, MAX>::stored_type &>(t);
}
template<
boost::intmax_t MIN,
boost::intmax_t MAX
>
std::istream & operator>>(std::istream & is, safe_signed_range<MIN, MAX> & t){
typename safe_signed_range<MIN, MAX>::stored_type tx;
is >> tx;
t = tx;
return is;
}
/////////////////////////////////////////////////////////////////
// safe_unsigned_range
template<
boost::uintmax_t MIN,
boost::uintmax_t MAX
>
class safe_unsigned_range : public
safe_range_base<
typename detail::unsigned_stored_type<MIN, MAX>::type,
safe_unsigned_range<MIN, MAX>
>
{
BOOST_STATIC_ASSERT_MSG(
MIN < MAX,
"minimum must be less than maximum"
);
public:
typedef typename boost::numeric::safe_range_base<
typename detail::unsigned_stored_type<MIN, MAX>::type,
safe_unsigned_range<MIN, MAX>
> base;
typedef typename detail::unsigned_stored_type<MIN, MAX>::type stored_type;
template<class T>
stored_type validate(const T & t) const {
const boost::uintmax_t tx = t;
if(MAX < tx
|| MIN > tx
)
overflow("safe range out of range");
return static_cast<stored_type>(t);
}
safe_unsigned_range(){}
template<class T>
safe_unsigned_range(const T & t) :
base(t)
{}
};
template<
boost::uintmax_t MIN,
boost::uintmax_t MAX
>
std::ostream & operator<<(std::ostream & os, const safe_unsigned_range<MIN, MAX> & t){
return os << static_cast<const typename safe_unsigned_range<MIN, MAX>::stored_type &>(t);
}
template<
boost::uintmax_t MIN,
boost::uintmax_t MAX
>
std::istream & operator>>(std::istream & is, safe_unsigned_range<MIN, MAX> & t){
typename safe_unsigned_range<MIN, MAX>::stored_type tx;
is >> tx;
t = tx;
return is;
}
} // numeric
} // boost
#endif // BOOST_NUMERIC_SAFE_RANGE_HPP

View File

@@ -0,0 +1,87 @@
#include "../include/safe_range.hpp"
void test1(){
boost::numeric::safe_signed_range<-64, 63> x, y, z;
x = 1;
y = 2;
z = 3;
z = x + y;
z = x - y;
short int yi, zi;
zi = x;
typedef boost::mpl::print<
boost::numeric::addition_result_bits<
boost::numeric::safe_signed_range<-64, 63>,
int
>::type
>::type t1;
typedef boost::mpl::print<
boost::numeric::bits<
boost::numeric::safe_signed_range<-64, 63>
>::type
>::type t2;
typedef boost::mpl::print<
boost::numeric::bits<
int
>::type
>::type t3;
zi = x + yi;
z = x + yi;
}
void test2(){
boost::numeric::safe_unsigned_range<0, 64> x, y, z;
x = 1;
y = 2;
z = 3;
z = x + y;
z = x - y;
int yi, zi;
zi = x;
zi = x + yi;
z = x + yi;
}
void test3(){
boost::numeric::safe_int x, y, z;
x = 1;
y = 2;
z = 3;
z = x + y;
z = x - y;
int yi, zi;
zi = x;
zi = x + yi;
z = x + yi;
}
void test4(){
boost::numeric::safe_unsigned_int x, y, z;
x = 1;
y = 2;
z = 3;
z = x + y;
z = x - y;
unsigned int yi, zi;
zi = x;
zi = x + yi;
z = x + yi;
zi = x + y;
}
#include <boost/cstdint.hpp>
void test5(){
boost::numeric::safe_integer<boost::uint64_t> x, y, z;
x = 1;
y = 2;
z = 3;
z = x + y;
z = x - y;
boost::uint64_t yi, zi;
zi = x;
zi = x + yi;
z = x + yi;
zi = x + y;
}

View File

@@ -0,0 +1,116 @@
#include <iostream>
#include <boost/cstdint.hpp>
#include <boost/limits.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/array/elem.hpp>
#include <boost/preprocessor/array/size.hpp>
#include <boost/preprocessor/list/adt.hpp>
#include <boost/preprocessor/list/to_tuple.hpp>
#include <boost/preprocessor/facilities/expand.hpp>
#include <boost/preprocessor/cat.hpp>
#include <boost/mpl/or.hpp>
#include <boost/mpl/if.hpp>
#include "../include/safe_integer.hpp"
#include "../include/safe_cast.hpp"
#define VALUES (20, ( \
0x10, \
0x20, \
0x7f, \
0x80, \
0xff, \
0x1000, \
0x2000, \
0x7fff, \
0x8000, \
0xffff, \
0x10000000, \
0x20000000, \
0x7fffffff, \
0x80000000, \
0xffffffff, \
0x100000000000, \
0x200000000000, \
0x7fffffffffff, \
0x80000000ffff, \
0xffffffffffff \
))
/**/
inline unsigned int
count_bits(boost::uintmax_t t){
unsigned int i = 0;
while(t != 0){
++i;
t >>= 1;
}
return i;
}
inline unsigned int
count_bits(boost::intmax_t t){
if(t < 0)
t = -t;
unsigned int i = 0;
while(t != 0){
++i;
t >>= 1;
}
return i;
}
#define EACH_VALUE2(z, l, list) \
BOOST_PP_EXPAND( \
TESTX \
BOOST_PP_LIST_TO_TUPLE( \
BOOST_PP_LIST_CONS( \
BOOST_PP_ARRAY_ELEM(l, VALUES), \
list \
) \
) \
) \
/**/
#define EACH_VALUE1(z, k, types) \
BOOST_PP_REPEAT( \
BOOST_PP_ARRAY_SIZE(VALUES), \
EACH_VALUE2, \
BOOST_PP_LIST_CONS( \
BOOST_PP_ARRAY_ELEM(k, VALUES), \
types \
) \
) \
/**/
#define EACH_TYPE2(i, j) \
BOOST_PP_REPEAT( \
BOOST_PP_ARRAY_SIZE(VALUES), \
EACH_VALUE1, \
(i, (j, BOOST_PP_NIL)) \
) \
/**/
#define EACH_TYPE1(i) \
EACH_TYPE2(i, boost::int8_t) \
EACH_TYPE2(i, boost::uint8_t) \
EACH_TYPE2(i, boost::int16_t) \
EACH_TYPE2(i, boost::uint16_t) \
EACH_TYPE2(i, boost::int32_t) \
EACH_TYPE2(i, boost::uint32_t) \
EACH_TYPE2(i, boost::int64_t) \
EACH_TYPE2(i, boost::uint64_t) \
/**/
#define TEST \
EACH_TYPE1(boost::int8_t) \
EACH_TYPE1(boost::uint8_t) \
EACH_TYPE1(boost::int16_t) \
EACH_TYPE1(boost::uint16_t) \
EACH_TYPE1(boost::int32_t) \
EACH_TYPE1(boost::uint32_t) \
EACH_TYPE1(boost::int64_t) \
EACH_TYPE1(boost::uint64_t) \
/**/

View File

@@ -0,0 +1,17 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
bool test_add1();
bool test_add2();
bool test_add3();
int main(int argc, char * argv[]){
bool result = true;
result = test_add1();
result &= test_add2();
result &= test_add3();
return ! result ;
}

View File

@@ -0,0 +1,178 @@
#ifndef TEST_ADD_HPP
#define TEST_ADD_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#include <algorithm>
#include <boost/cstdint.hpp>
#include <boost/limits.hpp>
#include "../include/safe_integer.hpp"
#include "../include/safe_cast.hpp"
#include "../include/safe_compare.hpp"
#include "test.hpp"
template<class T1, class T2, class V>
bool test_add(
V v1,
V v2,
const char *at1,
const char *at2,
const char *av1,
const char *av2
){
bool success;
T1 t1;
try{
t1 = boost::numeric::safe_cast<T1>(v1);
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(boost::numeric::safe_compare::greater_than(
v1,
std::numeric_limits<T1>::max()
)
|| boost::numeric::safe_compare::less_than(
v1,
std::numeric_limits<T1>::min()
)
){
std::cout
<< "constructed invalid value "
<< at1 << ' ' << av1
<< std::endl;
return false;
}
}
else{
if(! boost::numeric::safe_compare::greater_than(
v1,
std::numeric_limits<T1>::max()
)
&& ! boost::numeric::safe_compare::less_than(
v1,
std::numeric_limits<T1>::min()
)
){
std::cout
<< "failed to construct valid value "
<< at1 << ' ' << av1
<< std::endl;
return false;
}
return true;
}
T2 t2;
try{
t2 = boost::numeric::safe_cast<T2>(v2);
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(boost::numeric::safe_compare::greater_than(
v2,
std::numeric_limits<T2>::max()
)
|| boost::numeric::safe_compare::less_than(
v2,
std::numeric_limits<T2>::min()
)){
std::cout
<< "constructed invalid value "
<< at2 << ' ' << av2
<< std::endl;
return false;
}
}
else{
if(!boost::numeric::safe_compare::greater_than(
v2,
std::numeric_limits<T2>::max()
)
&& !boost::numeric::safe_compare::less_than(
v2,
std::numeric_limits<T2>::min()
)){
std::cout
<< "failed to construct valid value "
<< at2 << ' ' << av2
<< std::endl;
return false;
}
return true;
}
V result;
try{
result = t1 + t2;
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(result != v1 + v2){
std::cout
<< "failed to detect error in addition "
<< at1 << ' ' << at2 << ' ' << av1 << ' ' << av2
<< std::endl;
return false;
}
}
else{
if(boost::numeric::safe_compare::greater_than_equal(
boost::numeric::bits<boost::uintmax_t>::value,
std::max(count_bits(v1),count_bits(v2))
)){
std::cout
<< "erroneously detected error in addition "
<< at1 << ' ' << at2 << ' ' << av1 << ' ' << av2
<< std::endl;
return false;
}
}
return true; // correct result
}
template<class T1, class T2>
struct add_result {
typedef typename boost::mpl::if_<
boost::mpl::or_<
boost::numeric::is_signed<T1>,
boost::numeric::is_signed<T2>
>,
boost::intmax_t,
boost::uintmax_t
>::type type;
};
#define TEST_IMPL(a, b, c, d) \
rval &= test_add<d, c>( \
(static_cast<add_result<c, d>::type>(a)), \
(static_cast<add_result<c, d>::type>(b)), \
BOOST_PP_STRINGIZE(d), \
BOOST_PP_STRINGIZE(c), \
BOOST_PP_STRINGIZE(b), \
BOOST_PP_STRINGIZE(a) \
); \
/**/
#endif // TEST_ADD_HPP

View File

@@ -0,0 +1,37 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_add.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
boost::numeric::safe<c>, \
boost::numeric::safe<d> \
) \
/**/
bool test_add1(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,38 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_add.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
boost::numeric::safe<c>, \
d \
) \
/**/
bool test_add2(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,38 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_add.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
c, \
boost::numeric::safe<d> \
) \
/**/
bool test_add3(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,180 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#include <boost/cstdint.hpp>
#include <boost/limits.hpp>
#include <boost/preprocessor/repetition/repeat.hpp>
#include <boost/preprocessor/array/elem.hpp>
#include <boost/preprocessor/array/size.hpp>
#include <boost/preprocessor/tuple/elem.hpp>
#include <boost/preprocessor/stringize.hpp>
#include <boost/type_traits/is_signed.hpp>
#include "../include/safe_cast.hpp"
#include "../include/safe_integer.hpp"
#define TYPES (8, ( \
boost::int8_t, \
boost::uint8_t, \
boost::int16_t, \
boost::uint16_t, \
boost::int32_t, \
boost::uint32_t, \
boost::int64_t, \
boost::uint64_t \
))
#define VALUES (20, ( \
0x10, \
0x20, \
0x7f, \
0x80, \
0xff, \
0x1000, \
0x2000, \
0x7fff, \
0x8000, \
0xffff, \
0x10000000, \
0x20000000, \
0x7fffffff, \
0x80000000, \
0xffffffff, \
0x100000000000, \
0x200000000000, \
0x7fffffffffff, \
0x80000000ffff, \
0xffffffffffff \
))
// test conversion
template<class T1, class T2, class V>
bool test_conversion(V v, const char *a1, const char *a2, const char *a3){
bool success;
boost::numeric::safe<T2> t2;
/* test conversion constructor */
try{
t2 = v;
success = true;
}
catch(std::range_error e){
success = false;
}
if(success){
if(t2 > std::numeric_limits<T2>::max()
|| t2 < std::numeric_limits<T2>::min()){
std::cout
<< "failed to detect error in construction "
<< a1 << ' ' << a2
<< std::endl;
return false;
}
}
else{
if(v <= std::numeric_limits<T2>::max()
&& v >= std::numeric_limits<T2>::min()){
std::cout
<< "erroneasly detected error in construction "
<< a1 << ' ' << a2
<< std::endl;
return false;
}
return true; // correctly detected error
}
T1 t1;
try{
//t1 = t2;
t1 = boost::numeric::safe_cast<T1>(t2);
success = true;
}
catch(std::range_error e){
success = false;
}
if(success){
if(t1 > std::numeric_limits<T1>::max()
|| t1 < std::numeric_limits<T1>::min()){
std::cout
<< "failed to detect error in conversion "
<< a1 << ' ' << a2 << ' ' << a3
<< std::endl;
return false;
}
}
else{
if(t2 <= std::numeric_limits<T1>::max()
&& t2 >= std::numeric_limits<T1>::min()){
std::cout
<< "erroneasly detected error in conversion "
<< a1 << ' ' << a2 << ' ' << a3
<< std::endl;
return false;
}
return true; // correctly detected error
}
return true; // passed test
}
template<class T>
struct test_value {
typedef typename boost::mpl::if_<
boost::is_signed<T>,
boost::intmax_t,
boost::uintmax_t
>::type type;
};
#define TEST_CONVERSION(T1, T2, v) \
test_conversion<T1, T2>( \
static_cast<test_value<T2>::type>(v), \
BOOST_PP_STRINGIZE(T1), \
BOOST_PP_STRINGIZE(T2), \
BOOST_PP_STRINGIZE(v) \
);
#define TEST(z, k, ij) \
TEST_CONVERSION( \
BOOST_PP_ARRAY_ELEM( \
BOOST_PP_TUPLE_ELEM(2, 0, ij),\
TYPES \
), \
BOOST_PP_ARRAY_ELEM( \
BOOST_PP_TUPLE_ELEM(2, 1, ij),\
TYPES \
), \
BOOST_PP_ARRAY_ELEM(k, VALUES) \
)
#define EACH_VALUE(z, j, i) \
BOOST_PP_REPEAT( \
BOOST_PP_ARRAY_SIZE(VALUES), \
TEST, \
(i, j) \
) \
/**/
#define EACH_TYPE1(z, i, x) \
BOOST_PP_REPEAT( \
BOOST_PP_ARRAY_SIZE(TYPES), \
EACH_VALUE, \
i \
) \
/**/
int main(int argc, char *argv[]){
BOOST_PP_REPEAT(
BOOST_PP_ARRAY_SIZE(TYPES),
EACH_TYPE1,
x
)
/*
TEST(0, 0, (0, 1));
TEST_CONVERSION(boost::uint8_t, boost::uint8_t, 0x80)
*/
return 0;
}

View File

@@ -0,0 +1,11 @@
bool test_divide1();
bool test_divide2();
bool test_divide3();
int main(int argc, char * argv[]){
bool result = true;
result = test_divide1();
result &= test_divide2();
result &= test_divide3();
return ! result ;
}

View File

@@ -0,0 +1,178 @@
#ifndef TEST_DIVIDE_HPP
#define TEST_DIVIDE_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#include <algorithm>
#include <boost/cstdint.hpp>
#include <boost/limits.hpp>
#include "../include/safe_integer.hpp"
#include "../include/safe_cast.hpp"
#include "../include/safe_compare.hpp"
#include "test.hpp"
template<class T1, class T2, class V>
bool test_divide(
V v1,
V v2,
const char *at1,
const char *at2,
const char *av1,
const char *av2
){
bool success;
T1 t1;
try{
t1 = boost::numeric::safe_cast<T1>(v1);
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(boost::numeric::safe_compare::greater_than(
v1,
std::numeric_limits<T1>::max()
)
|| boost::numeric::safe_compare::less_than(
v1,
std::numeric_limits<T1>::min()
)
){
std::cout
<< "constructed invalid value "
<< at1 << ' ' << av1
<< std::endl;
return false;
}
}
else{
if(! boost::numeric::safe_compare::greater_than(
v1,
std::numeric_limits<T1>::max()
)
&& ! boost::numeric::safe_compare::less_than(
v1,
std::numeric_limits<T1>::min()
)
){
std::cout
<< "failed to construct valid value "
<< at1 << ' ' << av1
<< std::endl;
return false;
}
return true;
}
T2 t2;
try{
t2 = boost::numeric::safe_cast<T2>(v2);
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(boost::numeric::safe_compare::greater_than(
v2,
std::numeric_limits<T2>::max()
)
|| boost::numeric::safe_compare::less_than(
v2,
std::numeric_limits<T2>::min()
)){
std::cout
<< "constructed invalid value "
<< at2 << ' ' << av2
<< std::endl;
return false;
}
}
else{
if(!boost::numeric::safe_compare::greater_than(
v2,
std::numeric_limits<T2>::max()
)
&& !boost::numeric::safe_compare::less_than(
v2,
std::numeric_limits<T2>::min()
)){
std::cout
<< "failed to construct valid value "
<< at2 << ' ' << av2
<< std::endl;
return false;
}
return true;
}
V result;
try{
result = t1 / t2;
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(result != v1/v2){
std::cout
<< "failed to detect error in division "
<< at1 << ' ' << at2 << ' ' << av1 << ' ' << av2
<< std::endl;
return false;
}
}
else{
if(boost::numeric::safe_compare::greater_than_equal(
boost::numeric::bits<boost::uintmax_t>::value,
std::max(count_bits(v1),count_bits(v2))
)){
std::cout
<< "erroneously detected error in division "
<< at1 << ' ' << at2 << ' ' << av1 << ' ' << av2
<< std::endl;
return false;
}
}
return true; // correct result
}
template<class T1, class T2>
struct divide_result {
typedef typename boost::mpl::if_<
boost::mpl::or_<
boost::numeric::is_signed<T1>,
boost::numeric::is_signed<T2>
>,
boost::intmax_t,
boost::uintmax_t
>::type type;
};
#define TEST_IMPL(a, b, c, d) \
rval &= test_divide<d, c>( \
(static_cast<divide_result<c, d>::type>(a)), \
(static_cast<divide_result<c, d>::type>(b)), \
BOOST_PP_STRINGIZE(d), \
BOOST_PP_STRINGIZE(c), \
BOOST_PP_STRINGIZE(b), \
BOOST_PP_STRINGIZE(a) \
); \
/**/
#endif // TEST_DIVIDE_HPP

View File

@@ -0,0 +1,37 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_divide.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
boost::numeric::safe<c>, \
boost::numeric::safe<d> \
) \
/**/
bool test_divide1(){
bool rval = true;
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
return rval;
}

View File

@@ -0,0 +1,37 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_divide.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
boost::numeric::safe<c>, \
d \
) \
/**/
bool test_divide2(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,37 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_divide.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
c, \
boost::numeric::safe<d> \
) \
/**/
bool test_divide3(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,7 @@
#include <boost/integer.hpp>
#include <boost/static_assert.hpp>
#include <boost/type_traits/is_integral.hpp>
#include "../include/safe_integer.hpp"
BOOST_STATIC_ASSERT(boost::is_integral<boost::int8_t>::value);
BOOST_STATIC_ASSERT(boost::is_integral<boost::numeric::safe<boost::int8_t> >::value);

View File

@@ -0,0 +1,17 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
bool test_modulus1();
bool test_modulus2();
bool test_modulus3();
int main(int argc, char * argv[]){
bool result = true;
result = test_modulus1();
result &= test_modulus2();
result &= test_modulus3();
return ! result ;
}

View File

@@ -0,0 +1,178 @@
#ifndef TEST_MODULUS_HPP
#define TEST_MODULUS_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#include <algorithm>
#include <boost/cstdint.hpp>
#include <boost/limits.hpp>
#include "../include/safe_integer.hpp"
#include "../include/safe_cast.hpp"
#include "../include/safe_compare.hpp"
#include "test.hpp"
template<class T1, class T2, class V>
bool test_modulus(
V v1,
V v2,
const char *at1,
const char *at2,
const char *av1,
const char *av2
){
bool success;
T1 t1;
try{
t1 = boost::numeric::safe_cast<T1>(v1);
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(boost::numeric::safe_compare::greater_than(
v1,
std::numeric_limits<T1>::max()
)
|| boost::numeric::safe_compare::less_than(
v1,
std::numeric_limits<T1>::min()
)
){
std::cout
<< "constructed invalid value "
<< at1 << ' ' << av1
<< std::endl;
return false;
}
}
else{
if(! boost::numeric::safe_compare::greater_than(
v1,
std::numeric_limits<T1>::max()
)
&& ! boost::numeric::safe_compare::less_than(
v1,
std::numeric_limits<T1>::min()
)
){
std::cout
<< "failed to construct valid value "
<< at1 << ' ' << av1
<< std::endl;
return false;
}
return true;
}
T2 t2;
try{
t2 = boost::numeric::safe_cast<T2>(v2);
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(boost::numeric::safe_compare::greater_than(
v2,
std::numeric_limits<T2>::max()
)
|| boost::numeric::safe_compare::less_than(
v2,
std::numeric_limits<T2>::min()
)){
std::cout
<< "constructed invalid value "
<< at2 << ' ' << av2
<< std::endl;
return false;
}
}
else{
if(!boost::numeric::safe_compare::greater_than(
v2,
std::numeric_limits<T2>::max()
)
&& !boost::numeric::safe_compare::less_than(
v2,
std::numeric_limits<T2>::min()
)){
std::cout
<< "failed to construct valid value "
<< at2 << ' ' << av2
<< std::endl;
return false;
}
return true;
}
V result;
try{
result = t1 % t2;
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(result != v1 % v2){
std::cout
<< "failed to detect error in modulus "
<< at1 << ' ' << at2 << ' ' << av1 << ' ' << av2
<< std::endl;
return false;
}
}
else{
if(boost::numeric::safe_compare::greater_than_equal(
boost::numeric::bits<boost::uintmax_t>::value,
std::max(count_bits(v1),count_bits(v2))
)){
std::cout
<< "erroneously detected error in modulus "
<< at1 << ' ' << at2 << ' ' << av1 << ' ' << av2
<< std::endl;
return false;
}
}
return true; // correct result
}
template<class T1, class T2>
struct modulus_result {
typedef typename boost::mpl::if_<
boost::mpl::or_<
boost::numeric::is_signed<T1>,
boost::numeric::is_signed<T2>
>,
boost::intmax_t,
boost::uintmax_t
>::type type;
};
#define TEST_IMPL(a, b, c, d) \
rval &= test_modulus<d, c>( \
(static_cast<modulus_result<c, d>::type>(a)), \
(static_cast<modulus_result<c, d>::type>(b)), \
BOOST_PP_STRINGIZE(d), \
BOOST_PP_STRINGIZE(c), \
BOOST_PP_STRINGIZE(b), \
BOOST_PP_STRINGIZE(a) \
); \
/**/
#endif // TEST_MODULUS_HPP

View File

@@ -0,0 +1,37 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_modulus.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
boost::numeric::safe<c>, \
boost::numeric::safe<d> \
) \
/**/
bool test_modulus1(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,37 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_modulus.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
boost::numeric::safe<c>, \
d \
) \
/**/
bool test_modulus2(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,37 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_modulus.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
c, \
boost::numeric::safe<d> \
) \
/**/
bool test_modulus3(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,17 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
bool test_multiply1();
bool test_multiply2();
bool test_multiply3();
int main(int argc, char * argv[]){
bool result = true;
result = test_multiply1();
result &= test_multiply2();
result &= test_multiply3();
return ! result ;
}

View File

@@ -0,0 +1,174 @@
#ifndef TEST_MULTIPLY_HPP
#define TEST_MULTIPLY_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#include <boost/cstdint.hpp>
#include <boost/limits.hpp>
#include "../include/safe_integer.hpp"
#include "../include/safe_cast.hpp"
#include "../include/safe_compare.hpp"
#include "test.hpp"
template<class T1, class T2, class V>
bool test_multiply(
V v1,
V v2,
const char *at1,
const char *at2,
const char *av1,
const char *av2
){
bool success;
T1 t1;
try{
t1 = boost::numeric::safe_cast<T1>(v1);
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(boost::numeric::safe_compare::greater_than(
v1,
std::numeric_limits<T1>::max()
)
|| boost::numeric::safe_compare::less_than(
v1,
std::numeric_limits<T1>::min()
)
){
std::cout
<< "constructed invalid value "
<< at1 << ' ' << av1
<< std::endl;
return false;
}
}
else{
if(! boost::numeric::safe_compare::greater_than(
v1,
std::numeric_limits<T1>::max()
)
&& ! boost::numeric::safe_compare::less_than(
v1,
std::numeric_limits<T1>::min()
)
){
std::cout
<< "failed to construct valid value "
<< at1 << ' ' << av1
<< std::endl;
return false;
}
return true;
}
T2 t2;
try{
t2 = boost::numeric::safe_cast<T2>(v2);
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(boost::numeric::safe_compare::greater_than(
v2,
std::numeric_limits<T2>::max()
)
|| boost::numeric::safe_compare::less_than(
v2,
std::numeric_limits<T2>::min()
)){
std::cout
<< "constructed invalid value "
<< at2 << ' ' << av2
<< std::endl;
return false;
}
}
else{
if(! boost::numeric::safe_compare::greater_than(
v2,
std::numeric_limits<T2>::max()
)
&& ! boost::numeric::safe_compare::less_than(
v2,
std::numeric_limits<T2>::min()
)){
std::cout
<< "failed to construct valid value "
<< at2 << ' ' << av2
<< std::endl;
return false;
}
return true;
}
V result;
try{
result = t1 * t2;
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(result != v1 * v2){
std::cout
<< "failed to detect error in multiplication "
<< at1 << ' ' << at2 << ' ' << av1 << ' ' << av2
<< std::endl;
return false;
}
}
else{
if(64 >= count_bits(v1) + count_bits(v2)){
std::cout
<< "erroneously detected error in multiplication "
<< at1 << ' ' << at2 << ' ' << av1 << ' ' << av2
<< std::endl;
return false;
}
}
return true; // correct result
}
template<class T1, class T2>
struct multiply_result {
typedef typename boost::mpl::if_<
boost::mpl::or_<
boost::numeric::is_signed<T1>,
boost::numeric::is_signed<T2>
>,
boost::intmax_t,
boost::uintmax_t
>::type type;
};
#define TEST_IMPL(a, b, c, d) \
rval &= test_multiply<d, c>( \
(static_cast<multiply_result<c, d>::type>(a)), \
(static_cast<multiply_result<c, d>::type>(b)), \
BOOST_PP_STRINGIZE(d), \
BOOST_PP_STRINGIZE(c), \
BOOST_PP_STRINGIZE(b), \
BOOST_PP_STRINGIZE(a) \
); \
/**/
#endif // TEST_MODULUS_HPP

View File

@@ -0,0 +1,37 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_multiply.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
boost::numeric::safe<c>, \
boost::numeric::safe<d> \
) \
/**/
bool test_multiply1(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,37 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_multiply.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
boost::numeric::safe<c>, \
d \
) \
/**/
bool test_multiply2(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,37 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_multiply.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
c, \
boost::numeric::safe<d> \
) \
/**/
bool test_multiply3(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,230 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "../include/numeric.hpp"
#include <boost/limits.hpp>
#include <boost/cstdint.hpp>
#include <boost/integer.hpp>
#include <boost/integer_traits.hpp>
#include <boost/mpl/assert.hpp>
#include <boost/mpl/print.hpp>
//#include <boost/mpl/integral_c.hpp>
BOOST_MPL_ASSERT(( boost::numeric::is_signed<int> ));
BOOST_MPL_ASSERT_NOT(( boost::numeric::is_signed<unsigned int> ));
BOOST_MPL_ASSERT_RELATION(8, ==, boost::numeric::bits<boost::int8_t>::value);
BOOST_MPL_ASSERT_RELATION(16, ==, (boost::numeric::max_bits<boost::int8_t, boost::int16_t>::value));
BOOST_MPL_ASSERT_RELATION(16, ==, (boost::numeric::max_bits<boost::int8_t, boost::int16_t>::value));
////////////////////////////////////////////////////////////
// addition of various types of integers
template<class T, class U, class R, int B>
struct addition_display {
BOOST_MPL_ASSERT_RELATION(boost::numeric::bits<R>::value, >=, B);
};
// note we have to break this into two separate macros to avoid
// having gcc flag a compile error
#define TEST_ADDITION1(T, U) \
BOOST_MPL_ASSERT_RELATION( \
(boost::integer_traits< \
addition_result_type<T, U>::type \
>::const_max), \
>=, \
boost::integer_traits<T>::const_max \
+ boost::integer_traits<U>::const_max \
); \
{ \
typedef boost::mpl::print< \
addition_display< \
T, \
U, \
addition_result_type<T, U>::type, \
addition_result_bits<T, U>::value \
> \
>::type tdef; \
} \
/**/
#define TEST_ADDITION2(T, U) \
BOOST_MPL_ASSERT_RELATION( \
(boost::integer_traits< \
addition_result_type<T, U>::type \
>::const_min), \
<=, \
boost::integer_traits<T>::const_min \
+ boost::integer_traits<U>::const_min \
); \
{ \
typedef boost::mpl::print< \
addition_display< \
T, \
U, \
addition_result_type<T, U>::type, \
addition_result_bits<T, U>::value \
> \
>::type tdef; \
} \
/**/
void add(){
using namespace boost::numeric;
TEST_ADDITION1(boost::int8_t, boost::int8_t)
TEST_ADDITION1(boost::int16_t, boost::int8_t)
TEST_ADDITION1(boost::uint8_t, boost::uint8_t)
TEST_ADDITION1(boost::uint16_t, boost::uint8_t)
TEST_ADDITION1(boost::uint16_t, boost::int8_t)
TEST_ADDITION1(boost::int16_t, boost::uint8_t)
TEST_ADDITION2(boost::int8_t, boost::int8_t)
TEST_ADDITION2(boost::int16_t, boost::int8_t)
TEST_ADDITION2(boost::uint8_t, boost::uint8_t)
TEST_ADDITION2(boost::uint16_t, boost::uint8_t)
TEST_ADDITION2(boost::uint16_t, boost::int8_t)
TEST_ADDITION2(boost::int16_t, boost::uint8_t)
}
////////////////////////////////////////////////////////////
// subtraction of various types of integers
template<class T, class U, class R, int B>
struct subtraction_display {};
#if 1
// note we have to break this into two separate macros to avoid
// having gcc flag a compile error
#define TEST_SUBTRACTION1(T, U) \
BOOST_MPL_ASSERT_RELATION( \
(boost::integer_traits< \
subtraction_result_type<T, U>::type \
>::const_max), \
>=, \
boost::integer_traits<T>::const_max \
+ boost::integer_traits<U>::const_max \
); \
{ \
typedef boost::mpl::print< \
subtraction_display< \
T, \
U, \
subtraction_result_type<T, U>::type, \
subtraction_result_bits<T, U>::value \
> \
>::type tdef; \
} \
/**/
#define TEST_SUBTRACTION2(T, U) \
BOOST_MPL_ASSERT_RELATION( \
(boost::integer_traits< \
subtraction_result_type<T, U>::type \
>::const_min), \
<=, \
boost::integer_traits<T>::const_min \
+ boost::integer_traits<U>::const_min \
); \
{ \
typedef boost::mpl::print< \
subtraction_display< \
T, \
U, \
subtraction_result_type<T, U>::type, \
subtraction_result_bits<T, U>::value \
> \
>::type tdef; \
} \
/**/
void subtract(){
using namespace boost::numeric;
TEST_SUBTRACTION1(boost::int8_t, boost::int8_t)
TEST_SUBTRACTION1(boost::int16_t, boost::int8_t)
TEST_SUBTRACTION1(boost::uint8_t, boost::uint8_t)
TEST_SUBTRACTION1(boost::uint16_t, boost::uint8_t)
TEST_SUBTRACTION1(boost::uint16_t, boost::int8_t)
TEST_SUBTRACTION1(boost::int16_t, boost::uint8_t)
TEST_SUBTRACTION2(boost::int8_t, boost::int8_t)
TEST_SUBTRACTION2(boost::int16_t, boost::int8_t)
TEST_SUBTRACTION2(boost::uint8_t, boost::uint8_t)
TEST_SUBTRACTION2(boost::uint16_t, boost::uint8_t)
TEST_SUBTRACTION2(boost::uint16_t, boost::int8_t)
TEST_SUBTRACTION2(boost::int16_t, boost::uint8_t)
}
#endif
////////////////////////////////////////////////////////////
// multiplication of various types of integers
// note - this test is not really correct. What is really needed
// is compile time integer interval arithmetic - which we don't
// have. Addition is trivial - see above - but multiplication
// is more challanging. So, for now, this test fails to detect
// implementation errors in calculation of ranges of the result
// of multiplications
template<class T, class U, class R, int B>
struct multiply_display {};
#define TEST_MULTIPLICATION1(T, U) \
BOOST_MPL_ASSERT_RELATION( \
(boost::integer_traits< \
multiply_result_type<T, U>::type \
>::const_max), \
>=, \
boost::integer_traits<T>::const_max \
* boost::integer_traits<U>::const_max \
); \
{ \
typedef boost::mpl::print< \
multiply_display< \
T, \
U, \
multiply_result_type<T, U>::type, \
multiply_result_bits<T, U>::value \
> \
>::type tdef; \
} \
/**/
#define TEST_MULTIPLICATION2(T, U) \
BOOST_MPL_ASSERT_RELATION( \
(boost::integer_traits< \
multiply_result_type<T, U>::type \
>::const_min), \
<=, \
boost::integer_traits<T>::const_min \
+ boost::integer_traits<U>::const_min \
); \
{ \
typedef boost::mpl::print< \
multiply_display< \
T, \
U, \
multiply_result_type<T, U>::type, \
multiply_result_bits<T, U>::value \
> \
>::type tdef; \
} \
/**/
void multiply(){
using namespace boost::numeric;
TEST_MULTIPLICATION1(boost::int8_t, boost::int8_t)
TEST_MULTIPLICATION1(boost::int16_t, boost::int8_t)
TEST_MULTIPLICATION1(boost::uint8_t, boost::uint8_t)
TEST_MULTIPLICATION1(boost::uint16_t, boost::uint8_t)
TEST_MULTIPLICATION1(boost::uint16_t, boost::int8_t)
TEST_MULTIPLICATION1(boost::int16_t, boost::uint8_t)
TEST_MULTIPLICATION2(boost::int8_t, boost::int8_t)
TEST_MULTIPLICATION2(boost::int16_t, boost::int8_t)
TEST_MULTIPLICATION2(boost::uint8_t, boost::uint8_t)
TEST_MULTIPLICATION2(boost::uint16_t, boost::uint8_t)
TEST_MULTIPLICATION2(boost::uint16_t, boost::int8_t)
TEST_MULTIPLICATION2(boost::int16_t, boost::uint8_t)
}

View File

@@ -0,0 +1,17 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
bool test_subtract1();
bool test_subtract2();
bool test_subtract3();
int main(int argc, char * argv[]){
bool result = true;
result = test_subtract1();
result &= test_subtract2();
result &= test_subtract3();
return ! result ;
}

View File

@@ -0,0 +1,178 @@
#ifndef TEST_SUBTRACT_HPP
#define TEST_SUBTRACT_HPP
// MS compatible compilers support #pragma once
#if defined(_MSC_VER) && (_MSC_VER >= 1020)
# pragma once
#endif
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include <iostream>
#include <algorithm>
#include <boost/cstdint.hpp>
#include <boost/limits.hpp>
#include "../include/safe_integer.hpp"
#include "../include/safe_cast.hpp"
#include "../include/safe_compare.hpp"
#include "test.hpp"
template<class T1, class T2, class V>
bool test_subtract(
V v1,
V v2,
const char *at1,
const char *at2,
const char *av1,
const char *av2
){
bool success;
T1 t1;
try{
t1 = boost::numeric::safe_cast<T1>(v1);
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(boost::numeric::safe_compare::greater_than(
v1,
std::numeric_limits<T1>::max()
)
|| boost::numeric::safe_compare::less_than(
v1,
std::numeric_limits<T1>::min()
)
){
std::cout
<< "constructed invalid value "
<< at1 << ' ' << av1
<< std::endl;
return false;
}
}
else{
if(! boost::numeric::safe_compare::greater_than(
v1,
std::numeric_limits<T1>::max()
)
&& ! boost::numeric::safe_compare::less_than(
v1,
std::numeric_limits<T1>::min()
)
){
std::cout
<< "failed to construct valid value "
<< at1 << ' ' << av1
<< std::endl;
return false;
}
return true;
}
T2 t2;
try{
t2 = boost::numeric::safe_cast<T2>(v2);
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(boost::numeric::safe_compare::greater_than(
v2,
std::numeric_limits<T2>::max()
)
|| boost::numeric::safe_compare::less_than(
v2,
std::numeric_limits<T2>::min()
)){
std::cout
<< "constructed invalid value "
<< at2 << ' ' << av2
<< std::endl;
return false;
}
}
else{
if(!boost::numeric::safe_compare::greater_than(
v2,
std::numeric_limits<T2>::max()
)
&& !boost::numeric::safe_compare::less_than(
v2,
std::numeric_limits<T2>::min()
)){
std::cout
<< "failed to construct valid value "
<< at2 << ' ' << av2
<< std::endl;
return false;
}
return true;
}
V result;
try{
result = t1 - t2;
success = true;
}
catch(std::range_error){
success = false;
}
if(success){
if(result != v1 - v2){
std::cout
<< "failed to detect error in subtraction "
<< at1 << ' ' << at2 << ' ' << av1 << ' ' << av2
<< std::endl;
return false;
}
}
else{
if(boost::numeric::safe_compare::greater_than_equal(
boost::numeric::bits<boost::uintmax_t>::value,
std::max(count_bits(v1),count_bits(v2))
)){
std::cout
<< "erroneously detected error in subtraction "
<< at1 << ' ' << at2 << ' ' << av1 << ' ' << av2
<< std::endl;
return false;
}
}
return true; // correct result
}
template<class T1, class T2>
struct subtract_result {
typedef typename boost::mpl::if_<
boost::mpl::or_<
boost::numeric::is_signed<T1>,
boost::numeric::is_signed<T2>
>,
boost::intmax_t,
boost::uintmax_t
>::type type;
};
#define TEST_IMPL(a, b, c, d) \
rval &= test_subtract<d, c>( \
(static_cast<subtract_result<c, d>::type>(a)), \
(static_cast<subtract_result<c, d>::type>(b)), \
BOOST_PP_STRINGIZE(d), \
BOOST_PP_STRINGIZE(c), \
BOOST_PP_STRINGIZE(b), \
BOOST_PP_STRINGIZE(a) \
); \
/**/
#endif // TEST_SUBTRACT_HPP

View File

@@ -0,0 +1,37 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_subtract.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
boost::numeric::safe<c>, \
boost::numeric::safe<d> \
) \
/**/
bool test_subtract1(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,38 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_subtract.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
boost::numeric::safe<c>, \
d \
) \
/**/
bool test_subtract2(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,38 @@
// Copyright (c) 2012 Robert Ramey
//
// Distributed under the Boost Software License, Version 1.0. (See
// accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
#include "test_subtract.hpp"
#define TESTX(a, b, c, d) \
TEST_IMPL( \
a, \
b, \
c, \
boost::numeric::safe<d> \
) \
/**/
bool test_subtract3(){
bool rval = true;
#pragma message("0")
EACH_TYPE1(boost::int8_t);
#pragma message("1")
EACH_TYPE1(boost::uint8_t);
#pragma message("2")
EACH_TYPE1(boost::int16_t);
#pragma message("3")
EACH_TYPE1(boost::uint16_t);
#pragma message("4")
EACH_TYPE1(boost::int32_t);
#pragma message("5")
EACH_TYPE1(boost::uint32_t);
#pragma message("6")
EACH_TYPE1(boost::int64_t);
#pragma message("7")
EACH_TYPE1(boost::uint64_t);
return rval;
}

View File

@@ -0,0 +1,242 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="example_test"
ProjectGUID="{3E51E3C4-2F74-4470-8DE9-8B79D1F30FA8}"
RootNamespace="example_safe_int"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="c:\BoostRelease"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\..\examples\example1.cpp"
>
<FileConfiguration
Name="Debug|Win32"
ExcludedFromBuild="true"
>
<Tool
Name="VCCLCompilerTool"
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\examples\example2.cpp"
>
<FileConfiguration
Name="Debug|Win32"
ExcludedFromBuild="true"
>
<Tool
Name="VCCLCompilerTool"
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\examples\example3.cpp"
>
<FileConfiguration
Name="Debug|Win32"
ExcludedFromBuild="true"
>
<Tool
Name="VCCLCompilerTool"
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\examples\example4.cpp"
>
<FileConfiguration
Name="Debug|Win32"
ExcludedFromBuild="true"
>
<Tool
Name="VCCLCompilerTool"
/>
</FileConfiguration>
</File>
<File
RelativePath="..\..\examples\example_test.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@@ -0,0 +1,99 @@
Microsoft Visual Studio Solution File, Format Version 10.00
# Visual Studio 2008
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_add", "test_add.vcproj", "{4393B586-DBB1-40B9-AE5D-A1D9E459ACD2}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_conversion", "test_conversion.vcproj", "{0E0D4BCF-507C-413C-9B8C-E8C972209F8B}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_divide", "test_divide.vcproj", "{21DF917A-7E09-4F06-B599-0CD7E231F06E}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_modulus", "test_modulus.vcproj", "{3DE806F0-9703-4EF8-A57F-07DC922074D2}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_multiply", "test_multiply.vcproj", "{AF3D6B8E-EFF2-41C5-8FCE-9060BCD63974}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_subtract", "test_subtract.vcproj", "{B914598A-A377-4DFB-A3E7-DA1D0F3C4E37}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Tests", "Tests", "{06D85C87-845D-4C48-BC6F-2D2C5127AA81}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Library Headers", "Library Headers", "{98484F4D-80EE-431E-8B13-6DC167B628E1}"
ProjectSection(SolutionItems) = preProject
..\..\include\numeric.hpp = ..\..\include\numeric.hpp
..\..\include\overflow.hpp = ..\..\include\overflow.hpp
..\..\include\safe_cast.hpp = ..\..\include\safe_cast.hpp
..\..\include\safe_compare.hpp = ..\..\include\safe_compare.hpp
..\..\include\safe_integer.hpp = ..\..\include\safe_integer.hpp
..\..\include\safe_range.hpp = ..\..\include\safe_range.hpp
EndProjectSection
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "concept", "concept", "{95FF13B8-F33D-4F01-8E98-628666082703}"
ProjectSection(SolutionItems) = preProject
..\..\include\concept\numeric.hpp = ..\..\include\concept\numeric.hpp
EndProjectSection
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_numeric", "test_numeric.vcproj", "{239CF7B4-DB8F-43D0-BC2B-D6FDFC0D4EDF}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Example", "Example", "{64AAA74D-6F56-4C20-9287-989918B52012}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "example_test", "example_test.vcproj", "{3E51E3C4-2F74-4470-8DE9-8B79D1F30FA8}"
EndProject
Project("{8BC9CEB8-8B4A-11D0-8D11-00A0C91BC942}") = "test_safe_range", "test_safe_range.vcproj", "{6DBF6AE0-095C-4508-9420-FB2CD1B8BA3D}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Win32 = Debug|Win32
Release|Win32 = Release|Win32
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{4393B586-DBB1-40B9-AE5D-A1D9E459ACD2}.Debug|Win32.ActiveCfg = Debug|Win32
{4393B586-DBB1-40B9-AE5D-A1D9E459ACD2}.Debug|Win32.Build.0 = Debug|Win32
{4393B586-DBB1-40B9-AE5D-A1D9E459ACD2}.Release|Win32.ActiveCfg = Release|Win32
{4393B586-DBB1-40B9-AE5D-A1D9E459ACD2}.Release|Win32.Build.0 = Release|Win32
{0E0D4BCF-507C-413C-9B8C-E8C972209F8B}.Debug|Win32.ActiveCfg = Debug|Win32
{0E0D4BCF-507C-413C-9B8C-E8C972209F8B}.Debug|Win32.Build.0 = Debug|Win32
{0E0D4BCF-507C-413C-9B8C-E8C972209F8B}.Release|Win32.ActiveCfg = Release|Win32
{0E0D4BCF-507C-413C-9B8C-E8C972209F8B}.Release|Win32.Build.0 = Release|Win32
{21DF917A-7E09-4F06-B599-0CD7E231F06E}.Debug|Win32.ActiveCfg = Debug|Win32
{21DF917A-7E09-4F06-B599-0CD7E231F06E}.Debug|Win32.Build.0 = Debug|Win32
{21DF917A-7E09-4F06-B599-0CD7E231F06E}.Release|Win32.ActiveCfg = Release|Win32
{21DF917A-7E09-4F06-B599-0CD7E231F06E}.Release|Win32.Build.0 = Release|Win32
{3DE806F0-9703-4EF8-A57F-07DC922074D2}.Debug|Win32.ActiveCfg = Debug|Win32
{3DE806F0-9703-4EF8-A57F-07DC922074D2}.Debug|Win32.Build.0 = Debug|Win32
{3DE806F0-9703-4EF8-A57F-07DC922074D2}.Release|Win32.ActiveCfg = Release|Win32
{3DE806F0-9703-4EF8-A57F-07DC922074D2}.Release|Win32.Build.0 = Release|Win32
{AF3D6B8E-EFF2-41C5-8FCE-9060BCD63974}.Debug|Win32.ActiveCfg = Debug|Win32
{AF3D6B8E-EFF2-41C5-8FCE-9060BCD63974}.Debug|Win32.Build.0 = Debug|Win32
{AF3D6B8E-EFF2-41C5-8FCE-9060BCD63974}.Release|Win32.ActiveCfg = Release|Win32
{AF3D6B8E-EFF2-41C5-8FCE-9060BCD63974}.Release|Win32.Build.0 = Release|Win32
{B914598A-A377-4DFB-A3E7-DA1D0F3C4E37}.Debug|Win32.ActiveCfg = Debug|Win32
{B914598A-A377-4DFB-A3E7-DA1D0F3C4E37}.Debug|Win32.Build.0 = Debug|Win32
{B914598A-A377-4DFB-A3E7-DA1D0F3C4E37}.Release|Win32.ActiveCfg = Release|Win32
{B914598A-A377-4DFB-A3E7-DA1D0F3C4E37}.Release|Win32.Build.0 = Release|Win32
{239CF7B4-DB8F-43D0-BC2B-D6FDFC0D4EDF}.Debug|Win32.ActiveCfg = Debug|Win32
{239CF7B4-DB8F-43D0-BC2B-D6FDFC0D4EDF}.Debug|Win32.Build.0 = Debug|Win32
{239CF7B4-DB8F-43D0-BC2B-D6FDFC0D4EDF}.Release|Win32.ActiveCfg = Release|Win32
{239CF7B4-DB8F-43D0-BC2B-D6FDFC0D4EDF}.Release|Win32.Build.0 = Release|Win32
{3E51E3C4-2F74-4470-8DE9-8B79D1F30FA8}.Debug|Win32.ActiveCfg = Debug|Win32
{3E51E3C4-2F74-4470-8DE9-8B79D1F30FA8}.Debug|Win32.Build.0 = Debug|Win32
{3E51E3C4-2F74-4470-8DE9-8B79D1F30FA8}.Release|Win32.ActiveCfg = Release|Win32
{3E51E3C4-2F74-4470-8DE9-8B79D1F30FA8}.Release|Win32.Build.0 = Release|Win32
{6DBF6AE0-095C-4508-9420-FB2CD1B8BA3D}.Debug|Win32.ActiveCfg = Debug|Win32
{6DBF6AE0-095C-4508-9420-FB2CD1B8BA3D}.Debug|Win32.Build.0 = Debug|Win32
{6DBF6AE0-095C-4508-9420-FB2CD1B8BA3D}.Release|Win32.ActiveCfg = Release|Win32
{6DBF6AE0-095C-4508-9420-FB2CD1B8BA3D}.Release|Win32.Build.0 = Release|Win32
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(NestedProjects) = preSolution
{0E0D4BCF-507C-413C-9B8C-E8C972209F8B} = {06D85C87-845D-4C48-BC6F-2D2C5127AA81}
{21DF917A-7E09-4F06-B599-0CD7E231F06E} = {06D85C87-845D-4C48-BC6F-2D2C5127AA81}
{3DE806F0-9703-4EF8-A57F-07DC922074D2} = {06D85C87-845D-4C48-BC6F-2D2C5127AA81}
{AF3D6B8E-EFF2-41C5-8FCE-9060BCD63974} = {06D85C87-845D-4C48-BC6F-2D2C5127AA81}
{B914598A-A377-4DFB-A3E7-DA1D0F3C4E37} = {06D85C87-845D-4C48-BC6F-2D2C5127AA81}
{4393B586-DBB1-40B9-AE5D-A1D9E459ACD2} = {06D85C87-845D-4C48-BC6F-2D2C5127AA81}
{239CF7B4-DB8F-43D0-BC2B-D6FDFC0D4EDF} = {06D85C87-845D-4C48-BC6F-2D2C5127AA81}
{6DBF6AE0-095C-4508-9420-FB2CD1B8BA3D} = {06D85C87-845D-4C48-BC6F-2D2C5127AA81}
{95FF13B8-F33D-4F01-8E98-628666082703} = {98484F4D-80EE-431E-8B13-6DC167B628E1}
{3E51E3C4-2F74-4470-8DE9-8B79D1F30FA8} = {64AAA74D-6F56-4C20-9287-989918B52012}
EndGlobalSection
EndGlobal

View File

@@ -0,0 +1,206 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="test_add"
ProjectGUID="{4393B586-DBB1-40B9-AE5D-A1D9E459ACD2}"
RootNamespace="test_add"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="2"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="C:\BoostRelease"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
CommandLine="&quot;$(TargetPath)&quot;"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
CommandLine="&quot;$(TargetPath)&quot;"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\test_add.cpp"
>
</File>
<File
RelativePath="..\test_add1.cpp"
>
</File>
<File
RelativePath="..\test_add2.cpp"
>
</File>
<File
RelativePath="..\test_add3.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath="..\test_add.hpp"
>
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@@ -0,0 +1,188 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="test_comparison"
ProjectGUID="{5B708F29-AD02-47EE-8933-C26E15C90C88}"
RootNamespace="test_comparison"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="C:\BoostRelease"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\test_misc.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@@ -0,0 +1,198 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="test_conversion"
ProjectGUID="{0E0D4BCF-507C-413C-9B8C-E8C972209F8B}"
RootNamespace="test_conversion"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="C:\BoostRelease"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
CommandLine="&quot;$(TargetPath)&quot;"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
CommandLine="&quot;$(TargetPath)&quot;"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\test_conversion.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath="..\..\include\safe_cast.hpp"
>
</File>
<File
RelativePath="..\..\include\safe_integer.hpp"
>
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@@ -0,0 +1,210 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="test_divide"
ProjectGUID="{21DF917A-7E09-4F06-B599-0CD7E231F06E}"
RootNamespace="test_divide"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="C:\BoostRelease"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
CommandLine="&quot;$(TargetPath)&quot;"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
CommandLine="&quot;$(TargetPath)&quot;"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\test_divide.cpp"
>
</File>
<File
RelativePath="..\test_divide1.cpp"
>
</File>
<File
RelativePath="..\test_divide2.cpp"
>
</File>
<File
RelativePath="..\test_divide3.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath="..\..\include\safe_interval.hpp"
>
</File>
<File
RelativePath="..\test_divide.hpp"
>
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@@ -0,0 +1,206 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="test_modulus"
ProjectGUID="{3DE806F0-9703-4EF8-A57F-07DC922074D2}"
RootNamespace="test_modulus"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="C:\BoostRelease"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
CommandLine="&quot;$(TargetPath)&quot;"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
CommandLine="&quot;$(TargetPath)&quot;"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\test_modulus.cpp"
>
</File>
<File
RelativePath="..\test_modulus1.cpp"
>
</File>
<File
RelativePath="..\test_modulus2.cpp"
>
</File>
<File
RelativePath="..\test_modulus3.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath="..\test_modulus.hpp"
>
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@@ -0,0 +1,206 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="test_multiply"
ProjectGUID="{AF3D6B8E-EFF2-41C5-8FCE-9060BCD63974}"
RootNamespace="test_multiply"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="C:\BoostRelease"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
CommandLine="&quot;$(TargetPath)&quot;"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
CommandLine="&quot;$(TargetPath)&quot;"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\test_multiply.cpp"
>
</File>
<File
RelativePath="..\test_multiply1.cpp"
>
</File>
<File
RelativePath="..\test_multiply2.cpp"
>
</File>
<File
RelativePath="..\test_multiply3.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath="..\test_multiply.hpp"
>
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@@ -0,0 +1,202 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="test_numeric"
ProjectGUID="{239CF7B4-DB8F-43D0-BC2B-D6FDFC0D4EDF}"
RootNamespace="test_numeric"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="c:\BoostRelease"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\test_numeric.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath="..\..\include\fixed_range.hpp"
>
</File>
<File
RelativePath="..\..\include\numeric.hpp"
>
</File>
</Filter>
<Filter
Name="Resource Files"
Filter="rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav"
UniqueIdentifier="{67DA6AB6-F800-4c08-8B7A-83BB121AAD01}"
>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>

View File

@@ -0,0 +1,206 @@
<?xml version="1.0" encoding="Windows-1252"?>
<VisualStudioProject
ProjectType="Visual C++"
Version="9.00"
Name="test_subtract"
ProjectGUID="{B914598A-A377-4DFB-A3E7-DA1D0F3C4E37}"
RootNamespace="test_subtract"
Keyword="Win32Proj"
TargetFrameworkVersion="196613"
>
<Platforms>
<Platform
Name="Win32"
/>
</Platforms>
<ToolFiles>
</ToolFiles>
<Configurations>
<Configuration
Name="Debug|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="0"
AdditionalIncludeDirectories="C:\BoostRelease"
PreprocessorDefinitions="WIN32;_DEBUG;_CONSOLE"
MinimalRebuild="true"
BasicRuntimeChecks="3"
RuntimeLibrary="3"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="4"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="2"
GenerateDebugInformation="true"
SubSystem="1"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
CommandLine="&quot;$(TargetPath)&quot;"
/>
</Configuration>
<Configuration
Name="Release|Win32"
OutputDirectory="$(SolutionDir)$(ConfigurationName)"
IntermediateDirectory="$(ConfigurationName)"
ConfigurationType="1"
CharacterSet="1"
WholeProgramOptimization="1"
>
<Tool
Name="VCPreBuildEventTool"
/>
<Tool
Name="VCCustomBuildTool"
/>
<Tool
Name="VCXMLDataGeneratorTool"
/>
<Tool
Name="VCWebServiceProxyGeneratorTool"
/>
<Tool
Name="VCMIDLTool"
/>
<Tool
Name="VCCLCompilerTool"
Optimization="2"
EnableIntrinsicFunctions="true"
PreprocessorDefinitions="WIN32;NDEBUG;_CONSOLE"
RuntimeLibrary="2"
EnableFunctionLevelLinking="true"
UsePrecompiledHeader="0"
WarningLevel="3"
DebugInformationFormat="3"
/>
<Tool
Name="VCManagedResourceCompilerTool"
/>
<Tool
Name="VCResourceCompilerTool"
/>
<Tool
Name="VCPreLinkEventTool"
/>
<Tool
Name="VCLinkerTool"
LinkIncremental="1"
GenerateDebugInformation="true"
SubSystem="1"
OptimizeReferences="2"
EnableCOMDATFolding="2"
TargetMachine="1"
/>
<Tool
Name="VCALinkTool"
/>
<Tool
Name="VCManifestTool"
/>
<Tool
Name="VCXDCMakeTool"
/>
<Tool
Name="VCBscMakeTool"
/>
<Tool
Name="VCFxCopTool"
/>
<Tool
Name="VCAppVerifierTool"
/>
<Tool
Name="VCPostBuildEventTool"
CommandLine="&quot;$(TargetPath)&quot;"
/>
</Configuration>
</Configurations>
<References>
</References>
<Files>
<Filter
Name="Source Files"
Filter="cpp;c;cc;cxx;def;odl;idl;hpj;bat;asm;asmx"
UniqueIdentifier="{4FC737F1-C7A5-4376-A066-2A32D752A2FF}"
>
<File
RelativePath="..\test_subtract.cpp"
>
</File>
<File
RelativePath="..\test_subtract1.cpp"
>
</File>
<File
RelativePath="..\test_subtract2.cpp"
>
</File>
<File
RelativePath="..\test_subtract3.cpp"
>
</File>
</Filter>
<Filter
Name="Header Files"
Filter="h;hpp;hxx;hm;inl;inc;xsd"
UniqueIdentifier="{93995380-89BD-4b04-88EB-625FBE52EBFB}"
>
<File
RelativePath="..\test_subtract.hpp"
>
</File>
</Filter>
</Files>
<Globals>
</Globals>
</VisualStudioProject>