using boost::any for storing member pointers to any type

master
Marc Wäckerlin 16 years ago
parent c16fe5421e
commit 1e09b8a696
  1. 29
      src/xml-cxx/xml.hxx
  2. 53
      src/xml.cxx

@ -17,6 +17,7 @@
#include <memory>
#include <typeinfo>
#include <stdexcept>
#include <boost/any.hpp>
#include <cassert>
#include <iostream>
@ -836,10 +837,7 @@ namespace xml {
Serialize& persist(std::list<TYPE, ALLOC>& member,
const std::string& list,
const std::string& item) throw() {
mapName<std::list<TYPE, ALLOC> >()[std::make_pair(this, list)]
= &member;
mapMember<std::list<TYPE, ALLOC> >()[&member] = list;
_xmlNames[list] = &typeid(TYPE);
_xmlNames[list] = &member;
Serialize ser(list);
TYPE dummy;
ser.persist(dummy, item);
@ -886,31 +884,12 @@ namespace xml {
template<typename TYPE>
Serialize& persistSimpleType(TYPE& member,
const std::string& name) throw() {
ASSERT("type: "<<typeid(TYPE).name()<<"; name="<<name,
mapName<TYPE>().find(std::make_pair(this, name))
==mapName<TYPE>().end());
ASSERT("type: "<<typeid(TYPE).name()<<"; name="<<name,
mapMember<TYPE>().find(&member)==mapMember<TYPE>().end());
ASSERT("type: "<<typeid(TYPE).name()<<"; name="<<name,
_xmlNames.find(name)==_xmlNames.end());
mapName<TYPE>()[std::make_pair(this, name)] = &member;
mapMember<TYPE>()[&member] = name;
_xmlNames[name] = &typeid(TYPE);
_xmlNames[name] = &member;
xml::Node schema(*_xmlFactory);
schema<<xml::String(name).limits(1,1);
_xmlFactory = schema;
return *this;
}
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 {
static std::map<TYPE*, std::string> MAP;
return MAP;
}
template<typename TYPE> void fromNode(TYPE* member, xml::Node& node) {
*member = (TYPE)dynamic_cast<xml::String&>(node);
}
@ -926,7 +905,7 @@ namespace xml {
ss<<*member;
node.text(ss.str());
}
std::map<std::string, const std::type_info*> _xmlNames;
std::map<std::string, boost::any> _xmlNames;
xml::Factory _xmlFactory;
};

@ -1058,9 +1058,7 @@ namespace xml {
Serialize& Serialize::persist(Serialize& ser,
const std::string& name) throw() {
if (!ser._xmlFactory) ser.initXmlMembers();
mapName<Serialize>()[std::make_pair(this, name)] = &ser;
mapMember<Serialize>()[&ser] = name;
_xmlNames[name] = &typeid(Serialize);
_xmlNames[name] = &ser;
xml::Node schema(*_xmlFactory);
xml::Node node(xml::Node(name).limits(1,1));
for (xml::Node::size_type i(0); i<ser._xmlFactory->children(); ++i)
@ -1125,12 +1123,12 @@ namespace xml {
if (!_xmlFactory) const_cast<Serialize*>(this)->initXmlMembers();
xml::Node node(*_xmlFactory);
if (name.size()) node.name(name);
for (std::map<std::string, const std::type_info*>::const_iterator
for (std::map<std::string, boost::any>::const_iterator
it(_xmlNames.begin());
it!=_xmlNames.end(); ++it) {
#define QWERTZ_CHECK_TYPE_ZTREWQ___XXX(TYPE) \
if (*it->second==typeid(TYPE)) \
toNode(mapName<TYPE>()[std::make_pair(this, it->first)], \
if (it->second.type()==typeid(TYPE*)) \
toNode(boost::any_cast<TYPE*>(it->second), \
node[it->first]); \
else
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(Serialize)
@ -1150,7 +1148,7 @@ namespace xml {
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(long)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(signed long)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(unsigned long)
throw std::runtime_error(it->second->name());
throw std::runtime_error(it->first);
#undef QWERTZ_CHECK_TYPE_ZTREWQ___XXX
}
os<<node;
@ -1161,12 +1159,12 @@ 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, const std::type_info*>::const_iterator
for (std::map<std::string, boost::any>::const_iterator
it(_xmlNames.begin());
it!=_xmlNames.end(); ++it) {
#define QWERTZ_CHECK_TYPE_ZTREWQ___XXX(TYPE) \
if (*it->second==typeid(TYPE)) \
fromNode(mapName<TYPE>()[std::make_pair(this, it->first)], \
if (it->second.type()==typeid(TYPE*)) \
fromNode(boost::any_cast<TYPE*>(it->second), \
(*node)[it->first]); \
else
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(Serialize)
@ -1186,7 +1184,7 @@ namespace xml {
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(long)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(signed long)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(unsigned long)
throw std::runtime_error(it->second->name());
throw std::runtime_error(it->first);
#undef QWERTZ_CHECK_TYPE_ZTREWQ___XXX
}
return is;
@ -1199,40 +1197,11 @@ namespace xml {
}
void Serialize::initXmlMembers() {}
void Serialize::clear() throw() {
// Remove my entries from the maps
for (std::map<std::string, const std::type_info*>::const_iterator
it(_xmlNames.begin());
it!=_xmlNames.end(); ++it) {
#define QWERTZ_CHECK_TYPE_ZTREWQ___XXX(TYPE) \
if (*it->second==typeid(TYPE)) { \
mapMember<TYPE>() \
.erase(mapName<TYPE>()[std::make_pair(this, it->first)]); \
mapName<TYPE>().erase(std::make_pair(this, it->first)); \
} 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)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(bool)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(char)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(signed char)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(unsigned char)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(short)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(signed short)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(unsigned short)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(int)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(signed int)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(unsigned int)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(long)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(signed long)
QWERTZ_CHECK_TYPE_ZTREWQ___XXX(unsigned long)
{} // ignore coding error here, but memory leaks - better abort?
#undef QWERTZ_CHECK_TYPE_ZTREWQ___XXX
}
_xmlNames.clear();
_xmlFactory.reset();
}
void Serialize::copy(const Serialize& o) throw() {
clear();
_xmlFactory.reset();
initXmlMembers();
}

Loading…
Cancel
Save