vrq
/usr/src/RPM/BUILD/vrq-1.0.96/src/cuint.h
Go to the documentation of this file.
00001 /*****************************************************************************
00002  * Copyright (C) 1997-2007, Mark Hummel
00003  * This file is part of Vrq.
00004  *
00005  * Vrq is free software; you can redistribute it and/or
00006  * modify it under the terms of the GNU General Public
00007  * License as published by the Free Software Foundation; either
00008  * version 2 of the License, or (at your option) any later version.
00009  *
00010  * Vrq is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * General Public License for more details.
00014  *
00015  * You should have received a copy of the GNU General Public
00016  * License along with this library; if not, write to the Free Software
00017  * Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00018  * Boston, MA  02110-1301  USA
00019  *****************************************************************************
00020  */
00021 /*******************************************************************
00022  *
00023  *      cuint.hpp       
00024  *              - header file for cuint.hpp 
00025  *                
00026  *
00027  *******************************************************************
00028  */
00029 
00030 #ifndef CUINT_HPP
00031 #define CUINT_HPP
00032 
00033 #include <stdio.h>
00034 #include <stdlib.h>
00035 #include <string.h>
00036 #include "glue.h"
00037 #include "cobstack.h"
00038 
00039 
00040 #ifndef MAX
00041 #define MAX(a,b)        ((a)<(b)?(b):(a))
00042 #endif // MAX
00043 
00044 #define CUINT_DEFAULT_SIZE      (4)     // size in 32 bit words
00045 
00050 class   CUInt {
00051 private:
00052     unsigned int        defaultData[CUINT_DEFAULT_SIZE];
00053     unsigned int        *data;                          
00054     int         allocatedSize;                  
00055     int         size;                           
00056     CObstack*   heap;                           
00057 public:
00058 
00062     CUInt( void ) 
00063     {
00064         heap = NULL;
00065         allocatedSize = CUINT_DEFAULT_SIZE;
00066         data = defaultData;
00067         size = 1;
00068         data[0] = 0;
00069     };          
00070     
00075     CUInt( unsigned int aValue ) 
00076     {
00077         heap = NULL;
00078         allocatedSize = CUINT_DEFAULT_SIZE;
00079         data = defaultData;
00080         size = 1;
00081         data[0] = aValue;
00082     };          
00083     
00084     
00089     CUInt( const CUInt &aValue ) 
00090     {
00091         INT32   i;
00092         
00093         heap = NULL;
00094         allocatedSize = CUINT_DEFAULT_SIZE;
00095         data = defaultData;
00096         size = 1;
00097         grow( aValue.size );
00098         for( i = 0; i < size; i++ ) {
00099                 data[i] = aValue.data[i];
00100         }
00101     };          
00102     
00106     virtual ~CUInt()    
00107     {
00108         if( heap == NULL && data != defaultData ) {
00109                 shell_xfree( data, size );
00110                 data = NULL;
00111         }
00112     } ;
00113     
00114     
00119     void SetHeap( CObstack* aHeap )
00120     {
00121         heap = aHeap;
00122     }
00123     
00126     const CUInt &operator=( const CUInt &aValue )        
00127     {
00128         INT32   i;
00129         
00130         grow( aValue.size );
00131         for( i = 0; i < size; i++ ) {
00132                 data[i] = aValue.data[i];
00133         }
00134         return( aValue );
00135     };
00136     
00137     const UINT64 operator=( const UINT64 aValue )        
00138     {
00139         if( aValue>>32 != 0 ) {
00140                 grow( 2 );
00141                 data[0] = aValue;
00142                 data[1] = aValue>>32;
00143         } else {
00144                 grow( 1 );
00145                 data[0] = aValue;
00146         }
00147         return( aValue );
00148     };
00150     
00157     void        Truncate( int newSize )
00158     {
00159         grow( newSize );
00160     };
00161 
00164     CUInt operator~( void ) const;
00166     
00169     CUInt operator+( const CUInt & ) const;
00170     CUInt operator+( const unsigned int ) const;
00171     CUInt operator-( const CUInt & ) const;
00172     CUInt operator-( unsigned int ) const;
00173     CUInt operator*( const CUInt & ) const;
00174     CUInt operator*( unsigned int ) const;
00175     CUInt operator/( const CUInt & ) const;
00176     CUInt operator/( unsigned int ) const;
00177     CUInt operator%( const CUInt & ) const;
00178     CUInt operator%( unsigned int ) const;
00179     CUInt operator>>( const CUInt & ) const;
00180     CUInt operator>>( unsigned int ) const;
00181     CUInt operator<<( const CUInt & ) const;
00182     CUInt operator<<( unsigned int ) const;
00183     CUInt operator&( const CUInt & ) const;
00184     CUInt operator&( unsigned int ) const;
00185     CUInt operator|( const CUInt & ) const;
00186     CUInt operator|( unsigned int ) const;
00187     CUInt operator^( const CUInt & ) const;
00188     CUInt operator^( unsigned int ) const;
00190     
00193     int operator<( const CUInt & ) const;
00194     int operator<( unsigned int ) const;
00195     int operator<=( const CUInt & ) const;
00196     int operator<=( unsigned int ) const;
00197     int operator>( const CUInt & ) const;
00198     int operator>( unsigned int ) const;
00199     int operator>=( const CUInt & ) const;
00200     int operator>=( unsigned int ) const;
00201     int operator==( const CUInt & ) const;
00202     int operator==( unsigned int ) const;
00203     int operator!=( const CUInt & ) const;
00204     int operator!=( unsigned int ) const;
00206     
00209     UINT32 GetUINT32() const; 
00210     UINT64 GetUINT64() const;
00212     
00217     UINT32 Size();
00218     
00222 private:
00223     void calibrate( void )
00224     {
00225         int     i;
00226         for( i=size-1; i>0; i-- ) {
00227             if( data[i] != 0 ) {
00228                 break;
00229             }
00230         }       
00231         size = i+1;
00232         return;
00233     };
00234     void grow( int aSize )
00235     {
00236         int i;
00237     
00238         if( aSize <= size ) {
00239             size = aSize;
00240             return;
00241         }
00242         if( allocatedSize < aSize ) {
00243             if( heap == NULL ) {
00244                 if( data != defaultData ) {
00245                     unsigned int* newData;
00246                     newData = (unsigned int*)shell_xmalloc( aSize*4);
00247                     memcpy( newData, data, size*4 );
00248                     shell_xfree( data, allocatedSize*4 );
00249                     data = newData;
00250                 } else {
00251                     data = (unsigned int*)shell_xmalloc( aSize*4 );
00252                     memcpy( data, defaultData, size*4 );
00253                 }
00254             } else {
00255                 unsigned int*   newData;
00256                 newData = (unsigned int*)heap->Alloc( aSize*4 );
00257                 memcpy( newData, data, size*4 );
00258                 data = newData;
00259             }
00260             allocatedSize = aSize;
00261         }
00262         for( i=size; i < aSize; i++ ) {
00263             data[i] = 0;
00264         }
00265         size = aSize;
00266     };  
00270 };
00271 
00272 
00273 
00274 
00275 #endif // CUINT_HPP