cel_serializable.h

Go to the documentation of this file.
00001 //----------------------------------------------------------------------------
00004 //
00005 // (C) 2003-2006 Celartem Technology Inc. All rights reserved.
00006 //----------------------------------------------------------------------------
00007 
00008 #ifndef _cel_serializable_h_
00009 #define _cel_serializable_h_
00010 
00011 #include "cel_memory.h"
00012 #include "cel_autoptr.h"
00013 #include "cel_string.h"
00014 #include "cel_guid.h"
00015 #include <vector>
00016 #include <typeinfo>
00017 
00018 namespace Celartem
00019 {
00020     class Serializable;
00021     class Stream;
00022     struct SerializableTable;
00023 
00024     //------------------------------------------------------------------------
00070     typedef AutoPtr<Serializable> (* DeserializeFunc)(
00071         Stream *inStream, size_t inLevel, Endian inEndian);
00072 
00073     //------------------------------------------------------------------------
00092     class Serializable : public Referable
00093     {
00094     public:
00095         //--------------------------------------------------------------------
00110         virtual void serialize(
00111             Stream *inStream,
00112             size_t inLevel = 0,
00113             Endian inEndian = endianBig) = 0;
00114 
00115         //--------------------------------------------------------------------
00122         virtual AutoPtr<Serializable> duplicate() const = 0;
00123 
00124         //--------------------------------------------------------------------
00130         const Guid& getClassId() const;
00131 
00137         String getClassName() const;
00138 
00139         //--------------------------------------------------------------------
00140         // the functions below are generating class instances and usually you
00141         // do not have to know about them.
00142         //--------------------------------------------------------------------
00143         static AutoPtr<Serializable> deserializeClassInstance(
00144             const Guid& guid,
00145             Stream *inStream,
00146             size_t inLevel,
00147             Endian inEndian);
00148 
00149         static void registerClass(
00150             const Guid& guid, const char *inClassName, const char *inRttiName,
00151             DeserializeFunc inDeserializeFunc);
00152 
00153         static void registerClass(
00154             SerializableTable *inTable,
00155             const Guid& guid, const char *inClassName, const char *inRttiName,
00156             DeserializeFunc inDeserializeFunc);
00157 
00158         static void unregisterClass(
00159             const Guid& guid);
00160 
00161         static bool isRegistered(const Guid& guid);
00162         static const Guid& getClassId(const char *RttiName);
00163         static String getClassName(const Guid& guid);
00164         static String getClassName(const char *RttiName);
00165     };
00166 
00167     //------------------------------------------------------------------------
00172     template<typename T, bool IS_POD> struct SerializableDataHelper
00173     {
00174         static void serialize(
00175             T& t, Stream *inStream, size_t inLevel, Endian inEndian)
00176         {
00177             t.serialize(inStream, inLevel, inEndian);
00178         }
00179         
00180         static T deserialize(
00181             Stream *inStream, size_t inLevel, Endian inEndian)
00182         {
00183             T t;
00184             t.deserialize(inStream, inLevel, inEndian);
00185             return t;
00186         }
00187     
00188     private:
00189         SerializableDataHelper();
00190         SerializableDataHelper(const SerializableDataHelper&);
00191         SerializableDataHelper& operator=(const SerializableDataHelper&);
00192     };
00193 
00194     //------------------------------------------------------------------------
00199     template<typename T> struct SerializableDataHelper<T, true>
00200     {
00201         static void serialize(
00202             T& t, Stream *inStream, size_t inLevel, Endian inEndian)
00203         {
00204             write<T>(inStream, t, inEndian);
00205         }
00206         
00207         static T deserialize(
00208             Stream *inStream, size_t inLevel, Endian inEndian)
00209         {
00210             return read<T>(inStream, inEndian);
00211         }
00212     
00213     private:
00214         SerializableDataHelper();
00215         SerializableDataHelper(const SerializableDataHelper&);
00216         SerializableDataHelper& operator=(const SerializableDataHelper&);
00217     };
00218 
00219     //------------------------------------------------------------------------
00230     //------------------------------------------------------------------------
00231     template<class T> class SerializableData : public Serializable
00232     {
00233     public:
00234         //--------------------------------------------------------------------
00241         static Serializable *create(const T& t)
00242         {
00243             return new SerializableData(t);
00244         }
00245 
00246         //--------------------------------------------------------------------
00256         virtual void serialize(
00257             Stream *inStream, size_t inLevel, Endian inEndian)
00258         {
00259             SerializableDataHelper<T, DataTraits<T>::isPOD>::serialize(
00260                 m_obj, inStream, inLevel, inEndian);
00261         }
00262 
00263         //--------------------------------------------------------------------
00270         virtual AutoPtr<Serializable> duplicate() const
00271         {
00272             return new SerializableData(m_obj);
00273         }
00274 
00275         //--------------------------------------------------------------------
00287         static AutoPtr<Serializable> deserialize(
00288             Stream *inStream, size_t inLevel, Endian inEndian)
00289         {
00290             return new SerializableData<T>(
00291                 SerializableDataHelper<T, DataTraits<T>::isPOD>::deserialize(
00292                     inStream, inLevel, inEndian));
00293         }
00294 
00295         //--------------------------------------------------------------------
00299         T& getValue() {return m_obj;}
00300 
00304         const T& getValue() const {return m_obj;}
00305 
00306     private:
00307         T m_obj;
00308 
00309         // end-user should not call these functions
00310         SerializableData(const T& t) : m_obj(t) {}
00311         SerializableData(const SerializableData&);
00312         SerializableData& operator=(const SerializableData&);
00313     };
00314     
00315     typedef SerializableData<int> SerializableInt;
00316     typedef SerializableData<unsigned int> SerializableUInt;
00317     typedef SerializableData<int64_t> SerializableInt64;
00318     typedef SerializableData<uint64_t> SerializableUInt64;
00319     
00320 } // namespace Celartem
00321 
00322 //----------------------------------------------------------------------------
00338 #define REGISTER_SERIALIZABLECLASS(ID,CLASSNAME) \
00339     ::Celartem::Serializable::registerClass( \
00340         ID,#CLASSNAME, typeid(CLASSNAME).name(), CLASSNAME::deserialize)
00341 
00342 //----------------------------------------------------------------------------
00361 #define REGISTER_SERIALIZABLECLASS2(ID,CLASSNAME,DISPNAME) \
00362     ::Celartem::Serializable::registerClass( \
00363         ID,DISPNAME, typeid(CLASSNAME).name(), CLASSNAME::deserialize)
00364 
00365 #endif // _cel_serializable_h_

This document is automatically generated using doxygen 1.5.4 at Fri Jun 27 18:21:54 2008.