This section of the archives stores flipcode's complete Developer Toolbox collection, featuring a variety of mini-articles and source code contributions from our readers.

 

  Persistent Storage Framework
  Submitted by



This COTD is ripped straight from a report I recently made for the Performance Engineering Laboratory at the Department of Computer Science, University of Copenhagen. The following is the introdution from the report. Storage of data in computer programs can hardly be considered a difficult task, but it is often tedious and error prone. It is therefore desirable to automate this task if possible. Automatic storage of data (serialization here after) can easily be incorporated into a class hierarchy during construction, but it is much harder to achieve if legacy code is involved. So while being able to create serializable classes from scratch is essential, it must also be possible to augment legacy code, so that it supports serialization just as easily as specialized classes. There are many other problems when discussing serialization. For example, how should the data be stored, the choices are many; we could store it as binary data, raw text or XML, just to name a few. It would be nice if the ability of classes, or other data types, to be serialized were independent of the storage mode. A different problem is that of safety and by that I mean 'type safety' in the normal C++ sense, but also dealing with corrupt/mangled data files. We need an implementation that is robust. This project is about developing a serialization framework which deals with all the problems mentioned above. The three main principles should be flexibility, robustness and, ease of use. The implementation presented is based on two basic components, serializable data types and serializers. All basic C++ types (POD types) plus the most commonly used STL containers will be serializable. Tools for creating serializable classes and for augmenting legacy classes will also be included. Furthermore, it will feature three serializers, two for random access streams (binary files and XML files) and one for one-way streams, in the form of a pretty printer. Other serializers that this framework could support includes SQL DB serializers, zip file serializers, or serializing to a set of graphic controls so the user can change the data run-time. The XML serializer will be type safe in the sense that all serialized data will be tagged with a unique type identifier, which can be checked when deserializing. Finally, some effort will be spent on minimizing the size of serialized data, especially the binary data format. Although the (de)serializers may be important for working with persistent data types, their implementation is not considered a high priority in this project. The (de)serializers presented here will be fully functional, so testing can be performed and as a proof of concept. The reader of this report is expected to have a good understanding of C++, templates in particular, and generic programming in general. The report and the source are available for download at http://www.ghostdev.com/~jjj Enjoy John Juul Jensen

Currently browsing [psf.zip] (34,320 bytes) - [PrettyPrintSerializer.hpp] - (1,393 bytes)

#ifndef PRETTYPRINT_SERIALIZER_HPP
#define PRETTYPRINT_SERIALIZER_HPP

#include "Serializer.hpp" #include <iostream>

namespace PSF { template<class Elem> class TPrettyPrintSerializer : public ISerializer { public: TPrettyPrintSerializer( std::basic_ostream<Elem>& i_ostream, int i_nNesting = 0); virtual ~TPrettyPrintSerializer(); virtual void Serialize( const int& i_in); virtual void Serialize( const unsigned int& i_in); virtual void Serialize( const bool& i_in); virtual void Serialize( const char& i_in); virtual void Serialize( const unsigned char& i_in); virtual void Serialize( const short& i_in); virtual void Serialize( const unsigned short& i_in); virtual void Serialize( const long& i_in); virtual void Serialize( const unsigned long& i_in); virtual void Serialize( const float& i_in); virtual void Serialize( const double& i_in); // same as double // virtual void Serialize( const long double& i_in); virtual void Serialize( const wchar_t& i_in);

virtual std::auto_ptr<ISerializer> SerializeCompound( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type); virtual std::auto_ptr<ISerializer> SerializeRange( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type); protected: std::basic_ostream<Elem>& m_ostream; int m_nNesting; }; };

#include "PrettyPrintSerializer.inl"

#endif

Currently browsing [psf.zip] (34,320 bytes) - [Serialize.cpp] - (3,961 bytes)

#include "Serialize.hpp"

#ifndef PSF_PARTIAL_ORDERING_OF_TEMPLATES

// POD Serializers void PSF::Serialize( const char& i_in, ISerializer& i_serializer) { i_serializer.Serialize( i_in); }

void PSF::Serialize( const unsigned char& i_in, ISerializer& i_serializer) { i_serializer.Serialize( i_in); }

void PSF::Serialize( const short& i_in, ISerializer& i_serializer) { i_serializer.Serialize( i_in); }

void PSF::Serialize( const unsigned short& i_in, ISerializer& i_serializer) { i_serializer.Serialize( i_in); }

void PSF::Serialize( const int& i_in, ISerializer& i_serializer) { i_serializer.Serialize( i_in); }

void PSF::Serialize( const unsigned int& i_in, ISerializer& i_serializer) { i_serializer.Serialize( i_in); }

void PSF::Serialize( const long& i_in, ISerializer& i_serializer) { i_serializer.Serialize( i_in); }

void PSF::Serialize( const unsigned long& i_in, ISerializer& i_serializer) { i_serializer.Serialize( i_in); }

void PSF::Serialize( const float& i_in, ISerializer& i_serializer) { i_serializer.Serialize( i_in); }

void PSF::Serialize( const double& i_in, ISerializer& i_serializer) { i_serializer.Serialize( i_in); }

void PSF::Serialize( const bool& i_in, ISerializer& i_serializer) { i_serializer.Serialize( i_in); }

void PSF::Serialize( const wchar_t& i_in, ISerializer& i_serializer) { i_serializer.Serialize( i_in); }



void PSF::Serialize( const char& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName) { Serialize( i_in, *i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(char)))); }

void PSF::Serialize( const unsigned char& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName) { Serialize( i_in, *i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(unsigned char)))); }

void PSF::Serialize( const short& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName) { Serialize( i_in, *i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(short)))); }

void PSF::Serialize( const unsigned short& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName) { Serialize( i_in, *i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(unsigned short)))); }

void PSF::Serialize( const int& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName) { Serialize( i_in, *i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(int)))); }

void PSF::Serialize( const unsigned int& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName) { Serialize( i_in, *i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(unsigned int)))); }

void PSF::Serialize( const long& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName) { Serialize( i_in, *i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(long)))); }

void PSF::Serialize( const unsigned long& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName) { Serialize( i_in, *i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(unsigned long)))); }

void PSF::Serialize( const float& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName) { Serialize( i_in, *i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(float)))); }

void PSF::Serialize( const double& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName) { Serialize( i_in, *i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(double)))); }

void PSF::Serialize( const bool& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName) { Serialize( i_in, *i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(bool)))); }

void PSF::Serialize( const wchar_t& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName) { Serialize( i_in, *i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(wchar_t)))); } #endif

Currently browsing [psf.zip] (34,320 bytes) - [Serialize.hpp] - (7,504 bytes)

#ifndef PSF_SERIALIZE_HPP
#define PSF_SERIALIZE_HPP

#include <list> #include <vector> #include <map> #include <set>

#include "Serializer.hpp" #include "Assert.hpp"

#include "Compound.hpp"

#include "CompilerConfig.hpp"

namespace PSF { // Serialize PSF compunds template<class t_TypeList,class t_Unique,class t_Base> class TCompoundSerializer { public: typedef TCompound<t_TypeList,t_Unique,t_Base> Compound;

TCompoundSerializer( const Compound& i_compound) : m_compound( i_compound) { }

void Serialize( ISerializer& i_serializer) const { TInner<Compound> inner( m_compound); inner.Serialize( i_serializer); }

private: template<class t_InnerCompund> class TInner { public: TInner( const t_InnerCompund& i_innerCompound) : m_innerCompound( i_innerCompound) { }

void Serialize( ISerializer& i_serializer) const { TInner<t_InnerCompund::Base> inner( m_innerCompound); inner.Serialize( i_serializer); SerializeInternal<0>( i_serializer); } private:

enum { number_of_loops = t_InnerCompund::number_of_members-1};

template<int t_i> void SerializeInternal( ISerializer& i_serializer) const { PSF_ASSERT( t_InnerCompund::ms_namelist[t_i]); const wchar_t* pwszName = t_InnerCompund::ms_namelist[t_i]; PSF::Serialize( m_innerCompound.Member<t_i>(), i_serializer, pwszName); SerializeInternal<t_i+1>( i_serializer); }

template<> void SerializeInternal<number_of_loops>( ISerializer& i_serializer) const { PSF_ASSERT( t_InnerCompund::ms_namelist[number_of_loops]); const wchar_t* pwszName = t_InnerCompund::ms_namelist[number_of_loops]; PSF::Serialize( m_innerCompound.Member<number_of_loops>(), i_serializer, pwszName); } const t_InnerCompund& m_innerCompound; };

template<> class TInner<Loki::EmptyType> { public: TInner<Loki::EmptyType>( const t_InnerCompund& i_innerCompound) { }

void Serialize( ISerializer& i_serializer) const { } };

const Compound& m_compound; };

template<class t_TypeList,class t_Unique,class t_Base> void Serialize( const TCompound<t_TypeList,t_Unique,t_Base>& i_compound, ISerializer& i_serializer, const std::wstring& i_wstrName = L"Compound") { std::auto_ptr<ISerializer> pSerializer( i_serializer.SerializeCompound( i_wstrName, typeid( t_Unique))); TCompoundSerializer<t_TypeList,t_Unique,t_Base> compoundSerializer( i_compound); compoundSerializer.Serialize( *pSerializer); } };

namespace PSF { template<class t_iterType> void SerializeRange( t_iterType i_iterBegin, t_iterType i_iterEnd, ISerializer& i_serializer, const std::wstring& i_wstrName, const Loki::TypeInfo& i_typeInfo) { std::auto_ptr<ISerializer> pSerializer( i_serializer.SerializeRange( i_wstrName, i_typeInfo)); for ( ; i_iterBegin != i_iterEnd; ++i_iterBegin) { Serialize( *i_iterBegin, *pSerializer); } }

// Serializers for common containers template<typename t_type> void Serialize( const std::basic_string<t_type>& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName = L"String") { SerializeRange( i_in.begin(), i_in.end(), i_serializer, i_wstrName, Loki::TypeInfo(typeid(std::basic_string<void>))); }

template<typename t_type> void Serialize( const std::list<t_type>& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName = L"List") { SerializeRange( i_in.begin(), i_in.end(), i_serializer, i_wstrName, Loki::TypeInfo(typeid(std::list<void>))); }

template<typename t_type> void Serialize( const std::vector<t_type>& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName = L"Vector") { SerializeRange( i_in.begin(), i_in.end(), i_serializer, i_wstrName, Loki::TypeInfo(typeid(std::vector<void>))); }

template<typename t_type> void Serialize( const std::set<t_type>& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName = L"Set") { SerializeRange( i_in.begin(), i_in.end(), i_serializer, i_wstrName, Loki::TypeInfo(typeid(std::set<void>))); }

template<typename t_typeFirst,class t_typeSecond> void Serialize( const std::pair<t_typeFirst,t_typeSecond>& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName = L"Pair") { std::auto_ptr<ISerializer> pSerializer( i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(std::pair<void,void>)))); Serialize( i_in.first, *pSerializer, L"First"); Serialize( i_in.second, *pSerializer, L"Second"); }

template<typename t_key,typename t_type> void Serialize( const std::map<t_key,t_type>& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName = L"Map") { SerializeRange( i_in.begin(), i_in.end(), i_serializer, i_wstrName, Loki::TypeInfo(typeid(std::map<void,void>))); }

// POD Serializers #ifdef PSF_PARTIAL_ORDERING_OF_TEMPLATES // Template serializers template<typename t_type> void PSF::Serialize( t_type& i_in, ISerializer& i_serializer) { i_serializer.Serialize( i_in); }

template<typename t_type> void PSF::Serialize( t_type& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName) { Serialize( i_in, *i_serializer.SerializeCompound( i_wstrName, Loki::TypeInfo(typeid(t_type)))); } #else // Explicit serializers (for compilers that does not support partial ordering of templates) void Serialize( const char& i_in, ISerializer& i_serializer); void Serialize( const unsigned char& i_in, ISerializer& i_serializer); void Serialize( const short& i_in, ISerializer& i_serializer); void Serialize( const unsigned short& i_in, ISerializer& i_serializer); void Serialize( const int& i_in, ISerializer& i_serializer); void Serialize( const unsigned int& i_in, ISerializer& i_serializer); void Serialize( const long& i_in, ISerializer& i_serializer); void Serialize( const unsigned long& i_in, ISerializer& i_serializer); void Serialize( const float& i_in, ISerializer& i_serializer); void Serialize( const double& i_in, ISerializer& i_serializer); void Serialize( const bool& i_in, ISerializer& i_serializer); void Serialize( const wchar_t& i_in, ISerializer& i_serializer);

void Serialize( const char& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName); void Serialize( const unsigned char& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName); void Serialize( const short& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName); void Serialize( const unsigned short& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName); void Serialize( const int& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName); void Serialize( const unsigned int& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName); void Serialize( const long& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName); void Serialize( const unsigned long& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName); void Serialize( const float& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName); void Serialize( const double& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName); void Serialize( const bool& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName); void Serialize( const wchar_t& i_in, ISerializer& i_serializer, const std::wstring& i_wstrName); #endif };

#endif

Currently browsing [psf.zip] (34,320 bytes) - [Serializer.hpp] - (1,333 bytes)

#ifndef SERIALIZER_HPP
#define SERIALIZER_HPP

#include <string> #include <memory> #include "Loki/TypeInfo.h"

