Main  /  Edit version 1  /  Edit version 2  /   /  Users Area

Difference "cppcms::serializable" ver. 1 versus ver. 2


## Role
This class used for simple, fast and robust serialization of C++ object.
Header: `#include <cppcms/archive.hpp>`
## Rationale
Serialization of object is important task, there are many good libraries that do this job, like boost::serialization.
However, most of them are too general and give too much overhead for simple task. This may significantly hurt performance in many simple cases we use.
So, good enough is good for our case --- keep serialization as simple as possible.
## Base serializable class
This class has two pure virtual methods that should be
implemented by it's users -- load() and save().
It provides simple interface to efficient conversion to string object and from string object in order to be useful with session and cache API.
class serializable {
virtual void load(archive &a) = 0;
virtual void save(archive &a) const = 0;
operator std::string() const;
serializable const &operator=(std::string const &s);
void str(std::string const &s);
std::string str() const;
For example:
struct person : public serializable {
int user_id;
string name;
double age;
person per(id,"roy",35);
person per;
All save and load operations are performed through "archive" class.
## cppcms::archive
### Storage methods
`cppcms::archive` know to store and restore two types of data:
1. std::string
2. Old Plain Data -- or general "memcpy"-able structures.
These operations are available using following operators of `cppcms::archive`
template<typename T>
archive &operator<<(T const &val);
template<typename T>
archive &operator>>(T &val)
Save and restore old-plain-data to archive.
archive &operator<<(string const &val);
archive &operator>>(string &val);
Save and restore C++ strings.
### Simple objects
So, how should we serialize more complex objects? Let's see a simple example:
struct person : public serializable {
int id;
string name;
double age;
virtual void load(archive &a) {
virtual void save(archive &a) const {
That's it!
### Managing more complex data
What should we do with STL collections or other
more complex data?
struct person : public serializable {
int id;
string name;
double age;
set<string> colors;
virtual void load(archive &a) {
size_t n;
string color;
for(size_t i=0;i<n;i++) {
virtual void save(archive &a) const {
set<string>::const_iterator p;
That's it.


## Related
- [cppcms::cache\_iface](/wikipp/en/page/ref_cppcms_cache_iface)
- [cppcms::session\_interface](/wikipp/en/page/ref_cppcms_session_iface)


CppCMS is a web development framework for performance demanding applications.

Support This Project Logo

Поддержать проект

CppCMS needs You


Main Page

Valid CSS | Valid XHTML 1.0