OGRE  1.9
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 "OgreDataStream.h"
33 #include "OgreHeaderPrefix.h"
34 
35 namespace Ogre
36 {
67  {
68  public:
70  enum Endian
71  {
77  ENDIAN_LITTLE
78  };
79 
82  {
86  REAL_DOUBLE
87  };
88 
89 
91  struct Chunk : public StreamAlloc
92  {
101 
102  Chunk() : id(0), version(1), length(0), offset(0) {}
103  };
104 
122  StreamSerialiser(const DataStreamPtr& stream, Endian endianMode = ENDIAN_AUTO,
123  bool autoHeader = true,
125  RealStorageFormat realFormat = REAL_DOUBLE
126 #else
127  RealStorageFormat realFormat = REAL_FLOAT
128 #endif
129  );
130  virtual ~StreamSerialiser();
131 
137  virtual Endian getEndian() const { return mEndian; }
138 
145  static uint32 makeIdentifier(const String& code);
146 
153  size_t getCurrentChunkDepth() const { return mChunkStack.size(); }
154 
159  uint32 getCurrentChunkID() const;
160 
168  size_t getOffsetFromChunkStart() const;
169 
181  virtual const Chunk* readChunkBegin();
182 
195  virtual const Chunk* readChunkBegin(uint32 id, uint16 maxVersion, const String& msg = StringUtil::BLANK);
196 
206  virtual void undoReadChunk(uint32 id);
207 
209  virtual uint32 peekNextChunkID();
210 
219  virtual void readChunkEnd(uint32 id);
220 
224  virtual bool isEndOfChunk(uint32 id);
225 
227  virtual bool eof() const;
228 
230  virtual const Chunk* getCurrentChunk() const;
231 
246  virtual void writeChunkBegin(uint32 id, uint16 version = 1);
251  virtual void writeChunkEnd(uint32 id);
252 
259  virtual void writeData(const void* buf, size_t size, size_t count);
260 
262  template <typename T>
263  void write(const T* pT, size_t count = 1)
264  {
265  writeData(pT, sizeof(T), count);
266  }
267 
268  // Special-case Real since we need to deal with single/double precision
269  virtual void write(const Real* val, size_t count = 1);
270 
271  virtual void write(const Vector2* vec, size_t count = 1);
272  virtual void write(const Vector3* vec, size_t count = 1);
273  virtual void write(const Vector4* vec, size_t count = 1);
274  virtual void write(const Quaternion* q, size_t count = 1);
275  virtual void write(const Matrix3* m, size_t count = 1);
276  virtual void write(const Matrix4* m, size_t count = 1);
277  virtual void write(const String* string);
278  virtual void write(const AxisAlignedBox* aabb, size_t count = 1);
279  virtual void write(const Sphere* sphere, size_t count = 1);
280  virtual void write(const Plane* plane, size_t count = 1);
281  virtual void write(const Ray* ray, size_t count = 1);
282  virtual void write(const Radian* angle, size_t count = 1);
283  virtual void write(const Node* node, size_t count = 1);
284  virtual void write(const bool* boolean, size_t count = 1);
285 
286 
293  virtual void readData(void* buf, size_t size, size_t count);
294 
296  template <typename T>
297  void read(T* pT, size_t count = 1)
298  {
299  readData(pT, sizeof(T), count);
300  }
301 
302  // Special case Real, single/double-precision issues
303  virtual void read(Real* val, size_t count = 1);
304 
306  virtual void read(Vector2* vec, size_t count = 1);
307  virtual void read(Vector3* vec, size_t count = 1);
308  virtual void read(Vector4* vec, size_t count = 1);
309  virtual void read(Quaternion* q, size_t count = 1);
310  virtual void read(Matrix3* m, size_t count = 1);
311  virtual void read(Matrix4* m, size_t count = 1);
312  virtual void read(String* string);
313  virtual void read(AxisAlignedBox* aabb, size_t count = 1);
314  virtual void read(Sphere* sphere, size_t count = 1);
315  virtual void read(Plane* plane, size_t count = 1);
316  virtual void read(Ray* ray, size_t count = 1);
317  virtual void read(Radian* angle, size_t count = 1);
318  virtual void read(Node* node, size_t count = 1);
319  virtual void read(bool* val, size_t count = 1);
320 
324  virtual void startDeflate(size_t avail_in = 0);
327  virtual void stopDeflate();
328  protected:
338 
342 
343  virtual Chunk* readChunkImpl();
344  virtual void writeChunkImpl(uint32 id, uint16 version);
345  virtual void readHeader();
346  virtual void writeHeader();
347  virtual uint32 calculateChecksum(Chunk* c);
348  virtual void checkStream(bool failOnEof = false,
349  bool validateReadable = false, bool validateWriteable = false) const;
350 
351  virtual void flipEndian(void * pData, size_t size, size_t count);
352  virtual void flipEndian(void * pData, size_t size);
353  virtual void determineEndianness();
354  virtual Chunk* popChunk(uint id);
355 
356  virtual void writeFloatsAsDoubles(const float* val, size_t count);
357  virtual void writeDoublesAsFloats(const double* val, size_t count);
358  virtual void readFloatsAsDoubles(double* val, size_t count);
359  virtual void readDoublesAsFloats(float* val, size_t count);
360  template <typename T, typename U>
361  void writeConverted(const T* src, U typeToWrite, size_t count)
362  {
363  U* tmp = OGRE_ALLOC_T(U, count, MEMCATEGORY_GENERAL);
364  U* pDst = tmp;
365  const T* pSrc = src;
366  for (size_t i = 0; i < count; ++i)
367  *pDst++ = static_cast<U>(*pSrc++);
368 
369  writeData(tmp, sizeof(U), count);
370 
372  }
373  template <typename T, typename U>
374  void readConverted(T* dst, U typeToRead, size_t count)
375  {
376  U* tmp = OGRE_ALLOC_T(U, count, MEMCATEGORY_GENERAL);
377  readData(tmp, sizeof(U), count);
378 
379  T* pDst = dst;
380  const U* pSrc = tmp;
381  for (size_t i = 0; i < count; ++i)
382  *pDst++ = static_cast<T>(*pSrc++);
383 
384 
386  }
387 
388  };
391 }
392 
393 #include "OgreHeaderSuffix.h"
394 
395 #endif
396 
Representation of a ray in space, i.e.
Definition: OgreRay.h:46
#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:78
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:344
#define _OgreExport
Definition: OgrePlatform.h:260
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.
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:51
static const String BLANK
Constant blank string, useful for returning by ref where local does not exist.
Definition: OgreString.h:196
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:47
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
Use big endian (0x1000 is serialised as 0x10 0x00)
unsigned short uint16
Definition: OgrePlatform.h:345
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:64
Definition of a chunk of data in a file.
unsigned int uint