storing "almost-POD" classes to binary file

Discussion in 'C++' started by Jacek Dziedzic, Dec 18, 2005.

  1. Hello!

    Suppose I have a class that contains only public members
    of builtin types and a default constructor. Because of the
    default constructor it is no longer an aggregate and therefore
    no longer POD, according to my understanding of

    http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html

    I need to be able to serialize this class to/from a binary file
    and read() and write() look like the easiest way. But if I
    understand correctly, this is *not* guaranteed to work because
    the class is non-POD, right?

    Arrays of this class (let's call it 'data') need to be
    stored/restored to/from a binary file in a templated class
    datafile<data>. The class 'datafile' is a part of a library,
    while 'data' is supplied by the library user who wants to store
    arrays of 'data' into the file represented by 'datafile<data>'.

    How would you go around this? I could remove the default
    ctor and add a public init() method, which would make it POD,
    but what happens if the user tries her 'data' class that
    contains non-POD members, therefore making it non-POD.

    Perhaps I should require that the data class supplies
    some methods like serialize_to_bytes() and restore_from_bytes()?
    That would be easy for the library, but annoying for users
    who would then instead of simple

    class data {
    public:
    int myint;
    double mydouble;
    long int mylong;
    };

    have to use

    class data {
    public:
    int myint;
    double mydouble;
    long int mylong;

    void serialize_to_bytes(...) {
    // ... quite a few lines of code
    }

    void restore_from_bytes(...) {
    // ... quite a few lines of code
    }
    };

    Or perhaps storing/reading classes like that is safe even
    though they contain members that are not strictly POD because
    they have constructors? I am assuming no members contain
    static data, there are also no pointers or pointers-to-members
    involved.

    TIA,
    - J.
    Jacek Dziedzic, Dec 18, 2005
    #1
    1. Advertising

  2. On Sun, 18 Dec 2005 18:49:17 +0100, Jacek Dziedzic
    <jacek@no_spam.tygrys.no_spam.net> wrote:

    >
    > Hello!
    >
    > Suppose I have a class that contains only public members
    >of builtin types and a default constructor. Because of the
    >default constructor it is no longer an aggregate and therefore
    >no longer POD, according to my understanding of
    >
    >http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html
    >
    > I need to be able to serialize this class to/from a binary file
    >and read() and write() look like the easiest way. But if I
    >understand correctly, this is *not* guaranteed to work because
    >the class is non-POD, right?
    >
    > Arrays of this class (let's call it 'data') need to be
    >stored/restored to/from a binary file in a templated class
    >datafile<data>. The class 'datafile' is a part of a library,
    >while 'data' is supplied by the library user who wants to store
    >arrays of 'data' into the file represented by 'datafile<data>'.
    >
    > How would you go around this? I could remove the default
    >ctor and add a public init() method, which would make it POD,
    >but what happens if the user tries her 'data' class that
    >contains non-POD members, therefore making it non-POD.
    >
    > Perhaps I should require that the data class supplies
    >some methods like serialize_to_bytes() and restore_from_bytes()?
    >That would be easy for the library, but annoying for users
    >who would then instead of simple
    >
    >class data {
    >public:
    > int myint;
    > double mydouble;
    > long int mylong;
    >};
    >
    >have to use
    >
    >class data {
    >public:
    > int myint;
    > double mydouble;
    > long int mylong;
    >
    > void serialize_to_bytes(...) {
    > // ... quite a few lines of code
    > }
    >
    > void restore_from_bytes(...) {
    > // ... quite a few lines of code
    > }
    >};
    >
    > Or perhaps storing/reading classes like that is safe even
    >though they contain members that are not strictly POD because
    >they have constructors? I am assuming no members contain
    >static data, there are also no pointers or pointers-to-members
    >involved.
    >
    >TIA,
    >- J.


    I think the only workable idea is the one requiring users to implement
    or overload the serialization functions themselves. Even for POD
    structs, there might be byte padding and/or alignment issues, and the
    default case where there is no padding is trivial enough to implement
    that it isn't really worth putting into a library.

    --
    Bob Hairgrove
    Bob Hairgrove, Dec 18, 2005
    #2
    1. Advertising

  3. Jacek Dziedzic

    Guest

    Jacek Dziedzic wrote:
    > Hello!
    >
    > Suppose I have a class that contains only public members
    > of builtin types and a default constructor. Because of the
    > default constructor it is no longer an aggregate and therefore
    > no longer POD.


    Yep. OTOH, if you put all the data members in a base class, that
    base class will be a POD. You can serialize that class. Deserializing
    is a bit trickier. You can deserialize a temporary and then copy it C++
    style over the base object (or write another ctor that takes a base
    const&)

    HTH,
    Michiel Salters
    , Dec 19, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Gianni Mariani
    Replies:
    0
    Views:
    369
    Gianni Mariani
    Jan 6, 2004
  2. Arnold
    Replies:
    7
    Views:
    500
    Martijn Lievaart
    Jan 7, 2004
  3. Arnold
    Replies:
    9
    Views:
    436
    Martijn Lievaart
    Jan 7, 2004
  4. toton
    Replies:
    11
    Views:
    709
    toton
    Oct 13, 2006
  5. Jonathan Wood
    Replies:
    1
    Views:
    508
    Jonathan Wood
    Jun 2, 2008
Loading...

Share This Page