Shadowrun: Awakened 29 September 2011 - Build 871
Static Public Member Functions
SerialHelper Class Reference

#include <Serialization.h>

List of all members.

Static Public Member Functions

template<class T >
static void DeleteVector (std::vector< T * > *vec)
static size_t LoadLength (ISerializer *serializer)
template<class T >
static T LoadObject (ISerializer *serializer)
template<class T >
static TLoadObjectInstance (ISerializer *serializer)
template<class T >
static void LoadSerializable (ISerializer *serializer, T *&obj)
template<class T >
static void LoadSerializable (ISerializer *serializer, std::vector< T * > &collection)
static std::string * LoadString (ISerializer *serializer)
template<class T >
static void LoadVector (ISerializer *serializer, std::vector< T * > &vec)
static void SaveLength (ISerializer *serializer, size_t count)
template<class T >
static void SaveObject (ISerializer *serializer, const T &obj)
template<class T >
static void SaveSerializable (ISerializer *serializer, const std::vector< T * > &collection)
template<class T >
static void SaveSerializableObject (ISerializer *serializer, T *obj)
static void SaveString (ISerializer *serializer, const std::string &s)
template<class T >
static void SaveVector (ISerializer *serializer, const std::vector< T > &children)

Detailed Description

This class provides convenience functions for serializing ISerializble NOTE: Definition included in this file for compilation of templates across projects

Definition at line 36 of file Serialization.h.


Member Function Documentation

template<class T >
static void SerialHelper::DeleteVector ( std::vector< T * > *  vec) [inline, static]

Deletes the given vector and all items

Definition at line 44 of file Serialization.h.

References T.

    {
        for(size_t i=0; i<vec->size(); ++i)
        {
            T* t = (*vec)[i];
            delete t;
        }

        delete vec;
    }
static size_t SerialHelper::LoadLength ( ISerializer serializer) [inline, static]

Reads and returns the next size_t

Definition at line 67 of file Serialization.h.

References ISerializer::read().

Referenced by LoadSerializable(), LoadString(), and LoadVector().

    {
        //read the count
        size_t count;
        serializer->read(&count, sizeof(size_t));
        return count;
    }
template<class T >
static T SerialHelper::LoadObject ( ISerializer serializer) [inline, static]

Loads a simple (non-composite) object from the serializer returning a copy of the object

Definition at line 89 of file Serialization.h.

References ISerializer::read(), and T.

    {
        unsigned int byteSize = sizeof(T);
        char* buf = new char[byteSize];
        serializer->read(buf, byteSize);
        T* t = (T*)buf;
        T ret = *t;
        delete[] buf;
        return ret;
    }
template<class T >
static T* SerialHelper::LoadObjectInstance ( ISerializer serializer) [inline, static]

Loads a simple (non-composite) object from the serializer returning a ptr to a copy of the object

Definition at line 105 of file Serialization.h.

References ISerializer::read(), and T.

    {
        unsigned int byteSize = sizeof(T);
        char* buf = new char[byteSize];
        serializer->read(buf, byteSize);
        T* t = (T*)buf;
        T* ret = new T(*t);
        delete[] buf;
        return ret;
    }
template<class T >
static void SerialHelper::LoadSerializable ( ISerializer serializer,
T *&  obj 
) [inline, static]

Loads a collection of ISerializableObjects for each object, read it from file then call load

Definition at line 206 of file Serialization.h.

References ISerializable::load(), ISerializer::read(), and T.

    {
        //read in bytes for the serialized obj
        unsigned int byteCount = sizeof(T);
        char* buf = new char[byteCount];
        serializer->read(buf, byteCount);
        //use them to copy into a new object instance
        T* t = (T*)buf;
        T* loader = new T(*t);
        //cast into ISerializable and load
        ISerializable* ptr = static_cast<ISerializable*>(loader);
        ptr->load(serializer);
        delete[] buf;
        obj = loader;
    }
template<class T >
static void SerialHelper::LoadSerializable ( ISerializer serializer,
std::vector< T * > &  collection 
) [inline, static]

Loads a collection of ISerializableObjects for each object, read it from file then call load

Definition at line 248 of file Serialization.h.

References ISerializable::load(), LoadLength(), ISerializer::read(), and T.

    {
        //read the length
        unsigned int length = (unsigned int)LoadLength(serializer);

        //read in each serializable, calling load on it
        std::vector<T*>* ret = new std::vector<T*>;
        unsigned int byteCount = sizeof(T);
        for(size_t i=0; i<length; ++i)
        {
            char* buf = new char[byteCount];
            serializer->read(buf, byteCount);
            T* t = (T*)buf;
            T* loader = new T(*t);
            ISerializable* ptr = static_cast<ISerializable*>(loader);
            ptr->load(serializer);
            collection.push_back(loader);
        }
    }
