OGRE  1.9.0
OgreStreamSerialiser.h
Go to the documentation of this file.
1/*
2-----------------------------------------------------------------------------
3This source file is part of OGRE
4(Object-oriented Graphics Rendering Engine)
5For the latest info, see http://www.ogre3d.org/
6
7Copyright (c) 2000-2014 Torus Knot Software Ltd
8
9Permission is hereby granted, free of charge, to any person obtaining a copy
10of this software and associated documentation files (the "Software"), to deal
11in the Software without restriction, including without limitation the rights
12to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13copies of the Software, and to permit persons to whom the Software is
14furnished to do so, subject to the following conditions:
15
16The above copyright notice and this permission notice shall be included in
17all copies or substantial portions of the Software.
18
19THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
25THE 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
35namespace 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 );
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
160
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
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
344 virtual void writeChunkImpl(uint32 id, uint16 version);
345 virtual void readHeader();
346 virtual void writeHeader();
348 virtual void checkStream(bool failOnEof = false,
349 bool validateReadable = false, bool validateWriteable = false) const;
350
351 virtual void determineEndianness();
352 virtual Chunk* popChunk(uint id);
353
354 virtual void writeFloatsAsDoubles(const float* val, size_t count);
355 virtual void writeDoublesAsFloats(const double* val, size_t count);
356 virtual void readFloatsAsDoubles(double* val, size_t count);
357 virtual void readDoublesAsFloats(float* val, size_t count);
358 template <typename T, typename U>
359 void writeConverted(const T* src, U typeToWrite, size_t count)
360 {
361 U* tmp = OGRE_ALLOC_T(U, count, MEMCATEGORY_GENERAL);
362 U* pDst = tmp;
363 const T* pSrc = src;
364 for (size_t i = 0; i < count; ++i)
365 *pDst++ = static_cast<U>(*pSrc++);
366
367 writeData(tmp, sizeof(U), count);
368
370 }
371 template <typename T, typename U>
372 void readConverted(T* dst, U typeToRead, size_t count)
373 {
374 U* tmp = OGRE_ALLOC_T(U, count, MEMCATEGORY_GENERAL);
375 readData(tmp, sizeof(U), count);
376
377 T* pDst = dst;
378 const U* pSrc = tmp;
379 for (size_t i = 0; i < count; ++i)
380 *pDst++ = static_cast<T>(*pSrc++);
381
382
384 }
385
386 };
389}
390
391#include "OgreHeaderSuffix.h"
392
393#endif
394
#define OGRE_DOUBLE_PRECISION
If set to 1, Real is typedef'ed to double.
Definition: OgreConfig.h:63
#define _OgreExport
Definition: OgrePlatform.h:260
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
A 3D box aligned with the x/y/z axes.
A 3x3 matrix which can represent rotations around axes.
Definition: OgreMatrix3.h:69
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: OgreMatrix4.h:79
Class representing a general-purpose node an articulated scene graph.
Definition: OgreNode.h:65
Defines a plane in 3D space.
Definition: OgrePlane.h:62
Implementation of a Quaternion, i.e.
Wrapper class which indicates a given angle value is in Radians.
Definition: OgreMath.h:48
Representation of a ray in space, i.e.
Definition: OgreRay.h:47
A sphere primitive, mostly used for bounds checking.
Definition: OgreSphere.h:52
Utility class providing helper methods for reading / writing structured data held in a DataStream.
Endian
The endianness of files.
@ ENDIAN_AUTO
Automatically determine endianness.
@ ENDIAN_BIG
Use big endian (0x1000 is serialised as 0x10 0x00)
virtual void write(const Plane *plane, size_t count=1)
virtual void write(const Matrix4 *m, size_t count=1)
virtual void writeData(const void *buf, size_t size, size_t count)
Write arbitrary data to a stream.
ChunkStack mChunkStack
Current list of open chunks.
RealStorageFormat mRealFormat
virtual void read(Sphere *sphere, size_t count=1)
virtual uint32 calculateChecksum(Chunk *c)
virtual void read(Vector2 *vec, size_t count=1)
read a Vector3
virtual void read(Matrix4 *m, size_t count=1)
void write(const T *pT, size_t count=1)
Catch-all method to write primitive types.
virtual void readData(void *buf, size_t size, size_t count)
Read arbitrary data from a stream.
virtual void read(Quaternion *q, size_t count=1)
virtual void read(AxisAlignedBox *aabb, size_t count=1)
virtual void write(const bool *boolean, size_t count=1)
virtual void write(const Node *node, size_t count=1)
virtual void read(Real *val, size_t count=1)
deque< Chunk * >::type ChunkStack
virtual void writeChunkBegin(uint32 id, uint16 version=1)
Begin writing a new chunk.
uint32 getCurrentChunkID() const
Get the ID of the chunk that's currently being read/written, if any.
virtual void readHeader()
virtual void write(const Quaternion *q, size_t count=1)
virtual void write(const Sphere *sphere, size_t count=1)
virtual void write(const Real *val, size_t count=1)
virtual void read(Node *node, size_t count=1)
virtual void read(Vector3 *vec, size_t count=1)
virtual void write(const Vector2 *vec, size_t count=1)
virtual void write(const Matrix3 *m, size_t count=1)
virtual Chunk * readChunkImpl()
virtual void write(const AxisAlignedBox *aabb, size_t count=1)
RealStorageFormat
The storage format of Real values.
@ REAL_FLOAT
Real is stored as float, reducing precision if you're using OGRE_DOUBLE_PRECISION.
virtual void determineEndianness()
StreamSerialiser(const DataStreamPtr &stream, Endian endianMode=ENDIAN_AUTO, bool autoHeader=true, RealStorageFormat realFormat=REAL_FLOAT)
Constructor.
virtual void writeChunkImpl(uint32 id, uint16 version)
virtual void checkStream(bool failOnEof=false, bool validateReadable=false, bool validateWriteable=false) const
void read(T *pT, size_t count=1)
Catch-all method to read primitive types.
virtual Endian getEndian() const
Get the endian mode.
virtual void writeHeader()
virtual void write(const Radian *angle, size_t count=1)
void writeConverted(const T *src, U typeToWrite, size_t count)
virtual void readFloatsAsDoubles(double *val, size_t count)
virtual void write(const String *string)
virtual void read(Ray *ray, size_t count=1)
virtual const Chunk * readChunkBegin(uint32 id, uint16 maxVersion, const String &msg=StringUtil::BLANK)
Reads the start of the next chunk so long as it's of a given ID and version.
virtual void read(Plane *plane, size_t count=1)
virtual const Chunk * getCurrentChunk() const
Get the definition of the current chunk being read (if any).
virtual void read(Radian *angle, size_t count=1)
virtual void writeDoublesAsFloats(const double *val, size_t count)
virtual void writeFloatsAsDoubles(const float *val, size_t count)
virtual void readChunkEnd(uint32 id)
Finish the reading of a chunk.
virtual void write(const Vector4 *vec, size_t count=1)
virtual void read(Matrix3 *m, size_t count=1)
virtual bool eof() const
Reports whether the stream is at the end of file.
virtual uint32 peekNextChunkID()
Call this to 'peek' at the next chunk ID without permanently moving the stream pointer.
size_t getOffsetFromChunkStart() const
Get the current byte position relative to the start of the data section of the last chunk that was re...
virtual void read(String *string)
virtual void startDeflate(size_t avail_in=0)
Start (un)compressing data.
virtual void undoReadChunk(uint32 id)
Call this to 'rewind' the stream to just before the start of the current chunk.
void readConverted(T *dst, U typeToRead, size_t count)
virtual void stopDeflate()
Stop (un)compressing data.
virtual Chunk * popChunk(uint id)
virtual void write(const Ray *ray, size_t count=1)
virtual void writeChunkEnd(uint32 id)
End writing a chunk.
virtual bool isEndOfChunk(uint32 id)
Return whether the current data pointer is at the end of the current chunk.
static uint32 makeIdentifier(const String &code)
Pack a 4-character code into a 32-bit identifier.
virtual void write(const Vector3 *vec, size_t count=1)
size_t getCurrentChunkDepth() const
Report the current depth of the chunk nesting, whether reading or writing.
virtual void readDoublesAsFloats(float *val, size_t count)
virtual const Chunk * readChunkBegin()
Reads the start of the next chunk in the file.
virtual void read(Vector4 *vec, size_t count=1)
virtual void read(bool *val, size_t count=1)
Standard 2-dimensional vector.
Definition: OgreVector2.h:52
Standard 3-dimensional vector.
Definition: OgreVector3.h:52
4-dimensional homogeneous vector.
Definition: OgreVector4.h:46
#define OGRE_ALLOC_T(T, count, category)
Allocate a block of memory for a primitive type, and indicate the category of usage.
#define OGRE_FREE(ptr, category)
Free the memory allocated with OGRE_MALLOC or OGRE_ALLOC_T. Category is required to be restated to en...
@ MEMCATEGORY_GENERAL
General purpose.
float Real
Software floating point type.
unsigned short uint16
Definition: OgrePlatform.h:345
unsigned int uint
unsigned int uint32
Definition: OgrePlatform.h:344
_StringBase String
Definition of a chunk of data in a file.
uint32 offset
Location of the chunk (header) in bytes from the start of a stream (derived)
uint16 version
Version of the chunk (stored)
uint32 id
Identifier of the chunk (for example from makeIdentifier) (stored)
uint32 length
Length of the chunk data in bytes, excluding the header of this chunk (stored)
std::deque< T, A > type