|
Boost Users : |
From: Dan Thill (dgt_at_[hidden])
Date: 2007-06-05 15:36:55
Greetings,
I have a need to serialize plain old C character arrays as strings
within the xml and text archive types. By default, these are serialized
element by element. I've read the previous messages on the list on the
perils of doing such (buffer overflows), and I hit across a somewhat
acceptable solution using serialization wrappers. I'm new to all of
this, so I based my implementation off of "nvp" and "binary_object"
-------------------------------
struct char_array {
char* const m_t;
const std::size_t m_size;
template<class Archive>
void save(Archive & ar, const unsigned int /* file_version */) const {
const std::string t(m_t);
ar.operator<<(t);
}
template<class Archive>
void load(Archive & ar, const unsigned int /* file_version */) const {
std::string t;
ar.operator>>(t);
scASSERT_MSG(t.size() < m_size,
"A corruption occurred");
strncpy(const_cast<char*>(m_t), t.c_str(), m_size);
}
BOOST_SERIALIZATION_SPLIT_MEMBER()
explicit char_array( char* const t, std::size_t size) :
m_t(t),
m_size(size)
{}
char_array(const char_array & rhs) :
m_t(rhs.m_t),
m_size(rhs.m_size)
{}
};
-----------------------------------------------
The premise is simple, probably naive, and not too efficient: Convert
each char[] to a std::string, and serialize that. Loading is just the
reverse.
However, I keep getting a compile time exception in the xml archive
loading code. This is with Boost 1.33, VC8.
1>c:\Boost\include\boost-1_33_1\boost/archive/basic_xml_oarchive.hpp(86)
: error C2027: use of undefined type 'boost::STATIC_ASSERTION_FAILURE<x>'
1> with
1> [
1> x=false
1> ]
1>
c:\Boost\include\boost-1_33_1\boost/archive/detail/interface_oarchive.hpp(78)
: see reference to function template instantiation 'void
boost::archive::basic_xml_oarchive<Archive>::save_override<T>(T &,int)'
being compiled
1> with
1> [
1> Archive=boost::archive::xml_oarchive,
1> T=const std::string
1> ]
1> d:\SDEV8\SOURCE\include\sc_s11n/char_array.h(49) : see
reference to function template instantiation 'Archive
&boost::archive::detail::interface_oarchive<Archive>::operator <<<const
std::string>(T &)' being compiled
1> with
1> [
1> Archive=boost::archive::xml_oarchive,
1> T=const std::string
1> ]
It seems as if, after serializing the nvp object, it's expecting an nvp
object around the std::string. Is there a way that I can call the
default serialization for std::string from my char_array wrapper,
without having it go through the normal xml specializations which
require it to be part of an nvp?
I'm new with this, so please be gentle. I've consumed almost the entire
list archive and I can't come up with a fix.
Thanks,
Dan
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net