0

In one of my projects I have to cache positional information about certain data chunks found in large files. I've already implemented a small API built around std::basic_istream<char>::pos_type placed in maps.

Now I need to serialize these descriptors into a bytestream and write them on a disk for further usage (on other *nix-machines as well). I have read that this type is platform-dependent but still rather being a POD-type. So my questions are:

  • Whether it will be better to save something besides of just offsets? E.g. std::fpos<std::mbstate_t> keeping the state of reading structure?
  • How can I safely obtain and restore the offset data from std::basic_istream<char>::pos_type (and other info if it is need)?

Thank you in advance.

Crank
  • 55
  • 1
  • 7
  • Do you actually care to serialize the intra-character state? Are you really using UTF-8 or similar and may need to serialize a position which is WITHIN a single character? – John Zwinck Dec 21 '16 at 08:17
  • No I'm only interested in atomic byte offsets. But complain about any hidden states of stream-reading structs that possibly can affect the performance (which does matter for me). – Crank Dec 21 '16 at 08:24
  • It sounds like you can just serialize the `streamoff` which is just a number. Since you say you don't care to serialize intra-character positions, this is enough. You can store it in an int64_t. – John Zwinck Dec 21 '16 at 10:00
  • Ok, apparently I have overthought it. Thank you, John. – Crank Dec 21 '16 at 12:00

1 Answers1

0

The structure of std::fpos<mbstate_t> is unspecified and there may be non-trivial state in the mbstate_t. You certainly can't portably serialize these objects. You can obtain a value of the offset type (std::streamoff) which is an integer type and its value can be serialized.

Dietmar Kühl
  • 150,225
  • 13
  • 225
  • 380
  • I've implemented it by static casting the `pos_type` returned by `istream::tellg()` to `std::streamoff`. I guess it is a legitimate way since `char_traits::pos_type` is defined as `std::fpos` which has defined conversion operator to `std::streamoff` as well as ctr according to `std::fpos` [reference page](http://en.cppreference.com/w/cpp/io/fpos). Thank you for answer, Dietmar. – Crank Dec 21 '16 at 12:09