Jason Stredwick

jason.stredwick@gmail.com

Current Residence:
Bothell, WA 98011
me


Home
CV / Resume
Masters Work
WhirlingDervish
Programming
   Projects
   Various Code
     References
     Quantity/Builtin types
     Endian
     Type
     Functor
     Comm
   Additional
   Ideas
Misc. Ideas
Personal

Summary:


The Quantity class is meant to be a way of turning builtin types into classes that work transparent to the programmer. It also defines a unifying set of names for each of the builtin types for cross platform usage.
These classes are follow:

QuantityUnits.h
Quantity.h
QuantityBuiltin.h
QuantityTest.cpp A test program.

Source Code


QuantityUnits.h


/*

 * Copyright 2006 Jason Stredwick.

 * Distributed under the Boost Software License, Version 1.0. (See
 * LICENSE information at the bottom of this file or the copy at
 * http://www.boost.org/LICENSE_1_0.txt)

 */

#ifndef __QuantityUnits_h__
#define __QuantityUnits_h__

/*

 * This include standardizes the builtin types across the different
 *   operating systems and compilers.  If one is missing, simply add it
 *   and an appropriate typedef.  At a later date, new builtin types
 *   can easily be added regardless of their existence in other systems.
 *   However, if a new type is already defined for another system and 
 *   is then incorporated into another system, please make sure to 
 *   use the same name for compatibility.

 * This include is also meant to standardize the builtin data types 
 *   with a specific name based on the footprint size.  These units
 *   represent quantity size not numbers or types.

 * Jason Stredwick May 29, 2006 Changed the define for Microsoft
 *   Visual C++ from __MSCV__ to _MSC_VER.  I hope this is the last
 *   change to this define and that it always works without having
 *   to actually specify the define to the compiler.

 * Jason Stredwick May 7, 2006
 *   Made byte types lowercase to match with the other units
 *   Changed the define for Microsoft Visual C++ from __TWIN32__ to
 *     __MSVC__.
 *   Added the QuantityUnit class.

 * Jason Stredwick March 27, 2006 Finalized first version of these types

 */

#include <cstring>

#ifdef _MSC_VER

typedef unsigned __int8  byte;
typedef unsigned __int16 word;
typedef unsigned __int32 dword;
typedef unsigned __int64 qword;

typedef unsigned __int8  uint8;
typedef unsigned __int16 uint16;
typedef unsigned __int32 uint32;
typedef unsigned __int64 uint64;

typedef   signed __int8  int8;
typedef   signed __int16 int16;
typedef   signed __int32 int32;
typedef   signed __int64 int64;

#else

typedef unsigned char          byte;
typedef unsigned short         word;
typedef unsigned int           dword;
typedef long long unsigned int qword;

typedef unsigned char          uint8;
typedef unsigned short         uint16;
typedef unsigned int           uint32;
typedef long long unsigned int uint64;

typedef signed char            int8;
typedef signed short           int16;
typedef signed int             int32;
typedef long long signed int   int64;

#endif

template <typename T, std::size_t N>
class QuantityUnit
{
public:
  QuantityUnit(const bool zero) { if(zero) { Zero(); } return; }
  QuantityUnit(const QuantityUnit &qu) { Copy(qu); return; }
  ~QuantityUnit(void) { return; }

  const QuantityUnit &operator=(const QuantityUnit &qu)
  {
    Copy(qu);
    return *this;
  }

  const std::size_t Size(void) const { return N; }

  void Zero(void)
  {
    memset(d_data, 0x00, sizeof(T)*N);
    return;
  }

  void Copy(const QuantityUnit &qu)
  {
    if(&qu == this) { return; }
    memcpy(d_data, qu.d_data, sizeof(T) * N);
    return;
  }

public:
  T d_data[N];
};

#endif

/*

 * Boost Software License - Version 1.0 - August 17th, 2003

 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:

 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.

 */


Quantity.h


/*

 * Copyright 2006 Jason Stredwick.

 * Distributed under the Boost Software License, Version 1.0. (See
 * LICENSE information at the bottom of this file or the copy at
 * http://www.boost.org/LICENSE_1_0.txt)

 */

#ifndef __Quantity_h__
#define __Quantity_h__

/*

 * The Quantity class is meant to provide a class wrapper around builtin
 *   types without overhead storage-wise or performance-wise.  To maintain
 *   storage equivalence with a builtin type, this class is not virtual and
 *   is not meant to be inherited because that would artifically increase
 *   the footprint of this class with a vtable.  This class is also meant
 *   to perform all the operations that are performed on builtin types in
 *   the same fashion.  Thus there is no error checking such as type 
 *   conversion.  However, it should still allow the compiler to
 *   to generate warnings given the correct warning level is provided.  To
 *   accomplish this task, the constructors are templatized in order to 
 *   facilitate static casting of types to the type of the class.  This 
 *   allows the construction of a class whether through a parameterized
 *   constructor method or assignment will auto convert all builtin types
 *   and other Quantity types.  To facilitate seemless functionality the
 *   cast operators are used, which allow global builtin type operators 
 *   such as plus to work without having to explicitly overload that
 *   operator.  However, the cast operator that returns a pointer is not
 *   available and is not defined and is private.

 * Jason Stredwick March 31, 2006 Finalized first version of Quantity class

 */

