Re: Proposal: make Glib::KeyFile easier to use



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

Hi,

The current interface of Glib::KeyFile::get_* functions is quite 
inconvenient as exceptions are the only reliable way to receive
error conditions. Consider the following example:

Glib::KeyFile f; <...> int x = f.get_integer("a", "b");  // (1)

There are two possible error conditions that may cause (1) to
fail: * there's no key "b" in group "a" * the value of the key "b"
in group "a" is not an integer

One external thing to consider here is "who edits key files". I don't
expect non-pro guy doing anything there, so key files probably are
unlikely to be broken (low probability).


Whereas the former condition can be checked for via
KeyFile::has_key, to check for the latter, one has only the
following two options: * catch the thrown exception * get the value
via get_string and use a separate function to parse it

Both of these approaches are not optimal. I think the situation
could be improved.

My proposal is as follows:

* A new class template Glib::Optional<T> is created. The new class 
template would be identical to boost::optional[1].

* A new family of functions for reading a keyfile is created (say,
for example, get_*_opt). The new functions would return
Glib::Optional<T> instead of T. All error conditions would be
reported by returning 'empty' optional object. The new functions
would not throw any exceptions on any circumstances.

This would allow much simpler usage of the API. For example:

Glib::KeyFile f; <...> auto opt = f.get_integer_opt("a", "b");  //
let's say we can use C++11 if (opt) { // utilize the value in *opt 
int r = *opt; <...> }

Do think that "if" is more convenient that "try-catch"? I'm not sure
about that.
If you think of more convenient getter for key, I'd think of passing
default value as last argument, that would be returned in case key is
missing or not valid. I think that would cover most of the cases, the
remaining few could deal with exceptions.

- -- 
Aurimas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.15 (GNU/Linux)
Comment: Using GnuPG with Thunderbird - http://www.enigmail.net/

iQEcBAEBAgAGBQJSvH3sAAoJECtTeRAzEaLP3EEH+gJFPEZYwcV77j7/v0L7olAa
/YXt1IUxZrHU+qKLAeXUSCOopP0XHNfQdAjEmOCYgjbkx/FD2gW4wYhZqvSFH/4o
hfQ2r5hAgGTsqubzH3fMxPr0znONsTJyOE7vrBR9hsITQBjezb5iGLDH6sRex1Xl
rrp9QRttZSO/h+hrjpTAWRRm5tZvN7EhUyYC8hcu/kdFlTwN+XALw0gkDTim271v
iO/TaqumBQoIx7oOU8u8czrQ1Ay7nujxRLuxHptm4707RTi7pa7qdc0NEEXExK7Z
KerSm9NRBcPMM7BpohKDP0uYdvFQOu8xzfSVz1DxddbabtD4y4GDhZDP8eqUrJk=
=gTsV
-----END PGP SIGNATURE-----


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]