> Suppose we have a class with the members variable1 and variable2.
> This is great, and we live with this class for a year, and use the
> layout of it indirectly through XTL.
> Now, it turns out we need a variable3, so what are we to do? We would
> like to support the old layout, but at the same time use the new layout
> in the future.
Right. That's why we have been smart in the beginning and chosen some
format that can be extended. Like
<foo variable1="1" variable2="..." />
This could be parsed into some generic structure that enable access to
these parameters. This structure could even have some access function
that enables to specify default arguments.
So the foo's stream interface could look like
----------------------- snip ----------------------
#include "xmlstuff.h"
class foo {
private:
int variable1;
double variable2;
public:
void read(istream &);
}
void foo::read(istream & is)
{
xmlstuff x;
x.read(is); // read the "generic structure"
variable1 = x.get("variable1", 0); // first parameter is identifier,
variable2 = x.get("variable2", 0.0); // second parameter is default value
}
istream & operator>>(istream & is, foo & f)
{
f.read(is);
return is;
}
----------------------- snip ----------------------
class xmlstuff
{
private:
map <string, string> params_;
...
public:
void read(istream &);
template <class T>
T get(const string & what, const T & dflt);
}
template <class T>
T xmlstuff::get(const string & what, const T & dflt)
{
map<string, string>::iterator pos = params_.find(what);
return pos == params_.end() ? dflt : pos->second;
}
----------------------- snip ----------------------
Now, what happens, if wee need to extend foo?
We simply add 'bool variable3;' in the class definition and
'variable3 = x.get("variable3", false); ' in foo::read. That's it.
No long time user of the class needs to be updated, it just behaves as
it had specified variable3=false from the beginning.
> You can easily do this with XTL like this:
Switching behaviour on versions is not nice. You need to double some code
(which is prone to errors by itself) and maintainers need to be aware
of the history of the class including the indended behaviour at every
point in this history.
Of course, "my" solution does not come for free. There is a space and
runtime penalty compared to XTL. However, I argue that it's worthwhile.
> Conclusion: It's fairly easy to gracefully support different versions...
The both of us obviously have different opinions on "graceful support" ;-)
Andre'
PS: Writing is similar easy. One could even specify default arguments
for writing, such that values equal to the default value are not inserted
in the 'xmlstuff' structure at all. If you have 100 values and 99 of them
are default vlues, you need only transfer the non-default one.
Uh, and we obviously get some decent external format for free...
--
André Pönitz ........................................ [EMAIL PROTECTED]