Re[18]: сериализация данных в избранное  msdn  новое всё   Оценить +1123x:) +-   подписка   модер. 
От: k732 
Дата: 14.06.07 10:21
Оценка:3 (1)
Здравствуйте, Sergey, Вы писали:

S>Тогда показывай определение класса container_stream.


Примерно так
разделил классы

template<typename Container>
class rcontainer {
private:
    typedef typename Container::size_type   size_type;
    const Container&  m_container;
    size_type   m_index;

public:
    typedef typename Container::value_type    char_type;
    typedef boost::iostreams::source_tag    category;

    rcontainer (const Container& container) : m_container(container), m_index(0) { }
    ~rcontainer () { }

    std::streamsize read (char_type* value, std::streamsize size)
    {
        std::streamsize delta = static_cast<std::streamsize> (
            m_container.size() - m_index
        );

        std::streamsize result = std::min<std::streamsize> (size, delta);
        if (!result) return -1;

        std::copy (
            m_container.begin() + m_index,
            m_container.begin() + m_index + result, value
        );
        m_index += result;
        return result;
    }
};
/* -------------------------------------------------------------------------- */
template<typename Container>
class wcontainer {
private:
    typedef typename Container::size_type   size_type;
    Container&  m_container;
    size_type   m_index;

public:
    typedef typename Container::value_type    char_type;
    typedef boost::iostreams::sink_tag        category;

    wcontainer (Container& container)  : m_container(container), m_index(0) { }
    ~wcontainer () { }

    std::streamsize write (const char_type* value, std::streamsize size)
    {
        m_container.insert(m_container.end(), value, value + size);
        return size;
    }
};


Теперь парсер (попробуй у него поменять )
typedef std::vector<unsigned char> sbuffer;

на
typedef std::vector<char> sbuffer;


namespace parser
{
    /* ---------------------------------------------------------------------- */
    typedef std::vector<unsigned char> sbuffer;
    typedef rcontainer <sbuffer> rdevice;
    typedef wcontainer <sbuffer> wdevice;
    /* ---------------------------------------------------------------------- */
    template <class T>
    inline void serialize (sbuffer& buffer, const T& rhs)
    {
        try
        {
            namespace bio = boost::iostreams;

            wdevice device (buffer);
            bio::stream_buffer<wdevice> stream (device);
            std::basic_ostream <unsigned char> ostream (&stream);
            //std::ostream ostream (&stream);

            boost::archive::binary_oarchive archive(ostream);
            archive << BOOST_SERIALIZATION_NVP (rhs);

        }
        catch (std::exception& ex)
        {
            std::cerr << ex.what() << std::endl;
        }
    }
    /* ---------------------------------------------------------------------- */
    template <class T>
    inline void deserialize (const sbuffer& buffer, T& rhs)
    {
        try
        {
            namespace bio = boost::iostreams;

            rdevice device (buffer);
            bio::stream_buffer<rdevice> stream (device);
            std::istream istream (&stream);


            boost::archive::binary_iarchive archive (istream);
            archive >> BOOST_SERIALIZATION_NVP (rhs);
        }
        catch (std::exception& ex)
        {
            std::cerr << ex.what() << std::endl;
        }
    }
    /* ---------------------------------------------------------------------- */
}