Boost logo

Boost :

From: Vladimir Prus (ghost_at_[hidden])
Date: 2004-01-15 03:10:07


Robert Ramey wrote:

>> while(ifs) {
>> some_object obj;
>> ia >> obj;
>> objects.push_back(obj);
>> }
>
>>The problem is that I don't know what archive will do on stream errors.
>>For the above to work, it should stop doing anything. But I can't find any
>>documentation about what happens -- I think it's better to discuss and
>>document that.
>
> I recently added stream error checking to the archives. An attempt
> to read past eof will result in throwing and exception
> boost::archive::archive_exception::stream_error.

Say I catch that exception. Is it possible to tell if it was through because
of eofbit in stream -- which case I'll ignore, or because of
failbit/badbit?

> In my view the above would be an error. It couple the serialization
> of data to the archive type. There is not requirement that an archive
> be based on streams.

In fact, I don't think we need such requirement. What about:

   some_object obj;
   for(ia >> obj) {
       objects.push_back(obj);
   }

In this case the archive will report if it's in good state or not. It is
also possible to do the following, if errors are reported via exceptions

   try {
      some_object obj;
      for(;;) {
          ia >> obj;
          objects.push_back(obj);
      }
   }
   catch(boost::archive::eof_error&)
   {
   }

It's maybe better than supporting both non-throwing and throwing error
reporting, as in iostreams.

> So I would write the above as:
>
>
> ifstream ifs(...);
> boost::binary_iarchive ia(ifs);
>
> size_t count;
> ia >> count;

Not an option, unfortunately.

> or if havng a count is intolerable
>
> optional<T> obj;
> for(;;){
> ia >> obj;
> if(! obj.is_initialized)
> break;
> objects. push_back obj
> }

I don't think I like this approach either. It requires that you store some
different type, which you don't really need. And that type is larger, so
you get one word penalty for each item that you save, while it only matters
at the end of stream.

And in general, it's not possible to store some 'invalid' value, that can
signal end of data.

A solution that would be fine with me is to have a way to tell if exception
is thrown because of eof, or other reasons. What do you think?

- Volodya

>
> or whatever
>
> Robert Ramey
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk