pugixml icon indicating copy to clipboard operation
pugixml copied to clipboard

add std::optional-like return for your attribute-getters

Open LowLevelMahn opened this issue 11 years ago • 3 comments
trafficstars

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&)

LowLevelMahn avatar Nov 13 '14 17:11 LowLevelMahn

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.

zeux avatar Dec 14 '14 17:12 zeux

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 parse_int // was available? xml_value try_parse_int // was available AND valid?

LowLevelMahn avatar Dec 14 '14 19:12 LowLevelMahn

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

ghost avatar Jan 20 '15 10:01 ghost