Mudbox/stream.h Source File

stream.h
Go to the documentation of this file.
1 
2 //**************************************************************************/
3 // Copyright (c) 2008 Autodesk, Inc.
4 // All rights reserved.
5 //
6 // Use of this software is subject to the terms of the Autodesk license
7 // agreement provided at the time of installation or download, or which
8 // otherwise accompanies this software in either electronic or hard copy form.
9 //
10 //**************************************************************************/
11 // DESCRIPTION:
12 // CREATED: October 2008
13 //**************************************************************************/
14 
15 namespace mudbox {
16 
17 typedef Node *NodePointer;
18 
39 class MBDLL_DECL Stream : public Node
40 {
42 protected:
44  Stream( void );
45 
46 public:
47  enum { chunk = 0x800000 };
48 
50  virtual ~Stream( void );
51 
53  virtual QString FileName( void ) const;
54 
64  virtual bool IsValid( void );
65 
71  virtual bool IsOlderThan(
72  int iVersion
73  ) const;
74 
81  virtual bool IsNewerThan(
82  int iVersion
83  ) const;
84 
89  virtual int ClassVersion( const class ClassDesc *pClass ) const;
90 
102  template <typename Type>
104  int iVersion,
105  Type *
106  ) const { return ClassVersion( Type::StaticClass() ) < iVersion; };
107 
120  template <typename Type>
122  int iVersion,
123  Type *
124  ) const { return ClassVersion( Type::StaticClass() ) > iVersion; };
125 
128  virtual bool IsStoring( void ) const;
129 
132  virtual void SetError( bool bError );
133 
135  virtual int Version( void ) const;
136 
138  virtual uint64 CurrentPosition( void ) const;
139 
145  virtual void SetCurrentPosition(
146  uint64 iPosition
147  );
148 
153  virtual void ReportCorruption( void );
154 
158  virtual void Open(
159  const QString &sFileName,
160  bool bStoring = false,
161  int iProjectedFileSize = 0,
162  bool bProgressBar = true
163  );
164 
166  virtual void Reopen(
167  bool bStoring = false
168  );
169 
174  virtual bool Close( void );
175 
177  virtual bool Eof( void );
178 
183  virtual bool ReadContents( void );
184 
190  virtual size_t Read(
191  void *pBuffer,
192  size_t iSize
193  );
194 
198  virtual size_t Write(
199  const void *pBuffer,
200  size_t iSize
201  );
202 
204  inline int ReadInt( void ) { int i=0; operator >>( i ); return i; };
205 
208  virtual void ReadToFile(
209  const QString &sFile
210  );
211 
213  virtual void WriteFromFile(
214  const QString &sFile
215  );
216 
218  template < typename type >
219  inline Stream &operator >>( type &v ) { MB_VERIFY_EQ( Read( &v, sizeof(type) ), sizeof(type) ); return *this; };
220 
222  template < typename type >
223  inline Stream &operator <<( const type &v ) { MB_VERIFY_EQ( Write( &v, sizeof(type) ), sizeof(type) ); return *this; };
224 
226  virtual Node *ReadPointer( void );
227  virtual void WritePointer( Node *pPointer );
228 
232  template < typename type >
233  inline Stream &operator >>( type *&p ) { p = dynamic_cast<type *>( ReadPointer() ); return *this; };
234 
239  template < typename type >
240  inline Stream &operator <<( type *&p ) { WritePointer( p ); return *this; };
241 
243  template < typename type >
244  inline Stream &operator >>( Store<type> &s );
245 
247  template < typename type >
248  inline Stream &operator <<( const Store<type> &s );
249 
252  Stream &operator <<( const QString &s );
253 
257  template < typename type >
259  {
260  int iSize = ReadInt();
261  s.resize( iSize );
262  Read( s.data(), iSize*sizeof(type) );
263  return *this;
264  };
265 
269  template < typename type >
270  inline Stream &operator <<( const QVector<type> &s )
271  {
272  (*this) << s.size();
273  Write( s.data(), s.size()*sizeof(type) );
274  return *this;
275  };
276 
277  virtual void WriteIntArray( const Store< unsigned int >& s );
278 
279  virtual void ReadIntArray( Store< unsigned int >& s );
280 
281 private:
283  virtual void RegisterBlock( const QString &sName, qint64 iSize, qint64 iItemSize );
284 };
285 
287 template < typename type >
288 inline Stream &operator ==( Stream &s, type &v ) { if ( s.IsStoring() ) s << v; else s >> v; return s; };
289 
290 template < typename type >
291 inline Stream &operator ==( Stream &s, const type &v ) { if ( s.IsStoring() ) s << v; else s >> *((type*)&v); return s; };
292 
293 template < typename type >
295 {
296  if ( !s.IsStoring() )
297  SetItemCount( s.ReadInt() );
298  else
299  s << m_iSize;
300  for ( unsigned int i = 0; i < m_iSize; i++ )
301  s == operator[]( i );
302 };
303 
304 template < typename type >
305 inline Stream &Stream::operator <<( const Store<type> &o ) { (*this) << o.ItemCount(); Write( &(o[0]), sizeof(type)*o.ItemCount() ); RegisterBlock( o.Name(), o.Size(), sizeof(type) ); return *this; };
306 template < typename type >
307 inline Stream &Stream::operator >>( Store<type> &o ) { unsigned int iSize; (*this) >> iSize; if ( o.SetItemCount( iSize ) && iSize ) Read( &(o[0]), sizeof(type)*iSize ); RegisterBlock( o.Name(), o.Size(), sizeof(type) ); return *this; };
308 
309 
310 template<>
311 inline Stream &Stream::operator <<( const Store<unsigned int> &o ) { WriteIntArray(o); return *this; };
312 
313 template<>
314 inline Stream &Stream::operator >>( Store<unsigned int> &o ) { ReadIntArray(o); return *this; };
315 
316 
317 
318 template < typename type >
319 inline void AttributeInstance<type>::Serialize( Stream &s ) { s == m_cValue; };
320 template < typename type >
322 {
323  if ( s.IsStoring() )
325  else
326  SetValue( dynamic_cast<type *>( s.ReadPointer() ), true );
327 };
328 
329 
330 template< typename T >
332 {
333  s == m_iBlockItemCount;
334  s == m_iBlockPow;
335  s == m_iBlockMask;
336  s == m_iLogicalItemCount;
337  s == m_iAlignment;
338 
339  if( s.IsStoring() )
340  {
341  s << m_aBlocks.ItemCount();
342  for( unsigned int i = 0; i < m_aBlocks.ItemCount(); ++i )
343  s.Write( (void*)m_aBlocks[i], sizeof(T) * m_iBlockItemCount );
344  }
345  else
346  {
347  unsigned int iC = 0;
348  s >> iC;
349  m_aBlocks.SetItemCount( iC );
350  for( unsigned int i = 0; i < m_aBlocks.ItemCount(); ++i )
351  {
352  m_aBlocks[i] = (T*)Block::AlignedAlloc( sizeof(T) * m_iBlockItemCount, m_iAlignment );
353  s.Read( (void*)m_aBlocks[i], sizeof(T) * m_iBlockItemCount );
354  }
355  }
356 };
357 
358 }; // end of namespace mudbox
359 
GLuint GLuint GLsizei GLenum type
Definition: GLee.h:872
void resize(int size)
Definition: qvector.h:342
T * data()
Definition: qvector.h:152
Simple array class.
Definition: array.h:334
virtual size_t Write(const void *pBuffer, size_t iSize)
Writes data to the stream from a buffer you provide.
virtual void WritePointer(Node *pPointer)
mudbox::Vector & operator<<(mudbox::Vector &v, const HWVector &r)
Definition: SSE.h:245
long long qint64
Definition: qglobal.h:947
This is the base class for most classes in the Mudbox SDK.
Definition: node.h:740
unsigned long long uint64
Definition: mudbox.h:177
void Serialize(class Stream &s)
Serializes the the array and its contents from/to a stream.
Definition: stream.h:294
bool IsOlderThan(int iVersion, Type *) const
Checks if the file is older than a specified version for the given class.
Definition: stream.h:103
bool operator==(const Attribute &cA, const AttributeInstance< type > &cB)
This operator compares the two attributes and NOT their values.
Definition: node.h:577
virtual Node * ReadPointer(void)
Read/Writes node pointers.
#define MB_VERIFY_EQ(condition_, value_)
Definition: mudbox.h:89
void Serialize(Stream &s)
Definition: stream.h:331
const char * Name(void) const
Stream & operator>>(type &v)
Reads data from the stream to any data type.
Definition: stream.h:219
virtual void ReadIntArray(Store< unsigned int > &s)
bool SetItemCount(unsigned int iSize, bool bKeepContent=false)
Sets the logical size of the array.
Definition: array.h:403
This is a generic attribute which can be used instead of the standard built in types.
Definition: node.h:394
This structure represents and describes a class.
Definition: node.h:22
void Serialize(Stream &s)
Serializes the pointer value to a stream.
Definition: stream.h:321
int ReadInt(void)
Reads a single integer from the stream, and returns it.
Definition: stream.h:204
long long Size(void) const
Q_CORE_EXPORT QDataStream & operator>>(QDataStream &, QBitArray &)
const GLdouble * v
Definition: GLee.h:1174
virtual size_t Read(void *pBuffer, size_t iSize)
Reads data directly from the stream into a buffer you provide.
bool IsNewerThan(int iVersion, Type *) const
Checks if the file is newer than a specified version for the given class.
Definition: stream.h:121
GLfloat GLfloat p
Definition: GLee.h:5416
Class: ConvolutionKernel.
Definition: array.h:15
Streams are used to read information from a file, or to write it to a file.
Definition: stream.h:39
#define DECLARE_CLASS
This macro should be used in declaration of classes which are inherited from the Node class (or any d...
Definition: node.h:91
GLdouble s
Definition: GLee.h:1173
Node * NodePointer
Definition: stream.h:17
void Serialize(Stream &s)
Serialize the value of the attribute into a stream.
Definition: stream.h:319
#define MBDLL_DECL
Definition: dllinterface.h:35
virtual bool IsStoring(void) const
Returns true if the stream is in storing mode (i.e.
static void * AlignedAlloc(size_t iBytes, unsigned int iAlignment)