pugixml
pugixml copied to clipboard
add std::optional-like return for your attribute-getters
its still a huge difference if there is a attribute with valid or invalid content or complete absent of the attribute itself
xml_value<int> try_int()
usage
xml_value<int> value = attribute("xyz").try_int();
value.exists() = true/false (const)
value.valid() = successfull converted (const)
int x = value // conversion operator (const&)
I'm generally in favor of an optional-like interface. This depends on #15 since right now "invalid" state is not detected by pugixml.
Also I'm not sure if it makes sense to go with a custom wrapper - I would rather use std::optional if it's available... Of course this would mean that "attribute does not exist" and "there was an error parsing it" would be conflated.
I would rather use std::optional if it's available... Of course this would mean that "attribute does not exist" and "there was an error parsing it" would be conflated
i would use std::optional (if available) only internaly in the xml_value - the big benefit is getting rid of the default-value AND have the valid-info - maybe serveral versions of converters needed?
int get_int(int default); // your current
std::optional
To me, the operator bool is perfectly fine
To retreive values I would like to see an enhancement to xml_text similar to this instead
template<typename T> void xml_text::set( const T& value); // with specialization for const char_t*
and
//! @return Whether conversion to T was successful template<typename T> bool xml_text::get( T& value); // with specialization for const char_t**
or similar that would make life easier when using pugixml for generic serialization etc