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-2013 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 #include "OgreHeaderPrefix.h"
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 
00324         virtual void startDeflate(size_t avail_in = 0);
00327         virtual void stopDeflate();
00328     protected:
00329         DataStreamPtr mStream;
00330         DataStreamPtr mOriginalStream;
00331         Endian mEndian;
00332         bool mFlipEndian;
00333         bool mReadWriteHeader;
00334         RealStorageFormat mRealFormat;
00335         typedef deque<Chunk*>::type ChunkStack;
00337         ChunkStack mChunkStack;
00338 
00339         static uint32 HEADER_ID;
00340         static uint32 REVERSE_HEADER_ID;
00341         static uint32 CHUNK_HEADER_SIZE;
00342 
00343         virtual Chunk* readChunkImpl();
00344         virtual void writeChunkImpl(uint32 id, uint16 version);
00345         virtual void readHeader();
00346         virtual void writeHeader();
00347         virtual uint32 calculateChecksum(Chunk* c);
00348         virtual void checkStream(bool failOnEof = false, 
00349             bool validateReadable = false, bool validateWriteable = false) const;
00350 
00351         virtual void flipEndian(void * pData, size_t size, size_t count);
00352         virtual void flipEndian(void * pData, size_t size);
00353         virtual void determineEndianness();
00354         virtual Chunk* popChunk(uint id);
00355 
00356         virtual void writeFloatsAsDoubles(const float* val, size_t count);
00357         virtual void writeDoublesAsFloats(const double* val, size_t count);
00358         virtual void readFloatsAsDoubles(double* val, size_t count);
00359         virtual void readDoublesAsFloats(float* val, size_t count);
00360         template <typename T, typename U>
00361         void writeConverted(const T* src, U typeToWrite, size_t count)
00362         {
00363             U* tmp = OGRE_ALLOC_T(U, count, MEMCATEGORY_GENERAL);
00364             U* pDst = tmp;
00365             const T* pSrc = src;
00366             for (size_t i = 0; i < count; ++i)
00367                 *pDst++ = static_cast<U>(*pSrc++);
00368             
00369             writeData(tmp, sizeof(U), count);
00370 
00371             OGRE_FREE(tmp, MEMCATEGORY_GENERAL);
00372         }
00373         template <typename T, typename U>
00374         void readConverted(T* dst, U typeToRead, size_t count)
00375         {
00376             U* tmp = OGRE_ALLOC_T(U, count, MEMCATEGORY_GENERAL);
00377             readData(tmp, sizeof(U), count);
00378 
00379             T* pDst = dst;
00380             const U* pSrc = tmp;
00381             for (size_t i = 0; i < count; ++i)
00382                 *pDst++ = static_cast<T>(*pSrc++);
00383 
00384 
00385             OGRE_FREE(tmp, MEMCATEGORY_GENERAL);
00386         }
00387 
00388     };
00391 }
00392 
00393 #include "OgreHeaderSuffix.h"
00394 
00395 #endif
00396 

Copyright © 2012 Torus Knot Software Ltd
Creative Commons License
This work is licensed under a Creative Commons Attribution-ShareAlike 3.0 Unported License.
Last modified Mon Jul 27 2020 13:40:47