 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Frank Astier Guest
|
Posted: Thu Nov 18, 2004 12:40 am Post subject: Tools to generate operator<< and operator>> automatically? |
|
|
Hi -
I am getting tired of writing and maintening operator<< and operator>>
for my objects, when the code in there seems to be really
straightforward... and ready for automatic generation.
I use those operators for persistence, and I just want all data
members of my classes to get to/from the stream. If a data member is a
pointer or an instance to another class, just call operator>> and
operator<< on that class in turn.
I was wondering if there were some tools out there that could help?
Thanks,
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Daniel Krügler (ne Spange Guest
|
Posted: Thu Nov 18, 2004 12:14 pm Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
Hello Frank Astier,
Frank Astier schrieb:
| Quote: | Hi -
I am getting tired of writing and maintening operator<< and operator
for my objects, when the code in there seems to be really
straightforward... and ready for automatic generation.
I use those operators for persistence, and I just want all data
members of my classes to get to/from the stream. If a data member is a
pointer or an instance to another class, just call operator>> and
operator<< on that class in turn.
I was wondering if there were some tools out there that could help?
|
The current boost 1.32 tarball at http://www.boost.org contains a very
well designed serialization
library. I would try that! (Side note: Actually the targeted release
date for 1.32 should have been
november, 17th. Is the current web site not up-to-date or are there some
severe reasons for
the delay?)
Greetings from Bremen,
Daniel Krügler
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Thu Nov 18, 2004 12:17 pm Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
"Frank Astier" <fastier (AT) yahoo (DOT) com> wrote
| Quote: | Hi -
I am getting tired of writing and maintening operator<< and operator
for my objects, when the code in there seems to be really
straightforward... and ready for automatic generation.
I use those operators for persistence, and I just want all data
members of my classes to get to/from the stream. If a data member is a
pointer or an instance to another class, just call operator>> and
operator<< on that class in turn.
I was wondering if there were some tools out there that could help?
|
Emily winch wrote a paper about this: "Heterogeneous Lists of Named Objects"
(see http://www.oonumerics.org/tmpw01/winch.pdf)
Quoting from an introductory section: "There are many functions commonly forming
part of class definitions that are normally written according to a simple set of
rules, performing some operation on each member of the class in turn. These
include operator==, operator=, operator<<, operator>>, destructors, and copy
constructors. Storing the member variables of the class in a container makes it
possible to use algorithms like those in the Standard Library to generate most
of this code automatically. Members can then be added to the class without
adding code to any of these functions."
In the case of operator>> and operator<<, if you can give your class a container
or tuple-like interface -- which need not be intrusive -- you can use my library
Format Lite, at
http://home.comcast.net/~jturkanis/format_lite/
For a description of how to give your class a tuple-like interface, see
http://tinyurl.com/4gegk.
Best Regards,
Jonathan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
jakacki Guest
|
Posted: Thu Nov 18, 2004 12:22 pm Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
Frank wrote:
| Quote: | I am getting tired of writing and maintening operator
and operator>> for my objects, when the code in there
seems to be really straightforward... and ready for
automatic generation.
I use those operators for persistence, and I just want
all data members of my classes to get to/from the stream.
If a data member is a pointer or an instance to another
class, just call operator>> and operator<< on that class
in turn.
I was wondering if there were some tools out there that
could help?
|
http://opencxx.sf.net
BR
Grzegorz
--
Free C++ frontend library: http://opencxx.sourceforge.net
China from the inside: http://www.staryhutong.com
Myself: http://www.dziupla.net/gj/cv
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Michiel Salters Guest
|
Posted: Fri Nov 19, 2004 12:52 pm Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
[email]fastier (AT) yahoo (DOT) com[/email] (Frank Astier) wrote in message news:<40da7b0d.0411170850.6bf5e857 (AT) posting (DOT) google.com>...
| Quote: | Hi -
I am getting tired of writing and maintening operator<< and operator
for my objects, when the code in there seems to be really
straightforward... and ready for automatic generation.
|
Is it? How do you implement them given
struct foo {
string s1;
string s2;
};
Regards,
Michiel Salters
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
fabioppp Guest
|
Posted: Fri Nov 19, 2004 3:06 pm Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
Frank Astier wrote:
| Quote: | Hi -
I am getting tired of writing and maintening operator<< and operator
for my objects, when the code in there seems to be really
straightforward... and ready for automatic generation.
I use those operators for persistence, and I just want all data
members of my classes to get to/from the stream. If a data member is a
pointer or an instance to another class, just call operator>> and
operator<< on that class in turn.
I was wondering if there were some tools out there that could help?
|
You need something like a reflection support! ...a compile time
reflection maybe!
template <
typename class_type,
typename field_type,
field_type class_type::* ptom
{
static const field_type& get(const class_type& base)
{
return base.*ptom;
}
};
struct nil {};
template
struct fields
{
typedef head_ head;
typedef tail_ tail;
};
class Foo
{
int a;
char b;
typedef fields< field
fields< field
them you can write your generic serialization routine, which
use CLASS::_fields
--Fabio
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Frank Astier Guest
|
Posted: Fri Nov 19, 2004 3:34 pm Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
Guten Tag Daniel,
Wie geht's?
| Quote: | The current boost 1.32 tarball at http://www.boost.org contains a very
well designed serialization
library. I would try that! (Side note: Actually the targeted release
date for 1.32 should have been
november, 17th. Is the current web site not up-to-date or are there some
severe reasons for
the delay?)
|
I'm afraid I missed the serialization library... I'm just coming back
from their website, and I'm not sure which part of boost you are
talking about. Could you tell me the precise name of the library?
Thanks!
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Brian Wood Guest
|
Posted: Fri Nov 19, 2004 3:37 pm Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
[email]fastier (AT) yahoo (DOT) com[/email] (Frank Astier) wrote in message news:<40da7b0d.0411170850.6bf5e857 (AT) posting (DOT) google.com>...
| Quote: | Hi -
I am getting tired of writing and maintening operator<< and operator
for my objects, when the code in there seems to be really
straightforward... and ready for automatic generation.
I use those operators for persistence, and I just want all data
members of my classes to get to/from the stream. If a data member is a
pointer or an instance to another class, just call operator>> and
operator<< on that class in turn.
I was wondering if there were some tools out there that could help?
Thanks,
Frank
|
Our site might be of interest to you. We do what you mentioned except
we use Send/Receive functions rather than the <<, >> operators.
Brian Wood
Ebenezer Enterprises
www.webebenezer.net
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Allan W Guest
|
Posted: Fri Nov 19, 2004 4:09 pm Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
[email]fastier (AT) yahoo (DOT) com[/email] (Frank Astier) wrote
| Quote: | I am getting tired of writing and maintening operator<< and operator
for my objects, when the code in there seems to be really
straightforward... and ready for automatic generation.
I use those operators for persistence, and I just want all data
members of my classes to get to/from the stream.
|
What about private and protected members?
What about integers or longs used as bitflags?
What about enumerations?
What about references?
| Quote: | If a data member is a
pointer or an instance to another class, just call operator>> and
operator<< on that class in turn.
|
For linked-list structures you'll end up dumping the rest of the list.
For doubly-linked lists or lists with certain wrap-around characteristics,
you'll dump members endlessly until you get a stack overflow.
| Quote: | I was wondering if there were some tools out there that could help?
|
Not familiar with any.
Consider this (untested):
template
std::ostream & operator<<(std::ostream o,const T&t)
{ t.show(o); return o; }
Now, define a const member function "show(std::ostream&)" in each of your
classes, and you don't have to bother defining operator<< seperately.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jonathan Turkanis Guest
|
Posted: Fri Nov 19, 2004 4:10 pm Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
"Daniel Krügler (ne Spangenberg)" <dsp (AT) bdal (DOT) de> wrote:
| Quote: | Frank Astier schrieb:
I am getting tired of writing and maintening operator<< and operator
for my objects, when the code in there seems to be really
straightforward... and ready for automatic generation.
I use those operators for persistence, and I just want all data
members of my classes to get to/from the stream. If a data member is a
pointer or an instance to another class, just call operator>> and
operator<< on that class in turn.
I was wondering if there were some tools out there that could help?
The current boost 1.32 tarball at http://www.boost.org contains a very
well designed serialization library. I would try that!
|
I agree that the Boost Serialization library is an excellent choice for
persistence, and I have heard from many people that it's easy to use. But AFAIK
it does not provide an automated way to serialize the data members of a class --
you still have to write a serialize function in which you serialize the data
members one at a time. It does, however, eliminate the problem of having to keep
operator>> and operator<< in sync.
Jonathan
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Jeff Flinn Guest
|
Posted: Sat Nov 20, 2004 10:34 am Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
"Frank Astier" <fastier (AT) yahoo (DOT) com> wrote
| Quote: | Guten Tag Daniel,
Wie geht's?
The current boost 1.32 tarball at http://www.boost.org contains a very
well designed serialization
library. I would try that! (Side note: Actually the targeted release
date for 1.32 should have been
november, 17th. Is the current web site not up-to-date or are there some
severe reasons for
the delay?)
I'm afraid I missed the serialization library... I'm just coming back
from their website, and I'm not sure which part of boost you are
talking about. Could you tell me the precise name of the library?
|
It's called 'serialization'. As Daniel stated it's part of the soon to be
released 1.32 version. If you need it now you'll need to access the CVS
repository at http://www.boost.org/more/getting_started.html#CVS
Jeff Flinn
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Frank Astier Guest
|
Posted: Sat Nov 20, 2004 6:15 pm Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
I'm afraid I would be very naive, and just write:
inline std::ostream& operator<<(std::ostream& out, const foo& f) {
return out << s1 << endl << s2;
}
I'm curious about the pitfalls you can see in that naive approach?
Thanks,
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Frank Astier Guest
|
Posted: Sat Nov 20, 2004 6:16 pm Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
| Quote: | What about private and protected members?
What about integers or longs used as bitflags?
What about enumerations?
What about references?
For linked-list structures you'll end up dumping the rest of the list.
For doubly-linked lists or lists with certain wrap-around characteristics,
you'll dump members endlessly until you get a stack overflow.
|
I think you are right: it's not gonna fly in the most general case.
For the objects I work with though, maybe only part of the state can
be isolated and persisted, maybe as a heterogeneous list of named
objects:
class Scenario : public VarListType<size_t, index,
float, price,
float, quantity,
bool, allocation>
{
....
};
and somehow the VarListType would have generic operator<< and
operator>>, and getters and setters so that I don't have to write
those either, while we are at it.
Frank
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
James Dennett Guest
|
Posted: Sun Nov 21, 2004 6:52 am Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
Frank Astier wrote:
| Quote: | I'm afraid I would be very naive, and just write:
inline std::ostream& operator<<(std::ostream& out, const foo& f) {
return out << s1 << endl << s2;
}
I'm curious about the pitfalls you can see in that naive approach?
|
Embedded newlines in s1 and/or s2 are the obvious problem there;
there's no character you can be confident isn't used by one of
the component (consider, for example, for case where one of your
object's members itself uses the same approach).
-- James
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Brian Wood Guest
|
Posted: Mon Nov 22, 2004 11:03 pm Post subject: Re: Tools to generate operator<< and operator>> automaticall |
|
|
[email]allan_w (AT) my-dejanews (DOT) com[/email] (Allan W) wrote in message
news:<7f2735a5.0411180935.675d0607 (AT) posting (DOT) google.com>...
| Quote: | fastier (AT) yahoo (DOT) com (Frank Astier) wrote
I am getting tired of writing and maintening operator<< and operator
for my objects, when the code in there seems to be really
straightforward... and ready for automatic generation.
I use those operators for persistence, and I just want all data
members of my classes to get to/from the stream.
What about private and protected members?
|
I would approach it like this:
Add function prototypes to the class and use our software to
write the function implementations. As class members the
functions have access to the private data.
| Quote: | What about integers or longs used as bitflags?
|
I'm not sure what you are getting at here.
| Quote: | What about enumerations?
What about references?
If a data member is a
pointer or an instance to another class, just call operator>> and
operator<< on that class in turn.
For linked-list structures you'll end up dumping the rest of the list.
For doubly-linked lists or lists with certain wrap-around characteristics,
you'll dump members endlessly until you get a stack overflow.
|
We support the STL container classes, slist, and hash containers.
| Quote: | I was wondering if there were some tools out there that could help?
Not familiar with any.
Consider this (untested):
template
std::ostream & operator<<(std::ostream o,const T&t)
{ t.show(o); return o; }
Now, define a const member function "show(std::ostream&)" in each of your
classes, and you don't have to bother defining operator<< seperately.
|
Brian Wood
Ebenezer Enterprises
www.webebenezer.net
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|