Boost logo

Boost Users :

From: Bill Lear (rael_at_[hidden])
Date: 2005-01-24 17:46:27


On Monday, January 24, 2005 at 13:07:41 (-0600) Bill Lear writes:
>On Monday, January 24, 2005 at 08:54:23 (-0800) Robert Ramey writes:
>>How does it fail? Does it fail to compile? Does it give an exception? If
>>so which one. If it fails with text or binary archive, does it also fail on
>>xml archives? that would be useful information.
>
>I'll try to get together a complete test case by this evening.

Below is test code that shows the problem. It is working with text
and failing with XML serialization --- perhaps I'm simply not
understanding something else that I need to do, though we are using
XML serialization extensively.

The text serialization produces this as output:

22 serialization::archive 3 0 0 10 0 0 0 0 0 0 0 0 0 0 0 9

The XML serialization produces this as output:

<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE boost_serialization>
<boost_serialization signature="serialization::archive" version="3">
<buff class_id="0" tracking_level="0" version="0">
        <std::vector&lt;T&gt;>
                <count>10</count>
                <item>0</item>
                <item>0</item>
                <item>0</item>
                <item>0</item>
                <item>0</item>
                <item>0</item>
                <item>0</item>
                <item>0</item>
                <item>0</item>
                <item>0</item>
        </std::vector&lt;T&gt;>
        <_first_ind>0</_first_ind>
        <_last_ind>9</_last_ind>
</buff>

Following my sig is the code. The failure cases fail during the load.
This is the output:

% ./a.out
working_with_vector_text_archive()
working_with_ring_buffer_text_archive()
working_with_ring_buffer_pointer_text_archive()
failure_with_ring_buffer_pointer_xml_archive()
ERROR: stream error
failure_with_ring_buffer_xml_archive()
ERROR: stream error

Bill

#include <iostream>
#include <vector>
#include <fstream>
#include <stdexcept>

#include <boost/archive/text_iarchive.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/export.hpp>
#include <boost/serialization/vector.hpp>

template <class T>
class RingBuffer: private std::vector<T> {
public:
    RingBuffer()
        : _first_ind(0), _last_ind(0) {
    }

    double first() {
        return (*this)[_first_ind];
    }

    double last() {
        return (*this)[_last_ind];
    }

    void resize(size_t new_size) {
        std::vector<T>::resize(new_size);
        zero_fill();
    }

    void zero_fill() {
        fill(begin(), end(), T());
        _first_ind = 0;
        _last_ind = size() - 1;
    }

    void add(T v) {
        if (!size()) {
            throw std::logic_error("add to a ring buffer of zero size");
        }

        _last_ind = (_last_ind + 1) % size();

        (*this)[_last_ind] = v;

        if (_last_ind == _first_ind) {
            _first_ind = (_first_ind + 1) % size();
        }
    }

private:
    int _first_ind;
    int _last_ind;

    friend class boost::serialization::access;

    template<class Archive>
    void serialize(Archive& ar, const unsigned int) {
        ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(std::vector<T>)
           & BOOST_SERIALIZATION_NVP(_first_ind)
           & BOOST_SERIALIZATION_NVP(_last_ind);
    }
};

void working_with_vector_text_archive() {
    std::cerr << "working_with_vector_text_archive()\n";
    try {
        std::vector<int> buff;
        buff.resize(10);

        std::ofstream out("buf.out");
        boost::archive::text_oarchive oa(out);
        oa << buff;
        out.close();

        std::ifstream in("buf.out");
        boost::archive::text_iarchive ia(in);
        std::vector<int> restored;
        ia >> restored;
    } catch (std::exception& e) {
        std::cerr << "ERROR: " << e.what() << '\n';
    }
}

void working_with_ring_buffer_text_archive() {
    std::cerr << "working_with_ring_buffer_text_archive()\n";
    try {
        RingBuffer<int> buff;
        buff.resize(10);

        std::ofstream out("buf.out");
        boost::archive::text_oarchive oa(out);
        oa << buff;
        out.close();

        std::ifstream in("buf.out");
        boost::archive::text_iarchive ia(in);
        RingBuffer<int> restored;
        ia >> restored;
    } catch (std::exception& e) {
        std::cerr << "ERROR: " << e.what() << '\n';
    }
}

void working_with_ring_buffer_pointer_text_archive() {
    std::cerr << "working_with_ring_buffer_pointer_text_archive()\n";
    try {
        RingBuffer<int> buff;
        buff.resize(10);

        std::ofstream out("buf.out");
        boost::archive::text_oarchive oa(out);
        oa << buff;
        out.close();

        std::ifstream in("buf.out");
        boost::archive::text_iarchive ia(in);
        RingBuffer<int>* restored;
        ia >> restored;
    } catch (std::exception& e) {
        std::cerr << "ERROR: " << e.what() << '\n';
    }
}

void failure_with_ring_buffer_pointer_xml_archive() {
    std::cerr << "failure_with_ring_buffer_pointer_xml_archive()\n";
    try {
        RingBuffer<int> buff;
        buff.resize(10);

        std::ofstream out("buf.out");
        boost::archive::xml_oarchive oa(out);
        oa << BOOST_SERIALIZATION_NVP(buff);
        out.close();

        std::ifstream in("buf.out");
        boost::archive::xml_iarchive ia(in);
        RingBuffer<int>* restored;
        ia >> BOOST_SERIALIZATION_NVP(restored);
    } catch (std::exception& e) {
        std::cerr << "ERROR: " << e.what() << '\n';
    }
}

void failure_with_ring_buffer_xml_archive() {
    std::cerr << "failure_with_ring_buffer_xml_archive()\n";
    try {
        RingBuffer<int> buff;
        buff.resize(10);

        std::ofstream out("buf.out");
        boost::archive::xml_oarchive oa(out);
        oa << BOOST_SERIALIZATION_NVP(buff);
        out.close();

        std::ifstream in("buf.out");
        boost::archive::xml_iarchive ia(in);
        RingBuffer<int> restored;
        ia >> BOOST_SERIALIZATION_NVP(restored);
    } catch (std::exception& e) {
        std::cerr << "ERROR: " << e.what() << '\n';
    }
}

int main() {
    working_with_vector_text_archive();
    working_with_ring_buffer_text_archive();
    working_with_ring_buffer_pointer_text_archive();
    failure_with_ring_buffer_pointer_xml_archive();
    failure_with_ring_buffer_xml_archive();
}


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