namespace PSF { class ISerializer { public: virtual ~ISerializer() = 0 {};

virtual void Serialize( const char& i_in) = 0; virtual void Serialize( const unsigned char& i_in) = 0; virtual void Serialize( const short& i_in) = 0; virtual void Serialize( const unsigned short& i_in) = 0; virtual void Serialize( const int& i_in) = 0; virtual void Serialize( const unsigned int& i_in) = 0; virtual void Serialize( const long& i_in) = 0; virtual void Serialize( const unsigned long& i_in) = 0; virtual void Serialize( const float& i_in) = 0; virtual void Serialize( const double& i_in) = 0; // same as double // virtual void Serialize( const long double& i_in) = 0; virtual void Serialize( const bool& i_in) = 0; virtual void Serialize( const wchar_t& i_in) = 0;

// creates a scope within the serializer virtual std::auto_ptr<ISerializer> SerializeCompound( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type) = 0;

// Serializing a range means that all item serialized in this scope must be of the same type virtual std::auto_ptr<ISerializer> SerializeRange( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type) = 0; }; }; #endif

Currently browsing [psf.zip] (34,320 bytes) - [SerializerStream.hpp] - (29 bytes)

#include "Serializer.hpp"


Currently browsing [psf.zip] (34,320 bytes) - [StringUtil.cpp] - (2,833 bytes)

#include "StringUtil.hpp"
#include "Utils/utf16utf8_codecvt.h"
#include "Utils/convertstream.h"
#include <locale>

std::wostream& operator << ( std::wostream& i_wostream, const std::string& i_str) { std::wstring wstr; Utility::CharWCharConvert( i_str.begin(), i_str.end(), std::back_inserter( wstr)); return i_wostream.write( wstr.data(), wstr.size()); }

std::ostream& operator << ( std::ostream& i_ostream, const std::wstring& i_wstr) { std::locale loc( std::locale(), new utf16utf8_codecvt); basic_oconvertstream<wchar_t, std::char_traits<wchar_t>, char, std::char_traits<char> > n; n.imbue(loc); n << i_wstr; return i_ostream << n.str(); }

//#include <ctime> /* void Utility::FormatDateString(unsigned int i_nDate, std::string& o_strOut) { tm* ptm = localtime((time_t*)&i_nDate);

std::string strMonth; switch (ptm->tm_mon) { case 0: strMonth = "Jan"; break; case 1: strMonth = "Feb"; break; case 2: strMonth = "Mar"; break; case 3: strMonth = "Apr"; break; case 4: strMonth = "May"; break; case 5: strMonth = "Jun"; break; case 6: strMonth = "Jul"; break; case 7: strMonth = "Aug"; break; case 8: strMonth = "Sep"; break; case 9: strMonth = "Okt"; break; case 10: strMonth = "Nov"; break; case 11: strMonth = "Dec"; break; default: break; }

char* pszOut = new char[255]; sprintf(pszOut, "%d. %s\n(%02d:%02d)", ptm->tm_mday, strMonth.c_str(), ptm->tm_hour, ptm->tm_min); o_strOut = pszOut; delete[] pszOut; }

void Utility::FormatDateString(unsigned int i_nDate, std::wstring& o_wstrOut) { std::string strTemp; FormatDateString(i_nDate, strTemp); o_wstrOut = StringToWString(strTemp); }

void Utility::FormatDateStringHdr(unsigned int i_nDate, std::string& o_strOut) { tm* ptm = localtime((time_t*)&i_nDate);

std::string strMonth; switch (ptm->tm_mon) { case 0: strMonth = "Jan"; break; case 1: strMonth = "Feb"; break; case 2: strMonth = "Mar"; break; case 3: strMonth = "Apr"; break; case 4: strMonth = "May"; break; case 5: strMonth = "Jun"; break; case 6: strMonth = "Jul"; break; case 7: strMonth = "Aug"; break; case 8: strMonth = "Sep"; break; case 9: strMonth = "Okt"; break; case 10: strMonth = "Nov"; break; case 11: strMonth = "Dec"; break; default: break; }

char* pszOut = new char[255]; sprintf(pszOut, "%d. %s (%02d:%02d)", ptm->tm_mday, strMonth.c_str(), ptm->tm_hour, ptm->tm_min); o_strOut = pszOut; delete[] pszOut; }

void Utility::FormatDateStringHdr(unsigned int i_nDate, std::wstring& o_wstrOut) { std::string strTemp; FormatDateStringHdr(i_nDate, strTemp); o_wstrOut = StringToWString(strTemp); } */

Currently browsing [psf.zip] (34,320 bytes) - [StringUtil.hpp] - (5,216 bytes)

#ifndef _UTILITY_STRING_H_
#define _UTILITY_STRING_H_

#include <string> #include <sstream> #include <algorithm> #include <iterator> #include <locale> #include <ostream>

namespace Utility { class CCharWCharConvert { public: CCharWCharConvert() : m_ctype( std::use_facet<std::ctype<wchar_t> > ( std::locale::locale())) { }

CCharWCharConvert( std::locale& i_locale) : m_ctype( std::use_facet<std::ctype<wchar_t> > ( i_locale)) { }

char operator() ( wchar_t i_wc) { return m_ctype.narrow( i_wc); }

wchar_t operator() ( char i_c) { return m_ctype.widen( i_c); }

private: const std::ctype<wchar_t>& m_ctype;

};

template< class t_CharType> class CCharacterToLower { public: CCharacterToLower() : m_ctype( std::use_facet<std::ctype<t_CharType> > ( std::locale::locale())) { }

CCharacterToLower( std::locale& i_locale) : m_ctype( std::use_facet<std::ctype<t_CharType> > ( i_locale)) { }

void operator() ( t_CharType& i_char) { i_char = m_ctype.tolower( i_char); }

private: const std::ctype<t_CharType>& m_ctype;

};

template< class t_CharType> class CCharacterToUpper { public: CCharacterToUpper() : m_ctype( std::use_facet<std::ctype<t_CharType> > ( std::locale::locale())) { }

CCharacterToUpper( std::locale& i_locale) : m_ctype( std::use_facet<std::ctype<t_CharType> > ( i_locale)) { }

void operator() ( t_CharType& i_char) { i_char = m_ctype.toupper( i_char); }

private: const std::ctype<t_CharType>& m_ctype;

};

class CIsWhitespace { public: CIsWhitespace() : m_locale( std::locale::locale()) { }

CIsWhitespace( std::locale& i_locale) : m_locale( i_locale) { }

template< class t_CharType> bool operator() ( t_CharType i_char) { return !std::isgraph( i_char, m_locale); }

private: const std::locale& m_locale; };

class CIsNotWhitespace { public: CIsNotWhitespace() : m_locale( std::locale::locale()) { }

CIsNotWhitespace( std::locale& i_locale) : m_locale( i_locale) { }

template< class t_CharType> bool operator() ( t_CharType i_char) { return std::isgraph( i_char, m_locale); }

private: std::locale m_locale; };

template< class t_InputIterator, class t_OutputIterator> void CharWCharConvert( t_InputIterator i_iterBegin, t_InputIterator i_iterEnd, t_OutputIterator& o_iter) { std::transform( i_iterBegin, i_iterEnd, o_iter, CCharWCharConvert()); }

template< class t_InputIterator> void CharToLower( t_InputIterator i_iterBegin, t_InputIterator i_iterEnd) { std::for_each( i_iterBegin, i_iterEnd, CCharacterToLower<char>()); }

template< class t_InputIterator> void WCharToLower( t_InputIterator i_iterBegin, t_InputIterator i_iterEnd) { std::for_each( i_iterBegin, i_iterEnd, CCharacterToLower<wchar_t>()); }

template< class t_InputIterator> void CharToUpper( t_InputIterator i_iterBegin, t_InputIterator i_iterEnd) { std::for_each( i_iterBegin, i_iterEnd, CCharacterToUpper<char>()); }

template< class t_InputIterator> void WCharToUpper( t_InputIterator i_iterBegin, t_InputIterator i_iterEnd) { std::for_each( i_iterBegin, i_iterEnd, CCharacterToUpper<wchar_t>()); }

template< class t_InputIterator> t_InputIterator SkipWhiteSpace( t_InputIterator i_iterBegin, t_InputIterator i_iterEnd) { return std::find_if( i_iterBegin, i_iterEnd, CIsNotWhitespace()); }

template< class t_type> std::wstring ValueToString( const t_type& i_in) { std::wstringstream wstrstream; wstrstream << std::boolalpha << i_in; return wstrstream.str(); }

template< class t_type> void StringToValue( const std::wstring& i_in, t_type& o_out) { std::wstringstream wstrStream( i_in); wstrStream >> std::boolalpha >> o_out; }

template< class t_type> void StringToValue( const std::string& i_in, t_type& o_out) { std::stringstream strStream( i_in); strStream >> std::boolalpha >> o_out; }

/* template< class t_string, class t_OutputIterator> void Replace( const t_string& i_str, t_string i_strPattern, t_string i_strNew, t_string& o_str) { t_string::size_type nOffset = 0; while ( (nOffset != i_str::npos) && (nOffset < i_str.size())) { t_string::size_type nOldOffset = nOffset; nOffset = i_str.find_first_of( i_strPattern); if ( nOffset != i_str::npos) { o_str += i_str.substr( nOldOffset, nOffset - nOldOffset); o_str += i_strNew; nOffset += i_strPattern.size(); } else { o_str += i_str.substr( nOldOffset); } } }*/

/* void FormatDateString(unsigned int i_nDate, std::string& o_strOut); void FormatDateString(unsigned int i_nDate, std::wstring& o_wstrOut);

void FormatDateStringHdr(unsigned int i_nDate, std::string& o_strOut); void FormatDateStringHdr(unsigned int i_nDate, std::wstring& o_wstrOut); */
}

std::wostream& operator << ( std::wostream& i_wostream, const std::string& i_str); std::ostream& operator << ( std::ostream& i_ostream, const std::wstring& i_wstr); #endif

Currently browsing [psf.zip] (34,320 bytes) - [TestBase.cpp] - (4,179 bytes)

#include "TestBase.hpp"
#include "Serialize.hpp"
#include "DeSerialize.hpp"

const wchar_t* CSimple::ms_namelist[] = { L"Int", L"Bool", L"Float", 0}; const wchar_t* CComplex::ms_namelist[] = { L"Simple", L"List", 0};

const wchar_t* CBase::ms_namelist[] = { L"BaseMember1", L"BaseMember2", L"BaseMember3", 0}; const wchar_t* CSuper::ms_namelist[] = { L"Member1", L"Member2", 0};

void CTestBase::DefaultInit() { // POD like types m_wstr = L"Text"; m_nInt = -15; m_nUnsignedInt = 42; m_fFloat = 3.14; m_bBool = true;

// Simple class m_simple.SetInt( 84); m_simple.SetBool( false); m_simple.SetFloat( 2.3025f);

// Complex class m_complex.Simple() = m_simple; m_complex.List().insert( m_complex.List().end(), 3, m_simple);

// Super class m_super.BaseMember1() = 16; m_super.BaseMember2() = L"Base Text"; m_super.BaseMember3() = 32; m_super.Member1() = m_simple; m_super.Member2() = 256;

// standard containers int nInts[] = {1, 2, 3, 4, 5, 6, 7}; m_list.insert( m_list.end(), nInts, nInts + 7); m_vector.insert( m_vector.end(), nInts, nInts + 7); m_set.insert( nInts, nInts + 7); m_map[1] = 1; m_map[2] = 2; m_map[3] = 3; m_map[4] = 4; m_map[5] = 5; }

void CTestBase::DoSerialize( PSF::ISerializer& i_serializer) { // Serialize POD types PSF::Serialize( m_wstr, i_serializer, L"string"); PSF::Serialize( m_nInt, i_serializer, L"int"); PSF::Serialize( m_nUnsignedInt, i_serializer, L"unsigned_int"); PSF::Serialize( m_fFloat, i_serializer, L"float"); PSF::Serialize( m_bBool, i_serializer, L"bool");

// serialize simple class PSF::Serialize( m_simple, i_serializer, L"Simple");

// serialize complex class PSF::Serialize( m_complex, i_serializer, L"Complex");

// serialize super class PSF::Serialize( m_super, i_serializer, L"Super");

// serialize list PSF::Serialize( m_list, i_serializer, L"number_list");

// serialize vector PSF::Serialize( m_vector, i_serializer, L"number_vector");

// serialize map PSF::Serialize( m_map, i_serializer, L"number_map");

// serialize set PSF::Serialize( m_set, i_serializer, L"number_set"); }

void CTestBase::DoDeSerialize( PSF::IDeSerializer& i_deserializer) { // Deserialize POD types PSF::DeSerialize( m_wstr, i_deserializer, L"string"); PSF::DeSerialize( m_nInt, i_deserializer, L"int"); PSF::DeSerialize( m_nUnsignedInt, i_deserializer, L"unsigned_int"); PSF::DeSerialize( m_fFloat, i_deserializer, L"float"); PSF::DeSerialize( m_bBool, i_deserializer, L"bool");

// Deserialize simple class PSF::DeSerialize( m_simple, i_deserializer, L"Simple");

// Deserialize complex class PSF::DeSerialize( m_complex, i_deserializer, L"Complex");

// Deserialize super class PSF::DeSerialize( m_super, i_deserializer, L"Super");

// Deserialize list PSF::DeSerialize( m_list, i_deserializer, L"number_list");

// Deserialize vector PSF::DeSerialize( m_vector, i_deserializer, L"number_vector");

// Deserialize map PSF::DeSerialize( m_map, i_deserializer, L"number_map");

// Deserialize set PSF::DeSerialize( m_set, i_deserializer, L"number_set"); }

void CTestBase::DoInverseDeSerialize( PSF::IDeSerializer& i_deserializer) { // Deserialize set PSF::DeSerialize( m_set, i_deserializer, L"number_set");

// Deserialize map PSF::DeSerialize( m_map, i_deserializer, L"number_map");

// Deserialize vector PSF::DeSerialize( m_vector, i_deserializer, L"number_vector");

// Deserialize list PSF::DeSerialize( m_list, i_deserializer, L"number_list");

// Deserialize super class PSF::DeSerialize( m_super, i_deserializer, L"Super");

// Deserialize complex class PSF::DeSerialize( m_complex, i_deserializer, L"Complex");

// Deserialize simple class PSF::DeSerialize( m_simple, i_deserializer, L"Simple");

// Deserialize POD types PSF::DeSerialize( m_bBool, i_deserializer, L"bool"); PSF::DeSerialize( m_fFloat, i_deserializer, L"float"); PSF::DeSerialize( m_nUnsignedInt, i_deserializer, L"unsigned_int"); PSF::DeSerialize( m_nInt, i_deserializer, L"int"); PSF::DeSerialize( m_wstr, i_deserializer, L"string"); }

Currently browsing [psf.zip] (34,320 bytes) - [TestBase.hpp] - (1,599 bytes)

#ifndef PSF_TESTBASE_HPP
#define PSF_TESTBASE_HPP

#include "Compound.hpp" #include "Serializer.hpp" #include "DeSerializer.hpp"

#include <list> #include <vector> #include <map> #include <set> #include <string>

typedef TYPELIST_3(int,bool,float) SimpleTypes; class CSimple : public PSF::TCompound<SimpleTypes,CSimple> { public: ACCESSORS_3( Int, Bool, Float); };

typedef TYPELIST_2(CSimple,std::list<CSimple>) ComplexTypes; class CComplex : public PSF::TCompound<ComplexTypes,CComplex> { public: ACCESSORS_2( Simple, List); };

typedef TYPELIST_3(int,std::wstring,float) BaseTypes; class CBase : public PSF::TCompound<BaseTypes,CBase> { public: ACCESSORS_3( BaseMember1, BaseMember2, BaseMember3); };

typedef TYPELIST_2(CSimple,int) SuperTypes; class CSuper : public PSF::TCompound<SuperTypes,CSuper,CBase> { public: ACCESSORS_2( Member1, Member2); };

class CTestBase { public: void DefaultInit(); void DoSerialize( PSF::ISerializer& i_serializer); void DoDeSerialize( PSF::IDeSerializer& i_deserializer); void DoInverseDeSerialize( PSF::IDeSerializer& i_deserializer);

// Various data types used for testing // POD like types std::wstring m_wstr; int m_nInt; unsigned int m_nUnsignedInt; float m_fFloat; bool m_bBool;

// Simple class CSimple m_simple;

// Complex class CComplex m_complex;

// class that inherits from another compound class CSuper m_super;

// standard containers std::list<int> m_list; std::vector<int> m_vector; std::map<int,int> m_map; std::set<int> m_set; };

#endif

Currently browsing [psf.zip] (34,320 bytes) - [TypeMap.cpp] - (657 bytes)

#include <map>
#include <string>
//#include <utility>

#include "TypeMap.hpp"

DeSerializerTypeMap InvertTypeMap( const SerializerTypeMap& i_map) { DeSerializerTypeMap res; SerializerTypeMap::const_iterator iter; for ( iter = i_map.begin(); iter != i_map.end(); ++iter) { res.insert( std::make_pair( iter->second, iter->first)); } return res; }

SerializerTypeMap InvertTypeMap( const DeSerializerTypeMap& i_map) { SerializerTypeMap res; DeSerializerTypeMap::const_iterator iter; for ( iter = i_map.begin(); iter != i_map.end(); ++iter) { res.insert( std::make_pair( iter->second, iter->first)); } return res; }


Currently browsing [psf.zip] (34,320 bytes) - [TypeMap.hpp] - (124 bytes)

#include "Loki/TypeInfo.h"
#include <map>

namespace PSF { typedef std::map<Loki::TypeInfo,std::wstring> TypeMap; }

Currently browsing [psf.zip] (34,320 bytes) - [XMLDeSerializer.cpp] - (8,837 bytes)

#include "XMLDeSerializer.hpp"
//#include "XMLUtils.hpp"

#include <xercesc/util/PlatformUtils.hpp>
#include <xercesc/util/XMLString.hpp>
#include <xercesc/framework/LocalFileFormatTarget.hpp>
XERCES_CPP_NAMESPACE_USE

#include <exception> #include <sstream> #include <iostream>

template<class t_type> bool DeSerializePOD( t_type& o_out, std::wstringstream& i_wstrstream, const PSF::IChecker& i_checker, size_t& io_nItemsDeSerialized) { i_wstrstream >> std::boolalpha >> o_out; if ( !i_wstrstream.eof() && !i_wstrstream.good()) { i_checker.SematicError(); } if ( i_wstrstream.good()) io_nItemsDeSerialized++; return i_wstrstream.good(); }

XERCES_CPP_NAMESPACE::DOMElement* FindElement( const XERCES_CPP_NAMESPACE::DOMElement* i_pdomElementParent, const std::wstring& i_wstrName) { if ( !i_pdomElementParent) return 0; XERCES_CPP_NAMESPACE::DOMNode* pdomNode; for ( pdomNode = i_pdomElementParent->getFirstChild(); pdomNode != 0; pdomNode = pdomNode->getNextSibling()) { if ( XERCES_CPP_NAMESPACE::DOMNode::ELEMENT_NODE != pdomNode->getNodeType() || i_wstrName != pdomNode->getNodeName()) continue; return (XERCES_CPP_NAMESPACE::DOMElement*)pdomNode; } return 0; }

XERCES_CPP_NAMESPACE::DOMElement* NextElement( const XERCES_CPP_NAMESPACE::DOMElement* i_pdomElement) { if ( !i_pdomElement) return 0; XERCES_CPP_NAMESPACE::DOMNode* pdomNode = i_pdomElement->getNextSibling(); while ( pdomNode && XERCES_CPP_NAMESPACE::DOMNode::ELEMENT_NODE != pdomNode->getNodeType()) { pdomNode = pdomNode->getNextSibling(); }

return (XERCES_CPP_NAMESPACE::DOMElement*)pdomNode; }

////////////////////////////////////////////////////////////////////////////////////// // CXMLDeSerializerBase : Implmentation ////////////////////////////////////////////////////////////////////////////////////// PSF::CXMLDeSerializerBase::CXMLDeSerializerBase( DOMElement* i_pdomElementRoot, bool i_bSearch, const SerializerTypeMap& i_mapType, const IChecker& i_checker) : m_pdomElementRoot( i_pdomElementRoot), m_pdomElementCurrent( 0), m_bSearch( i_bSearch), m_mapType( i_mapType), m_checker( i_checker), m_nItemsDeSerialized( 0), m_nRangeSize( -1), m_wstrstream( std::ios_base::in) { const wchar_t* pszSize = m_pdomElementRoot->getAttribute( L"size"); if ( pszSize) { std::wstringstream wstrstream( pszSize); wstrstream >> m_nRangeSize; }

DOMNode* pdomNode = m_pdomElementRoot->getFirstChild(); while ( pdomNode && DOMNode::ELEMENT_NODE != pdomNode->getNodeType()) { pdomNode = pdomNode->getNextSibling(); }

m_pdomElementCurrent = (DOMElement*)pdomNode;

std::wstring wstrText; for ( pdomNode = m_pdomElementRoot->getFirstChild(); pdomNode != 0; pdomNode = pdomNode->getNextSibling()) { if ( pdomNode->getNodeType() == XERCES_CPP_NAMESPACE::DOMNode::TEXT_NODE) { wstrText += pdomNode->getNodeValue(); } }

m_wstrstream.str( wstrText); }

PSF::CXMLDeSerializerBase::~CXMLDeSerializerBase() { if ( m_nRangeSize != -1) { if ( m_nItemsDeSerialized != m_nRangeSize) { m_checker.RangeSizeError(); // emit range error } } // todo : assert member are null }

XERCES_CPP_NAMESPACE::DOMElement* PSF::CXMLDeSerializerBase::UpdateCurrentElement( const std::wstring& i_wstrName) { while ( m_pdomElementCurrent && i_wstrName != m_pdomElementCurrent->getTagName()) { m_pdomElementCurrent = NextElement( m_pdomElementCurrent); }

if ( !m_pdomElementCurrent && m_bSearch) { m_pdomElementCurrent = FindElement( m_pdomElementRoot, i_wstrName); }

DOMElement* pdomElement = m_pdomElementCurrent; m_pdomElementCurrent = NextElement( m_pdomElementCurrent); return pdomElement; }

bool PSF::CXMLDeSerializerBase::DeSerialize( char& o_out) { wchar_t wch; m_wstrstream >> std::noskipws; bool bResult = DeSerializePOD( wch, m_wstrstream, m_checker, m_nItemsDeSerialized); m_wstrstream >> std::skipws; if ( bResult) o_out = wch; return bResult; }

bool PSF::CXMLDeSerializerBase::DeSerialize( unsigned char& o_out) { short n; bool bResult = DeSerializePOD( n, m_wstrstream, m_checker, m_nItemsDeSerialized); if ( bResult) o_out = n; return bResult; }

bool PSF::CXMLDeSerializerBase::DeSerialize( short& o_out) { return DeSerializePOD( o_out, m_wstrstream, m_checker, m_nItemsDeSerialized); }

bool PSF::CXMLDeSerializerBase::DeSerialize( unsigned short& o_out) { return DeSerializePOD( o_out, m_wstrstream, m_checker, m_nItemsDeSerialized); }

bool PSF::CXMLDeSerializerBase::DeSerialize( int& o_out) { return DeSerializePOD( o_out, m_wstrstream, m_checker, m_nItemsDeSerialized); }

bool PSF::CXMLDeSerializerBase::DeSerialize( unsigned int& o_out) { return DeSerializePOD( o_out, m_wstrstream, m_checker, m_nItemsDeSerialized); }

bool PSF::CXMLDeSerializerBase::DeSerialize( long& o_out) { return DeSerializePOD( o_out, m_wstrstream, m_checker, m_nItemsDeSerialized); }

bool PSF::CXMLDeSerializerBase::DeSerialize( unsigned long& o_out) { return DeSerializePOD( o_out, m_wstrstream, m_checker, m_nItemsDeSerialized); }

bool PSF::CXMLDeSerializerBase::DeSerialize( float& o_out) { return DeSerializePOD( o_out, m_wstrstream, m_checker, m_nItemsDeSerialized); }

bool PSF::CXMLDeSerializerBase::DeSerialize( double& o_out) { return DeSerializePOD( o_out, m_wstrstream, m_checker, m_nItemsDeSerialized); }

bool PSF::CXMLDeSerializerBase::DeSerialize( wchar_t& o_out) { // streams skip whitespace per default, but for strings it is important m_wstrstream >> std::noskipws; bool bResult = DeSerializePOD( o_out, m_wstrstream, m_checker, m_nItemsDeSerialized); m_wstrstream >> std::skipws; return bResult; }

bool PSF::CXMLDeSerializerBase::DeSerialize( bool& o_out) { return DeSerializePOD( o_out, m_wstrstream, m_checker, m_nItemsDeSerialized); }

std::auto_ptr<PSF::IDeSerializer> PSF::CXMLDeSerializerBase::DeSerializeCompound( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type) { DOMElement* pdomElement = UpdateCurrentElement( i_wstrName);

if ( !pdomElement) return std::auto_ptr<IDeSerializer>( 0);

CheckType( pdomElement->getAttribute( L"type"), i_type); m_nItemsDeSerialized++; return std::auto_ptr<IDeSerializer>( new CXMLDeSerializerBase( pdomElement, true, m_mapType, m_checker)); }

std::auto_ptr<PSF::IDeSerializer> PSF::CXMLDeSerializerBase::DeSerializeRange( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type) { DOMElement* pdomElement = UpdateCurrentElement( i_wstrName);

if ( !pdomElement) return std::auto_ptr<IDeSerializer>( 0);

CheckType( pdomElement->getAttribute( L"type"), i_type); m_nItemsDeSerialized++; return std::auto_ptr<IDeSerializer>( new CXMLDeSerializerBase( pdomElement, false, m_mapType, m_checker)); }

void PSF::CXMLDeSerializerBase::CheckType( const std::wstring& i_wstrType, const Loki::TypeInfo& i_type) { if ( i_wstrType.empty()) { // No type info m_checker.TypeMissing(); return; }

SerializerTypeMap::const_iterator iterType = m_mapType.find( i_type); if ( m_mapType.end() == iterType) { // Unknown type m_checker.TypeUnknown(); return; }

if ( iterType->second != i_wstrType) { // Type mismatch m_checker.TypeMismatch(); } }

////////////////////////////////////////////////////////////////////////////////////// // ////////////////////////////////////////////////////////////////////////////////////// PSF::CXMLDeSerializerProxy::CXMLDeSerializerProxy( const std::wstring& i_wstrFilename, const SerializerTypeMap& i_mapType, const IChecker& i_checker) : m_pdomImpl( 0), m_pdomBuilder( 0), m_pdomDoc( 0), m_pBase( 0) { try { XMLPlatformUtils::Initialize(); m_pdomImpl = (DOMImplementation*)DOMImplementationRegistry::getDOMImplementation( L"Core XML 1.0 LS"); m_pdomBuilder = m_pdomImpl->createDOMBuilder( DOMImplementationLS::MODE_SYNCHRONOUS, 0);

m_pdomDoc = m_pdomBuilder->parseURI( i_wstrFilename.c_str());

m_pBase = std::auto_ptr<CXMLDeSerializerBase>( new CXMLDeSerializerBase( m_pdomDoc->getDocumentElement(), true, i_mapType, i_checker)); } catch (const DOMException& toCatch) { char* pszMessage = XMLString::transcode( toCatch.msg); throw std::exception( pszMessage); } catch (const XMLException& toCatch) { char* pszMessage = XMLString::transcode( toCatch.getMessage()); throw std::exception( pszMessage); } }

PSF::CXMLDeSerializerProxy::~CXMLDeSerializerProxy() { m_pdomBuilder->release(); XMLPlatformUtils::Terminate(); }

PSF::CXMLDeSerializerProxy::operator PSF::IDeSerializer&() { return *m_pBase; }

Currently browsing [psf.zip] (34,320 bytes) - [XMLDeSerializer.hpp] - (2,529 bytes)

#ifndef XML_DESERIALIZER_HPP
#define XML_DESERIALIZER_HPP

#include "DeSerializer.hpp" #include "TypeMap.hpp" #include "Checker.hpp"

#include <xercesc/dom/DOM.hpp>

#include <sstream>

namespace PSF { class CXMLDeSerializerBase : public IDeSerializer { public: CXMLDeSerializerBase( XERCES_CPP_NAMESPACE::DOMElement* i_pdomElementRoot, bool i_bSearch, const SerializerTypeMap& i_mapType, const IChecker& i_checker);

virtual ~CXMLDeSerializerBase();

virtual bool DeSerialize( char& o_out); virtual bool DeSerialize( unsigned char& o_out); virtual bool DeSerialize( short& o_out); virtual bool DeSerialize( unsigned short& o_out); virtual bool DeSerialize( int& o_out); virtual bool DeSerialize( unsigned int& o_out); virtual bool DeSerialize( long& o_out); virtual bool DeSerialize( unsigned long& o_out); virtual bool DeSerialize( float& o_out); virtual bool DeSerialize( double& o_out); // same as double // virtual bool DeSerialize( long double& o_out); virtual bool DeSerialize( wchar_t& o_out); virtual bool DeSerialize( bool& o_out);

virtual std::auto_ptr<IDeSerializer> DeSerializeCompound( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type); virtual std::auto_ptr<IDeSerializer> DeSerializeRange( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type); protected: // returns the element used for deserialization and updates the current element XERCES_CPP_NAMESPACE::DOMElement* UpdateCurrentElement( const std::wstring& i_wstrName); void CheckType( const std::wstring& i_wstrType, const Loki::TypeInfo& i_type);

CXMLDeSerializerBase(); XERCES_CPP_NAMESPACE::DOMElement* m_pdomElementRoot; XERCES_CPP_NAMESPACE::DOMElement* m_pdomElementCurrent; bool m_bSearch; const SerializerTypeMap& m_mapType; const IChecker& m_checker; size_t m_nItemsDeSerialized; size_t m_nRangeSize;

std::wstringstream m_wstrstream; };

class CXMLDeSerializerProxy { public: CXMLDeSerializerProxy( const std::wstring& i_wstrFilename, const SerializerTypeMap& i_mapType, const IChecker& i_checker);

virtual ~CXMLDeSerializerProxy();

operator IDeSerializer&();

protected: XERCES_CPP_NAMESPACE::DOMImplementation* m_pdomImpl; XERCES_CPP_NAMESPACE::DOMBuilder* m_pdomBuilder; XERCES_CPP_NAMESPACE::DOMDocument* m_pdomDoc; std::auto_ptr<CXMLDeSerializerBase> m_pBase; }; };

#endif

Currently browsing [psf.zip] (34,320 bytes) - [XMLSerializer.cpp] - (5,961 bytes)

#include "XMLSerializer.hpp"

#include <xercesc/util/PlatformUtils.hpp> #include <xercesc/util/XMLString.hpp> #include <xercesc/framework/LocalFileFormatTarget.hpp> XERCES_CPP_NAMESPACE_USE

#include <exception> #include <sstream> #include <iostream>

template<class t_type> void SerializePOD( const t_type& i_in, std::wstringstream& i_wstrstream, wchar_t i_wcDelimiter = L' ') { i_wstrstream << std::boolalpha << i_in; if ( i_wcDelimiter) i_wstrstream << i_wcDelimiter; }

////////////////////////////////////////////////////////////////////////////////////// // CXMLSerializerBase : Implmentation ////////////////////////////////////////////////////////////////////////////////////// PSF::CXMLSerializerBase::CXMLSerializerBase( DOMDocument* i_pdomDoc, DOMElement* i_pdomElementRoot, const SerializerTypeMap& i_mapType, bool i_bRange) : m_pdomDoc( i_pdomDoc), m_pdomElementRoot( i_pdomElementRoot), m_mapType( i_mapType), m_bRange( i_bRange), m_nItemsSerialized( 0) { }

PSF::CXMLSerializerBase::~CXMLSerializerBase() { XERCES_CPP_NAMESPACE::DOMText* pdomText = m_pdomDoc->createTextNode( m_wstrstream.str().c_str()); m_pdomElementRoot->appendChild( pdomText);

if ( m_bRange) { std::wstringstream wstrstream; wstrstream << m_nItemsSerialized; m_pdomElementRoot->setAttribute( L"size", wstrstream.str().c_str()); } }

void PSF::CXMLSerializerBase::Serialize( const char& i_in) { SerializePOD( i_in, m_wstrstream, 0); m_nItemsSerialized++; }

void PSF::CXMLSerializerBase::Serialize( const unsigned char& i_in) { SerializePOD( i_in, m_wstrstream); m_nItemsSerialized++; }

void PSF::CXMLSerializerBase::Serialize( const short& i_in) { SerializePOD( i_in, m_wstrstream); m_nItemsSerialized++; }

void PSF::CXMLSerializerBase::Serialize( const unsigned short& i_in) { SerializePOD( i_in, m_wstrstream); m_nItemsSerialized++; }

void PSF::CXMLSerializerBase::Serialize( const int& i_in) { SerializePOD( i_in, m_wstrstream); m_nItemsSerialized++; }

void PSF::CXMLSerializerBase::Serialize( const unsigned int& i_in) { SerializePOD( i_in, m_wstrstream); m_nItemsSerialized++; }

void PSF::CXMLSerializerBase::Serialize( const long& i_in) { SerializePOD( i_in, m_wstrstream); m_nItemsSerialized++; }

void PSF::CXMLSerializerBase::Serialize( const unsigned long& i_in) { SerializePOD( i_in, m_wstrstream); m_nItemsSerialized++; }

void PSF::CXMLSerializerBase::Serialize( const float& i_in) { SerializePOD( i_in, m_wstrstream); m_nItemsSerialized++; }

void PSF::CXMLSerializerBase::Serialize( const double& i_in) { SerializePOD( i_in, m_wstrstream); m_nItemsSerialized++; }

void PSF::CXMLSerializerBase::Serialize( const wchar_t& i_in) { SerializePOD( i_in, m_wstrstream, 0); m_nItemsSerialized++; }

void PSF::CXMLSerializerBase::Serialize( const bool& i_in) { SerializePOD( i_in, m_wstrstream); m_nItemsSerialized++; }

std::auto_ptr<PSF::ISerializer> PSF::CXMLSerializerBase::SerializeCompound( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type) { m_nItemsSerialized++; DOMElement* pdomElement = m_pdomDoc->createElement( i_wstrName.c_str()); m_pdomElementRoot->appendChild( pdomElement); SerializerTypeMap::const_iterator iterType = m_mapType.find( i_type); if ( m_mapType.end() != iterType) { pdomElement->setAttribute( L"type", iterType->second.c_str()); }

return std::auto_ptr<ISerializer>( new CXMLSerializerBase( m_pdomDoc, pdomElement, m_mapType, false)); }

std::auto_ptr<PSF::ISerializer> PSF::CXMLSerializerBase::SerializeRange( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type) { m_nItemsSerialized++; DOMElement* pdomElement = m_pdomDoc->createElement( i_wstrName.c_str()); m_pdomElementRoot->appendChild( pdomElement); SerializerTypeMap::const_iterator iterType = m_mapType.find( i_type); if ( m_mapType.end() != iterType) { pdomElement->setAttribute( L"type", iterType->second.c_str()); }

return std::auto_ptr<ISerializer>( new CXMLSerializerBase( m_pdomDoc, pdomElement, m_mapType, true)); }

////////////////////////////////////////////////////////////////////////////////////// // CXMLSerializerProxy : Implmentation ////////////////////////////////////////////////////////////////////////////////////// PSF::CXMLSerializerProxy::CXMLSerializerProxy( const std::wstring& i_wstrFilename, const std::wstring& i_wstrRoot, const SerializerTypeMap& i_mapType) : m_wstrFilename( i_wstrFilename), m_pdomImpl( 0), m_pdomDoc( 0) { try { XMLPlatformUtils::Initialize(); m_pdomImpl = (DOMImplementation*)DOMImplementationRegistry::getDOMImplementation( L"Core XML 1.0 LS"); m_pdomDoc = m_pdomImpl->createDocument( 0, i_wstrRoot.c_str(), 0); XERCES_CPP_NAMESPACE::DOMElement* pdomElementRoot = m_pdomDoc->getDocumentElement(); m_pBase = std::auto_ptr<CXMLSerializerBase>( new CXMLSerializerBase( m_pdomDoc, pdomElementRoot, i_mapType, false)); } catch (const XMLException& toCatch) { char* pszMessage = XMLString::transcode( toCatch.getMessage()); throw std::exception( pszMessage); } }

PSF::CXMLSerializerProxy::~CXMLSerializerProxy() { DOMWriter* pdomWriter = m_pdomImpl->createDOMWriter();

if ( pdomWriter->canSetFeature(XMLUni::fgDOMWRTDiscardDefaultContent, true)) { pdomWriter->setFeature(XMLUni::fgDOMWRTDiscardDefaultContent, true); }

if ( pdomWriter->canSetFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true)) { pdomWriter->setFeature(XMLUni::fgDOMWRTFormatPrettyPrint, true); }

{ LocalFileFormatTarget formatTarget( m_wstrFilename.c_str()); pdomWriter->writeNode( &formatTarget, *m_pdomDoc); }

XMLPlatformUtils::Terminate(); }

PSF::CXMLSerializerProxy::operator PSF::ISerializer&() { return *m_pBase; }

Currently browsing [psf.zip] (34,320 bytes) - [XMLSerializer.hpp] - (2,197 bytes)

#ifndef XML_SERIALIZER_HPP
#define XML_SERIALIZER_HPP

#include "Serializer.hpp" #include "TypeMap.hpp"

#include <xercesc/dom/DOM.hpp>

#include <sstream>

namespace PSF { class CXMLSerializerBase : public ISerializer { public: CXMLSerializerBase( XERCES_CPP_NAMESPACE::DOMDocument* i_pdomDoc, XERCES_CPP_NAMESPACE::DOMElement* i_pdomElementRoot, const SerializerTypeMap& i_mapType, bool i_bRange);

virtual ~CXMLSerializerBase(); virtual void Serialize( const int& i_in); virtual void Serialize( const unsigned int& i_in); virtual void Serialize( const bool& i_in); virtual void Serialize( const char& i_in); virtual void Serialize( const unsigned char& i_in); virtual void Serialize( const short& i_in); virtual void Serialize( const unsigned short& i_in); virtual void Serialize( const long& i_in); virtual void Serialize( const unsigned long& i_in); virtual void Serialize( const float& i_in); virtual void Serialize( const double& i_in); // same as double // virtual void Serialize( const long double& i_in); virtual void Serialize( const wchar_t& i_in);

virtual std::auto_ptr<ISerializer> SerializeCompound( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type); virtual std::auto_ptr<ISerializer> SerializeRange( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type);

protected: CXMLSerializerBase(); XERCES_CPP_NAMESPACE::DOMDocument* m_pdomDoc; XERCES_CPP_NAMESPACE::DOMElement* m_pdomElementRoot; const SerializerTypeMap& m_mapType; size_t m_nItemsSerialized; bool m_bRange; std::wstringstream m_wstrstream; };

class CXMLSerializerProxy { public: CXMLSerializerProxy( const std::wstring& i_wstrFilename, const std::wstring& i_wstrRoot, const SerializerTypeMap& i_mapType = SerializerTypeMap());

virtual ~CXMLSerializerProxy();

operator ISerializer&();

protected: std::wstring m_wstrFilename; XERCES_CPP_NAMESPACE::DOMImplementation* m_pdomImpl; XERCES_CPP_NAMESPACE::DOMDocument* m_pdomDoc; std::auto_ptr<CXMLSerializerBase> m_pBase; }; };

#endif

Currently browsing [psf.zip] (34,320 bytes) - [assert.hpp] - (170 bytes)

#ifndef PSF_ASSERT_H
#define PSF_ASSERT_H

#ifdef NDEBUG #define PSF_ASSERT( __EXP) #else #define PSF_ASSERT( __EXP) { if (!(__EXP)) _asm{ int 3}} #endif

#endif

Currently browsing [psf.zip] (34,320 bytes) - [BinaryDeSerializer.cpp] - (4,623 bytes)

#include "BinaryDeSerializer.hpp"
#include "BinaryUtils.hpp"

#include <iostream>

template<class t_type> bool DeSerializePOD( t_type& o_out, std::istream& i_istream, std::streamsize i_nMaxPos) { // Check if stream is valid if ( !i_istream) return false; // Check if the current position has exceeded the specified size for this chunk if ( i_istream.tellg() >= i_nMaxPos) return false;

std::streamsize nTypeSize = sizeof( t_type); // i_istream.read( reinterpret_cast<char*>(&nTypeSize), 4); i_istream.read( reinterpret_cast<char*>(&o_out), nTypeSize); return i_istream.good(); }

////////////////////////////////////////////////////////////////////////////////////// // CBinaryDeSerializer : Implmentation ////////////////////////////////////////////////////////////////////////////////////// PSF::CBinaryDeSerializer::CBinaryDeSerializer( std::istream& i_istream, bool i_bSearch) : m_istream( i_istream), m_bSearch( i_bSearch) { m_istream.read( reinterpret_cast<char*>(&m_nSize), 4); m_nStartPos = m_istream.tellg(); }

PSF::CBinaryDeSerializer::~CBinaryDeSerializer() { // seek to end when done m_istream.seekg( m_nStartPos + m_nSize, std::ios_base::beg); }

bool PSF::CBinaryDeSerializer::FindElement( const std::wstring& i_wstrName) { // Check if stream is valid if ( !m_istream) return false;

unsigned int nNameHash = HashString( i_wstrName);

// Check if the current position has exceeded the specified size for this chunk if ( m_istream.tellg() < m_nStartPos + m_nSize) { // Check if the current position matches the requested unsigned int nReadHash; m_istream.read( reinterpret_cast<char*>(&nReadHash), 4); if ( nReadHash == nNameHash) return true; }

// The position didn't match can we search? if ( !m_bSearch) return false;

// seek to start of block when starting search m_istream.seekg( m_nStartPos, std::ios_base::beg);

// Iterate through blocks while ( &m_istream && m_istream.tellg() < m_nStartPos + m_nSize) { unsigned int nReadHash; m_istream.read( reinterpret_cast<char*>(&nReadHash), 4); if ( nReadHash == nNameHash) return true; unsigned int nSkip; m_istream.read( reinterpret_cast<char*>(&nSkip), 4); m_istream.seekg( nSkip, std::ios_base::cur); }

return false; }

bool PSF::CBinaryDeSerializer::DeSerialize( int& o_out) { return DeSerializePOD( o_out, m_istream, m_nStartPos + m_nSize); }

bool PSF::CBinaryDeSerializer::DeSerialize( unsigned int& o_out) { return DeSerializePOD( o_out, m_istream, m_nStartPos + m_nSize); }

bool PSF::CBinaryDeSerializer::DeSerialize( bool& o_out) { return DeSerializePOD( o_out, m_istream, m_nStartPos + m_nSize); }

bool PSF::CBinaryDeSerializer::DeSerialize( char& o_out) { return DeSerializePOD( o_out, m_istream, m_nStartPos + m_nSize); }

bool PSF::CBinaryDeSerializer::DeSerialize( unsigned char& o_out) { return DeSerializePOD( o_out, m_istream, m_nStartPos + m_nSize); }

bool PSF::CBinaryDeSerializer::DeSerialize( short& o_out) { return DeSerializePOD( o_out, m_istream, m_nStartPos + m_nSize); }

bool PSF::CBinaryDeSerializer::DeSerialize( unsigned short& o_out) { return DeSerializePOD( o_out, m_istream, m_nStartPos + m_nSize); }

bool PSF::CBinaryDeSerializer::DeSerialize( long& o_out) { return DeSerializePOD( o_out, m_istream, m_nStartPos + m_nSize); }

bool PSF::CBinaryDeSerializer::DeSerialize( unsigned long& o_out) { return DeSerializePOD( o_out, m_istream, m_nStartPos + m_nSize); }

bool PSF::CBinaryDeSerializer::DeSerialize( float& o_out) { return DeSerializePOD( o_out, m_istream, m_nStartPos + m_nSize); }

bool PSF::CBinaryDeSerializer::DeSerialize( double& o_out) { return DeSerializePOD( o_out, m_istream, m_nStartPos + m_nSize); }

bool PSF::CBinaryDeSerializer::DeSerialize( wchar_t& o_out) { return DeSerializePOD( o_out, m_istream, m_nStartPos + m_nSize); }

std::auto_ptr<PSF::IDeSerializer> PSF::CBinaryDeSerializer::DeSerializeCompound( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type) { if ( !FindElement( i_wstrName)) return std::auto_ptr<IDeSerializer>( 0); return std::auto_ptr<IDeSerializer>( new CBinaryDeSerializer( m_istream, true)); }

std::auto_ptr<PSF::IDeSerializer> PSF::CBinaryDeSerializer::DeSerializeRange( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type) { if ( !FindElement( i_wstrName)) return std::auto_ptr<IDeSerializer>( 0); return std::auto_ptr<IDeSerializer>( new CBinaryDeSerializer( m_istream, false)); }

Currently browsing [psf.zip] (34,320 bytes) - [BinaryDeSerializer.hpp] - (1,363 bytes)

#ifndef PSF_BINARY_DESERIALIZER_HPP
#define PSF_BINARY_DESERIALIZER_HPP

#include "DeSerializer.hpp"

namespace PSF { class CBinaryDeSerializer : public IDeSerializer { public: CBinaryDeSerializer( std::istream& i_istream, bool i_bSearch = true); virtual ~CBinaryDeSerializer();

virtual bool DeSerialize( int& o_out); virtual bool DeSerialize( unsigned int& o_out); virtual bool DeSerialize( bool& o_out); virtual bool DeSerialize( char& o_out); virtual bool DeSerialize( unsigned char& o_out); virtual bool DeSerialize( short& o_out); virtual bool DeSerialize( unsigned short& o_out); virtual bool DeSerialize( long& o_out); virtual bool DeSerialize( unsigned long& o_out); virtual bool DeSerialize( float& o_out); virtual bool DeSerialize( double& o_out); // same as double // virtual bool DeSerialize( long double& o_out); virtual bool DeSerialize( wchar_t& o_out);

virtual std::auto_ptr<IDeSerializer> DeSerializeCompound( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type); virtual std::auto_ptr<IDeSerializer> DeSerializeRange( const std::wstring& i_wstrName, const Loki::TypeInfo& i_typeRange); protected: bool FindElement( const std::wstring& i_wstrName);

std::istream& m_istream; size_t m_nStartPos; unsigned int m_nSize; bool m_bSearch; }; }; #endif

Currently browsing [psf.zip] (34,320 bytes) - [BinarySerializer.cpp] - (2,953 bytes)

#include "BinarySerializer.hpp"
#include "BinaryUtils.hpp"

#include <iostream>

template<class t_type> void SerializePOD( const t_type& i_in, std::ostream& i_ostream) { std::streamsize nTypeSize = sizeof(t_type); //i_ostream.write( reinterpret_cast<const char*>(&nTypeSize), 4); i_ostream.write( reinterpret_cast<const char*>(&i_in), nTypeSize); }

////////////////////////////////////////////////////////////////////////////////////// // CBinarySerializer : Implmentation ////////////////////////////////////////////////////////////////////////////////////// PSF::CBinarySerializer::CBinarySerializer( std::ostream& i_ostream) : m_ostream( i_ostream), m_nStartPos( i_ostream.tellp()) { unsigned int nTypeSize = 0; m_ostream.write( reinterpret_cast<const char*>(&nTypeSize), 4); }

PSF::CBinarySerializer::~CBinarySerializer() { size_t nEndPos = m_ostream.tellp(); size_t nTypeSize = nEndPos - m_nStartPos - 4; m_ostream.seekp( m_nStartPos, std::ios_base::beg); m_ostream.write( reinterpret_cast<const char*>(&nTypeSize), 4); m_ostream.seekp( nEndPos, std::ios_base::beg); }

void PSF::CBinarySerializer::Serialize( const int& i_in) { SerializePOD( i_in, m_ostream); }

void PSF::CBinarySerializer::Serialize( const unsigned int& i_in) { SerializePOD( i_in, m_ostream); }

void PSF::CBinarySerializer::Serialize( const bool& i_in) { SerializePOD( i_in, m_ostream); }

void PSF::CBinarySerializer::Serialize( const char& i_in) { SerializePOD( i_in, m_ostream); }

void PSF::CBinarySerializer::Serialize( const unsigned char& i_in) { SerializePOD( i_in, m_ostream); }

void PSF::CBinarySerializer::Serialize( const short& i_in) { SerializePOD( i_in, m_ostream); }

void PSF::CBinarySerializer::Serialize( const unsigned short& i_in) { SerializePOD( i_in, m_ostream); }

void PSF::CBinarySerializer::Serialize( const long& i_in) { SerializePOD( i_in, m_ostream); }

void PSF::CBinarySerializer::Serialize( const unsigned long& i_in) { SerializePOD( i_in, m_ostream); }

void PSF::CBinarySerializer::Serialize( const float& i_in) { SerializePOD( i_in, m_ostream); }

void PSF::CBinarySerializer::Serialize( const double& i_in) { SerializePOD( i_in, m_ostream); }

void PSF::CBinarySerializer::Serialize( const wchar_t& i_in) { SerializePOD( i_in, m_ostream); }

std::auto_ptr<PSF::ISerializer> PSF::CBinarySerializer::SerializeCompound( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type) { unsigned int nNameHash = HashString( i_wstrName); m_ostream.write( reinterpret_cast<const char*>(&nNameHash), 4); return std::auto_ptr<ISerializer>( new CBinarySerializer( m_ostream)); }

std::auto_ptr<PSF::ISerializer> PSF::CBinarySerializer::SerializeRange( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type) { return SerializeCompound( i_wstrName, i_type); }


Currently browsing [psf.zip] (34,320 bytes) - [BinarySerializer.hpp] - (1,252 bytes)

#ifndef PSF_BINARY_SERIALIZER_HPP
#define PSF_BINARY_SERIALIZER_HPP

#include "Serializer.hpp"

namespace PSF { class CBinarySerializer : public ISerializer { public: CBinarySerializer( std::ostream& i_ostream); virtual ~CBinarySerializer(); virtual void Serialize( const int& i_in); virtual void Serialize( const unsigned int& i_in); virtual void Serialize( const bool& i_in); virtual void Serialize( const char& i_in); virtual void Serialize( const unsigned char& i_in); virtual void Serialize( const short& i_in); virtual void Serialize( const unsigned short& i_in); virtual void Serialize( const long& i_in); virtual void Serialize( const unsigned long& i_in); virtual void Serialize( const float& i_in); virtual void Serialize( const double& i_in); // same as double // virtual void Serialize( const long double& i_in); virtual void Serialize( const wchar_t& i_in);

virtual std::auto_ptr<ISerializer> SerializeCompound( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type); virtual std::auto_ptr<ISerializer> SerializeRange( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type); protected: std::ostream& m_ostream; size_t m_nStartPos; }; };

#endif

Currently browsing [psf.zip] (34,320 bytes) - [BinaryUtils.cpp] - (288 bytes)

#include <string>
#include "BinaryUtils.hpp"

unsigned int PSF::HashString( const std::wstring& i_wstr) { unsigned int nHash = 0; for ( std::wstring::const_iterator iter = i_wstr.begin(); i_wstr.end() != iter; ++iter) { nHash = (nHash << 1)^*iter; } return nHash; }

Currently browsing [psf.zip] (34,320 bytes) - [BinaryUtils.hpp] - (148 bytes)

#ifndef PSF_BINARY_UTILS_HPP
#define PSF_BINARY_UTILS_HPP

namespace PSF { unsigned int HashString( const std::wstring& i_wstr); };

#endif

Currently browsing [psf.zip] (34,320 bytes) - [Checker.cpp] - (1,821 bytes)

#include "Checker.hpp"
#include "assert.hpp"
#include <stdexcept>

//////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////// PSF::CCheckerNull::~CCheckerNull() { }

void PSF::CCheckerNull::TypeMissing() const { }

void PSF::CCheckerNull::TypeMismatch() const { }

void PSF::CCheckerNull::TypeUnknown() const { }

void PSF::CCheckerNull::SematicError() const { }

void PSF::CCheckerNull::RangeSizeError() const { }

//////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////// PSF::CCheckerAssert::~CCheckerAssert() { }

void PSF::CCheckerAssert::TypeMissing() const { PSF_ASSERT(false); }

void PSF::CCheckerAssert::TypeMismatch() const { PSF_ASSERT(false); }

void PSF::CCheckerAssert::TypeUnknown() const { PSF_ASSERT(false); }

void PSF::CCheckerAssert::SematicError() const { PSF_ASSERT(false); }

void PSF::CCheckerAssert::RangeSizeError() const { PSF_ASSERT(false); }

//////////////////////////////////////////////////////////////// // //////////////////////////////////////////////////////////////// PSF::CCheckerException::~CCheckerException() { }

void PSF::CCheckerException::TypeMissing() const { throw std::runtime_error( "Type missing"); }

void PSF::CCheckerException::TypeMismatch() const { throw std::runtime_error( "Type mismath"); }

void PSF::CCheckerException::TypeUnknown() const { throw std::runtime_error( "Type unknown"); }

void PSF::CCheckerException::SematicError() const { throw std::runtime_error( "Sematic error"); }

void PSF::CCheckerException::RangeSizeError() const { throw std::runtime_error( "Range size error"); }

Currently browsing [psf.zip] (34,320 bytes) - [Checker.hpp] - (2,237 bytes)

#ifndef CHECKER_HPP
#define CHECKER_HPP

#include <string> #include <memory> #include "Loki/TypeInfo.h"

namespace PSF { class IChecker { public: virtual ~IChecker() {};

// Type Error reporting functions // TypeMissing indicates that no type info was present in the data stream virtual void TypeMissing() const = 0; // TypeMismatch indicates that the type of data in the stream does not match // the type supplied by the program virtual void TypeMismatch() const = 0; // TypeUnknown indicates that the program failed to supply a type identifier virtual void TypeUnknown() const = 0; // SemanticError indicates that the format of data in the stream does not // match the expected type, fx. the stream contains the string 'abc', but // the program expects an integer virtual void SematicError() const = 0;

// Range Error reporting functions // RangeSizeError indicates that an error occured while deserializing a range. // The expected size of the range did not match the number of items deserialized // from that range virtual void RangeSizeError() const = 0; };

class CCheckerNull : public IChecker { public: virtual ~CCheckerNull();

// Type Error reporting functions virtual void TypeMissing() const; virtual void TypeMismatch() const; virtual void TypeUnknown() const; virtual void SematicError() const;

// Range Error reporting functions virtual void RangeSizeError() const; };

class CCheckerAssert : public IChecker { public: virtual ~CCheckerAssert();

// Type Error reporting functions virtual void TypeMissing() const; virtual void TypeMismatch() const; virtual void TypeUnknown() const; virtual void SematicError() const;

// Range Error reporting functions virtual void RangeSizeError() const; };

class CCheckerException : public IChecker { public: virtual ~CCheckerException();

// Type Error reporting functions virtual void TypeMissing() const; virtual void TypeMismatch() const; virtual void TypeUnknown() const; virtual void SematicError() const;

// Range Error reporting functions virtual void RangeSizeError() const; }; }; #endif

Currently browsing [psf.zip] (34,320 bytes) - [CompilerConfig.hpp] - (151 bytes)

#ifndef PSF_COMPILER_CONFIG_HPP
#define PSF_COMPILER_CONFIG_HPP

#if (_MSC_VER > 1300) # define PSF_PARTIAL_ORDERING_OF_TEMPLATES #endif

#endif

Currently browsing [psf.zip] (34,320 bytes) - [Compound.hpp] - (5,285 bytes)

#ifndef COMPOUND_HPP
#define COMPOUND_HPP

#include "Loki/Typelist.h" #include "Loki/HierarchyGenerators.h"

namespace PSF { template<class t_type> class TValue { public: t_type m_value; };

template<class t_TypeList,class t_Unique,class t_Base = Loki::EmptyType> class TCompound : public Loki::GenScatterHierarchy< t_TypeList, TValue>, public t_Base { public: enum { number_of_members = Loki::TL::Length<t_TypeList>::value}; static const wchar_t* ms_namelist[]; typedef t_TypeList TypeList; typedef t_Unique Unique; typedef t_Base Base; typedef Loki::GenScatterHierarchy< t_TypeList, TValue> ScatterBase; template<unsigned int t_i> Loki::TL::TypeAt<TypeList,t_i>::Result& Member() { return Loki::Field<t_i>(static_cast<ScatterBase&>(*this)).m_value; }

template<unsigned int t_i> const Loki::TL::TypeAt<TypeList,t_i>::Result& Member() const { return Loki::Field<t_i>(static_cast<const ScatterBase&>(*this)).m_value; } }; };

#define ACCESSORS_1(T1)\ const Loki::TL::TypeAt<TypeList,0>::Result& T1() const { return Member<0>();}\ Loki::TL::TypeAt<TypeList,0>::Result& T1() { return Member<0>();}\ const Loki::TL::TypeAt<TypeList,0>::Result& Get##T1() const { return Member<0>();}\ void Set##T1( const Loki::TL::TypeAt<TypeList,0>::Result& i_in) { Member<0>() = i_in;}

#define ACCESSORS_2(T1,T2) ACCESSORS_1(T1)\ const Loki::TL::TypeAt<TypeList,1>::Result& T2() const { return Member<1>();}\ Loki::TL::TypeAt<TypeList,1>::Result& T2() { return Member<1>();}\ const Loki::TL::TypeAt<TypeList,1>::Result& Get##T2() const { return Member<1>();}\ void Set##T2( const Loki::TL::TypeAt<TypeList,1>::Result& i_in) { Member<1>() = i_in;}

#define ACCESSORS_3(T1,T2,T3) ACCESSORS_2(T1,T2)\ const Loki::TL::TypeAt<TypeList,2>::Result& T3() const { return Member<2>();}\ Loki::TL::TypeAt<TypeList,2>::Result& T3() { return Member<2>();}\ const Loki::TL::TypeAt<TypeList,2>::Result& Get##T3() const { return Member<2>();}\ void Set##T3( const Loki::TL::TypeAt<TypeList,2>::Result& i_in) { Member<2>() = i_in;}

#define ACCESSORS_4(T1,T2,T3,T4) ACCESSORS_3(T1,T2,T3)\ const Loki::TL::TypeAt<TypeList,3>::Result& T4() const { return Member<3>();}\ Loki::TL::TypeAt<TypeList,3>::Result& T4() { return Member<3>();}\ const Loki::TL::TypeAt<TypeList,3>::Result& Get##T4() const { return Member<3>();}\ void Set##T4( const Loki::TL::TypeAt<TypeList,3>::Result& i_in) { Member<3>() = i_in;}

#define ACCESSORS_5(T1,T2,T3,T4,T5) ACCESSORS_4(T1,T2,T3,T4)\ const Loki::TL::TypeAt<TypeList,4>::Result& T5() const { return Member<4>();}\ Loki::TL::TypeAt<TypeList,4>::Result& T5() { return Member<4>();}\ const Loki::TL::TypeAt<TypeList,4>::Result& Get##T5() const { return Member<4>();}\ void Set##T5( const Loki::TL::TypeAt<TypeList,4>::Result& i_in) { Member<4>() = i_in;}

#define ACCESSORS_6(T1,T2,T3,T4,T5,T6) ACCESSORS_5(T1,T2,T3,T4,T5)\ const Loki::TL::TypeAt<TypeList,5>::Result& T6() const { return Member<5>();}\ Loki::TL::TypeAt<TypeList,5>::Result& T6() { return Member<5>();}\ const Loki::TL::TypeAt<TypeList,5>::Result& Get##T6() const { return Member<5>();}\ void Set##T6( const Loki::TL::TypeAt<TypeList,5>::Result& i_in) { Member<5>() = i_in;}

#define ACCESSORS_7(T1,T2,T3,T4,T5,T6,T7) ACCESSORS_6(T1,T2,T3,T4,T5,T6)\ const Loki::TL::TypeAt<TypeList,6>::Result& T7() const { return Member<6>();}\ Loki::TL::TypeAt<TypeList,6>::Result& T7() { return Member<6>();}\ const Loki::TL::TypeAt<TypeList,6>::Result& Get##T7() const { return Member<6>();}\ void Set##T7( const Loki::TL::TypeAt<TypeList,6>::Result& i_in) { Member<6>() = i_in;}

#define ACCESSORS_8(T1,T2,T3,T4,T5,T6,T7,T8) ACCESSORS_7(T1,T2,T3,T4,T5,T6,T7)\ const Loki::TL::TypeAt<TypeList,7>::Result& T8() const { return Member<7>();}\ Loki::TL::TypeAt<TypeList,7>::Result& T8() { return Member<7>();}\ const Loki::TL::TypeAt<TypeList,7>::Result& Get##T8() const { return Member<7>();}\ void Set##T8( const Loki::TL::TypeAt<TypeList,7>::Result& i_in) { Member<7>() = i_in;}

#define ACCESSORS_9(T1,T2,T3,T4,T5,T6,T7,T8,T9) ACCESSORS_8(T1,T2,T3,T4,T5,T6,T7,T8)\ const Loki::TL::TypeAt<TypeList,8>::Result& T9() const { return Member<8>();}\ Loki::TL::TypeAt<TypeList,8>::Result& T9() { return Member<8>();}\ const Loki::TL::TypeAt<TypeList,8>::Result& Get##T9() const { return Member<8>();}\ void Set##T9( const Loki::TL::TypeAt<TypeList,8>::Result& i_in) { Member<8>() = i_in;}

#define ACCESSORS_10(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10) ACCESSORS_9(T1,T2,T3,T4,T5,T6,T7,T8,T9)\ const Loki::TL::TypeAt<TypeList,9>::Result& T10() const { return Member<9>();}\ Loki::TL::TypeAt<TypeList,9>::Result& T10() { return Member<9>();}\ const Loki::TL::TypeAt<TypeList,9>::Result& Get##T10() const { return Member<9>();}\ void Set##T10( const Loki::TL::TypeAt<TypeList,9>::Result& i_in) { Member<9>() = i_in;}

#define ACCESSORS_11(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10,T11) ACCESSORS_10(T1,T2,T3,T4,T5,T6,T7,T8,T9,T10)\ const Loki::TL::TypeAt<TypeList,10>::Result& T11() const { return Member<10>();}\ Loki::TL::TypeAt<TypeList,10>::Result& T11() { return Member<10>();}\ const Loki::TL::TypeAt<TypeList,10>::Result& Get##T11() const { return Member<10>();}\ void Set##T11( const Loki::TL::TypeAt<TypeList,10>::Result& i_in) { Member<10>() = i_in;}

#endif

Currently browsing [psf.zip] (34,320 bytes) - [DeSerialize.cpp] - (5,583 bytes)

#include "DeSerialize.hpp"

#ifndef PSF_PARTIAL_ORDERING_OF_TEMPLATES

// POD DeSerializers bool PSF::DeSerialize( char& o_out, IDeSerializer& i_deserializer) { return i_deserializer.DeSerialize( o_out); }

bool PSF::DeSerialize( unsigned char& o_out, IDeSerializer& i_deserializer) { return i_deserializer.DeSerialize( o_out); }

bool PSF::DeSerialize( short& o_out, IDeSerializer& i_deserializer) { return i_deserializer.DeSerialize( o_out); }

bool PSF::DeSerialize( unsigned short& o_out, IDeSerializer& i_deserializer) { return i_deserializer.DeSerialize( o_out); }

bool PSF::DeSerialize( int& o_out, IDeSerializer& i_deserializer) { return i_deserializer.DeSerialize( o_out); }

bool PSF::DeSerialize( unsigned int& o_out, IDeSerializer& i_deserializer) { return i_deserializer.DeSerialize( o_out); }

bool PSF::DeSerialize( long& o_out, IDeSerializer& i_deserializer) { return i_deserializer.DeSerialize( o_out); }

bool PSF::DeSerialize( unsigned long& o_out, IDeSerializer& i_deserializer) { return i_deserializer.DeSerialize( o_out); }

bool PSF::DeSerialize( float& o_out, IDeSerializer& i_deserializer) { return i_deserializer.DeSerialize( o_out); }

bool PSF::DeSerialize( double& o_out, IDeSerializer& i_deserializer) { return i_deserializer.DeSerialize( o_out); }

bool PSF::DeSerialize( bool& o_out, IDeSerializer& i_deserializer) { return i_deserializer.DeSerialize( o_out); }

bool PSF::DeSerialize( wchar_t& o_out, IDeSerializer& i_deserializer) { return i_deserializer.DeSerialize( o_out); }



bool PSF::DeSerialize( char& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(char)))); if ( !pDeSerializer.get()) return false;

return DeSerialize( o_out, *pDeSerializer); }

bool PSF::DeSerialize( unsigned char& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(unsigned char)))); if ( !pDeSerializer.get()) return false;

return DeSerialize( o_out, *pDeSerializer); }

bool PSF::DeSerialize( short& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(short)))); if ( !pDeSerializer.get()) return false;

return DeSerialize( o_out, *pDeSerializer); }

bool PSF::DeSerialize( unsigned short& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(unsigned short)))); if ( !pDeSerializer.get()) return false;

return DeSerialize( o_out, *pDeSerializer); }

bool PSF::DeSerialize( int& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(int)))); if ( !pDeSerializer.get()) return false;

return DeSerialize( o_out, *pDeSerializer); }

bool PSF::DeSerialize( unsigned int& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(unsigned int)))); if ( !pDeSerializer.get()) return false;

return DeSerialize( o_out, *pDeSerializer); }

