master
Marc Wäckerlin 16 years ago
parent 0871fd927f
commit 13c7266c9f
  1. 4
      doc/examples/list_serialization.cxx
  2. 152
      src/xml-cxx/any.hxx
  3. 74
      src/xml-cxx/xml.hxx
  4. 73
      src/xml.cxx

@ -41,7 +41,7 @@ int main(int, char**) {
"<b>1234</b>"
"<a><list><item>guguseli</item></list></a>"
"<As>"
"<item>\n"
"<A>\n"
"\t<list>\n"
"\t\t<item>Hello</item>\n"
"\t\t<item>World</item>\n"
@ -49,7 +49,7 @@ int main(int, char**) {
"\t\t<item>are</item>\n"
"\t\t<item>you</item>\n"
"\t</list>\n"
"</item>"
"</A>"
"</As>"
"</B>");
B b;

@ -0,0 +1,152 @@
/*! @file
@id $Id$
taken from boost.org boost::Any
*/
// 1 2 3 4 5 6 7 8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890
// See http://www.boost.org/libs/any for Documentation.
#ifndef XML_CXX_ANY_INCLUDED
#define XML_CXX_ANY_INCLUDED
#include <algorithm>
#include <typeinfo>
#include <stdexcept>
namespace xml {
class Any {
public: // structors
Any(): content(0) {}
template<typename ValueType> Any(const ValueType& value):
content(new holder<ValueType>(value)) {
}
Any(const Any& other): content(other.content?other.content->clone():0) {}
~Any() {
delete content;
}
public: // modifiers
Any& swap(Any& rhs) {
std::swap(content, rhs.content);
return *this;
}
template<typename ValueType> Any& operator=(const ValueType& rhs) {
Any(rhs).swap(*this);
return *this;
}
Any& operator=(const Any& rhs) {
Any(rhs).swap(*this);
return *this;
}
public: // queries
bool empty() const {
return !content;
}
const std::type_info& type() const {
return content?content->type():typeid(void);
}
private: // types
class placeholder {
public: // structors
virtual ~placeholder() {}
virtual const std::type_info& type() const = 0;
virtual placeholder* clone() const = 0;
};
template<typename ValueType> class holder: public placeholder {
public:
holder(const ValueType& value): held(value) {}
virtual const std::type_info& type() const {
return typeid(ValueType);
}
virtual placeholder* clone() const {
return new holder(held);
}
ValueType held;
};
private: // representation
template<typename ValueType> friend ValueType* any_cast(Any*);
template<typename ValueType> friend ValueType& any_cast(Any&);
placeholder* content;
};
template<typename ValueType> ValueType* any_cast(Any* operand) {
return operand && operand->type() == typeid(ValueType)
? &static_cast<Any::holder<ValueType>*>(operand->content)->held
: 0;
}
template<typename ValueType> const ValueType* any_cast(const Any* operand) {
return any_cast<ValueType>(const_cast<Any*>(operand));
}
template<typename ValueType> ValueType& any_cast(Any& operand) {
if (operand.type()==typeid(ValueType))
return static_cast<Any::holder<ValueType>*>(operand.content)->held;
throw std::bad_cast();
}
template<typename ValueType> const ValueType& any_cast(const Any& operand) {
return any_cast<ValueType>(const_cast<Any&>(operand));
}
}
// Copyright Kevlin Henney, 2000, 2001, 2002. All rights reserved.
//
// Distributed under the Boost Software License, Version 1.0. (See
// http://www.boost.org/LICENSE_1_0.txt)
// Boost Software License - Version 1.0 - August 17th, 2003
// Permission is hereby granted, free of charge, to any person or organization
// obtaining a copy of the software and accompanying documentation covered by
// this license (the "Software") to use, reproduce, display, distribute,
// execute, and transmit the Software, and to prepare derivative works of the
// Software, and to permit third-parties to whom the Software is furnished to
// do so, all subject to the following:
// The copyright notices in the Software and this entire statement, including
// the above license grant, this restriction and the following disclaimer,
// must be included in all copies of the Software, in whole or in part, and
// all derivative works of the Software, unless such copies or derivative
// works are solely in the form of machine-executable object code generated by
// a source language processor.
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
// SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
// FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
// ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.
#endif

@ -18,7 +18,7 @@
#include <memory>
#include <typeinfo>
#include <stdexcept>
#include <boost/any.hpp>
#include <xml-cxx/any.hxx>
#include <cassert>
#include <iostream>
@ -829,8 +829,8 @@ namespace xml {
class Serialize {
public:
typedef bool(*FromNodeFunc)(boost::any, const xml::Node&);
typedef bool(*ToNodeFunc)(const boost::any, xml::Node&);
typedef bool(*FromNodeFunc)(Any, const xml::Node&);
typedef bool(*ToNodeFunc)(const Any, xml::Node&);
//! You must call Serialize::className() if you use this constructor!
Serialize() throw();
Serialize(const std::string& className) throw();
@ -896,8 +896,8 @@ namespace xml {
_xmlFactory = schema;
return *this;
}
virtual void fromNode(boost::any member, const xml::Node& node);
virtual void toNode(const boost::any member, xml::Node& node) const;
virtual void fromNode(Any member, const xml::Node& node);
virtual void toNode(const Any member, xml::Node& node) const;
/*
template<typename TYPE, class ALLOC>
void fromNode(std::list<TYPE, ALLOC>* member, xml::Node& node) {
@ -905,12 +905,61 @@ namespace xml {
for (xml::Node::size_type i(0); i<node.children(); ++i)
...
}*/
std::map<std::string, boost::any> _xmlNames;
std::map<std::string, Any> _xmlNames;
xml::Factory _xmlFactory;
static std::set<FromNodeFunc> _fromNode;
static std::set<ToNodeFunc> _toNode;
};
const int MAX_NUM(14);
template<int NUM> struct ToType {typedef Serialize Type;};
template<> struct ToType<1> {typedef Serialize Type;};
template<> struct ToType<2> {typedef std::string Type;};
template<> struct ToType<3> {typedef bool Type;};
template<> struct ToType<4> {typedef unsigned char Type;};
template<> struct ToType<5> {typedef signed char Type;};
template<> struct ToType<6> {typedef char Type;};
template<> struct ToType<7> {typedef unsigned short Type;};
template<> struct ToType<8> {typedef signed short Type;};
template<> struct ToType<9> {typedef unsigned int Type;};
template<> struct ToType<10> {typedef signed int Type;};
template<> struct ToType<11> {typedef unsigned long Type;};
template<> struct ToType<12> {typedef signed long Type;};
template<> struct ToType<13> {typedef float Type;};
template<> struct ToType<14> {typedef double Type;};
template<typename T> struct ToNum {static const int NUM = 1;};
template<> struct ToNum<Serialize > {static const int NUM = 1;};
template<> struct ToNum<std::string > {static const int NUM = 2;};
template<> struct ToNum<bool > {static const int NUM = 3;};
template<> struct ToNum<unsigned char > {static const int NUM = 4;};
template<> struct ToNum<signed char > {static const int NUM = 5;};
template<> struct ToNum< char > {static const int NUM = 6;};
template<> struct ToNum<unsigned short> {static const int NUM = 7;};
template<> struct ToNum<signed short> {static const int NUM = 8;};
template<> struct ToNum<unsigned int > {static const int NUM = 9;};
template<> struct ToNum<signed int > {static const int NUM = 10;};
template<> struct ToNum<unsigned long > {static const int NUM = 11;};
template<> struct ToNum<signed long > {static const int NUM = 12;};
template<> struct ToNum<float > {static const int NUM = 13;};
template<> struct ToNum<double > {static const int NUM = 14;};
template<typename T> bool isSerialize() {
return ToNum<T>::NUM == 1;
}
template <typename T, bool GOOD=(ToNum<T>::NUM==1)> struct Mapper {
static Serialize* toSerialize(T& obj) {
return 0;
}
};
template <typename T> struct Mapper<T, true> {
static Serialize* toSerialize(T& obj) {
return dynamic_cast<Serialize*>(&obj);
}
};
template<class TYPE, class ALLOC=std::allocator<TYPE> > class List:
public std::list<TYPE, ALLOC>, public Serialize {
public:
@ -923,17 +972,20 @@ namespace xml {
std::string itemName("item");
TYPE tmp;
LOG("initXmlMembers List for: "<<typeid(TYPE).name());
if (typeid(TYPE*)==typeid(Serialize*)) {
if (isSerialize<TYPE>()) {
LOG("Liste von Serialize");
Serialize* ser((Serialize*)&tmp);
Serialize* ser(Mapper<TYPE>::toSerialize(tmp));
assert(ser);
assert(ser!=this);
assert((void*)ser==(void*)&tmp);
checkInit(ser);
itemName = **ser->_xmlFactory;
itemName = ser->_xmlFactory->name();
}
_xmlFactory = xml::Node("dummyroot"); // dummy root, (uninitialized exc)
persist(tmp, itemName); // add _reference as child of dummyroot
(*_xmlFactory)[0].limits(0, 0); // any number of children possible
}
virtual void fromNode(boost::any member, const xml::Node& node) {
virtual void fromNode(Any member, const xml::Node& node) {
this->clear();
for (xml::Node::size_type i(0); i<node.parent().children(); ++i) {
TYPE tmp;
@ -941,7 +993,7 @@ namespace xml {
push_back(tmp);
}
}
virtual void toNode(const boost::any member, xml::Node& node) const {
virtual void toNode(const Any member, xml::Node& node) const {
std::auto_ptr<xml::Node> tpl(node.clone());
xml::Node& parent(node.parent());
parent.clear(); // "node" is now invalid

@ -1109,7 +1109,7 @@ namespace xml {
checkInit();
xml::Node node(*_xmlFactory);
if (name.size()) node.name(name);
for (std::map<std::string, boost::any>::const_iterator
for (std::map<std::string, Any>::const_iterator
it(_xmlNames.begin());
it!=_xmlNames.end(); ++it)
toNode(it->second, node[it->first]);
@ -1121,7 +1121,7 @@ namespace xml {
xml::Factory factory(_xmlFactory);
if (name.size()) factory->name(name);
std::auto_ptr<xml::Node> node(factory.read(is));
for (std::map<std::string, boost::any>::const_iterator
for (std::map<std::string, Any>::const_iterator
it(_xmlNames.begin());
it!=_xmlNames.end(); ++it)
fromNode(it->second, (*node)[it->first]);
@ -1150,38 +1150,59 @@ namespace xml {
reset();
initXmlMembers();
}
void Serialize::fromNode(boost::any member, const xml::Node& node) {
void Serialize::fromNode(Any member, const xml::Node& node) {
for (std::set<FromNodeFunc>::const_iterator it(_fromNode.begin());
it!=_fromNode.end(); ++it)
if ((**it)(member, node)) return; // found match
throw type_not_registered(member.type().name(), node.name(), node);
/*
if (!RealType<member>::isSerialize())
*/
throw type_not_registered(member.type().name(), node.name(), node);
/*
Serialize* ser(RealType<TYPE>::serialize(member, 0));
// from assignFromNode<Serialize>
//! @todo improve this (inefficient)
std::stringstream ss; // simple but inefficient: rewrite and reread
ss<<node;
ser->loadXml(ss, node.name());
*/
}
void Serialize::toNode(const boost::any member, xml::Node& node) const {
void Serialize::toNode(const Any member, xml::Node& node) const {
for (std::set<ToNodeFunc>::const_iterator it(_toNode.begin());
it!=_toNode.end(); ++it)
if ((**it)(member, node)) return; // found match
throw type_not_registered(member.type().name(), node.name(), node);
/*
if (!RealType<TYPE>::isSerialize())
*/
throw type_not_registered(member.type().name(), node.name(), node);
/*
// from assignToNode<Serialize>
std::stringstream ss;
((Serialize*)member)->saveXml(ss, node.name());
xml::Factory factory(node);
node = *factory.read(ss);
*/
}
std::set<Serialize::FromNodeFunc> Serialize::_fromNode;
std::set<Serialize::ToNodeFunc> Serialize::_toNode;
//=============================================================== Assign Types
template<typename TYPE> bool assignFromNode(boost::any member,
template<typename TYPE> bool assignFromNode(Any member,
const xml::Node& node) {
if (!boost::any_cast<TYPE*>(&member)) return false;
*boost::any_cast<TYPE*>(member) =
if (!any_cast<TYPE*>(&member)) return false;
*any_cast<TYPE*>(member) =
(TYPE)dynamic_cast<const xml::String&>(node);
return true;
}
template<typename TYPE> bool assignToNode(const boost::any member,
template<typename TYPE> bool assignToNode(const Any member,
xml::Node& node) {
if (!boost::any_cast<TYPE*>(&member)) return false;
if (!any_cast<TYPE*>(&member)) return false;
std::stringstream ss;
ss<<*boost::any_cast<TYPE*>(member);
ss<<*any_cast<TYPE*>(member);
node.text(ss.str());
return true;
}
template<> bool assignFromNode<bool>(boost::any member,
template<> bool assignFromNode<bool>(Any member,
const xml::Node& node) {
if (member.type()!=typeid(bool*)) return false;
std::string s(*dynamic_cast<const xml::String&>(node));
@ -1189,46 +1210,46 @@ namespace xml {
start(s.find_first_not_of(" \t\n\r")),
end(s.find_last_not_of(" \t\n\r"));
if (start==std::string::npos) {
*boost::any_cast<bool*>(member) = false;
*any_cast<bool*>(member) = false;
} else {
s = s.substr(start, end-start+1);
*boost::any_cast<bool*>(member) =
*any_cast<bool*>(member) =
s!="0" && s!="false" && s!="no" && s!="off";
}
return true;
}
template<> bool assignToNode<bool>(const boost::any member,
template<> bool assignToNode<bool>(const Any member,
xml::Node& node) {
if (member.type()!=typeid(bool*)) return false;
node.text(*boost::any_cast<bool*>(member)?"true":"false");
node.text(*any_cast<bool*>(member)?"true":"false");
return true;
}
template<> bool assignFromNode<Serialize>(boost::any member,
template<> bool assignFromNode<Serialize>(Any member,
const xml::Node& node) {
if (!boost::any_cast<Serialize*>(&member)) return false;
if (!any_cast<Serialize*>(&member)) return false;
//! @todo improve this (inefficient)
std::stringstream ss; // simple but inefficient: rewrite and reread
ss<<node;
boost::any_cast<Serialize*>(member)->loadXml(ss, node.name());
any_cast<Serialize*>(member)->loadXml(ss, node.name());
/*
Serialize* ser(boost::any_cast<Serialize*>(member));
Serialize* ser(any_cast<Serialize*>(member));
for (xml::Node::size_type i(0); i<node.children(); ++i)
ser->fromNode(ser->_xmlNames[*node[i]], node[i]);*/
return true;
}
template<> bool assignToNode<Serialize>(const boost::any member,
template<> bool assignToNode<Serialize>(const Any member,
xml::Node& node) {
if (!boost::any_cast<Serialize*>(&member)) return false;
if (!any_cast<Serialize*>(&member)) return false;
std::stringstream ss;
boost::any_cast<Serialize*>(member)->saveXml(ss, node.name());
any_cast<Serialize*>(member)->saveXml(ss, node.name());
xml::Factory factory(node);
node = *factory.read(ss);
return true;
}
// Register for all simple Types =============================================
template<int NUM> struct SimpleTypes {};
template<> struct SimpleTypes<1> {typedef std::string Type;};
template<> struct SimpleTypes<2> {typedef Serialize Type;};
template<> struct SimpleTypes<1> {typedef Serialize Type;};
template<> struct SimpleTypes<2> {typedef std::string Type;};
template<> struct SimpleTypes<3> {typedef bool Type;};
template<> struct SimpleTypes<4> {typedef unsigned char Type;};
template<> struct SimpleTypes<5> {typedef signed char Type;};

Loading…
Cancel
Save