static std::string* SerialHelper::LoadString ( ISerializer serializer) [inline, static]

Loads an std::string from the given serializer

Definition at line 170 of file Serialization.h.

References LoadLength(), and ISerializer::read().

    {
        //read the length
        size_t length = LoadLength(serializer);

        if(length == 0)
            return new std::string("");

        //read the string
        size_t bytePerChar = sizeof(char);
        unsigned int total = (unsigned int)bytePerChar*length;
        char* buf = new char[total+1]; //one more for 0
        buf[total] = 0;
        serializer->read(buf, total);
        std::string* ret = new std::string(buf);
        delete[] buf;
        return ret;
    }
template<class T >
static void SerialHelper::LoadVector ( ISerializer serializer,
std::vector< T * > &  vec 
) [inline, static]

This generic function is good for easily loading simple (non-composite) types from a serialization source and instancing them as a vector

Definition at line 135 of file Serialization.h.

References LoadLength(), ISerializer::read(), and T.

    {
        size_t count = LoadLength(serializer);
        //load the items
        unsigned int byteSize = sizeof(T);
        char* buf = new char[byteSize];
        //each time, we load into the buffer, then copy by pushing back
        for(size_t i=0; i <count; ++i)
        {
            serializer->read(buf, byteSize);
            T* t = (T*)buf;
            T* loader = new T(*t);
            vec.push_back(loader);
        }
        delete[] buf;
    }
static void SerialHelper::SaveLength ( ISerializer serializer,
size_t  count 
) [inline, static]

Saves a size_t to the serializer

Definition at line 58 of file Serialization.h.

References ISerializer::write().

Referenced by SaveSerializable(), SaveString(), and SaveVector().

    {
        //save the count
        serializer->write(&count, sizeof(size_t));
    }
template<class T >
static void SerialHelper::SaveObject ( ISerializer serializer,
const T obj 
) [inline, static]

Saves a simple (non-composite) object to the serializer

Definition at line 79 of file Serialization.h.

References T, and ISerializer::write().

    {
        serializer->write(&obj, sizeof(T));
    }
template<class T >
static void SerialHelper::SaveSerializable ( ISerializer serializer,
const std::vector< T * > &  collection 
) [inline, static]

Saves a collection of ISerializable objects For each object, write it to file then call save

Definition at line 227 of file Serialization.h.

References test::s, ISerializable::save(), SaveLength(), T, and ISerializer::write().

    {
        //save the length
        SaveLength(serializer, collection.size());

        //save the collection
        unsigned int byteCount = sizeof(T);
        for(size_t i=0; i<collection.size(); ++i)
        {
            T* t = collection[i];
            serializer->write(t, byteCount);
            ISerializable* s = static_cast<ISerializable*>(t);
            s->save(serializer);
        }
    }
template<class T >
static void SerialHelper::SaveSerializableObject ( ISerializer serializer,
T obj 
) [inline, static]

Saves a single instance of an ISerializable objects For each object, write it to file then call save

Definition at line 194 of file Serialization.h.

References mission1::obj, test::s, ISerializable::save(), T, and ISerializer::write().

    {
        serializer->write(obj, sizeof(T));
        ISerializable* s = static_cast<ISerializable*>(obj);
        s->save(serializer);
    }
static void SerialHelper::SaveString ( ISerializer serializer,
const std::string &  s 
) [inline, static]

Saves a string to the serializer, including its length before characters

Definition at line 155 of file Serialization.h.

References SaveLength(), and ISerializer::write().

    {
        //save the length
        SaveLength(serializer, s.size());

        if(s.size() == 0)
            return;
        //save the string
        size_t byteCount = sizeof(char);
        serializer->write(s.data(), (unsigned int)(byteCount*s.size()));
    }
template<class T >
static void SerialHelper::SaveVector ( ISerializer serializer,
const std::vector< T > &  children 
) [inline, static]

This generic function is good for easily saving simple (non-composite) types to serializations sources

Definition at line 121 of file Serialization.h.

References SaveLength(), T, and ISerializer::write().

    {
        SaveLength(serializer, children.size());
        //save the collection
        unsigned int byteSize = sizeof(T);
        for(size_t i=0; i<children.size(); ++i)
            serializer->write(&(children[i]), byteSize);
    }

The documentation for this class was generated from the following file:

Copyright © 2007-2010 by The Shadowrun: Awakened Team. This work is licensed under the GNU Lesser General Public License 3.

GNU Lesser General Public License 3 Sourceforge.net