taml.h

Engine/source/persistence/taml/taml.h

More...

Classes:

class

Public Defines

define
TAML_JSON_STRICT_VARIBLE() "$pref::T2D::JSONStrict"
define
TAML_SCHEMA_VARIABLE() "$pref::T2D::TAMLSchema"
define
TAML_SIGNATURE() "Taml"

Detailed Description

Public Defines

TAML_JSON_STRICT_VARIBLE() "$pref::T2D::JSONStrict"
TAML_SCHEMA_VARIABLE() "$pref::T2D::TAMLSchema"
TAML_SIGNATURE() "Taml"

Public Variables

StringTableEntry tamlNamedObjectName 
StringTableEntry tamlRefIdName 
StringTableEntry tamlRefToIdName 
  1
  2//-----------------------------------------------------------------------------
  3// Copyright (c) 2013 GarageGames, LLC
  4//
  5// Permission is hereby granted, free of charge, to any person obtaining a copy
  6// of this software and associated documentation files (the "Software"), to
  7// deal in the Software without restriction, including without limitation the
  8// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  9// sell copies of the Software, and to permit persons to whom the Software is
 10// furnished to do so, subject to the following conditions:
 11//
 12// The above copyright notice and this permission notice shall be included in
 13// all copies or substantial portions of the Software.
 14//
 15// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 16// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 17// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 18// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 19// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 20// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 21// IN THE SOFTWARE.
 22//-----------------------------------------------------------------------------
 23
 24#ifndef _TAML_H_
 25#define _TAML_H_
 26
 27#ifndef _TAML_CALLBACKS_H_
 28#include "persistence/taml/tamlCallbacks.h"
 29#endif
 30
 31#ifndef _TAML_CUSTOM_H_
 32#include "persistence/taml/tamlCustom.h"
 33#endif
 34
 35#ifndef _TAML_CHILDREN_H_
 36#include "persistence/taml/tamlChildren.h"
 37#endif
 38
 39#ifndef _TAML_WRITE_NODE_H_
 40#include "persistence/taml/tamlWriteNode.h"
 41#endif
 42
 43#ifndef _TAML_VISITOR_H_
 44#include "persistence/taml/tamlVisitor.h"
 45#endif
 46
 47#ifndef _SIMBASE_H_
 48#include "console/simBase.h"
 49#endif
 50
 51#ifndef _TDICTIONARY_H_
 52#include "core/util/tDictionary.h"
 53#endif
 54
 55#ifndef _FILESTREAM_H_
 56#include "core/stream/fileStream.h"
 57#endif
 58
 59//-----------------------------------------------------------------------------
 60
 61extern StringTableEntry tamlRefIdName;
 62extern StringTableEntry tamlRefToIdName;
 63extern StringTableEntry tamlNamedObjectName;
 64
 65//-----------------------------------------------------------------------------
 66
 67#define TAML_SIGNATURE                  "Taml"
 68#define TAML_SCHEMA_VARIABLE            "$pref::T2D::TAMLSchema"
 69#define TAML_JSON_STRICT_VARIBLE        "$pref::T2D::JSONStrict"
 70
 71class TiXmlElement;
 72
 73//-----------------------------------------------------------------------------
 74
 75/// @ingroup tamlGroup
 76/// @see tamlGroup
 77class Taml : public SimObject
 78{
 79public:
 80    enum TamlFormatMode
 81    {
 82        InvalidFormat = 0,
 83        XmlFormat,
 84        BinaryFormat,
 85        JSONFormat,
 86    };
 87
 88private:
 89    typedef SimObject Parent;
 90    typedef Vector<TamlWriteNode*>                  typeNodeVector;
 91    typedef HashTable<SimObjectId, TamlWriteNode*>  typeCompiledHash;
 92
 93    typeNodeVector      mCompiledNodes;
 94    typeCompiledHash    mCompiledObjects;
 95    U32                 mMasterNodeId;
 96    TamlFormatMode      mFormatMode;
 97    StringTableEntry    mAutoFormatXmlExtension;
 98    StringTableEntry    mAutoFormatBinaryExtension;
 99    StringTableEntry    mAutoFormatJSONExtension;
100    bool                mJSONStrict;
101    bool                mBinaryCompression;
102    bool                mAutoFormat;
103    bool                mWriteDefaults;
104    bool                mProgenitorUpdate;
105    char                mFilePathBuffer[1024];
106
107private:
108    void resetCompilation( void );
109
110    TamlWriteNode* compileObject( SimObject* pSimObject, const bool forceId = false );
111    void compileStaticFields( TamlWriteNode* pTamlWriteNode );
112    void compileDynamicFields( TamlWriteNode* pTamlWriteNode );
113    void compileChildren( TamlWriteNode* pTamlWriteNode );
114    void compileCustomState( TamlWriteNode* pTamlWriteNode );
115    void compileCustomNodeState( TamlCustomNode* pCustomNode );
116
117    bool write( FileStream& stream, SimObject* pSimObject, const TamlFormatMode formatMode );
118    SimObject* read( FileStream& stream, const TamlFormatMode formatMode );
119    template<typename T> inline T* read( FileStream& stream, const TamlFormatMode formatMode )
120    {
121        SimObject* pSimObject = read( stream, formatMode );
122        if ( pSimObject == NULL )
123            return NULL;
124        T* pObj = dynamic_cast<T*>( pSimObject );
125        if ( pObj != NULL )
126            return pObj;
127        pSimObject->deleteObject();
128        return NULL;
129    }
130
131public:
132    Taml();
133    virtual ~Taml() {}
134
135    virtual bool onAdd();
136    virtual void onRemove();
137    static void initPersistFields();
138
139    /// Format mode.
140    inline void setFormatMode( const TamlFormatMode formatMode ) { mFormatMode = formatMode != Taml::InvalidFormat ? formatMode : Taml::XmlFormat; }
141    inline TamlFormatMode getFormatMode( void ) const { return mFormatMode; }
142
143    /// Auto-Format mode.
144    inline void setAutoFormat( const bool autoFormat ) { mAutoFormat = autoFormat; }
145    inline bool getAutoFormat( void ) const { return mAutoFormat; }
146
147    /// Write defaults.
148    inline void setWriteDefaults( const bool writeDefaults ) { mWriteDefaults = writeDefaults; }
149    inline bool getWriteDefaults( void ) const { return mWriteDefaults; }
150
151    /// Progenitor.
152    inline void setProgenitorUpdate( const bool progenitorUpdate ) { mProgenitorUpdate = progenitorUpdate; }
153    inline bool getProgenitorUpdate( void ) const { return mProgenitorUpdate; }
154
155    /// Auto-format extensions.
156    inline void setAutoFormatXmlExtension( const char* pExtension ) { mAutoFormatXmlExtension = StringTable->insert( pExtension ); }
157    inline StringTableEntry getAutoFormatXmlExtension( void ) const { return mAutoFormatXmlExtension; }
158    inline void setAutoFormatBinaryExtension( const char* pExtension ) { mAutoFormatBinaryExtension = StringTable->insert( pExtension ); }
159    inline StringTableEntry getAutoFormatBinaryExtension( void ) const { return mAutoFormatBinaryExtension; }
160
161    /// Compression.
162    inline void setBinaryCompression( const bool compressed ) { mBinaryCompression = compressed; }
163    inline bool getBinaryCompression( void ) const { return mBinaryCompression; }
164
165    /// JSON Strict RFC4627 mode.
166    inline void setJSONStrict( const bool jsonStrict ) { mJSONStrict = jsonStrict; }
167    inline bool getJSONStrict( void ) const { return mJSONStrict; }
168
169    TamlFormatMode getFileAutoFormatMode( const char* pFilename );
170
171    const char* getFilePathBuffer( void ) const { return mFilePathBuffer; }
172
173    /// Write.
174    bool write( SimObject* pSimObject, const char* pFilename );
175
176    /// Read.
177    template<typename T> inline T* read( const char* pFilename )
178    {
179        SimObject* pSimObject = read( pFilename );
180        if ( pSimObject == NULL )
181            return NULL;
182        T* pObj = dynamic_cast<T*>( pSimObject );
183        if ( pObj != NULL )
184            return pObj;
185        pSimObject->deleteObject();
186        return NULL;
187    }
188    SimObject* read( const char* pFilename );
189
190    /// Parse.
191    bool parse( const char* pFilename, TamlVisitor& visitor );
192
193    /// Create type.
194    static SimObject* createType( StringTableEntry typeName, const Taml* pTaml, const char* pProgenitorSuffix = NULL );
195
196    /// Schema generation.
197    static bool generateTamlSchema();
198
199    /// Write a unrestricted custom Taml schema.
200    static void WriteUnrestrictedCustomTamlSchema( const char* pCustomNodeName, const AbstractClassRep* pClassRep, TiXmlElement* pParentElement );
201
202    /// Get format mode info.
203    static TamlFormatMode getFormatModeEnum( const char* label );
204    static const char* getFormatModeDescription( const TamlFormatMode formatMode );
205
206    /// Taml callbacks.
207    inline void tamlPreWrite( TamlCallbacks* pCallbacks )                                           { pCallbacks->onTamlPreWrite(); }
208    inline void tamlPostWrite( TamlCallbacks* pCallbacks )                                          { pCallbacks->onTamlPostWrite(); }
209    inline void tamlPreRead( TamlCallbacks* pCallbacks )                                            { pCallbacks->onTamlPreRead(); }
210    inline void tamlPostRead( TamlCallbacks* pCallbacks, const TamlCustomNodes& customNodes )       { pCallbacks->onTamlPostRead( customNodes ); }
211    inline void tamlAddParent( TamlCallbacks* pCallbacks, SimObject* pParentObject )                { pCallbacks->onTamlAddParent( pParentObject ); }
212    inline void tamlCustomWrite( TamlCallbacks* pCallbacks, TamlCustomNodes& customNodes )          { pCallbacks->onTamlCustomWrite( customNodes ); }
213    inline void tamlCustomRead( TamlCallbacks* pCallbacks, const TamlCustomNodes& customNodes )     { pCallbacks->onTamlCustomRead( customNodes ); }
214
215    /// Declare Console Object.
216    DECLARE_CONOBJECT( Taml );
217};
218
219#endif // _TAML_H_
220