serialization question (using composite STL classes)

Discussion in 'C++' started by Bit Byte, Jan 3, 2007.

  1. Bit Byte

    Bit Byte Guest

    Suppose I have a data variable defined thus:

    typedef std::vector<std::pair<std::string,std::string> > InfoVector ;

    I have written a template function to generalize serialization:

    template <class T> std::eek:stream& Serialize(std::eek:stream& out, const T&
    value)
    {
    out.write(reinterpret_cast<const char*>(&value), sizeof(T)) ) ;
    }

    Can I pass InfoVector to my template function (or do I need a partial
    specialized template ?). Any clarification on this issue will be very
    helpful
     
    Bit Byte, Jan 3, 2007
    #1
    1. Advertising

  2. Bit Byte wrote:

    > Suppose I have a data variable defined thus:
    > typedef std::vector<std::pair<std::string,std::string> > InfoVector ;


    This is not the definition of a variable.

    > I have written a template function to generalize serialization:
    >
    > template <class T> std::eek:stream& Serialize(std::eek:stream& out, const T&
    > value)
    > {
    > out.write(reinterpret_cast<const char*>(&value), sizeof(T)) ) ;
    > }


    This is not serialization at all, is just raw data writing.

    http://en.wikipedia.org/wiki/Serialization

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 3, 2007
    #2
    1. Advertising

  3. Bit Byte

    bjeremy Guest

    Bit Byte wrote:
    > Suppose I have a data variable defined thus:
    >
    > typedef std::vector<std::pair<std::string,std::string> > InfoVector ;
    >
    > I have written a template function to generalize serialization:
    >
    > template <class T> std::eek:stream& Serialize(std::eek:stream& out, const T&
    > value)
    > {
    > out.write(reinterpret_cast<const char*>(&value), sizeof(T)) ) ;
    > }
    >
    > Can I pass InfoVector to my template function (or do I need a partial
    > specialized template ?). Any clarification on this issue will be very
    > helpful


    Your writing the address of your InfoVector (plus some garbage since
    you are writing sizeof(InfoVector) bytes which is greater than and
    address). As Julian points out, this is not serialization. You would
    need to step through the vector and serialize the value (not reference)
    of each pair respectively in such a way as a copy of the original
    vector can be pieced back together again. Java has some langauge
    support for this, I think Boost has some libraries for C++, however, I
    never used them.
     
    bjeremy, Jan 4, 2007
    #3
  4. Bit Byte

    David Harmon Guest

    On Wed, 03 Jan 2007 21:51:13 +0000 in comp.lang.c++, Bit Byte
    <> wrote,
    >Can I pass InfoVector to my template function


    Not a chance! std::vector, std::string, and many other classes manage
    dynamic data that has no resemblance to anything compile-time sizeof
    knows about.
     
    David Harmon, Jan 4, 2007
    #4
  5. Julián Albo wrote:
    > Bit Byte wrote:
    >
    > > Suppose I have a data variable defined thus:
    > > typedef std::vector<std::pair<std::string,std::string> > InfoVector ;

    >
    > This is not the definition of a variable.
    >
    > > I have written a template function to generalize serialization:
    > >
    > > template <class T> std::eek:stream& Serialize(std::eek:stream& out, const T&
    > > value)
    > > {
    > > out.write(reinterpret_cast<const char*>(&value), sizeof(T)) ) ;
    > > }

    >
    > This is not serialization at all, is just raw data writing.
    >
    > http://en.wikipedia.org/wiki/Serialization
    >
    > --
    > Salu2


    But raw writing is useful when endianess is not an issue
     
    Diego Martins, Jan 4, 2007
    #5
  6. Diego Martins wrote:

    >> This is not serialization at all, is just raw data writing.

    >
    > But raw writing is useful when endianess is not an issue


    Usefulness or lack or it are not reasons to call it serialization.

    --
    Salu2
     
    =?ISO-8859-15?Q?Juli=E1n?= Albo, Jan 4, 2007
    #6
  7. Julián Albo wrote:
    > Diego Martins wrote:
    >
    > >> This is not serialization at all, is just raw data writing.

    > >
    > > But raw writing is useful when endianess is not an issue

    >
    > Usefulness or lack or it are not reasons to call it serialization.
    >
    > --
    > Salu2


    you are right

    but my point is, in most cases, one can use raw writing which is fast
    and simple, instead of creating a bunch of boring serialization classes
     
    Diego Martins, Jan 5, 2007
    #7
    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. sleigh
    Replies:
    1
    Views:
    2,707
    sleigh
    Feb 12, 2004
  2. Replies:
    8
    Views:
    2,273
    deadsea
    Jan 2, 2005
  3. Replies:
    3
    Views:
    1,056
  4. Dimitri Ognibene
    Replies:
    4
    Views:
    797
    Dimitri Ognibene
    Sep 2, 2006
  5. Ramunas Urbonas
    Replies:
    1
    Views:
    412
    Dino Chiesa [Microsoft]
    Jul 27, 2004
Loading...

Share This Page