Back to index

nux  3.0.0
NSerializer.h
Go to the documentation of this file.
00001 /*
00002  * Copyright 2010 Inalogic® Inc.
00003  *
00004  * This program is free software: you can redistribute it and/or modify it
00005  * under the terms of the GNU Lesser General Public License, as
00006  * published by the  Free Software Foundation; either version 2.1 or 3.0
00007  * of the License.
00008  *
00009  * This program is distributed in the hope that it will be useful, but
00010  * WITHOUT ANY WARRANTY; without even the implied warranties of
00011  * MERCHANTABILITY, SATISFACTORY QUALITY or FITNESS FOR A PARTICULAR
00012  * PURPOSE.  See the applicable version of the GNU Lesser General Public
00013  * License for more details.
00014  *
00015  * You should have received a copy of both the GNU Lesser General Public
00016  * License along with this program. If not, see <http://www.gnu.org/licenses/>
00017  *
00018  * Authored by: Jay Taoko <jaytaoko@inalogic.com>
00019  *
00020  */
00021 
00022 
00023 #ifndef NSERIALIZER_H
00024 #define NSERIALIZER_H
00025 
00026 namespace nux
00027 {
00028 
00029   class NSerializer
00030   {
00031   public:
00032     enum
00033     {
00034       OutputErrorIfFail   = 0x01,
00035       NoOverWrite         = 0x02,
00036       OverWriteReadOnly   = 0x04,
00037       Unbuffered          = 0x08,
00038       Append                    = 0x10,
00039       Read                = 0x20,
00040       Write               = 0x40,
00041     };
00042 
00043     typedef enum
00044     {
00045       SeekStart           = 0x00,
00046       SeekCurrent         = 0x01,
00047       SeekEnd             = 0x02,
00048     } SeekPos;
00049 
00050     // NSerializer interface.
00051     virtual ~NSerializer() {}
00052     virtual void SerializeFinal (void *V, long long Length) = 0;
00053 //     template<typename T>
00054 //     void SerializeBuffer( T* buffer, unsigned long long NumberOfElements, unsigned long long ElementSize = sizeof(T))
00055 //     {
00056 //         for(unsigned long long i = 0; i < NumberOfElements; i++)
00057 //         {
00058 //             unsigned char* bytebuffer = (unsigned char*)(&buffer[i]);
00059 //             Serialize(bytebuffer, ElementSize);
00060 //         }
00061 //     }
00062     virtual bool isReader() = 0;
00063     virtual bool isWriter() = 0;
00064     virtual long long Tell() = 0;
00065     virtual long long GetFileSize()
00066     {
00067       return -1;
00068     }
00069     virtual long long Seek (long long FilePos, NSerializer::SeekPos) = 0;
00070     virtual bool Precache (INT PrecacheOffset, INT PrecacheSize)
00071     {
00072       return TRUE;
00073     }
00074     virtual void Flush() {};
00075     virtual bool Close() = 0;
00076     virtual bool GetError()
00077     {
00078       return m_ErrorCode;
00079     }
00080 
00081     NSerializer &ByteOrderSerialize (void *V, INT Length )
00082     {
00083       BOOL SwapBytes = 0;
00084 
00085       if ( SwapBytes )
00086       {
00087         // Transferring between memory and file, so flip the byte order.
00088         for ( INT i = Length - 1; i >= 0; i-- )
00089           Serialize ( (unsigned char *) V + i, 1 );
00090       }
00091       else
00092       {
00093         // Transferring around within memory, so keep the byte order.
00094         Serialize ( (unsigned char *) V, Length);
00095       }
00096 
00097       return *this;
00098     }
00099 
00100     // Constructor.
00101     NSerializer()
00102     {
00103       Reset();
00104     }
00105 
00106     NUX_INLINE bool IsError() const
00107     {
00108       return m_ErrorCode;
00109     }
00110 
00111     virtual void Serialize (char   &data);
00112     virtual void Serialize (wchar_t  &data);
00113     virtual void Serialize (bool   &data);
00114     virtual void Serialize (unsigned char     &data);
00115     virtual void Serialize (unsigned short    &data);
00116     virtual void Serialize (short    &data);
00117     virtual void Serialize (unsigned int &data);
00118     virtual void Serialize (int  &data);
00119     virtual void Serialize (long   &data);
00120     virtual void Serialize (unsigned long  &data);
00121     virtual void Serialize (float  &data);
00122     virtual void Serialize (double &data);
00123     virtual void Serialize (unsigned long long    &data);
00124     virtual void Serialize (long long    &data);
00125 
00126     virtual void Serialize (wchar_t  *buffer, unsigned int len, unsigned int stride = sizeof (wchar_t) );
00127     virtual void Serialize (bool   *buffer, unsigned int len, unsigned int stride = sizeof (bool)  );
00128     virtual void Serialize (char   *buffer, unsigned int len, unsigned int stride = sizeof (char) );
00129     virtual void Serialize (unsigned char     *buffer, unsigned int len, unsigned int stride = sizeof (unsigned char)    );
00130     virtual void Serialize (unsigned short    *buffer, unsigned int len, unsigned int stride = sizeof (unsigned short)   );
00131     virtual void Serialize (short    *buffer, unsigned int len, unsigned int stride = sizeof (short)   );
00132     virtual void Serialize (unsigned int *buffer, unsigned int len, unsigned int stride = sizeof (unsigned int) );
00133     virtual void Serialize (int  *buffer, unsigned int len, unsigned int stride = sizeof (int) );
00134     virtual void Serialize (long   *buffer, unsigned int len, unsigned int stride = sizeof (long)  );
00135     virtual void Serialize (unsigned long  *buffer, unsigned int len, unsigned int stride = sizeof (unsigned long) );
00136     virtual void Serialize (float  *buffer, unsigned int len, unsigned int stride = sizeof (float) );
00137     virtual void Serialize (double *buffer, unsigned int len, unsigned int stride = sizeof (double) );
00138     virtual void Serialize (unsigned long long    *buffer, unsigned int len, unsigned int stride = sizeof (unsigned long long)   );
00139     virtual void Serialize (long long    *buffer, unsigned int len, unsigned int stride = sizeof (long long)   );
00140 
00141     virtual void Identify (const char *name) {};
00142     virtual void Begin() {};
00143     virtual void End() {};
00144 
00145   protected:
00146     void Reset (void)
00147     {
00148       m_ErrorCode = FALSE;
00149     }
00150     bool m_ErrorCode;
00151   };
00152 
00153   NUX_INLINE NSerializer &operator << (NSerializer &Sr, bool      &v)
00154   {
00155     Sr.Serialize (v);
00156     return Sr;
00157   }
00158   NUX_INLINE NSerializer &operator << (NSerializer &Sr, char     &v)
00159   {
00160     Sr.Serialize (v);
00161     return Sr;
00162   }
00163   NUX_INLINE NSerializer &operator << (NSerializer &Sr, unsigned char        &v)
00164   {
00165     Sr.Serialize (v);
00166     return Sr;
00167   }
00168   NUX_INLINE NSerializer &operator << (NSerializer &Sr, unsigned short       &v)
00169   {
00170     Sr.Serialize (v);
00171     return Sr;
00172   }
00173   NUX_INLINE NSerializer &operator << (NSerializer &Sr, short       &v)
00174   {
00175     Sr.Serialize (v);
00176     return Sr;
00177   }
00178   NUX_INLINE NSerializer &operator << (NSerializer &Sr, unsigned int    &v)
00179   {
00180     Sr.Serialize (v);
00181     return Sr;
00182   }
00183   NUX_INLINE NSerializer &operator << (NSerializer &Sr, int     &v)
00184   {
00185     Sr.Serialize (v);
00186     return Sr;
00187   }
00188   NUX_INLINE NSerializer &operator << (NSerializer &Sr, long      &v)
00189   {
00190     Sr.Serialize (v);
00191     return Sr;
00192   }
00193   NUX_INLINE NSerializer &operator << (NSerializer &Sr, unsigned long     &v)
00194   {
00195     Sr.Serialize (v);
00196     return Sr;
00197   }
00198   NUX_INLINE NSerializer &operator << (NSerializer &Sr, float     &v)
00199   {
00200     Sr.Serialize (v);
00201     return Sr;
00202   }
00203   NUX_INLINE NSerializer &operator << (NSerializer &Sr, double    &v)
00204   {
00205     Sr.Serialize (v);
00206     return Sr;
00207   }
00208   NUX_INLINE NSerializer &operator << (NSerializer &Sr, unsigned long long       &v)
00209   {
00210     Sr.Serialize (v);
00211     return Sr;
00212   }
00213   NUX_INLINE NSerializer &operator << (NSerializer &Sr, long long       &v)
00214   {
00215     Sr.Serialize (v);
00216     return Sr;
00217   }
00218 
00219 
00220 }
00221 
00222 #endif // NSERIALIZER_H
00223