From 17dc91d35e604fc8fa86de10aaed6111426e4202 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Fri, 24 Apr 2009 07:13:10 +0000 Subject: [PATCH] serialization nearly ready for containment --- doc/examples/contain_serialization.cxx | 59 ++++++++++++++++++++++++++ doc/examples/inherit_serialization.cxx | 4 +- doc/examples/makefile.am | 8 ++-- src/xml-cxx/xml.hxx | 37 +++++++++++++++- src/xml.cxx | 56 ++++++++++++------------ test/serialization_test.cxx | 4 +- 6 files changed, 133 insertions(+), 35 deletions(-) create mode 100644 doc/examples/contain_serialization.cxx diff --git a/doc/examples/contain_serialization.cxx b/doc/examples/contain_serialization.cxx new file mode 100644 index 0000000..e362185 --- /dev/null +++ b/doc/examples/contain_serialization.cxx @@ -0,0 +1,59 @@ +/*! @file + + @id $Id: inherit_serialization.cxx 25 2009-04-23 15:10:21Z $ +*/ +// 1 2 3 4 5 6 7 8 +// 45678901234567890123456789012345678901234567890123456789012345678901234567890 + +// g++ -I../../src ../../src/xml.cxx contain_serialization.cxx + +#include +#include +#include + +class A: public xml::Serialize { + public: + int a; + std::string txt; + protected: + void initXmlMembers() { + className("a") + .persist(a, "a") + .persist(txt, "txt"); + } +}; + +class B: public xml::Serialize { + public: + int i; + std::string txt; + A a; + protected: + void initXmlMembers() { + className("b") + .persist(i, "i") + .persist(txt, "txt") + .persist(a); + } +}; + +int main(int, char**) { + { // Serialization as a member + std::stringstream ss("\n" + "\t1234\n" + "\tDies ist Class B\n" + "\t\n" + "\t\t5678\n" + "\t\tDies ist Class A\n" + "\t" + ""); + B b; + b.loadXml(ss); + if (b.i==1234) b.i=4321; + if (b.a.a==5678) b.a.a=8765; + std::cout<<"Text B: "< #include -class B: public xml::Serialize { +class A: public xml::Serialize { public: int a; std::string txt; @@ -23,7 +23,7 @@ class B: public xml::Serialize { } }; -class B: public xml::Serialize { +class B: public A { public: int a; std::string txt; diff --git a/doc/examples/makefile.am b/doc/examples/makefile.am index 795514a..33cad71 100644 --- a/doc/examples/makefile.am +++ b/doc/examples/makefile.am @@ -4,11 +4,13 @@ ## 45678901234567890123456789012345678901234567890123456789012345678901234567890 AM_CXXFLAGS += -I ${top_srcdir}/src -AM_LDFLAGS = -L${top_builddir}/src +AM_LDFLAGS = -L${top_builddir}/src -lxml-cxx -noinst_PROGRAMS = address +noinst_PROGRAMS = address node_macros serialization contain_serialization address_SOURCES = address.cxx -address_LDADD = -lxml-cxx +node_macros_SOURCES = node_macros.cxx +serialization_SOURCES = serialization.cxx +contain_serialization_SOURCES = contain_serialization.cxx MAINTAINERCLEANFILES = makefile.in diff --git a/src/xml-cxx/xml.hxx b/src/xml-cxx/xml.hxx index e2d6e87..764241a 100644 --- a/src/xml-cxx/xml.hxx +++ b/src/xml-cxx/xml.hxx @@ -17,6 +17,34 @@ #include #include +#include +#include +#include +class MethodTrace { + public: + MethodTrace(const void* addr, const std::string& name) throw(): + _addr(addr), _name(name) { + std::clog<() const throw(factory_not_valid); operator bool() const throw(); friend std::ostream& operator<<(std::ostream& os, const Factory& factory) throw(); @@ -764,9 +793,13 @@ namespace xml { Serialize() throw(); Serialize(const std::string& className) throw(); virtual ~Serialize(); - void className(const std::string& name) throw(); + Serialize& className(const std::string& name) throw(); + Serialize& persist(Serialize& ser) throw(); template - Serialize& persist(TYPE& member, const std::string& name) { + Serialize& persist(TYPE& member, const std::string& name) throw() { + assert(mapName().find(name)==mapName().end()); + assert(mapMember().find(&member)==mapMember().end()); + assert(_xmlNames.find(name)==_xmlNames.end()); mapName()[name] = &member; mapMember()[&member] = name; _xmlNames[name] = &typeid(TYPE); diff --git a/src/xml.cxx b/src/xml.cxx index 8c5782d..c138d4f 100644 --- a/src/xml.cxx +++ b/src/xml.cxx @@ -10,31 +10,7 @@ #include #include -#include -#include -class MethodTrace { - public: - MethodTrace(const void* addr, const std::string& name) throw(): - _addr(addr), _name(name) { - std::clog<() const throw(factory_not_valid) try { + return &_template[0]; + } catch (...) { + throw factory_not_valid(); + } Factory::operator bool() const throw() { return _template.children()>0; } @@ -1009,14 +990,35 @@ namespace xml { //============================================================== Serialization + //---------------------------------------------------------------------------- + template<> void Serialize::fromNode(Serialize* member, + xml::Node& node) { + member->_xmlFactory = node[member->_xmlFactory->name()]; + } + template<> void Serialize::toNode(Serialize* member, + xml::Node& node) const { + node[member->_xmlFactory->name()] = *member->_xmlFactory; + } //---------------------------------------------------------------------------- Serialize::Serialize() throw() {} Serialize::Serialize(const std::string& className) throw(): _xmlFactory(xml::Node(xml::String(className).limits(1,1))) { } Serialize::~Serialize() {}; - void Serialize::className(const std::string& name) throw() { + Serialize& Serialize::className(const std::string& name) throw() { _xmlFactory=xml::Node(xml::String(name).limits(1,1)); + return *this; + } + Serialize& Serialize::persist(Serialize& ser) throw() { + ser.initXmlMembers(); + std::string name(ser._xmlFactory->name()); + mapName()[name] = &ser; + mapMember()[&ser] = name; + _xmlNames[name] = &typeid(Serialize); + xml::Node schema(*_xmlFactory); + schema<<*ser._xmlFactory; + _xmlFactory = schema; + return *this; } std::ostream& Serialize::saveXml(std::ostream& os) const throw() { if (!_xmlFactory) const_cast(this)->initXmlMembers(); @@ -1028,6 +1030,7 @@ namespace xml { if (*it->second==typeid(TYPE)) \ toNode(mapName()[it->first], node); \ else + QWERTZ_CHECK_TYPE_ZTREWQ___XXX(Serialize) QWERTZ_CHECK_TYPE_ZTREWQ___XXX(std::string) QWERTZ_CHECK_TYPE_ZTREWQ___XXX(float) QWERTZ_CHECK_TYPE_ZTREWQ___XXX(double) @@ -1060,6 +1063,7 @@ namespace xml { if (*it->second==typeid(TYPE)) \ fromNode(mapName()[it->first], *node); \ else + QWERTZ_CHECK_TYPE_ZTREWQ___XXX(Serialize) QWERTZ_CHECK_TYPE_ZTREWQ___XXX(std::string) QWERTZ_CHECK_TYPE_ZTREWQ___XXX(float) QWERTZ_CHECK_TYPE_ZTREWQ___XXX(double) @@ -1082,5 +1086,5 @@ namespace xml { return is; } void Serialize::initXmlMembers() {} - + } diff --git a/test/serialization_test.cxx b/test/serialization_test.cxx index 94115b4..db5fe5e 100644 --- a/test/serialization_test.cxx +++ b/test/serialization_test.cxx @@ -11,7 +11,7 @@ #include #include -class A: public xml::Serialization { +class A: public xml::Serialize { public: int _anInteger; bool _aBool; @@ -41,7 +41,7 @@ class SerializationTest: public CppUnit::TestFixture { _a._aDouble = 123.456; _a._aString = "Hello World"; _a._anotherString = "This is another Text"; - _a._aLong = 4123674622; + _a._aLong = 4123674622ul; try { std::stringstream ss; _a.saveXml(ss);