template <typename T>
class Quantity {
public:
  /*** Constructors ******************************************************/
  Quantity(void) : d_data(0)                                   { return; }
  Quantity(const T &t) : d_data(t)                             { return; }
  template <typename U>
  Quantity(const U &u) : d_data(static_cast<T>(u))             { return; }
  Quantity(const Quantity<T> &q) : d_data(q.d_data)            { return; }
  template <typename U>
  Quantity(const Quantity<U> &q) : d_data(static_cast<T>(q())) { return; }

  /*** Destructor ********************************************************/
  ~Quantity(void) { return; }

  /*** Assignment ********************************************************/
  // Don't need assignment to type T or U because the other assignment
  //   operators will auto create a Quantity of the appropriate type
  //   including static_cast.
  const Quantity<T> &operator=(const Quantity<T> &q)
  {
    if(&q != this) { d_data = q.d_data; }
    return *this;
  }

  template <typename U>
  const Quantity<T> &operator=(const Quantity<U> &q)
  {
    d_data = static_cast<T>(q());
    return *this;
  }

  //*** Accessors/Modifyiers *********************************************/
  const T  Value(void) const { return d_data; }
        T &Value(void)       { return d_data; }

  const T  operator()(void) const { return d_data; }
        T &operator()(void)       { return d_data; }

  /*** Cast Operators ****************************************************/
  operator const T &() const { return d_data; }
  operator       T &()       { return d_data; }

private:
  T d_data;
};

#endif

/*

 * Boost Software License - Version 1.0 - August 17th, 2003

 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:

 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.

 */


QuantityBuiltin.h


/*

 * Copyright 2006 Jason Stredwick.

 * Distributed under the Boost Software License, Version 1.0. (See
 * LICENSE information at the bottom of this file or the copy at
 * http://www.boost.org/LICENSE_1_0.txt)

 */

#ifndef __QuantityBuiltin_h__
#define __QuantityBuiltin_h__

/*

 * This header defines the standard C++ builtin types in terms of
 *   Quantity.  They are meant to turn builtin types into classes
 *   with the same size footprint and will transparently use global
 *   operators as if they are their internal type.

 * Jason Stredwick March 31, 2006 Finalized first version of builtin
 *   quantities.

 */

#include "Quantity.h"
#include "QuantityUnits.h"

typedef Quantity < byte  > Byte;
typedef Quantity < word  > Word;
typedef Quantity < dword > DWord;
typedef Quantity < qword > QWord;

typedef Quantity <   int8 > Int8;
typedef Quantity <  uint8 > UInt8;
typedef Quantity <  int16 > Int16;
typedef Quantity < uint16 > UInt16;
typedef Quantity <  int32 > Int32;
typedef Quantity < uint32 > UInt32;
typedef Quantity <  int64 > Int64;
typedef Quantity < uint64 > UInt64;

typedef Quantity <   signed char  > Char;
typedef Quantity < unsigned char  > UChar;
typedef Quantity <   signed short > Short;
typedef Quantity < unsigned short > UShort;
typedef Quantity <   signed int   > Int;
typedef Quantity < unsigned int   > UInt;
typedef Quantity <   signed long  > Long;
typedef Quantity < unsigned long  > ULong;

typedef Quantity < float > Float;
typedef Quantity < double > Double;

typedef Quantity < size_t > Size_t;

typedef QWord MaxQ; // MAX_Q is the largest builtin unsigned quantity
typedef DWord SecQ; // SEC_Q is the second largest builtin unsigned quantity

const Size_t BITS_PER_BYTE         = 8;
const Size_t BYTES_PER_SIZEOF_UNIT = 1;

#endif

/*

 * Boost Software License - Version 1.0 - August 17th, 2003

 * Permission is hereby granted, free of charge, to any person or organization
 * obtaining a copy of the software and accompanying documentation covered by
 * this license (the "Software") to use, reproduce, display, distribute,
 * execute, and transmit the Software, and to prepare derivative works of the
 * Software, and to permit third-parties to whom the Software is furnished to
 * do so, all subject to the following:

 * The copyright notices in the Software and this entire statement, including
 * the above license grant, this restriction and the following disclaimer,
 * must be included in all copies of the Software, in whole or in part, and
 * all derivative works of the Software, unless such copies or derivative
 * works are solely in the form of machine-executable object code generated by
 * a source language processor.

 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.

 */