bool PSF::DeSerialize( long& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(long)))); if ( !pDeSerializer.get()) return false;

return DeSerialize( o_out, *pDeSerializer); }

bool PSF::DeSerialize( unsigned long& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(unsigned long)))); if ( !pDeSerializer.get()) return false;

return DeSerialize( o_out, *pDeSerializer); }

bool PSF::DeSerialize( float& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(float)))); if ( !pDeSerializer.get()) return false;

return DeSerialize( o_out, *pDeSerializer); }

bool PSF::DeSerialize( double& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(double)))); if ( !pDeSerializer.get()) return false;

return DeSerialize( o_out, *pDeSerializer); }

bool PSF::DeSerialize( bool& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(bool)))); if ( !pDeSerializer.get()) return false;

return DeSerialize( o_out, *pDeSerializer); }

bool PSF::DeSerialize( wchar_t& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(wchar_t)))); if ( !pDeSerializer.get()) return false;

return DeSerialize( o_out, *pDeSerializer); } #endif

Currently browsing [psf.zip] (34,320 bytes) - [DeSerialize.hpp] - (8,515 bytes)

#ifndef DESERIALIZE_HPP
#define DESERIALIZE_HPP

#include <list> #include <vector> #include <map> #include <set>

#include "DeSerializer.hpp" #include "Assert.hpp"

