stream.h

Go to the documentation of this file.
00001 
00002 //**************************************************************************/
00003 // Copyright (c) 2008 Autodesk, Inc.
00004 // All rights reserved.
00005 //
00006 // Use of this software is subject to the terms of the Autodesk license
00007 // agreement provided at the time of installation or download, or which
00008 // otherwise accompanies this software in either electronic or hard copy form.
00009 //
00010 //**************************************************************************/
00011 // DESCRIPTION:
00012 // CREATED: October 2008
00013 //**************************************************************************/
00014 
00015 namespace mudbox {
00016 
00017 typedef Node *NodePointer;
00018 
00039 class MBDLL_DECL Stream : public Node
00040 {
00041     DECLARE_CLASS;
00042 protected:
00044     Stream( void );
00045     
00046 public:
00047     enum { chunk = 0x800000 };
00048 
00050     virtual ~Stream( void );
00051 
00053     virtual QString FileName( void ) const;
00054 
00064     virtual bool IsValid( void );
00065 
00071     virtual bool IsOlderThan(
00072         int iVersion    
00073         ) const;
00074 
00081     virtual bool IsNewerThan(
00082                              int iVersion   
00083                              ) const;
00084     
00089     virtual int ClassVersion( const class ClassDesc *pClass ) const;
00090     
00102     template <typename Type>
00103     bool IsOlderThan(
00104                      int iVersion,  
00105                      Type * 
00106                      ) const { return ClassVersion( Type::StaticClass() ) < iVersion; };
00107     
00120     template <typename Type>
00121     bool IsNewerThan(
00122                      int iVersion,  
00123                      Type * 
00124                      ) const { return ClassVersion( Type::StaticClass() ) > iVersion; };
00125 
00128     virtual bool IsStoring( void ) const;
00129 
00132     virtual void SetError( bool bError );
00133 
00135     virtual int Version( void ) const;
00136 
00138     virtual uint64 CurrentPosition( void ) const;
00139 
00145     virtual void SetCurrentPosition(
00146         uint64 iPosition        
00147         );
00148 
00153     virtual void ReportCorruption( void );
00154     
00158     virtual void Open(
00159         const QString &sFileName,       
00160         bool bStoring = false,      
00161         int iProjectedFileSize = 0,     
00162         bool bProgressBar = true        
00163     );
00164     
00166     virtual void Reopen(
00167         bool bStoring = false   
00168         );
00169 
00174     virtual bool Close( void );
00175 
00177     virtual bool Eof( void );
00178 
00183     virtual bool ReadContents( void );
00184 
00190     virtual size_t Read(
00191         void *pBuffer,  
00192         size_t iSize    
00193         );
00194 
00198     virtual size_t Write(
00199         const void *pBuffer,    
00200         size_t iSize            
00201         );
00202 
00204     inline int ReadInt( void ) { int i=0; operator >>( i ); return i; };
00205 
00208     virtual void ReadToFile(
00209         const QString &sFile    
00210         );
00211 
00213     virtual void WriteFromFile(
00214         const QString &sFile    
00215         );
00216 
00218     template < typename type >
00219     inline Stream &operator >>( type &v ) { MB_VERIFY_EQ( Read( &v, sizeof(type) ), sizeof(type) ); return *this; };
00220 
00222     template < typename type >
00223     inline Stream &operator <<( const type &v ) { MB_VERIFY_EQ( Write( &v, sizeof(type) ), sizeof(type) ); return *this; };
00224 
00226     virtual Node *ReadPointer( void );
00227     virtual void WritePointer( Node *pPointer );
00228 
00232     template < typename type >
00233     inline Stream &operator >>( type *&p ) { p = dynamic_cast<type *>( ReadPointer() ); return *this; }; 
00234 
00239     template < typename type >
00240     inline Stream &operator <<( type *&p ) { WritePointer( p ); return *this; };
00241 
00243     template < typename type >
00244     inline Stream &operator >>( Store<type> &s );
00245 
00247     template < typename type >
00248     inline Stream &operator <<( const Store<type> &s );
00249 
00251     Stream &operator >>( QString &s );
00252     Stream &operator <<( const QString &s );
00253 
00257     template < typename type >
00258     inline Stream &operator >>( QVector<type> &s )
00259     {
00260         int iSize = ReadInt();
00261         s.resize( iSize );
00262         Read( s.data(), iSize*sizeof(type) );
00263         return *this;
00264     };
00265 
00269     template < typename type >
00270     inline Stream &operator <<( const QVector<type> &s )
00271     {
00272         (*this) << s.size();
00273         Write( s.data(), s.size()*sizeof(type) );
00274         return *this;
00275     };
00276 };
00277 
00279 template < typename type >
00280 inline Stream &operator ==( Stream &s, type &v ) { if ( s.IsStoring() ) s << v; else s >> v; return s; };
00281 
00282 template < typename type >
00283 inline Stream &operator ==( Stream &s, const type &v ) { if ( s.IsStoring() ) s << v; else s >> *((type*)&v); return s; };
00284 
00285 template < typename type >
00286 void Store<type>::Serialize( class Stream &s )
00287 {
00288     if ( !s.IsStoring() )
00289         SetItemCount( s.ReadInt() );
00290     else
00291         s << m_iSize;
00292     for ( unsigned int i = 0; i < m_iSize; i++ )
00293         s == operator[]( i );
00294 };
00295 
00296 template < typename type >
00297 inline Stream &Stream::operator <<( const Store<type> &o ) { (*this) << o.ItemCount(); Write( &(o[0]), sizeof(type)*o.ItemCount() ); return *this; };
00298 template < typename type >
00299 inline Stream &Stream::operator >>( Store<type> &o ) { unsigned int iSize; (*this) >> iSize; if ( o.SetItemCount( iSize ) && iSize ) Read( &(o[0]), sizeof(type)*iSize ); return *this; };
00300 
00301 template < typename type >
00302 inline void AttributeInstance<type>::Serialize( Stream &s ) { s == m_cValue; };
00303 template < typename type >
00304 inline void AttributePointer<type>::Serialize( Stream &s )
00305 {
00306     if ( s.IsStoring() )
00307         s.WritePointer( AttributeInstance<type *>::Value() );
00308     else
00309         SetValue( dynamic_cast<type *>( s.ReadPointer() ) );
00310 };
00311 
00312 }; // end of namespace mudbox
00313