save: reflection?

D

danny van elsen

hello all,

in my application, I want to save the state of a number of classes to disk: what would be the best way to do this?

right now, I'm hard coding the fields of the classes, but of course after
every change to the classes, I also have to adapt the 'save' method. Is there a better way, perhaps using some kind of reflection?

I've searched boost.org for reflection, but there don't seem to be ready made classes?

thanks for any answer,
Danny.
 
T

Tom Widmer

hello all,

in my application, I want to save the state of a number of classes to disk: what would be the best way to do this?

right now, I'm hard coding the fields of the classes, but of course after
every change to the classes, I also have to adapt the 'save' method. Is there a better way, perhaps using some kind of reflection?

I've searched boost.org for reflection, but there don't seem to be ready made classes?

One approach is to only ever have one data member, a boost::tuple.
That's not nice though, since you have to add a load of private
accessor methods really to avoid nasty syntax (and you may need
const/non-const overloads). Alternatively, accept the small
maintenance overhead (which does give you backwards compatibility and
versioning, unlike if you use reflection directly as is often done in
Java), and use the boost serialization library.

Tom
 
M

msalters

danny said:
hello all,

in my application, I want to save the state of a number of classes to
disk: what would be the best way to do this?

Are they PODs?
right now, I'm hard coding the fields of the classes, but of course after
every change to the classes, I also have to adapt the 'save' method.
Is there a better way, perhaps using some kind of reflection?

How do you deal with load(), especially with loading older files?
There is a common notion that classes related to file structures
should not change, period. If you need another field, create
a new C++ type and a new file format.

Another solution is to use an XSD to C++ compiler. If you want a new
field, change the XSD and recompile it. They usually will generate
the "save to disk" funcions for you as well.
In the same vein you could use an O/R mapper to save objects to a DB.
Regards,
Michiel Salters
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,184
Messages
2,570,979
Members
47,580
Latest member
kim2namjoon

Latest Threads

Top