#include "Compound.hpp"

#include "CompilerConfig.hpp"

namespace PSF { // DeSerialize PSF compunds template<class t_TypeList,class t_Unique,class t_Base> class TCompoundDeSerializer { public: typedef TCompound<t_TypeList,t_Unique,t_Base> Compound;

TCompoundDeSerializer( Compound& i_compound) : m_compound( i_compound) { }

bool DeSerialize( IDeSerializer& i_deserializer) { TInner<Compound> inner( m_compound); return inner.DeSerialize( i_deserializer); }

private: template<class t_InnerCompund> class TInner { public: TInner( t_InnerCompund& i_innerCompound) : m_innerCompound( i_innerCompound) { }

bool DeSerialize( IDeSerializer& i_deserializer) { TInner<t_InnerCompund::Base> inner( m_innerCompound); bool bResult = inner.DeSerialize( i_deserializer); return bResult && DeSerializeInternal<0>( i_deserializer); } private:

enum { number_of_loops = t_InnerCompund::number_of_members-1};

template<int t_i> bool DeSerializeInternal( IDeSerializer& i_deserializer) { PSF_ASSERT( t_InnerCompund::ms_namelist[t_i]); const wchar_t* pwszName = t_InnerCompund::ms_namelist[t_i]; bool bResult = PSF::DeSerialize( m_innerCompound.Member<t_i>(), i_deserializer, pwszName); return bResult && DeSerializeInternal<t_i+1>( i_deserializer); }

template<> bool DeSerializeInternal<number_of_loops>( IDeSerializer& i_deserializer) { PSF_ASSERT( t_InnerCompund::ms_namelist[number_of_loops]); const wchar_t* pwszName = t_InnerCompund::ms_namelist[number_of_loops]; return PSF::DeSerialize( m_innerCompound.Member<number_of_loops>(), i_deserializer, pwszName); }

t_InnerCompund& m_innerCompound; };

template<> class TInner<Loki::EmptyType> { public: TInner<Loki::EmptyType>( const t_InnerCompund& i_innerCompound) { }

bool DeSerialize( IDeSerializer& i_deserializer) const { return true; } };

Compound& m_compound; };

