OgreStreamSerialiser.h
Go to the documentation of this file.
/*
-----------------------------------------------------------------------------
This source file is part of OGRE
(Object-oriented Graphics Rendering Engine)
For the latest info, see http://www.ogre3d.org/
Copyright (c) 2000-2013 Torus Knot Software Ltd
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
-----------------------------------------------------------------------------
*/
#ifndef __StreamSerialiser_H__
#define __StreamSerialiser_H__
#include "OgrePrerequisites.h"
#include "OgreDataStream.h"
#include "OgreHeaderPrefix.h"
namespace Ogre
{
class _OgreExport StreamSerialiser : public StreamAlloc
{
public:
enum Endian
{
ENDIAN_AUTO,
ENDIAN_BIG,
ENDIAN_LITTLE
};
enum RealStorageFormat
{
REAL_FLOAT,
REAL_DOUBLE
};
struct Chunk : public StreamAlloc
{
uint32 id;
uint16 version;
uint32 length;
uint32 offset;
Chunk() : id(0), version(1), length(0), offset(0) {}
};
StreamSerialiser(const DataStreamPtr& stream, Endian endianMode = ENDIAN_AUTO,
bool autoHeader = true,
#if OGRE_DOUBLE_PRECISION
RealStorageFormat realFormat = REAL_DOUBLE
#else
RealStorageFormat realFormat = REAL_FLOAT
#endif
);
virtual ~StreamSerialiser();
virtual Endian getEndian() const { return mEndian; }
static uint32 makeIdentifier(const String& code);
size_t getCurrentChunkDepth() const { return mChunkStack.size(); }
uint32 getCurrentChunkID() const;
size_t getOffsetFromChunkStart() const;
virtual const Chunk* readChunkBegin();
virtual const Chunk* readChunkBegin(uint32 id, uint16 maxVersion, const String& msg = StringUtil::BLANK);
virtual void undoReadChunk(uint32 id);
virtual uint32 peekNextChunkID();
virtual void readChunkEnd(uint32 id);
virtual bool isEndOfChunk(uint32 id);
virtual bool eof() const;
virtual const Chunk* getCurrentChunk() const;
virtual void writeChunkBegin(uint32 id, uint16 version = 1);
virtual void writeChunkEnd(uint32 id);
virtual void writeData(const void* buf, size_t size, size_t count);
template <typename T>
void write(const T* pT, size_t count = 1)
{
writeData(pT, sizeof(T), count);
}
// Special-case Real since we need to deal with single/double precision
virtual void write(const Real* val, size_t count = 1);
virtual void write(const Vector2* vec, size_t count = 1);
virtual void write(const Vector3* vec, size_t count = 1);
virtual void write(const Vector4* vec, size_t count = 1);
virtual void write(const Quaternion* q, size_t count = 1);
virtual void write(const Matrix3* m, size_t count = 1);
virtual void write(const Matrix4* m, size_t count = 1);
virtual void write(const String* string);
virtual void write(const AxisAlignedBox* aabb, size_t count = 1);
virtual void write(const Sphere* sphere, size_t count = 1);
virtual void write(const Plane* plane, size_t count = 1);
virtual void write(const Ray* ray, size_t count = 1);
virtual void write(const Radian* angle, size_t count = 1);
virtual void write(const Node* node, size_t count = 1);
virtual void write(const bool* boolean, size_t count = 1);
virtual void readData(void* buf, size_t size, size_t count);
template <typename T>
void read(T* pT, size_t count = 1)
{
readData(pT, sizeof(T), count);
}
// Special case Real, single/double-precision issues
virtual void read(Real* val, size_t count = 1);
virtual void read(Vector2* vec, size_t count = 1);
virtual void read(Vector3* vec, size_t count = 1);
virtual void read(Vector4* vec, size_t count = 1);
virtual void read(Quaternion* q, size_t count = 1);
virtual void read(Matrix3* m, size_t count = 1);
virtual void read(Matrix4* m, size_t count = 1);
virtual void read(String* string);
virtual void read(AxisAlignedBox* aabb, size_t count = 1);
virtual void read(Sphere* sphere, size_t count = 1);
virtual void read(Plane* plane, size_t count = 1);
virtual void read(Ray* ray, size_t count = 1);
virtual void read(Radian* angle, size_t count = 1);
virtual void read(Node* node, size_t count = 1);
virtual void read(bool* val, size_t count = 1);
virtual void startDeflate(size_t avail_in = 0);
virtual void stopDeflate();
protected:
DataStreamPtr mStream;
DataStreamPtr mOriginalStream;
Endian mEndian;
bool mFlipEndian;
bool mReadWriteHeader;
RealStorageFormat mRealFormat;
typedef deque<Chunk*>::type ChunkStack;
ChunkStack mChunkStack;
static uint32 HEADER_ID;
static uint32 REVERSE_HEADER_ID;
static uint32 CHUNK_HEADER_SIZE;
virtual Chunk* readChunkImpl();
virtual void writeChunkImpl(uint32 id, uint16 version);
virtual void readHeader();
virtual void writeHeader();
virtual uint32 calculateChecksum(Chunk* c);
virtual void checkStream(bool failOnEof = false,
bool validateReadable = false, bool validateWriteable = false) const;
virtual void flipEndian(void * pData, size_t size, size_t count);
virtual void flipEndian(void * pData, size_t size);
virtual void determineEndianness();
virtual Chunk* popChunk(uint id);
virtual void writeFloatsAsDoubles(const float* val, size_t count);
virtual void writeDoublesAsFloats(const double* val, size_t count);
virtual void readFloatsAsDoubles(double* val, size_t count);
virtual void readDoublesAsFloats(float* val, size_t count);
template <typename T, typename U>
void writeConverted(const T* src, U typeToWrite, size_t count)
{
U* tmp = OGRE_ALLOC_T(U, count, MEMCATEGORY_GENERAL);
U* pDst = tmp;
const T* pSrc = src;
for (size_t i = 0; i < count; ++i)
*pDst++ = static_cast<U>(*pSrc++);
writeData(tmp, sizeof(U), count);
OGRE_FREE(tmp, MEMCATEGORY_GENERAL);
}
template <typename T, typename U>
void readConverted(T* dst, U typeToRead, size_t count)
{
U* tmp = OGRE_ALLOC_T(U, count, MEMCATEGORY_GENERAL);
readData(tmp, sizeof(U), count);
T* pDst = dst;
const U* pSrc = tmp;
for (size_t i = 0; i < count; ++i)
*pDst++ = static_cast<T>(*pSrc++);
OGRE_FREE(tmp, MEMCATEGORY_GENERAL);
}
};
}
#include "OgreHeaderSuffix.h"
#endif
Ogre::StreamSerialiser::Chunk
Definition of a chunk of data in a file.
Definition: OgreStreamSerialiser.h:91
_OgreExport
#define _OgreExport
Definition: OgrePlatform.h:257
Ogre::StreamSerialiser::mChunkStack
ChunkStack mChunkStack
Current list of open chunks.
Definition: OgreStreamSerialiser.h:337
Ogre::uint
unsigned int uint
Definition: OgrePrerequisites.h:114
Ogre::StreamSerialiser::ENDIAN_BIG
Use big endian (0x1000 is serialised as 0x10 0x00)
Definition: OgreStreamSerialiser.h:75
Ogre::Quaternion
Implementation of a Quaternion, i.e.
Definition: OgreQuaternion.h:57
Ogre::StreamSerialiser::RealStorageFormat
RealStorageFormat
The storage format of Real values.
Definition: OgreStreamSerialiser.h:81
Ogre::Sphere
A sphere primitive, mostly used for bounds checking.
Definition: OgreSphere.h:51
Ogre::Matrix3
A 3x3 matrix which can represent rotations around axes.
Definition: OgreMatrix3.h:68
Ogre::Vector4
4-dimensional homogeneous vector.
Definition: OgreVector4.h:45
Ogre::Vector2
Standard 2-dimensional vector.
Definition: OgreVector2.h:51
Ogre::Real
float Real
Software floating point type.
Definition: OgrePrerequisites.h:70
OGRE_ALLOC_T
#define OGRE_ALLOC_T(T, count, category)
Allocate a block of memory for a primitive type, and indicate the category of usage.
Definition: OgreMemoryAllocatorConfig.h:428
Ogre::StreamSerialiser::read
void read(T *pT, size_t count=1)
Catch-all method to read primitive types.
Definition: OgreStreamSerialiser.h:297
Ogre::StreamSerialiser::Chunk::version
uint16 version
Version of the chunk (stored)
Definition: OgreStreamSerialiser.h:96
Ogre::StreamSerialiser::mReadWriteHeader
bool mReadWriteHeader
Definition: OgreStreamSerialiser.h:333
OgreDataStream.h
Ogre::Plane
Defines a plane in 3D space.
Definition: OgrePlane.h:61
Ogre::StreamSerialiser::getEndian
virtual Endian getEndian() const
Get the endian mode.
Definition: OgreStreamSerialiser.h:137
Ogre::StreamSerialiser::mFlipEndian
bool mFlipEndian
Definition: OgreStreamSerialiser.h:332
Ogre::StreamSerialiser::writeConverted
void writeConverted(const T *src, U typeToWrite, size_t count)
Definition: OgreStreamSerialiser.h:361
Ogre::Matrix4
Class encapsulating a standard 4x4 homogeneous matrix.
Definition: OgreMatrix4.h:78
Ogre::Vector3
Standard 3-dimensional vector.
Definition: OgreVector3.h:51
Ogre::StreamSerialiser::ENDIAN_AUTO
Automatically determine endianness.
Definition: OgreStreamSerialiser.h:73
OgrePrerequisites.h
Ogre::StreamSerialiser::readConverted
void readConverted(T *dst, U typeToRead, size_t count)
Definition: OgreStreamSerialiser.h:374
Ogre::deque
Definition: OgrePrerequisites.h:477
Ogre::StreamSerialiser::Chunk::offset
uint32 offset
Location of the chunk (header) in bytes from the start of a stream (derived)
Definition: OgreStreamSerialiser.h:100
Ogre::StreamSerialiser::getCurrentChunkDepth
size_t getCurrentChunkDepth() const
Report the current depth of the chunk nesting, whether reading or writing.
Definition: OgreStreamSerialiser.h:153
Ogre::Node
Class representing a general-purpose node an articulated scene graph.
Definition: OgreNode.h:64
Ogre::StreamSerialiser::mStream
DataStreamPtr mStream
Definition: OgreStreamSerialiser.h:329
Ogre::StreamSerialiser::CHUNK_HEADER_SIZE
static uint32 CHUNK_HEADER_SIZE
Definition: OgreStreamSerialiser.h:341
Ogre::MEMCATEGORY_GENERAL
General purpose.
Definition: OgreMemoryAllocatorConfig.h:162
OGRE_FREE
#define OGRE_FREE(ptr, category)
Free the memory allocated with OGRE_MALLOC or OGRE_ALLOC_T. Category is required to be restated to en...
Definition: OgreMemoryAllocatorConfig.h:430
Ogre::StreamSerialiser::mRealFormat
RealStorageFormat mRealFormat
Definition: OgreStreamSerialiser.h:334
Ogre::StreamSerialiser::ChunkStack
deque< Chunk * >::type ChunkStack
Definition: OgreStreamSerialiser.h:335
Ogre::StreamSerialiser::write
void write(const T *pT, size_t count=1)
Catch-all method to write primitive types.
Definition: OgreStreamSerialiser.h:263
Ogre::StreamSerialiser::Chunk::id
uint32 id
Identifier of the chunk (for example from makeIdentifier) (stored)
Definition: OgreStreamSerialiser.h:94
Ogre::String
_StringBase String
Definition: OgrePrerequisites.h:439
Ogre::StreamSerialiser::Endian
Endian
The endianness of files.
Definition: OgreStreamSerialiser.h:70
Ogre::StreamSerialiser::mEndian
Endian mEndian
Definition: OgreStreamSerialiser.h:331
Ogre::StreamSerialiser::REVERSE_HEADER_ID
static uint32 REVERSE_HEADER_ID
Definition: OgreStreamSerialiser.h:340
Ogre::StreamSerialiser
Utility class providing helper methods for reading / writing structured data held in a DataStream...
Definition: OgreStreamSerialiser.h:66
OGRE_DOUBLE_PRECISION
#define OGRE_DOUBLE_PRECISION
If set to 1, Real is typedef&#39;ed to double.
Definition: OgreConfig.h:63
OgreHeaderSuffix.h
Ogre::StreamSerialiser::Chunk::Chunk
Chunk()
Definition: OgreStreamSerialiser.h:102
Ogre::uint32
unsigned int uint32
Definition: OgrePlatform.h:359
Ogre
Definition: OgreAndroidLogListener.h:34
Ogre::StreamSerialiser::Chunk::length
uint32 length
Length of the chunk data in bytes, excluding the header of this chunk (stored)
Definition: OgreStreamSerialiser.h:98
Ogre::SharedPtr< DataStream >
Ogre::StreamSerialiser::REAL_FLOAT
Real is stored as float, reducing precision if you&#39;re using OGRE_DOUBLE_PRECISION.
Definition: OgreStreamSerialiser.h:84
OgreHeaderPrefix.h
Ogre::StreamSerialiser::HEADER_ID
static uint32 HEADER_ID
Definition: OgreStreamSerialiser.h:339
Ogre::StreamSerialiser::mOriginalStream
DataStreamPtr mOriginalStream
Definition: OgreStreamSerialiser.h:330
Ogre::AxisAlignedBox
A 3D box aligned with the x/y/z axes.
Definition: OgreAxisAlignedBox.h:54
Ogre::StringUtil::BLANK
static const String BLANK
Constant blank string, useful for returning by ref where local does not exist.
Definition: OgreString.h:196
Ogre::uint16
unsigned short uint16
Definition: OgrePlatform.h:360
Ogre::AllocatedObject
Superclass for all objects that wish to use custom memory allocators when their new / delete operator...
Definition: OgreMemoryAllocatedObject.h:58
Ogre::Ray
Representation of a ray in space, i.e.
Definition: OgreRay.h:46
Ogre::Radian
Wrapper class which indicates a given angle value is in Radians.
Definition: OgreMath.h:47

Copyright © 2012 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.