nux-1.14.0
|
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, t_s64 Length) = 0; 00053 // template<typename T> 00054 // void SerializeBuffer( T* buffer, t_u64 NumberOfElements, t_u64 ElementSize = sizeof(T)) 00055 // { 00056 // for(t_u64 i = 0; i < NumberOfElements; i++) 00057 // { 00058 // t_u8* bytebuffer = (t_u8*)(&buffer[i]); 00059 // Serialize(bytebuffer, ElementSize); 00060 // } 00061 // } 00062 virtual bool isReader() = 0; 00063 virtual bool isWriter() = 0; 00064 virtual t_s64 Tell() = 0; 00065 virtual t_s64 GetFileSize() 00066 { 00067 return -1; 00068 } 00069 virtual t_s64 Seek (t_s64 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 ( (t_u8 *) V + i, 1 ); 00090 } 00091 else 00092 { 00093 // Transferring around within memory, so keep the byte order. 00094 Serialize ( (t_u8 *) 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 (t_char &data); 00112 virtual void Serialize (t_wchar &data); 00113 virtual void Serialize (t_bool &data); 00114 virtual void Serialize (t_s8 &data); 00115 virtual void Serialize (t_u8 &data); 00116 virtual void Serialize (t_u16 &data); 00117 virtual void Serialize (t_s16 &data); 00118 virtual void Serialize (t_uint32 &data); 00119 virtual void Serialize (t_int32 &data); 00120 virtual void Serialize (t_long &data); 00121 virtual void Serialize (t_ulong &data); 00122 virtual void Serialize (t_float &data); 00123 virtual void Serialize (t_double &data); 00124 virtual void Serialize (t_u64 &data); 00125 virtual void Serialize (t_s64 &data); 00126 00127 virtual void Serialize (t_char *buffer, t_u32 len, t_u32 stride = sizeof (t_char) ); 00128 virtual void Serialize (t_wchar *buffer, t_u32 len, t_u32 stride = sizeof (t_wchar) ); 00129 virtual void Serialize (t_bool *buffer, t_u32 len, t_u32 stride = sizeof (t_bool) ); 00130 virtual void Serialize (t_s8 *buffer, t_u32 len, t_u32 stride = sizeof (t_s8) ); 00131 virtual void Serialize (t_u8 *buffer, t_u32 len, t_u32 stride = sizeof (t_u8) ); 00132 virtual void Serialize (t_u16 *buffer, t_u32 len, t_u32 stride = sizeof (t_u16) ); 00133 virtual void Serialize (t_s16 *buffer, t_u32 len, t_u32 stride = sizeof (t_s16) ); 00134 virtual void Serialize (t_uint32 *buffer, t_u32 len, t_u32 stride = sizeof (t_uint32) ); 00135 virtual void Serialize (t_int32 *buffer, t_u32 len, t_u32 stride = sizeof (t_int32) ); 00136 virtual void Serialize (t_long *buffer, t_u32 len, t_u32 stride = sizeof (t_long) ); 00137 virtual void Serialize (t_ulong *buffer, t_u32 len, t_u32 stride = sizeof (t_ulong) ); 00138 virtual void Serialize (t_float *buffer, t_u32 len, t_u32 stride = sizeof (t_float) ); 00139 virtual void Serialize (t_double *buffer, t_u32 len, t_u32 stride = sizeof (t_double) ); 00140 virtual void Serialize (t_u64 *buffer, t_u32 len, t_u32 stride = sizeof (t_u64) ); 00141 virtual void Serialize (t_s64 *buffer, t_u32 len, t_u32 stride = sizeof (t_s64) ); 00142 00143 virtual void Identify (const char *name) {}; 00144 virtual void Begin() {}; 00145 virtual void End() {}; 00146 00147 protected: 00148 void Reset (void) 00149 { 00150 m_ErrorCode = FALSE; 00151 } 00152 bool m_ErrorCode; 00153 }; 00154 00155 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_char &v) 00156 { 00157 Sr.Serialize (v); 00158 return Sr; 00159 } 00160 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_bool &v) 00161 { 00162 Sr.Serialize (v); 00163 return Sr; 00164 } 00165 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_s8 &v) 00166 { 00167 Sr.Serialize (v); 00168 return Sr; 00169 } 00170 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_u8 &v) 00171 { 00172 Sr.Serialize (v); 00173 return Sr; 00174 } 00175 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_u16 &v) 00176 { 00177 Sr.Serialize (v); 00178 return Sr; 00179 } 00180 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_s16 &v) 00181 { 00182 Sr.Serialize (v); 00183 return Sr; 00184 } 00185 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_uint32 &v) 00186 { 00187 Sr.Serialize (v); 00188 return Sr; 00189 } 00190 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_int32 &v) 00191 { 00192 Sr.Serialize (v); 00193 return Sr; 00194 } 00195 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_long &v) 00196 { 00197 Sr.Serialize (v); 00198 return Sr; 00199 } 00200 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_ulong &v) 00201 { 00202 Sr.Serialize (v); 00203 return Sr; 00204 } 00205 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_float &v) 00206 { 00207 Sr.Serialize (v); 00208 return Sr; 00209 } 00210 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_double &v) 00211 { 00212 Sr.Serialize (v); 00213 return Sr; 00214 } 00215 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_u64 &v) 00216 { 00217 Sr.Serialize (v); 00218 return Sr; 00219 } 00220 NUX_INLINE NSerializer &operator << (NSerializer &Sr, t_s64 &v) 00221 { 00222 Sr.Serialize (v); 00223 return Sr; 00224 } 00225 00226 00227 } 00228 00229 #endif // NSERIALIZER_H 00230