template<class t_TypeList,class t_Unique,class t_Base> bool DeSerialize( TCompound<t_TypeList,t_Unique,t_Base>& o_compound, IDeSerializer& i_deserializer, const std::wstring& i_wstrName = L"Compound") { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, typeid(t_Unique))); if ( !pDeSerializer.get()) return false; TCompoundDeSerializer<t_TypeList,t_Unique,t_Base> compoundDeSerializer( o_compound); return compoundDeSerializer.DeSerialize( *pDeSerializer); } };

namespace PSF { template<class t_iteratorType> bool DeSerializeRange( t_iteratorType o_iter, IDeSerializer& i_deserializer, const std::wstring& i_wstrName, const Loki::TypeInfo& i_typeInfo) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeRange( i_wstrName, i_typeInfo)); if ( !pDeSerializer.get()) return false;

t_iteratorType::container_type::value_type val; while ( DeSerialize( val, *pDeSerializer)) { *o_iter = val; o_iter++; } return true; }

// DeSerializers for common containers template<typename t_type> bool DeSerialize( std::basic_string<t_type>& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName = L"String") { std::back_insert_iterator<std::basic_string<t_type> > iter( o_out); return DeSerializeRange( iter, i_deserializer, i_wstrName, Loki::TypeInfo(typeid(std::basic_string<void>))); }

