<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<HTML>
<HEAD>
  <meta name="Classification" content="Software Development">
  <meta name="KeyWords" content="C++,Quality,Code Generation,Codign Standard">
<TITLE>Listing 2</TITLE>
</HEAD>
<BODY>

<table CELLSPACING=0 CELLPADDING=6 WIDTH="100%" BGCOLOR="#009000">
  <tr><td>
    <table COLS=2 WIDTH="100%">
      <tr>
        <td COLSPAN="2">
          <font color="#FFFFFF" size=+4>Sven Rosvall</font>
        </td>
      </tr>
      <tr>
        <td>
          <i><font color="#FFFFFF"></font></i>
        </td>
        <td ALIGN=RIGHT>
          <font color="#FFFFFF">
          <a href="index.html"><font color="#FFFFFF">Home</font></a>
          -
          <a href="/Contact.html"><font color="#FFFFFF">Contact Info</font></a>
          </font>
        </td>
      </tr>
    </table>
  </td></tr>
</table>
<TABLE CELLSPACING=0 CELLPADDING=10>
<TR><TD VALIGN=TOP BGCOLOR="#CCFFFF">
·&nbsp;<A HREF="../index.html">Start</A><BR>
·&nbsp;<A HREF="../Sven-E.html">Sven&nbsp;Rosvall</A><BR>
&nbsp;&nbsp;·&nbsp;<A HREF="../Sven_CV.html">CV</A><BR>
&nbsp;&nbsp;·&nbsp;<A HREF="../Sven_Proj.html">Projects</A><BR>
&nbsp;&nbsp;·&nbsp;<A HREF="Articles.html">Articles</A><BR>
&nbsp;&nbsp;&nbsp;&nbsp;·&nbsp;<A HREF="ArtStrings.html">Mixing&nbsp;Strings&nbsp;in&nbsp;C++</A><BR>
&nbsp;&nbsp;&nbsp;&nbsp;·&nbsp;<A HREF="ArtSafeC.html">C++&nbsp;as&nbsp;a&nbsp;Safer&nbsp;C</A><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;·&nbsp;<A HREF="BoundedInt.h">Listing&nbsp;1</A><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;·&nbsp;<B>Listing&nbsp;2</B><BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;·&nbsp;<A HREF="BoundedPointer.h">Listing&nbsp;3</A><BR>
&nbsp;&nbsp;&nbsp;&nbsp;·&nbsp;<A HREF="CppLookup.html">C++&nbsp;Lookup&nbsp;Mysteries</A><BR>
·&nbsp;<A HREF="../Kari.html">Kari&nbsp;Rosvall</A><BR>
·&nbsp;<A HREF="../Sven_Kari.html">The&nbsp;Rosvalls</A><BR>
</TD><TD VALIGN=TOP>
<H2>Listing 2</H2>
<PRE>
// BoundedIntTraits.h
// Information on types based on a given value range.
//

#ifndef BoundedIntTraits_h
#define BoundedIntTraits_h

#include &lt;climits&gt;

// Compile time assertion:
template &lt;bool condition&gt;
struct StaticAssert;
template &lt;&gt;
struct StaticAssert&lt;true&gt; {};

#ifndef NO_PARTIAL_SPECIALIZATIONS

// Template for finding the smallest built-in type that can
// hold a given value range, based on a set of conditions.
template &lt;bool sign, bool negbyte, bool negshort, bool negint,
          bool sbyte, bool ubyte, bool sshort, bool ushort, bool sint&gt;
struct BoundedIntType;

template &lt;&gt;
struct BoundedIntType&lt;true, true, true, true,
                      true, true, true, true, true&gt;
{ typedef signed char Type; };

template &lt;bool negbyte, bool sbyte, bool ubyte&gt;
struct BoundedIntType&lt;true, negbyte, true, true,
                      sbyte, ubyte, true, true, true&gt;
{ typedef signed short Type; };

template &lt;bool negbyte, bool negshort,
          bool sbyte, bool ubyte, bool sshort, bool ushort&gt;
struct BoundedIntType&lt;true, negbyte, negshort, true,
                      sbyte, ubyte, sshort, ushort, true&gt;
{ typedef signed int Type; };

template &lt;bool sbyte&gt;
struct BoundedIntType&lt;false, true, true, true,
                      sbyte, true, true, true, true&gt;
{ typedef unsigned char Type; };

template &lt;bool sbyte, bool ubyte, bool sshort&gt;
struct BoundedIntType&lt;false, true, true, true,
                      sbyte, ubyte, sshort, true, true&gt;
{ typedef unsigned short Type; };

template &lt;bool sbyte, bool ubyte, bool sshort, bool ushort, bool sint&gt;
struct BoundedIntType&lt;false, true, true, true,
                      sbyte, ubyte, sshort, ushort, sint&gt;
{ typedef unsigned int Type; };
#endif

//
// The traits template provides value range information to
// the BoundedIntType to get the smallest possible type.
//
template &lt;int Lower, int Upper&gt;
struct BoundedIntTraits
{
  StaticAssert&lt;(Lower &lt;= Upper)&gt; check;
#ifdef NO_PARTIAL_SPECIALIZATIONS
  typedef int Type;
#else
  typedef typename
    BoundedIntType&lt;Lower &lt; 0,
                   Lower &gt;= CHAR_MIN,
                   Lower &gt;= SHRT_MIN,
                   Lower &gt;= INT_MIN,
                   Upper &lt;= CHAR_MAX,
                   Upper &lt;= UCHAR_MAX,
                   Upper &lt;= SHRT_MAX,
                   Upper &lt;= USHRT_MAX,
                   Upper &lt;= INT_MAX &gt;::Type Type;
#endif
};
</PRE>
<P><HR>
<FONT SIZE=-1> Copyright 2003-2010</FONT>
</TD></TR>
</TABLE>
</BODY></HTML>

