Examples
Hello World!
The classic "Hello World!" using Sweet Persist:
#include <string>
#include <sweet/persist.hpp>
using namespace std;
using namespace sweet::persist;
class HelloWorld
{
string message_;
public:
HelloWorld()
: message_()
{
}
HelloWorld( const string& message )
: message_( message )
{
}
template <class Archive> void enter( Archive& archive )
{
}
template <class Archive> void exit( Archive& archive )
{
}
template <class Archive> void persist( Archive& archive )
{
archive.enter( "HelloWorld", 1, *this );
archive.value( "message", message_ );
}
};
void persist_hello_world_example()
{
{
HelloWorld hello_world( "Hello World!" );
XmlWriter xml_writer;
xml_writer.write( "hello_world.xml", "hello_world", hello_world );
}
{
HelloWorld hello_world;
XmlReader xml_reader;
xml_reader.read( "hello_world.xml", "hello_world", hello_world );
}
}
Social Networking
The data model from a simple social networking application:
#include <string>
#include <sweet/persist.hpp>
#include <sweet/persist/vector.hpp>
using namespace std;
using namespace sweet::persist;
enum Category
{
CATEGORY_NONE = 0x00,
CATEGORY_SOCIAL = 0x01,
CATEGORY_WORK = 0x02,
CATEGORY_CUSTOMER = 0x04
};
enum Status
{
STATUS_UNKNOWN,
STATUS_ACTIVE,
STATUS_INACTIVE
};
class Person
{
wstring name_;
int categories_;
Status status_;
vector<Person*> friends_;
public:
Person()
: name_(),
categories_(),
status_(),
friends_()
{
}
Person( const wstring& name, int categories, Status status )
: name_( name ),
categories_( categories ),
status_( status ),
friends_()
{
}
void add_friend( Person* person )
{
friends_.push_back( person );
}
template <class Archive> void persist( Archive& archive )
{
static const MaskFilter::Conversion CATEGORY[] =
{
{ CATEGORY_NONE, "None" },
{ CATEGORY_SOCIAL, "Social" },
{ CATEGORY_WORK, "Work" },
{ CATEGORY_CUSTOMER, "Customer" },
{ 0, 0 }
};
static const EnumFilter::Conversion STATUS[] =
{
{ STATUS_UNKNOWN, "Unknown" },
{ STATUS_ACTIVE, "Active" },
{ STATUS_INACTIVE, "Inactive" },
{ 0, 0 }
};
archive.value( "name", name_ );
archive.value( "categories", categories_, mask_filter(CATEGORY) );
archive.value( "status", status_, enum_filter(STATUS) );
archive.refer( "friends", "friend", friends_ );
}
};
class SocialNetwork
{
vector<Person> people_;
public:
SocialNetwork()
: people_()
{
}
void create()
{
people_.push_back( Person(L"Alfred", CATEGORY_NONE, STATUS_ACTIVE) );
people_.push_back( Person(L"Ben", CATEGORY_SOCIAL, STATUS_INACTIVE) );
people_.push_back( Person(L"Camilla", CATEGORY_SOCIAL | CATEGORY_WORK, STATUS_INACTIVE) );
people_.push_back( Person(L"Denise", CATEGORY_WORK | CATEGORY_CUSTOMER, STATUS_ACTIVE) );
Person& alfred = people_[0];
Person& ben = people_[1];
Person& camilla = people_[2];
Person& denise = people_[3];
alfred.add_friend( &ben );
alfred.add_friend( &camilla );
ben.add_friend( &alfred );
ben.add_friend( &denise );
camilla.add_friend( &alfred );
camilla.add_friend( &ben );
camilla.add_friend( &denise );
denise.add_friend( &camilla );
}
template <class Archive> void enter( Archive& archive )
{
archive.declare <Person> ( "Person", PERSIST_NORMAL );
archive.declare <SocialNetwork> ( "SocialNetwork", PERSIST_NORMAL );
}
template <class Archive> void exit( Archive& archive )
{
}
template <class Archive> void persist( Archive& archive )
{
archive.enter( "Social Network", 1, *this );
archive.value( "people", "person", people_ );
}
};
void persist_social_network_example()
{
{
SocialNetwork social_network;
social_network.create();
XmlWriter xml_writer;
xml_writer.write( "social_network.xml", "social_network", social_network );
}
{
SocialNetwork social_network;
XmlReader xml_reader;
xml_reader.read( "social_network.xml", "social_network", social_network );
}
{
SocialNetwork social_network;
social_network.create();
JsonWriter json_writer;
json_writer.write( "social_network.json", "social_network", social_network );
}
{
SocialNetwork social_network;
JsonReader json_reader;
json_reader.read( "social_network.json", "social_network", social_network );
}
{
SocialNetwork social_network;
social_network.create();
BinaryWriter binary_writer;
binary_writer.write( "social_network.bin", "social_network", social_network );
}
{
SocialNetwork social_network;
BinaryReader binary_reader;
binary_reader.read( "social_network.bin", "social_network", social_network );
}
}