# OgreStreamSerialiser.h

Go to the documentation of this file.

    00001 /*
    00002 -----------------------------------------------------------------------------
    00003 This source file is part of OGRE
    00004 (Object-oriented Graphics Rendering Engine)
    00005 For the latest info, see http://www.ogre3d.org/
    00006
    00007 Copyright (c) 2000-2009 Torus Knot Software Ltd
    00008
    00009 Permission is hereby granted, free of charge, to any person obtaining a copy
    00010 of this software and associated documentation files (the "Software"), to deal
    00011 in the Software without restriction, including without limitation the rights
    00012 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
    00013 copies of the Software, and to permit persons to whom the Software is
    00014 furnished to do so, subject to the following conditions:
    00015
    00016 The above copyright notice and this permission notice shall be included in
    00017 all copies or substantial portions of the Software.
    00018
    00019 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    00020 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    00021 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    00022 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    00023 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    00024 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    00025 THE SOFTWARE.
    00026 -----------------------------------------------------------------------------
    00027 */
    00028 #ifndef __StreamSerialiser_H__
    00029 #define __StreamSerialiser_H__
    00030
    00031 #include "OgrePrerequisites.h"
    00032 #include "OgreDataStream.h"
    00033
    00034
    00035 namespace Ogre
    00036 {
    00066     class _OgreExport StreamSerialiser : public StreamAlloc
    00067     {
    00068     public:
    00070         enum Endian
    00071         {
    00073             ENDIAN_AUTO,
    00075             ENDIAN_BIG,
    00077             ENDIAN_LITTLE
    00078         };
    00079
    00081         enum RealStorageFormat
    00082         {
    00084             REAL_FLOAT,
    00086             REAL_DOUBLE
    00087         };
    00088
    00089
    00091         struct Chunk : public StreamAlloc
    00092         {
    00094             uint32 id;
    00096             uint16 version;
    00098             uint32 length;
    00100             uint32 offset;
    00101
    00102             Chunk() : id(0), version(1), length(0), offset(0) {}
    00103         };
    00104
    00122         StreamSerialiser(const DataStreamPtr& stream, Endian endianMode = ENDIAN_AUTO,
    00123             bool autoHeader = true,
    00124 #if OGRE_DOUBLE_PRECISION
    00125             RealStorageFormat realFormat = REAL_DOUBLE
    00126 #else
    00127             RealStorageFormat realFormat = REAL_FLOAT
    00128 #endif
    00129             );
    00130         virtual ~StreamSerialiser();
    00131
    00137         virtual Endian getEndian() const { return mEndian; }
    00138
    00145         static uint32 makeIdentifier(const String& code);
    00146
    00153         size_t getCurrentChunkDepth() const { return mChunkStack.size(); }
    00154
    00159         uint32 getCurrentChunkID() const;
    00160
    00168         size_t getOffsetFromChunkStart() const;
    00169
    00181         virtual const Chunk* readChunkBegin();
    00182
    00195         virtual const Chunk* readChunkBegin(uint32 id, uint16 maxVersion, const String& msg = StringUtil::BLANK);
    00196
    00206         virtual void undoReadChunk(uint32 id);
    00207
    00209         virtual uint32 peekNextChunkID();
    00210
    00219         virtual void readChunkEnd(uint32 id);
    00220
    00224         virtual bool isEndOfChunk(uint32 id);
    00225
    00227         virtual bool eof() const;
    00228
    00230         virtual const Chunk* getCurrentChunk() const;
    00231
    00246         virtual void writeChunkBegin(uint32 id, uint16 version = 1);
    00251         virtual void writeChunkEnd(uint32 id);
    00252
    00259         virtual void writeData(const void* buf, size_t size, size_t count);
    00260
    00262         template <typename T>
    00263         void write(const T* pT, size_t count = 1)
    00264         {
    00265             writeData(pT, sizeof(T), count);
    00266         }
    00267
    00268         // Special-case Real since we need to deal with single/double precision
    00269         virtual void write(const Real* val, size_t count = 1);
    00270
    00271         virtual void write(const Vector2* vec, size_t count = 1);
    00272         virtual void write(const Vector3* vec, size_t count = 1);
    00273         virtual void write(const Vector4* vec, size_t count = 1);
    00274         virtual void write(const Quaternion* q, size_t count = 1);
    00275         virtual void write(const Matrix3* m, size_t count = 1);
    00276         virtual void write(const Matrix4* m, size_t count = 1);
    00277         virtual void write(const String* string);
    00278         virtual void write(const AxisAlignedBox* aabb, size_t count = 1);
    00279         virtual void write(const Sphere* sphere, size_t count = 1);
    00280         virtual void write(const Plane* plane, size_t count = 1);
    00281         virtual void write(const Ray* ray, size_t count = 1);
    00282         virtual void write(const Radian* angle, size_t count = 1);
    00283         virtual void write(const Node* node, size_t count = 1);
    00284         virtual void write(const bool* boolean, size_t count = 1);
    00285
    00286
    00293         virtual void readData(void* buf, size_t size, size_t count);
    00294
    00296         template <typename T>
    00297         void read(T* pT, size_t count = 1)
    00298         {
    00299             readData(pT, sizeof(T), count);
    00300         }
    00301
    00302         // Special case Real, single/double-precision issues
    00303         virtual void read(Real* val, size_t count = 1);
    00304
    00306         virtual void read(Vector2* vec, size_t count = 1);
    00307         virtual void read(Vector3* vec, size_t count = 1);
    00308         virtual void read(Vector4* vec, size_t count = 1);
    00309         virtual void read(Quaternion* q, size_t count = 1);
    00310         virtual void read(Matrix3* m, size_t count = 1);
    00311         virtual void read(Matrix4* m, size_t count = 1);
    00312         virtual void read(String* string);
    00313         virtual void read(AxisAlignedBox* aabb, size_t count = 1);
    00314         virtual void read(Sphere* sphere, size_t count = 1);
    00315         virtual void read(Plane* plane, size_t count = 1);
    00316         virtual void read(Ray* ray, size_t count = 1);
    00317         virtual void read(Radian* angle, size_t count = 1);
    00318         virtual void read(Node* node, size_t count = 1);
    00319         virtual void read(bool* val, size_t count = 1);
    00320
    00321     protected:
    00322         DataStreamPtr mStream;
    00323         Endian mEndian;
    00324         bool mFlipEndian;
    00325         bool mReadWriteHeader;
    00326         RealStorageFormat mRealFormat;
    00327         typedef deque<Chunk*>::type ChunkStack;
    00329         ChunkStack mChunkStack;
    00330
    00331         static uint32 HEADER_ID;
    00332         static uint32 REVERSE_HEADER_ID;
    00333         static uint32 CHUNK_HEADER_SIZE;
    00334
    00335         virtual Chunk* readChunkImpl();
    00336         virtual void writeChunkImpl(uint32 id, uint16 version);
    00337         virtual void readHeader();
    00338         virtual void writeHeader();
    00339         virtual uint32 calculateChecksum(Chunk* c);
    00340         virtual void checkStream(bool failOnEof = false,
    00341             bool validateReadable = false, bool validateWriteable = false) const;
    00342
    00343         virtual void flipEndian(void * pData, size_t size, size_t count);
    00344         virtual void flipEndian(void * pData, size_t size);
    00345         virtual void determineEndianness();
    00346         virtual Chunk* popChunk(uint id);
    00347
    00348         virtual void writeFloatsAsDoubles(const float* val, size_t count);
    00349         virtual void writeDoublesAsFloats(const double* val, size_t count);
    00350         virtual void readFloatsAsDoubles(double* val, size_t count);
    00351         virtual void readDoublesAsFloats(float* val, size_t count);
    00352         template <typename T, typename U>
    00353         void writeConverted(const T* src, U typeToWrite, size_t count)
    00354         {
    00355             U* tmp = OGRE_ALLOC_T(U, count, MEMCATEGORY_GENERAL);
    00356             U* pDst = tmp;
    00357             const T* pSrc = src;
    00358             for (size_t i = 0; i < count; ++i)
    00359                 *pDst++ = static_cast<U>(*pSrc++);
    00360
    00361             writeData(tmp, sizeof(U), count);
    00362
    00363             OGRE_FREE(tmp, MEMCATEGORY_GENERAL);
    00364         }
    00365         template <typename T, typename U>
    00366         void readConverted(T* dst, U typeToRead, size_t count)
    00367         {
    00368             U* tmp = OGRE_ALLOC_T(U, count, MEMCATEGORY_GENERAL);
    00369             readData(tmp, sizeof(U), count);
    00370
    00371             T* pDst = dst;
    00372             const U* pSrc = tmp;
    00373             for (size_t i = 0; i < count; ++i)
    00374                 *pDst++ = static_cast<T>(*pSrc++);
    00375
    00376
    00377             OGRE_FREE(tmp, MEMCATEGORY_GENERAL);
    00378         }
    00379
    00380     };
    00383 }
    00384
    00385 #endif
    00386

Copyright © 2008 Torus Knot Software Ltd
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Wed Nov 3 2010 19:24:52