Connecting Tech Pros Worldwide Forums | Help | Site Map

serialization question (using composite STL classes)

Bit Byte
Guest
 
Posts: n/a
#1: Jan 3 '07
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 Tstd::ostream& Serialize(std::ostream& 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

=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
 
Posts: n/a
#2: Jan 3 '07

re: serialization question (using composite STL classes)


Bit Byte wrote:
Quote:
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.
Quote:
I have written a template function to generalize serialization:
>
template <class Tstd::ostream& Serialize(std::ostream& 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
bjeremy
Guest
 
Posts: n/a
#3: Jan 4 '07

re: serialization question (using composite STL classes)



Bit Byte wrote:
Quote:
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 Tstd::ostream& Serialize(std::ostream& 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.

David Harmon
Guest
 
Posts: n/a
#4: Jan 4 '07

re: serialization question (using composite STL classes)


On Wed, 03 Jan 2007 21:51:13 +0000 in comp.lang.c++, Bit Byte
<root@yourbox.comwrote,
Quote:
>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.

Diego Martins
Guest
 
Posts: n/a
#5: Jan 4 '07

re: serialization question (using composite STL classes)



Julián Albo wrote:
Quote:
Bit Byte wrote:
>
Quote:
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.
>
Quote:
I have written a template function to generalize serialization:

template <class Tstd::ostream& Serialize(std::ostream& 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

=?ISO-8859-15?Q?Juli=E1n?= Albo
Guest
 
Posts: n/a
#6: Jan 4 '07

re: serialization question (using composite STL classes)


Diego Martins wrote:
Quote:
Quote:
>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
Diego Martins
Guest
 
Posts: n/a
#7: Jan 5 '07

re: serialization question (using composite STL classes)



Julián Albo wrote:
Quote:
Diego Martins wrote:
>
Quote:
Quote:
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

Closed Thread