containment can be serialized

This commit is contained in:
Marc Wäckerlin
2009-04-24 15:12:44 +00:00
parent 17dc91d35e
commit dd834119f3
3 changed files with 116 additions and 55 deletions

View File

@@ -739,52 +739,69 @@ namespace xml {
@section serActual Actual Status
Instead of:
The following member types are supported
- All built-in C++ types are supported
- std::string is supported
- Contained classes are supported
The following will be supported soon (ideas):
- lists, maps
- inheritance
- choices (one of)
- optional members (pointer)
Pointers cannot be stored.
There are many ways of implemenation. best practice is to
inherit xml::Serialize and to overwrite
xml::Serialize::initXmlMembers:
@code
class A {
protected:
int _anInteger;
bool _aBool;
double _aDouble;
std::string _aString;
std::string _anotherString;
unsigned long _aLong;
class A: public xml::Serialize {
protected:
// all persitent members must be registered
virtual void initXmlMembers() {
className("A"); // giving a class name is very important
persist(_anInteger, "anInteger");
persist(_aBool, "aBool");
persist(_aDouble, "aDouble");
persist(_aString, "aString");
persist(_anotherString, "anotherString");
persist(_aLong, "aLong");
}
private:
int _anInteger;
bool _aBool;
double _aDouble;
std::string _aString;
std::string _anotherString;
unsigned long _aLong;
};
class B: public xml::Serialize {
protected:
virtual void initXmlMembers() {
className("B");
persist(_a); // name must not be given, it's already known
persist(_anInteger, "anInteger");
}
private:
A _a; // contains an A
int _anInteger;
};
int main(int, char**) {
A a;
B b;
// ... do something with a and b, then write it to stdout:
a.saveXml(std::out)<<std::endl;
b.saveXml(std::out)<<std::endl;
return 0;
}
@endcode
You have to write:
@code
class A {
protected:
XML_DECLARE_MEMBER(int, anInteger);
XML_DECLARE_MEMBER(bool, aBool);
XML_DECLARE_MEMBER(double, aDouble);
XML_DECLARE_MEMBER(std::string, aString);
XML_DECLARE_MEMBER(std::string, anotherString);
XML_DECLARE_MEMBER(unsigned long, aLong);
private:
XML_INIT_BEGIN(A);
XML_INIT_MEMBER(A, anInteger);
XML_INIT_MEMBER(A, aBool);
XML_INIT_MEMBER(A, aDouble);
XML_INIT_MEMBER(A, aString);
XML_INIT_MEMBER(A, anotherString);
XML_INIT_MEMBER(A, aLong);
XML_INIT_END;
};
@endcode
You get:
- All the members (with @c _ as member prefix)
- method <code>void saveXml(std::ostream&) const</code>
- method <code>void loadXml(std::istream&)</code>
@todo Up to now: Only base types plus std::string are supported,
no list, no inheritance is possible and no optional members.
@example serialization.cxx */
@example serialization.cxx
@example contain_serialization.cxx */
//@{
class Serialize {
@@ -797,10 +814,10 @@ namespace xml {
Serialize& persist(Serialize& ser) throw();
template<typename TYPE>
Serialize& persist(TYPE& member, const std::string& name) throw() {
assert(mapName<TYPE>().find(name)==mapName<TYPE>().end());
assert(mapName<TYPE>().find(std::make_pair(this, name))==mapName<TYPE>().end());
assert(mapMember<TYPE>().find(&member)==mapMember<TYPE>().end());
assert(_xmlNames.find(name)==_xmlNames.end());
mapName<TYPE>()[name] = &member;
mapName<TYPE>()[std::make_pair(this, name)] = &member;
mapMember<TYPE>()[&member] = name;
_xmlNames[name] = &typeid(TYPE);
xml::Node schema(*_xmlFactory);
@@ -813,8 +830,10 @@ namespace xml {
protected:
virtual void initXmlMembers();
private:
template<typename TYPE> std::map<std::string, TYPE*>& mapName() const {
static std::map<std::string, TYPE*> MAP;
template<typename TYPE>
std::map<std::pair<const Serialize*, std::string>, TYPE*>&
mapName() const {
static std::map<std::pair<const Serialize*, std::string>, TYPE*> MAP;
return MAP;
}
template<typename TYPE> std::map<TYPE*, std::string>& mapMember() const {