| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Bit Byte" |
| Date: |
03 Jan 2007 03:51:13 PM |
| Object: |
serialization question (using composite STL classes) |
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::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
.
|
|
| User: "bjeremy" |
|
| Title: Re: serialization question (using composite STL classes) |
03 Jan 2007 07:04:36 PM |
|
|
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::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.
.
|
|
|
|
| User: "=?ISO-8859-15?Q?Juli=E1n?= Albo" |
|
| Title: Re: serialization question (using composite STL classes) |
03 Jan 2007 03:59:08 PM |
|
|
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::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
.
|
|
|
| User: "Diego Martins" |
|
| Title: Re: serialization question (using composite STL classes) |
04 Jan 2007 10:22:45 AM |
|
|
Juli=E1n 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::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
--=20
Salu2
But raw writing is useful when endianess is not an issue
.
|
|
|
| User: "=?ISO-8859-15?Q?Juli=E1n?= Albo" |
|
| Title: Re: serialization question (using composite STL classes) |
04 Jan 2007 12:33:06 PM |
|
|
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
.
|
|
|
| User: "Diego Martins" |
|
| Title: Re: serialization question (using composite STL classes) |
05 Jan 2007 10:35:06 AM |
|
|
Juli=E1n 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
.
|
|
|
|
|
|
|
| User: "David Harmon" |
|
| Title: Re: serialization question (using composite STL classes) |
03 Jan 2007 08:56:48 PM |
|
|
On Wed, 03 Jan 2007 21:51:13 +0000 in comp.lang.c++, Bit Byte
<root@yourbox.com> 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.
.
|
|
|
|

|
Related Articles |
|
|