OGRE  2.0
Object-Oriented Graphics Rendering Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
OgreStreamSerialiser.h
Go to the documentation of this file.
1 /*
2 -----------------------------------------------------------------------------
3 This source file is part of OGRE
4 (Object-oriented Graphics Rendering Engine)
5 For the latest info, see http://www.ogre3d.org/
6 
7 Copyright (c) 2000-2014 Torus Knot Software Ltd
8 
9 Permission is hereby granted, free of charge, to any person obtaining a copy
10 of this software and associated documentation files (the "Software"), to deal
11 in the Software without restriction, including without limitation the rights
12 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13 copies of the Software, and to permit persons to whom the Software is
14 furnished to do so, subject to the following conditions:
15 
16 The above copyright notice and this permission notice shall be included in
17 all copies or substantial portions of the Software.
18 
19 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25 THE SOFTWARE.
26 -----------------------------------------------------------------------------
27 */
28 #ifndef __StreamSerialiser_H__
29 #define __StreamSerialiser_H__
30 
31 #include "OgrePrerequisites.h"
32 #include "OgreCommon.h"
33 #include "OgreDataStream.h"
34 #include "OgreHeaderPrefix.h"
35 
36 namespace Ogre
37 {
68  {
69  public:
71  enum Endian
72  {
78  ENDIAN_LITTLE
79  };
80 
83  {
87  REAL_DOUBLE
88  };
89 
90 
92  struct Chunk : public StreamAlloc
93  {
102 
103  Chunk() : id(0), version(1), length(0), offset(0) {}
104  };
105 
123  StreamSerialiser(const DataStreamPtr& stream, Endian endianMode = ENDIAN_AUTO,
124  bool autoHeader = true,
126  RealStorageFormat realFormat = REAL_DOUBLE
127 #else
128  RealStorageFormat realFormat = REAL_FLOAT
129 #endif
130  );
131  virtual ~StreamSerialiser();
132 
138  virtual Endian getEndian() const { return mEndian; }
139 
146  static uint32 makeIdentifier(const String& code);
147 
154  size_t getCurrentChunkDepth() const { return mChunkStack.size(); }
155 
160  uint32 getCurrentChunkID() const;
161 
169  size_t getOffsetFromChunkStart() const;
170 
182  virtual const Chunk* readChunkBegin();
183 
196  virtual const Chunk* readChunkBegin(uint32 id, uint16 maxVersion, const String& msg = BLANKSTRING);
197 
207  virtual void undoReadChunk(uint32 id);
208 
210  virtual uint32 peekNextChunkID();
211 
220  virtual void readChunkEnd(uint32 id);
221 
225  virtual bool isEndOfChunk(uint32 id);
226 
228  virtual bool eof() const;
229 
231  virtual const Chunk* getCurrentChunk() const;
232 
247  virtual void writeChunkBegin(uint32 id, uint16 version = 1);
252  virtual void writeChunkEnd(uint32 id);
253 
260  virtual void writeData(const void* buf, size_t size, size_t count);
261 
263  template <typename T>
264  void write(const T* pT, size_t count = 1)
265  {
266  writeData(pT, sizeof(T), count);
267  }
268 
269  // Special-case Real since we need to deal with single/double precision
270  virtual void write(const Real* val, size_t count = 1);
271 
272  virtual void write(const Vector2* vec, size_t count = 1);
273  virtual void write(const Vector3* vec, size_t count = 1);
274  virtual void write(const Vector4* vec, size_t count = 1);
275  virtual void write(const Quaternion* q, size_t count = 1);
276  virtual void write(const Matrix3* m, size_t count = 1);
277  virtual void write(const Matrix4* m, size_t count = 1);
278  virtual void write(const String* string);
279  virtual void write(const AxisAlignedBox* aabb, size_t count = 1);
280  virtual void write(const Sphere* sphere, size_t count = 1);
281  virtual void write(const Plane* plane, size_t count = 1);
282  virtual void write(const Ray* ray, size_t count = 1);
283  virtual void write(const Radian* angle, size_t count = 1);
284  virtual void write(const Node* node, size_t count = 1);
285  virtual void write(const bool* boolean, size_t count = 1);
286 
287 
294  virtual void readData(void* buf, size_t size, size_t count);
295 
297  template <typename T>
298  void read(T* pT, size_t count = 1)
299  {
300  readData(pT, sizeof(T), count);
301  }
302 
303  // Special case Real, single/double-precision issues
304  virtual void read(Real* val, size_t count = 1);
305 
307  virtual void read(Vector2* vec, size_t count = 1);
308  virtual void read(Vector3* vec, size_t count = 1);
309  virtual void read(Vector4* vec, size_t count = 1);
310  virtual void read(Quaternion* q, size_t count = 1);
311  virtual void read(Matrix3* m, size_t count = 1);
312  virtual void read(Matrix4* m, size_t count = 1);
313  virtual void read(String* string);
314  virtual void read(AxisAlignedBox* aabb, size_t count = 1);
315  virtual void read(Sphere* sphere, size_t count = 1);
316  virtual void read(Plane* plane, size_t count = 1);
317  virtual void read(Ray* ray, size_t count = 1);
318  virtual void read(Radian* angle, size_t count = 1);
319  virtual void read(Node* node, size_t count = 1);
320  virtual void read(bool* val, size_t count = 1);
321 
325  virtual void startDeflate(size_t avail_in = 0);
328  virtual void stopDeflate();
329  protected:
339 
343 
344  virtual Chunk* readChunkImpl();
345  virtual void writeChunkImpl(uint32 id, uint16 version);
346  virtual void readHeader();
347  virtual void writeHeader();
348  virtual uint32 calculateChecksum(Chunk* c);
349  virtual void checkStream(bool failOnEof = false,
350  bool validateReadable = false, bool validateWriteable = false) const;
351 
352  virtual void flipEndian(void * pData, size_t size, size_t count);
353  virtual void flipEndian(void * pData, size_t size);
354  virtual void determineEndianness();
355  virtual Chunk* popChunk(uint id);
356 
357  virtual void writeFloatsAsDoubles(const float* val, size_t count);
358  virtual void writeDoublesAsFloats(const double* val, size_t count);
359  virtual void readFloatsAsDoubles(double* val, size_t count);
360  virtual void readDoublesAsFloats(float* val, size_t count);
361  template <typename T, typename U>
362  void writeConverted(const T* src, U typeToWrite, size_t count)
363  {
364  U* tmp = OGRE_ALLOC_T(U, count, MEMCATEGORY_GENERAL);
365  U* pDst = tmp;
366  const T* pSrc = src;
367  for (size_t i = 0; i < count; ++i)
368  *pDst++ = static_cast<U>(*pSrc++);
369 
370  writeData(tmp, sizeof(U), count);
371 
373  }
374  template <typename T, typename U>
375  void readConverted(T* dst, U typeToRead, size_t count)
376  {
377  U* tmp = OGRE_ALLOC_T(U, count, MEMCATEGORY_GENERAL);
378  readData(tmp, sizeof(U), count);
379 
380  T* pDst = dst;
381  const U* pSrc = tmp;
382  for (size_t i = 0; i < count; ++i)
383  *pDst++ = static_cast<T>(*pSrc++);
384 
385 
387  }
388 
389  };
392 }
393 
394 #include "OgreHeaderSuffix.h"
395 
396 #endif
397 
Representation of a ray in space, i.e.
Definition: OgreRay.h:45
#define OGRE_ALLOC_T(T, count, category)
Allocate a block of memory for a primitive type, and indicate the category of usage.
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: OgreMatrix4.h:79
size_t getCurrentChunkDepth() const
Report the current depth of the chunk nesting, whether reading or writing.
float Real
Software floating point type.
unsigned int uint32
Definition: OgrePlatform.h:420
#define _OgreExport
Definition: OgrePlatform.h:255
ChunkStack mChunkStack
Current list of open chunks.
Defines a plane in 3D space.
Definition: OgrePlane.h:61
uint32 length
Length of the chunk data in bytes, excluding the header of this chunk (stored)
void read(T *pT, size_t count=1)
Catch-all method to read primitive types.
A 3D box aligned with the x/y/z axes.
A 3x3 matrix which can represent rotations around axes.
Definition: OgreMatrix3.h:68
deque< Chunk * >::type ChunkStack
uint32 id
Identifier of the chunk (for example from makeIdentifier) (stored)
Automatically determine endianness.
const String BLANKSTRING
Constant blank string, useful for returning by ref where local does not exist.
Definition: OgreCommon.h:574
Implementation of a Quaternion, i.e.
void write(const T *pT, size_t count=1)
Catch-all method to write primitive types.
uint32 offset
Location of the chunk (header) in bytes from the start of a stream (derived)
uint16 version
Version of the chunk (stored)
Real is stored as float, reducing precision if you're using OGRE_DOUBLE_PRECISION.
A sphere primitive, mostly used for bounds checking.
Definition: OgreSphere.h:51
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Standard 2-dimensional vector.
Definition: OgreVector2.h:51
Utility class providing helper methods for reading / writing structured data held in a DataStream...
RealStorageFormat mRealFormat
Standard 3-dimensional vector.
Definition: OgreVector3.h:50
Endian
The endianness of files.
virtual Endian getEndian() const
Get the endian mode.
Wrapper class which indicates a given angle value is in Radians.
Definition: OgreMath.h:49
RealStorageFormat
The storage format of Real values.
void writeConverted(const T *src, U typeToWrite, size_t count)
#define OGRE_FREE(ptr, category)
Free the memory allocated with OGRE_MALLOC or OGRE_ALLOC_T. Category is required to be restated to en...
#define OGRE_DOUBLE_PRECISION
If set to 1, Real is typedef'ed to double.
Definition: OgreConfig.h:63
_StringBase String
Definition: OgreCommon.h:53
Use big endian (0x1000 is serialised as 0x10 0x00)
unsigned short uint16
Definition: OgrePlatform.h:421
4-dimensional homogeneous vector.
Definition: OgreVector4.h:45
void readConverted(T *dst, U typeToRead, size_t count)
Class representing a general-purpose node an articulated scene graph.
Definition: OgreNode.h:58
Definition of a chunk of data in a file.
unsigned int uint