template<typename t_type> bool DeSerialize( std::list<t_type>& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName = L"List") { std::back_insert_iterator<std::list<t_type> > iter( o_out); return DeSerializeRange( iter, i_deserializer, i_wstrName, Loki::TypeInfo(typeid(std::list<void>))); }

template<typename t_type> bool DeSerialize( std::vector<t_type>& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName = L"Vector") { std::back_insert_iterator<std::vector<t_type> > iter( o_out); return DeSerializeRange( iter, i_deserializer, i_wstrName, Loki::TypeInfo(typeid(std::vector<void>))); }

template<typename t_type> bool DeSerialize( std::set<t_type>& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName = L"Set") { std::insert_iterator<std::set<t_type> > iter( o_out, o_out.end()); return DeSerializeRange( iter, i_deserializer, i_wstrName, Loki::TypeInfo(typeid(std::set<void>))); }

template<typename T> T& UnConst( const T& i_in) { return const_cast<T&>( i_in); }

template<typename t_typeFirst,typename t_typeSecond> bool DeSerialize( std::pair<t_typeFirst,t_typeSecond>& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName = L"Pair") { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(std::pair<void,void>)))); if ( !pDeSerializer.get()) return false;

// Should be compile-time const removal, but sadly that does not seem // to be possible i VC7 return DeSerialize( UnConst(o_out.first), *pDeSerializer, L"First") && DeSerialize( o_out.second, *pDeSerializer, L"Second"); }

