Boost logo

Boost Users :

Subject: Re: [Boost-users] How to serialize basic_string<unsigned char>
From: Martin Dietze (di_at_[hidden])
Date: 2010-04-10 14:31:39


As a workaround the code below works. However this
solution is only possible because <char> and <unsigned
char> can be assigned like this. I'd still be interested
to learn how to do this without ugly code like this...

------------------------ SNIP -------------------------

BOOST_CLASS_IMPLEMENTATION(std::basic_string<unsigned char>,
                           boost::serialization::primitive_type)
class A {
public:
  std::basic_string<unsigned char> m_string;
private:
  friend class boost::serialization::access;
  template<class Archive>
  void save (Archive & ar, const unsigned int version) const
  {
    std::basic_string<char> s;
    s.assign (m_string.size (), (char)0);
    for (int i = 0; i < m_string.size (); ++i)
      {
        s[i] = (char)m_string[i];
      }
    ar & s;
  }
  template<class Archive>
  void load (Archive & ar, const unsigned int version)
  {
    std::basic_string<char> s;
    ar & s;
    m_string.assign (s.size (), (unsigned char)0);
    for (int i = 0; i < s.size (); ++i)
      {
        m_string[i] = (unsigned char)s[i];
      }
  }
  BOOST_SERIALIZATION_SPLIT_MEMBER()
};

------------------------ SNIP -------------------------

M'bert

-- 
----------- / http://herbert.the-little-red-haired-girl.org / -------------
=+= 
It was hard to code, so it should be hard to understand!

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