serialization_classes.h
//
// Copyright (C) 2008-2012 Artyom Beilis (Tonkikh) <artyomtnk@yahoo.com>
//
// See accompanying file COPYING.TXT file for licensing details.
//
#ifndef CPPCMS_SERIALIZATION_CLASSES_H
#define CPPCMS_SERIALIZATION_CLASSES_H
#include <cppcms/defs.h>
#include <booster/copy_ptr.h>
#include <string>
#include <booster/backtrace.h>
#include <booster/traits/enable_if.h>
#include <booster/traits/is_base_of.h>
namespace cppcms {
#ifdef CPPCMS_DOXYGEN_DOCS
template<typename Object>
struct archive_traits
{
static void save(Object const &d,archive &a);
static void load(Object &d,archive &a);
};
#else
template<typename Object,typename Enable = void>
struct archive_traits;
#endif
class archive_error : public booster::runtime_error {
public:
archive_error(std::string const &e) : booster::runtime_error("cppcms::archive_error: " + e)
{
}
};
class CPPCMS_API archive {
public:
void reserve(size_t size);
void write_chunk(void const *begin,size_t len);
void read_chunk(void *begin,size_t len);
size_t next_chunk_size();
bool eof();
std::string read_chunk_as_string();
typedef enum {
save_to_archive,
load_from_archive
} mode_type;
void mode(mode_type m);
mode_type mode();
void reset();
std::string str();
void str(std::string const &str);
archive();
~archive();
archive(archive const &);
archive const &operator=(archive const &);
private:
std::string buffer_;
size_t ptr_;
mode_type mode_;
struct _data;
booster::copy_ptr<_data> d;
};
template<typename Archivable>
archive & operator &(archive &a,Archivable &object)
{
if(a.mode() == archive::save_to_archive)
archive_traits<Archivable>::save(object,a);
else
archive_traits<Archivable>::load(object,a);
return a;
}
template<typename Archivable>
archive & operator <<(archive &a,Archivable const &object)
{
archive_traits<Archivable>::save(object,a);
return a;
}
template<typename Archivable>
archive & operator >>(archive &a,Archivable &object)
{
archive_traits<Archivable>::load(object,a);
return a;
}
class serializable_base {
public:
virtual void load(archive &serialized_object) = 0;
virtual void save(archive &serialized_object) const = 0;
virtual ~serializable_base()
{
}
};
class serializable : public serializable_base {
public:
virtual void serialize(archive &a) = 0;
virtual void load(archive &a)
{
serialize(a);
}
virtual void save(archive &a) const
{
const_cast< serializable *>(this)->serialize(a);
}
};
#ifdef CPPCMS_DOXYGEN_DOCS
template<typename Object>
struct serialization_traits {
static void load(std::string const &serialized_object,Object &real_object);
static void save(Object const &real_object,std::string &serialized_object);
};
#else
template<typename Object,typename Enable = void>
struct serialization_traits;
template<typename D>
struct serialization_traits<D,typename booster::enable_if<booster::is_base_of<serializable_base,D> >::type> {
static void load(std::string const &serialized_object,serializable_base &real_object)
{
archive a;
a.str(serialized_object);
real_object.load(a);
}
static void save(serializable_base const &real_object,std::string &serialized_object)
{
archive a;
real_object.save(a);
serialized_object = a.str();
}
};
#endif
}
#endif
cppcms::serializable
Abstract class for serialization object.
Definition: serialization_classes.h:217
cppcms::archive_error
Error thrown in case of serialization error.
Definition: serialization_classes.h:49
cppcms::archive_traits
Special traits class that describes how to serialize various objects that are not defived from serial...
Definition: serialization_classes.h:27
booster::runtime_error
Same as std::runtime_error but records stack trace.
Definition: backtrace.h:158
cppcms::archive::mode
void mode(mode_type m)
booster::copy_ptr< _data >
cppcms::operator>>
archive & operator>>(archive &a, Archivable &object)
Definition: serialization_classes.h:186
cppcms::operator&
archive & operator&(archive &a, Archivable &object)
Definition: serialization_classes.h:163
cppcms::serialization_traits::load
static void load(std::string const &serialized_object, Object &real_object)
cppcms::archive_traits::load
static void load(Object &d, archive &a)
cppcms::serializable_base::load
virtual void load(archive &serialized_object)=0
cppcms::archive_traits::save
static void save(Object const &d, archive &a)
cppcms::serializable::load
virtual void load(archive &a)
Definition: serialization_classes.h:245
cppcms::serializable::serialize
virtual void serialize(archive &a)=0
cppcms::serializable_base::save
virtual void save(archive &serialized_object) const =0
cppcms::archive::str
std::string str()
cppcms::serializable::save
virtual void save(archive &a) const
Definition: serialization_classes.h:252
cppcms::serializable_base
Base abstract class for object that can be serialized into std::string.
Definition: serialization_classes.h:198
cppcms::operator<<
archive & operator<<(archive &a, Archivable const &object)
Definition: serialization_classes.h:176
cppcms::serialization_traits
This is the traits class for serialization traits.
Definition: serialization_classes.h:296
cppcms::archive
Class that represents a binary archive that can be stored in persistent storage or transfered...
Definition: serialization_classes.h:61
cppcms::serialization_traits::save
static void save(Object const &real_object, std::string &serialized_object)