TCValue.h

Go to the documentation of this file.
00001 //*******************************************************************************
00002 //
00003 // *******   ***   ***               *
00004 //    *     *     *                  *
00005 //    *    *      *                *****
00006 //    *    *       ***  *   *   **   *    **    ***
00007 //    *    *          *  * *   *     *   ****  * * *
00008 //    *     *         *   *      *   * * *     * * *
00009 //    *      ***   ***    *     **   **   **   *   *
00010 //                        *
00011 //*******************************************************************************
00012 // see http://sourceforge.net/projects/tcsystem/ for details.
00013 // Copyright (C) 2003 - 2008 Thomas Goessler. All Rights Reserved. 
00014 //*******************************************************************************
00015 //
00016 // TCSystem is the legal property of its developers.
00017 // Please refer to the COPYRIGHT file distributed with this source distribution.
00018 // 
00019 // This library is free software; you can redistribute it and/or             
00020 // modify it under the terms of the GNU Lesser General Public                
00021 // License as published by the Free Software Foundation; either              
00022 // version 2.1 of the License, or (at your option) any later version.        
00023 //                                                                           
00024 // This library is distributed in the hope that it will be useful,           
00025 // but WITHOUT ANY WARRANTY; without even the implied warranty of            
00026 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU         
00027 // Lesser General Public License for more details.                           
00028 //                                                                           
00029 // You should have received a copy of the GNU Lesser General Public          
00030 // License along with this library; if not, write to the Free Software       
00031 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.
00032 //*******************************************************************************
00033 //  $Id: TCValue.h 809 2008-02-10 22:21:18Z the_____tiger $
00034 //*******************************************************************************
00035 
00036 #ifndef _TCVALUE_H_
00037 #define _TCVALUE_H_
00038 
00039 #include "TCUtil.h"
00040 
00041 namespace TC
00042 {
00059    template < class T>
00060    class Value
00061    {
00062    public:
00068       Value()
00069       {
00070          m_val = (T)0;
00071       }
00076       Value(const Value<T> &val)
00077       {
00078          m_val = val.m_val;
00079       }
00084       Value(const T &val)
00085       {
00086          m_val = val;
00087       }
00088 
00090       operator const T&() const
00091       {
00092          return m_val;
00093       }
00094 
00096       const uchar* GetBytes() const { return reinterpret_cast<const uchar*>(&m_val); }
00098       uchar* GetBytes() { return reinterpret_cast<uchar*>(&m_val); }
00100       uint32 GetNumBytes() const { return (uint32)sizeof(T); }
00101 
00103       Value<T>& operator=(const T &a)
00104       {
00105          m_val = a;
00106          return *this;
00107       }
00108 
00109    private:
00111       T m_val;  
00112    };
00113 
00117    template < class T, bool IS_LITTLE_ENDIAN >
00118    class ByteOrderedValue
00119    {
00120    protected:
00126       ByteOrderedValue()
00127       {
00128          m_val = (T)0;
00129          SwapBytes(m_val);
00130       }
00135       ByteOrderedValue(const ByteOrderedValue<T, IS_LITTLE_ENDIAN> &val)
00136       {
00137          m_val = val.m_val;
00138       }
00143       ByteOrderedValue(const T &val)
00144       {
00145          m_val = val;
00146          SwapBytes(m_val);
00147       }
00148 
00149    public:
00151       operator T() const
00152       {
00153          T valOut = m_val;
00154          SwapBytes(valOut);
00155          return valOut;
00156       }
00157 
00159       const uchar* GetBytes() const { return reinterpret_cast<const uchar*>(&m_val); }
00161       uchar* GetBytes() { return reinterpret_cast<uchar*>(&m_val); }
00163       uint32 GetNumBytes() const { return (uint32)sizeof(T); }
00164       
00166       ByteOrderedValue<T, IS_LITTLE_ENDIAN>& operator=(const T &a)
00167       {
00168          m_val = a;
00169          SwapBytes(m_val);
00170          return *this;
00171       }
00172 
00173    private:
00175       inline void SwapBytes(T &val) const
00176       {
00177          if (Util::IsLittleEndian()!=IS_LITTLE_ENDIAN)
00178          {
00179             Util::SwapBytes(val);
00180          }
00181       }
00182 
00184       T m_val;  
00185    };
00186 
00191    template <class T>
00192    class LittleEndianValue: public ByteOrderedValue<T, true>
00193    {
00194    public:
00195       LittleEndianValue():ByteOrderedValue<T, true>() {}
00196       LittleEndianValue(const LittleEndianValue<T> &val):ByteOrderedValue<T, true>(val) {}
00197       LittleEndianValue(const T &val):ByteOrderedValue<T, true>(val) {}
00198    };
00199 
00204    template <class T>
00205    class BigEndianValue: public ByteOrderedValue<T, false>
00206    {
00207    public:
00208       BigEndianValue():ByteOrderedValue<T, false>() {}
00209       BigEndianValue(const BigEndianValue<T> &val):ByteOrderedValue<T, false>(val) {}
00210       BigEndianValue(const T &val):ByteOrderedValue<T, false>(val) {}
00211    };
00212 
00216 }
00217 
00218 #endif // _TCVALUE_H_

Copyright (c) Thomas Goessler 2003 - 2008