template<typename t_key,typename t_type> bool DeSerialize( std::map<t_key,t_type>& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName = L"Map") { std::insert_iterator<std::map<t_key,t_type> > iter( o_out, o_out.end()); return DeSerializeRange( iter, i_deserializer, i_wstrName, Loki::TypeInfo(typeid(std::map<void,void>))); }

// POD DeSerializers #ifdef PSF_PARTIAL_ORDERING_OF_TEMPLATES // Template serializers template<typename t_type> bool PSF::DeSerialize( t_type& o_out, IDeSerializer& i_deserializer) { return i_deserializer.DeSerialize( o_out); }

template<typename t_type> bool PSF::DeSerialize( t_type& o_out, IDeSerializer& i_deserializer, const std::wstring& i_wstrName) { std::auto_ptr<IDeSerializer> pDeSerializer( i_deserializer.DeSerializeCompound( i_wstrName, Loki::TypeInfo(typeid(t_type)))); if ( !pDeSerializer.get()) return false; return DeSerialize( o_out, *pDeSerializer); } #else // Explicit deserializers (for compilers that does not support partial ordering of templates) bool DeSerialize( char& i_in, IDeSerializer& i_deserializer); bool DeSerialize( unsigned char& i_in, IDeSerializer& i_deserializer); bool DeSerialize( short& i_in, IDeSerializer& i_deserializer); bool DeSerialize( unsigned short& i_in, IDeSerializer& i_deserializer); bool DeSerialize( int& i_in, IDeSerializer& i_deserializer); bool DeSerialize( unsigned int& i_in, IDeSerializer& i_deserializer); bool DeSerialize( long& i_in, IDeSerializer& i_deserializer); bool DeSerialize( unsigned long& i_in, IDeSerializer& i_deserializer); bool DeSerialize( float& i_in, IDeSerializer& i_deserializer); bool DeSerialize( double& i_in, IDeSerializer& i_deserializer); bool DeSerialize( bool& i_in, IDeSerializer& i_deserializer); bool DeSerialize( wchar_t& i_in, IDeSerializer& i_deserializer);

bool DeSerialize( char& i_in, IDeSerializer& i_deserializer, const std::wstring& i_wstrName); bool DeSerialize( unsigned char& i_in, IDeSerializer& i_deserializer, const std::wstring& i_wstrName); bool DeSerialize( short& i_in, IDeSerializer& i_deserializer, const std::wstring& i_wstrName); bool DeSerialize( unsigned short& i_in, IDeSerializer& i_deserializer, const std::wstring& i_wstrName); bool DeSerialize( int& i_in, IDeSerializer& i_deserializer, const std::wstring& i_wstrName); bool DeSerialize( unsigned int& i_in, IDeSerializer& i_deserializer, const std::wstring& i_wstrName); bool DeSerialize( long& i_in, IDeSerializer& i_deserializer, const std::wstring& i_wstrName); bool DeSerialize( unsigned long& i_in, IDeSerializer& i_deserializer, const std::wstring& i_wstrName); bool DeSerialize( float& i_in, IDeSerializer& i_deserializer, const std::wstring& i_wstrName); bool DeSerialize( double& i_in, IDeSerializer& i_deserializer, const std::wstring& i_wstrName); bool DeSerialize( bool& i_in, IDeSerializer& i_deserializer, const std::wstring& i_wstrName); bool DeSerialize( wchar_t& i_in, IDeSerializer& i_deserializer, const std::wstring& i_wstrName); #endif }

#endif

Currently browsing [psf.zip] (34,320 bytes) - [DeSerializer.hpp] - (1,318 bytes)

#ifndef DESERIALIZER_HPP
#define DESERIALIZER_HPP

#include <string> #include <memory> #include "Loki/TypeInfo.h"

