[bind10-dev] data classes

Jelte Jansen jelte at isc.org
Thu Oct 22 16:25:10 UTC 2009


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Earlier today I committed two new files to my experiments dir; data.cc and data.h.

Unless we go into heavy template programming i think this nears the
type-indepence i want for this specific part (don't get me wrong; i like type
checking. Just not for this :p); I think we could use this for both low-level
config objects and data messaging stuff.

The base class is an Element, which has derived classes IntElement,
DoubleElement, StringElement, ListElement and MapElement (respectively
containing an int, a double, a string, a vector of Elements and a
string->Element map).

The base class has a factory function create() which returns an ElementPtr
(which is in fact a boost::shared_ptr pointing to the specific derived
XXXElement where XXX is dependent on the type of argument you pass to create()).
The create() function should never throw; if no memory can be allocated it
returns a null shared_ptr (of course if a user decides to instantiate a specific
Element type himself it can throw bad_alloc).

To get values back there are currently two versions; a set like
int ElementPtr->int_value(), std::string ElementPtr->string_value() etc. But
these are of course only relevant to one specific subtype, and for all the
others i can't really think of a general way to return a decent value if the
type is something else. So this would be a place to throw an exception (as in
principle you should be able to know or at least ask what type you would be
getting).

I've also added another approach;
bool ElementPtr->get_value(int &i), bool ElementPtr->get_value(std::string &s)
etc. The default implementations of these return false, and they are overridden
by the XXXElement class the argument is relevant to, which then sets the value
and returns true. This is at least exception-safe, and it's one common function
name for all types. But otoh it's less convenient in use; you have to declare a
variable for the value you're retrieving and cannot directly use the return
value from for instance int_value().

I'm not really happy with exposing the exact type that a derived class uses atm
(like std::vector in ListElement and std::map in MapElement), so i've also added
some manipulation functions for those. But then again, that's multiple functions
in the base class where they are only relevant for one specific derived class...

And of course what's still missing is direct multilevel deep accessort
(get("/foo/bar/myname") type of things).

Anyhew, please take a look at them and see if i missed any C++ features which
would make this a lot easier.

Jelte
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (GNU/Linux)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org

iEYEARECAAYFAkrgh2YACgkQ4nZCKsdOncUVzACgmeJ5jsu/mwNFSxsY1WRz7QoK
hAwAniRRTfuUcTLXl3NtMUn4iRpPJLUE
=kiGs
-----END PGP SIGNATURE-----



More information about the bind10-dev mailing list