forked from boostorg/safe_numerics
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
attempt to move pointer to common ancestor
- Loading branch information
Showing
19 changed files
with
406 additions
and
66 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
<?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 environment 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.</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 | ||
<boost/safe_numerics/overflow.hpp> </code></ulink></para> | ||
</section> | ||
|
||
<section> | ||
<title>Example of use</title> | ||
|
||
<programlisting>#include <cstdio> | ||
|
||
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
<?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<T, U></title> | ||
|
||
<section> | ||
<title>Synopsis</title> | ||
|
||
<programlisting>template<class T, class U> | ||
T safe_cast(const U & u);</programlisting> | ||
</section> | ||
|
||
<section> | ||
<title>Description</title> | ||
|
||
<para>Converts one <link linkend="safe_numerics.numeric">Numeric</link> | ||
type to another. Throws an <code>std::out_of_range</code> exception if | ||
such a conversion is not possible without changing the value. This | ||
function is part of the implementation of the safe numerics library. It's | ||
been made publicly because it might be useful in related contexts.</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><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>Preconditions</title> | ||
|
||
<para>The value of u must be representable 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>Header</title> | ||
|
||
<para><filename><ulink url="../../include/safe_cast.hpp">#include | ||
<boost/numeric/safe_cast.hpp> </ulink></filename></para> | ||
</section> | ||
|
||
<section> | ||
<title>Example of use</title> | ||
|
||
<programlisting>#include <boost/numeric/safe_cast.hpp> | ||
#include <boost/numeric/safe_integer.hpp> | ||
|
||
void f(){ | ||
safe_integer<char> i; | ||
unsigned char j; | ||
i = 1; | ||
j = safe_cast<unsigned char>(i); // ok | ||
i = -1; | ||
j = safe_cast<unsigned char>(i); // throws std::out_of_range exception | ||
i = 1024; | ||
j = safe_cast<unsigned char>(i); // throws std::out_of_range exception | ||
}</programlisting> | ||
</section> | ||
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
<?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<T, U></title> | ||
|
||
<section> | ||
<title>Synopsis</title> | ||
|
||
<para>safe_compare is several functions:.</para> | ||
|
||
<programlisting>template<class T, class U> | ||
bool safe_compare::less_than(const T & lhs, const U & rhs); | ||
|
||
template<class T, class U> | ||
bool safe_compare::less_than_equal(const T & lhs, const U & rhs); | ||
|
||
template<class T, class U> | ||
bool safe_compare::greater_than(const T & lhs, const U & rhs); | ||
|
||
template<class T, class U> | ||
bool safe_compare::greater_than_equal(const T & lhs, const U & rhs); | ||
|
||
template<class T, class U> | ||
bool safe_compare::equal(const T & lhs, const U & rhs); | ||
|
||
template<class T, class U> | ||
bool safe_compare::not_equal(const T & lhs, const U & 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 guarantees a | ||
correct result regardless of the types of the arguments. It's used in the | ||
implementation of the safe numerics library. It has been made publicly | ||
accessible in because it might be useful in other contexts.</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><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><ulink url="../../include/safe_compare.hpp"><filename>#include | ||
<boost/numeric/safe_compare.hpp> </filename></ulink></para> | ||
</section> | ||
|
||
<section> | ||
<title>Example of use</title> | ||
|
||
<programlisting>#include <boost/numeric/safe_compare.hpp> | ||
|
||
void f(){ | ||
unsigned char i = 129; | ||
unsigned int j = 1; | ||
assert(j < i); // program traps because expression is false. But this is a surprise because 1 < 129 | ||
assert( safe_compare::less_than(j,i)); // expression is true as we would expect | ||
} | ||
|
||
// safe_compare is used to implement comparison operators for safe types. So we can do this: | ||
void g(){ | ||
safe<unsigned char> int i = 0x129; | ||
safe<int> j = 1; | ||
assert(j < i); // program works as expected | ||
}</programlisting> | ||
</section> | ||
</section> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.