namespace PSF { class IDeSerializer { public: virtual ~IDeSerializer() = 0 {};

virtual bool DeSerialize( char& o_out) = 0; virtual bool DeSerialize( unsigned char& o_out) = 0; virtual bool DeSerialize( short& o_out) = 0; virtual bool DeSerialize( unsigned short& o_out) = 0; virtual bool DeSerialize( int& o_out) = 0; virtual bool DeSerialize( unsigned int& o_out) = 0; virtual bool DeSerialize( long& o_out) = 0; virtual bool DeSerialize( unsigned long& o_out) = 0; virtual bool DeSerialize( float& o_out) = 0; virtual bool DeSerialize( double& o_out) = 0; // same as double // virtual bool DeSerialize( long double& o_out) = 0; virtual bool DeSerialize( bool& o_out) = 0; virtual bool DeSerialize( wchar_t& o_out) = 0;

// creates a scope within the deserializer virtual std::auto_ptr<IDeSerializer> DeSerializeCompound( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type) = 0;

// DeSerializing a range means that all item deserialized in this scope must be of the same type virtual std::auto_ptr<IDeSerializer> DeSerializeRange( const std::wstring& i_wstrName, const Loki::TypeInfo& i_type) = 0; }; };

#endif

Currently browsing [psf.zip] (34,320 bytes) - [DOMSaver.cpp] - (4,495 bytes)

#include "DOMSaver.hpp"

std::wostream& operator<<( std::wostream& i_ostream, const DOM::Node<std::wstring>& i_toWrite); std::wostream& operator<<( std::wostream& i_ostream, const DOM::Node<std::string>& i_toWrite); std::ostream& operator<<( std::ostream& i_ostream, const DOM::Node<std::string>& i_toWrite);

std::ostream& operator<<( std::ostream& i_ostream, const DOM::Node<std::wstring>& i_toWrite); { switch ( i_toWrite.getNodeType()) { case DOM::Node<StringT>::TEXT_NODE: { i_ostream << i_toWrite.getNodeValue(); break; }

case DOM::Node<StringT>::PROCESSING_INSTRUCTION_NODE : { i_ostream << "<?" << i_toWrite.getNodeName() << ' ' << i_toWrite.getNodeValue() << "?>"; break; }

case DOM::Node<StringT>::DOCUMENT_NODE : { i_ostream << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << std::endl; for ( DOM::Node<StringT> child = i_toWrite.getFirstChild(); child != 0; child = child.getNextSibling()) { i_ostream << child; // add linefeed in requested output encoding i_ostream << std::endl; } break; }

case DOM::Node<StringT>::ELEMENT_NODE : { // The name has to be representable without any escapes i_ostream << '<' << i_toWrite.getNodeName();

// Output the element start tag. // Output any attributes on this element DOM::NamedNodeMap<StringT> attributes = i_toWrite.getAttributes(); for ( unsigned int i = 0; i < attributes.getLength(); i++) { DOM::Node<StringT> attribute = attributes.item(i); i_ostream << ' ' << attribute.getNodeName() << "=\"" << attribute.getNodeValue() << '\"'; }

// // Test for the presence of children, which includes both // text content and nested elements. // DOM::Node<StringT> child = i_toWrite.getFirstChild(); if (child != 0) { // There are children. Close start-tag, and output children. // No escapes are legal here i_ostream << '>';

for (; child != 0; child = child.getNextSibling()) { i_ostream << child; }

// // Done with children. Output the end tag. // i_ostream << "</" << i_toWrite.getNodeName() << '>'; } else { // // There were no children. Output the short form close of // the element start tag, making it an empty-element tag. // i_ostream << "/>"; } break; }

case DOM::Node<StringT>::ENTITY_REFERENCE_NODE: { DOM::Node<StringT> child; for ( child = i_toWrite.getFirstChild(); child != 0; child = child.getNextSibling()) { i_ostream << child; } break; }

case DOM::Node<StringT>::CDATA_SECTION_NODE: { i_ostream << "<![CDATA[" << i_toWrite.getNodeValue() << "]]>"; break; }

case DOM::Node<StringT>::COMMENT_NODE: { i_ostream << "<!--" << i_toWrite.getNodeValue() << "-->"; break; }

/* case DOM::Node<StringT>::DOCUMENT_TYPE_NODE: { const DOM::DocumentType<StringT> doctype( static_cast<DOM::DocumentType<StringT> >(i_toWrite));

i_ostream << "<!DOCTYPE " << doctype.getNodeName() << " PUBLIC \"" << doctype.getPublicId() << "\" SYSTEM \"" << doctype.getSystemId() << '[' << doctype.getInternalSubset() << "]>";

break; } */


case DOM::Node<StringT>::ENTITY_NODE: { i_ostream << "<!ENTITY " << i_toWrite.getNodeName();

const DOM::Entity<StringT> entity( static_cast<DOM::Entity<StringT> >(i_toWrite)); i_ostream << "PUBLIC \"" << entity.getPublicId() << '\"'; i_ostream << "SYSTEM \"" << entity.getSystemId() << '\"'; i_ostream << "NDATA \"" << entity.getNotationName() << '\"';

i_ostream << '>' << std::endl;

break; }



default: std::cerr << "Unrecognized node type = " << (long)i_toWrite.getNodeType() << std::endl; } return i_ostream; }

Currently browsing [psf.zip] (34,320 bytes) - [DOMSaver.hpp] - (7,089 bytes)

#ifndef _DOM_SAVER_H_
#define _DOM_SAVER_H_

#include "DOM/Node.h" #include <iostream> #include "StringUtil.hpp" //std::ostream& operator<<( std::ostream& i_ostream, const DOM::Node<StringT>& i_toWrite); template<class StringT> std::ostream& operator<<( std::ostream& i_ostream, const DOM::Node<StringT>& i_toWrite) { switch ( i_toWrite.getNodeType()) { case DOM::Node<StringT>::TEXT_NODE: { i_ostream << i_toWrite.getNodeValue(); break; }

case DOM::Node<StringT>::PROCESSING_INSTRUCTION_NODE : { i_ostream << "<?" << i_toWrite.getNodeName() << ' ' << i_toWrite.getNodeValue() << "?>"; break; }

case DOM::Node<StringT>::DOCUMENT_NODE : {

//std::locale loc( std::locale(), new utf16utf8_codecvt); //i_ostream.imbue(loc); i_ostream << "<?xml version=\"1.0\" encoding=\"utf-8\"?>" << std::endl; for ( DOM::Node<StringT> child = i_toWrite.getFirstChild(); child != 0; child = child.getNextSibling()) { i_ostream << child; // add linefeed in requested output encoding i_ostream << std::endl; } break; }

case DOM::Node<StringT>::ELEMENT_NODE : { // The name has to be representable without any escapes i_ostream << '<' << i_toWrite.getNodeName();

// Output the element start tag. // Output any attributes on this element DOM::NamedNodeMap<StringT> attributes = i_toWrite.getAttributes(); for ( unsigned int i = 0; i < attributes.getLength(); i++) { DOM::Node<StringT> attribute = attributes.item(i); i_ostream << ' ' << attribute.getNodeName() << "=\"" << attribute.getNodeValue() << '\"'; }

// // Test for the presence of children, which includes both // text content and nested elements. // DOM::Node<StringT> child = i_toWrite.getFirstChild(); if (child != 0) { // There are children. Close start-tag, and output children. // No escapes are legal here i_ostream << '>';

for (; child != 0; child = child.getNextSibling()) { i_ostream << child; }

// // Done with children. Output the end tag. // i_ostream << "</" << i_toWrite.getNodeName() << '>'; } else { // // There were no children. Output the short form close of // the element start tag, making it an empty-element tag. // i_ostream << "/>"; } break; }

case DOM::Node<StringT>::ENTITY_REFERENCE_NODE: { DOM::Node<StringT> child; for ( child = i_toWrite.getFirstChild(); child != 0; child = child.getNextSibling()) { i_ostream << child; } break; }

case DOM::Node<StringT>::CDATA_SECTION_NODE: { i_ostream << "<![CDATA[" << i_toWrite.getNodeValue() << "]]>"; break; }

case DOM::Node<StringT>::COMMENT_NODE: { i_ostream << "<!--" << i_toWrite.getNodeValue() << "-->"; break; }

/* case DOM::Node<StringT>::DOCUMENT_TYPE_NODE: { const DOM::DocumentType<StringT> doctype( static_cast<DOM::DocumentType<StringT> >(i_toWrite));

i_ostream << "<!DOCTYPE " << doctype.getNodeName() << " PUBLIC \"" << doctype.getPublicId() << "\" SYSTEM \"" << doctype.getSystemId() << '[' << doctype.getInternalSubset() << "]>";

break; } */


case DOM::Node<StringT>::ENTITY_NODE: { i_ostream << "<!ENTITY " << i_toWrite.getNodeName();

const DOM::Entity<StringT> entity( static_cast<DOM::Entity<StringT> >(i_toWrite)); i_ostream << "PUBLIC \"" << entity.getPublicId() << '\"'; i_ostream << "SYSTEM \"" << entity.getSystemId() << '\"'; i_ostream << "NDATA \"" << entity.getNotationName() << '\"';

i_ostream << '>' << std::endl;

break; }



default: std::cerr << "Unrecognized node type = " << (long)i_toWrite.getNodeType() << std::endl; } return i_ostream; }

//// --------------------------------------------------------------------------- //// Local const data //// //// Note: This is the 'safe' way to do these strings. If you compiler supports //// L"" style strings, and portability is not a concern, you can use //// those types constants directly. //// --------------------------------------------------------------------------- //static const XMLCh gEndElement[] = { chOpenAngle, chForwardSlash, chNull }; //static const XMLCh gEndPI[] = { chQuestion, chCloseAngle, chNull}; //static const XMLCh gStartPI[] = { chOpenAngle, chQuestion, chNull }; //static const XMLCh gXMLDecl1[] = //{ // chOpenAngle, chQuestion, chLatin_x, chLatin_m, chLatin_l // , chSpace, chLatin_v, chLatin_e, chLatin_r, chLatin_s, chLatin_i // , chLatin_o, chLatin_n, chEqual, chDoubleQuote, chDigit_1 // , chPeriod, chDigit_0, chNull //}; //static const XMLCh gXMLDecl2[] = //{ // chDoubleQuote, chSpace, chLatin_e, chLatin_n, chLatin_c // , chLatin_o, chLatin_d, chLatin_i, chLatin_n, chLatin_g, chEqual // , chDoubleQuote, chNull //}; //static const XMLCh gXMLDecl3[] = //{ // chDoubleQuote, chQuestion, chCloseAngle // , chLF, chNull //}; // //static const XMLCh gStartCDATA[] = //{ // chOpenAngle, chBang, chOpenSquare, chLatin_C, chLatin_D, // chLatin_A, chLatin_T, chLatin_A, chOpenSquare, chNull //}; // //static const XMLCh gEndCDATA[] = //{ // chCloseSquare, chCloseSquare, chCloseAngle, chNull //}; //static const XMLCh gStartComment[] = //{ // chOpenAngle, chBang, chDash, chDash, chNull //}; // //static const XMLCh gEndComment[] = //{ // chDash, chDash, chCloseAngle, chNull //}; // //static const XMLCh gStartDoctype[] = //{ // chOpenAngle, chBang, chLatin_D, chLatin_O, chLatin_C, chLatin_T, // chLatin_Y, chLatin_P, chLatin_E, chSpace, chNull //}; //static const XMLCh gPublic[] = //{ // chLatin_P, chLatin_U, chLatin_B, chLatin_L, chLatin_I, // chLatin_C, chSpace, chDoubleQuote, chNull //}; //static const XMLCh gSystem[] = //{ // chLatin_S, chLatin_Y, chLatin_S, chLatin_T, chLatin_E, // chLatin_M, chSpace, chDoubleQuote, chNull //}; //static const XMLCh gStartEntity[] = //{ // chOpenAngle, chBang, chLatin_E, chLatin_N, chLatin_T, chLatin_I, // chLatin_T, chLatin_Y, chSpace, chNull //}; //static const XMLCh gNotation[] = //{ // chLatin_N, chLatin_D, chLatin_A, chLatin_T, chLatin_A, // chSpace, chDoubleQuote, chNull //};

#endif

Currently browsing [psf.zip] (34,320 bytes) - [Main.cpp] - (10,922 bytes)

// Render test.cpp : Defines the entry point for the application.
//


//#include "Render test.h" #include <iostream> #include <fstream> #include <list> #include <map> #include "Ghost/System/StreamRedirect.h"

#include "XMLSerializer.hpp" #include "XMLDeSerializer.hpp" #include "BinarySerializer.hpp" #include "BinaryDeSerializer.hpp" #include "PrettyPrintSerializer.hpp"

#include "TestBase.hpp" #include "Serialize.hpp"

#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers // Windows Header Files: #include <windows.h>



////////////////////////////////////////////////////////////////////////// // Initialize data and output as xml to 'out.xml' ////////////////////////////////////////////////////////////////////////// SerializerTypeMap CreateTypeMap() { SerializerTypeMap mapType; mapType[typeid(int)] = L"int"; mapType[typeid(unsigned int)] = L"unsigned int"; mapType[typeid(float)] = L"float"; mapType[typeid(bool)] = L"bool"; mapType[typeid(char)] = L"char"; mapType[typeid(wchar_t)] = L"wide char"; mapType[typeid(std::basic_string<void>)] = L"string"; mapType[typeid(std::list<void>)] = L"list"; mapType[typeid(std::vector<void>)] = L"vector"; mapType[typeid(std::map<void,void>)] = L"map"; mapType[typeid(std::set<void>)] = L"set"; mapType[typeid(std::pair<void,void>)] = L"pair"; mapType[typeid(CSimple)] = L"simple"; mapType[typeid(CComplex)] = L"complex"; mapType[typeid(CSuper)] = L"super"; return mapType; }

SerializerTypeMap CreateIncompleteTypeMap() { SerializerTypeMap mapType; mapType[typeid(int)] = L"int"; mapType[typeid(unsigned int)] = L"unsigned int"; //mapType[typeid(float)] = L"float"; mapType[typeid(bool)] = L"bool"; mapType[typeid(char)] = L"char"; mapType[typeid(wchar_t)] = L"wide char"; mapType[typeid(std::basic_string<void>)] = L"string"; mapType[typeid(std::list<void>)] = L"list"; mapType[typeid(std::vector<void>)] = L"vector"; mapType[typeid(std::map<void,void>)] = L"map"; mapType[typeid(std::set<void>)] = L"set"; mapType[typeid(std::pair<void,void>)] = L"pair"; mapType[typeid(CSimple)] = L"simple"; mapType[typeid(CComplex)] = L"complex"; mapType[typeid(CSuper)] = L"super"; return mapType; }

////////////////////////////////////////////////////////////////////////// // Initialize data and output as xml ////////////////////////////////////////////////////////////////////////// void Test_ToXML( std::wstring i_wstrFilenameXML) { CTestBase test; test.DefaultInit();

SerializerTypeMap mapType = CreateTypeMap();

// Construct XML serializer PSF::CXMLSerializerProxy serializer( i_wstrFilenameXML, L"root", mapType); test.DoSerialize( serializer); }

////////////////////////////////////////////////////////////////////////// // Initialize data and output as binary ////////////////////////////////////////////////////////////////////////// void Test_ToBinary( std::ostream& i_ostream) { CTestBase test; test.DefaultInit();

SerializerTypeMap mapType = CreateTypeMap();

// Construct XML serializer PSF::CBinarySerializer serializer( i_ostream); test.DoSerialize( serializer); }

////////////////////////////////////////////////////////////////////////// // Deserialize data from xml file and output pretty printed to stream ////////////////////////////////////////////////////////////////////////// void Test_XMLToPretty( std::wstring i_wstrFilenameXML, std::ostream& i_ostream) { CTestBase test;

SerializerTypeMap mapType = CreateTypeMap();

// Construct XML Serializer PSF::CCheckerAssert checker; PSF::CXMLDeSerializerProxy deserializer( i_wstrFilenameXML, mapType, checker);

test.DoDeSerialize( deserializer);

// Construct pretty print serializer //std::wfstream fileTxt( "out.txt", std::ios::out); PSF::TPrettyPrintSerializer<std::ostream::char_type> serializer( i_ostream);

test.DoSerialize( serializer); }

////////////////////////////////////////////////////////////////////////// // Deserialize data from xml file inverted and output pretty printed to stream ////////////////////////////////////////////////////////////////////////// void Test_InverseXMLToPretty( std::wstring i_wstrFilenameXML, std::ostream& i_ostream) { CTestBase test;

SerializerTypeMap mapType = CreateTypeMap();

// Construct XML Serializer PSF::CCheckerAssert checker; PSF::CXMLDeSerializerProxy deserializer( i_wstrFilenameXML, mapType, checker);

test.DoInverseDeSerialize( deserializer);

// Construct pretty print serializer //std::wfstream fileTxt( "out.txt", std::ios::out); PSF::TPrettyPrintSerializer<std::ostream::char_type> serializer( i_ostream);

test.DoSerialize( serializer); }

////////////////////////////////////////////////////////////////////////// // Deserialize data from binary stream and output pretty printed to stream ////////////////////////////////////////////////////////////////////////// void Test_BinaryToPretty( std::istream& i_istream, std::ostream& i_ostream) { CTestBase test;

SerializerTypeMap mapType = CreateTypeMap();

// Construct Binary Serializer PSF::CBinaryDeSerializer deserializer( i_istream);

test.DoDeSerialize( deserializer);

// Construct pretty print serializer PSF::TPrettyPrintSerializer<std::ostream::char_type> serializer( i_ostream);

test.DoSerialize( serializer); }

////////////////////////////////////////////////////////////////////////// // Deserialize data from binary stream inverted and output pretty printed to stream ////////////////////////////////////////////////////////////////////////// void Test_InverseBinaryToPretty( std::istream& i_istream, std::ostream& i_ostream) { CTestBase test;

SerializerTypeMap mapType = CreateTypeMap();

// Construct Binary Serializer PSF::CBinaryDeSerializer deserializer( i_istream);

test.DoInverseDeSerialize( deserializer);

// Construct pretty print serializer PSF::TPrettyPrintSerializer<std::ostream::char_type> serializer( i_ostream);

test.DoSerialize( serializer); }

////////////////////////////////////////////////////////////////////////// // Deserialize data from xml files with errors to test xml deserializers ////////////////////////////////////////////////////////////////////////// void Test_XMLError( std::ostream& i_ostream) { CTestBase test;

PSF::CCheckerException checker; SerializerTypeMap mapType = CreateTypeMap(); SerializerTypeMap mapTypeIncomplete = CreateIncompleteTypeMap();

// Testing TypeMissing try { PSF::CXMLDeSerializerProxy deserializer( L"MissingType.xml", mapType, checker); test.DoDeSerialize( deserializer); } catch ( std::runtime_error& ex) { i_ostream << "An exception occurred while deserializing MissingType.xml" << std::endl; i_ostream << "Expected error message : Type missing" << std::endl; i_ostream << "Actual error message : " << ex.what() << std::endl; }

// Testing TypeMismatch try { PSF::CXMLDeSerializerProxy deserializer( L"WrongType.xml", mapType, checker); test.DoDeSerialize( deserializer); } catch ( std::runtime_error& ex) { i_ostream << "An exception occurred while deserializing WrongType.xml" << std::endl; i_ostream << "Expected error message : Type mismatch" << std::endl; i_ostream << "Actual error message : " << ex.what() << std::endl; }

// Testing TypeUnknown try { PSF::CXMLDeSerializerProxy deserializer( L"out.xml", mapTypeIncomplete, checker); test.DoDeSerialize( deserializer); } catch ( std::runtime_error& ex) { i_ostream << "An exception occurred while deserializing out.xml" << std::endl; i_ostream << "Expected error message : Type unknown" << std::endl; i_ostream << "Actual error message : " << ex.what() << std::endl; }

// Testing SemanticError try { PSF::CXMLDeSerializerProxy deserializer( L"SemanticError.xml", mapType, checker); test.DoDeSerialize( deserializer); } catch ( std::runtime_error& ex) { i_ostream << "An exception occurred while deserializing SemanticError.xml" << std::endl; i_ostream << "Expected error message : Sematic error" << std::endl; i_ostream << "Actual error message : " << ex.what() << std::endl; }

// Testing RangeSizeError try { PSF::CXMLDeSerializerProxy deserializer( L"RangeError.xml", mapType, checker); test.DoDeSerialize( deserializer); } catch ( std::runtime_error& ex) { i_ostream << "An exception occurred while deserializing RangeError.xml" << std::endl; i_ostream << "Expected error message : Range size error" << std::endl; i_ostream << "Actual error message : " << ex.what() << std::endl; } }

void Test() { // Create XML file std::clog << "Creating xml file" << std::endl; Test_ToXML( L"out.xml");

// Create Binary file { std::clog << "Creating binary file" << std::endl; std::fstream fileBin( "out.bin", std::ios::out | std::ios::binary); Test_ToBinary( fileBin); }

// Deserialize XML and binary and pretty print std::clog << std::endl << "Outputting deserialized xml data as pretty printed text" << std::endl; Test_XMLToPretty( L"out.xml", std::clog); std::clog << std::endl;

std::clog << std::endl << "Outputting deserialized binary data as pretty printed text" << std::endl; { std::fstream fileBin( "out.bin", std::ios::in | std::ios::binary); Test_BinaryToPretty( fileBin, std::clog); } std::clog << std::endl;

// Deserialize XML and binary inverted and pretty print std::clog << std::endl << "Outputting inverse deserialized xml data as pretty printed text" << std::endl; Test_InverseXMLToPretty( L"out.xml", std::clog); std::clog << std::endl;

std::clog << std::endl << "Outputting inverse deserialized binary data as pretty printed text" << std::endl; { std::fstream fileBin( "out.bin", std::ios::in | std::ios::binary); Test_InverseBinaryToPretty( fileBin, std::clog); } std::clog << std::endl;

// Testing various type errors Test_XMLError( std::clog); }

int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { std::ofstream streamError( "Error.txt"); System::TStreamRedirect<std::ios::char_type,std::ios::traits_type> streamRedirect_cerr( std::cerr, streamError);

std::ofstream streamLog( "Log.txt"); System::TStreamRedirect<std::ios::char_type,std::ios::traits_type> streamRedirect_clog( std::clog, streamLog);

#if !defined(_DEBUG) try #endif { Test();

} #if !defined(_DEBUG) catch( std::exception& ex) { std::cerr << "Std exception : " << ex.what() << std::endl; } catch(...) { std::cerr << "Unhandled exception" << std::endl; } #endif streamError.close(); streamLog.close();

return 1; }

The zip file viewer built into the Developer Toolbox made use of the zlib library, as well as the zlibdll source additions.

 

Copyright 1999-2008 (C) FLIPCODE.COM and/or the original content author(s). All rights reserved.
Please read our Terms, Conditions, and Privacy information.