Allow disabling exception handling
This PR is an attempt at allowing to use cpptoml with exception handling disabled. All changes are guarded behind the CPPTOML_NO_EXCEPTIONS macro, which by default is undefined. The following changes are included:
- replace
throws byTHROW_/THROW2_macros, which in turn calldie(...), which in turn callsstd::exit(1) - in
table::get_asandtable::get_qualified_as, the control logic was changed from exceptions to using thecontainsandcontains_qualifiedmethods - in
parser::parse_intandparser::parse_float, exception handling was disabled completely such that exceptions fromstd::stollandstd::stodare not caught anymore but directly propagated upwards
If you feel that this is the wrong approach or could be improved somehow, I'd be happy for suggestions.
First, thanks for working on this!
While this certainly allows for compilation without exceptions enabled, I think the more ideal situation would be to specify an alternate API where the errors are propagated up to the caller via some other mechanism so they can then be handled at the call site, rather than aborting the program entirely. I'm imagining something like this:
cpptoml::parse_error error;
auto config = cpptoml::parse_file("config.toml", error);
if (!error)
{
// use config as usual
}
else
{
auto why = error.message();
// do something useful to e.g. alert the user about the parsing error
}
Such a change should be somewhat mechanical. For private methods we can simply modify them to instead use the out-param error method for error reporting instead of throwing. For public methods, we can first modify them to use the out-param error method, and then add a throwing stub method for backwards-compatibility:
T fun(Args... args)
{
parse_error error;
auto res = fun(args..., error);
if (error) throw_parse_error(error.message());
return res;
}
Does that sound reasonable?