Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
LowLevelMahn opened this issue Nov 13, 2014 · 3 comments
Open

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

LowLevelMahn opened this issue Nov 13, 2014 · 3 comments

Comments

@LowLevelMahn
Copy link

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&)
@zeux
Copy link
Owner

zeux commented Dec 14, 2014

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.

@LowLevelMahn
Copy link
Author

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?

@ghost
Copy link

ghost commented Jan 20, 2015

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
void xml_text::set( const T& value); // with specialization for const char_t*

and

//! @return Whether conversion to T was successful
template
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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants