|
Boost Users : |
From: Robert Ramey (ramey_at_[hidden])
Date: 2007-06-05 22:04:05
Your basic idea to use a wrapper is a good one.
However, I think you picked the wrong wrapper.
Consider binary_object. This is just a wrapper which
includes a size (number of bytes) - and raw data.
If you want to use the facilities of text streams to
alter the characters between languages, this won't work
but I'm doubtful that you want that.
Another idea is to cast your character array to a ...
character array - like static_cast< int [64] &>(my array address)
but that would require a constant size which might not work
for you.
Finally you can use the blunt instrument approach.
save(....
std::string temp_string;
// copy data into temp_string
ar << temp_string
load
.. do the opposite.
Robert Ramey
Dan Thill wrote:
> 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