mayah / tinytoml

A header only C++11 library for parsing TOML
BSD 2-Clause "Simplified" License
167 stars 31 forks source link

Comparison operator #9

Closed RobertBColton closed 8 years ago

RobertBColton commented 8 years ago

Hello, I am wondering if it would be possible to add support for comparing toml::Value using the equality operators? toml::Value has some type information to cast and do the comparison. It would also be nice to have .compare_as<T,T>() for comparisons.

mayah commented 8 years ago

Ah, I haven't defined any operator==(), so currently we cannot compare values. It won't be hard to add operator==() and operator!=().

For compare_as(), how do you use? Something like compare_as(a, b) when a and b are toml::Value ? If so, a.as() < b.as() or something?

RobertBColton commented 8 years ago

I would suggest the following as a static method:

toml::Value::compare_as<int,bool>(myValue1,myvalue2);

And by default the comparison operators would just use the type enum of the two values to compare them, for convenience.

mayah commented 8 years ago

I'm still wondering the behavior of compare_as().

What should happen if type error?

What is the result? If it returns true when the values are the same, equal_as() would be better name, and operator== is the best for this purpose.

I feel user preference would be different, so it might be good a user defines their one by themself.

Anyway, I'll add operator== and operator!= today (maybe 12~14 hours later)

RobertBColton commented 8 years ago

@mayah Thank you so much! == and != are all we really need actually. We're making an entity-component game engine and need to check if the value actually changes before firing an update event.

I'm not sure I have an opinion on how the exception handling would work, I just assume it would throw the same error as .as() does if the types are not compatible. It would work for us either way.

As far as compare_as goes, I was thinking along the lines of std::compare: http://www.cplusplus.com/reference/string/string/compare/

And so you wouldn't have to overload every operator, a third argument could be a function object with a boxed operator. Something like how STL comparators work: http://stackoverflow.com/questions/24689092/how-does-the-compare-function-in-stdmap-in-c-work-if-it-is-reflexively-true

But we have no foreseeable need for such a feature, and would be happy to overload the operators ourselves. This is simply all I need for now with our Qt IDE:

    // TODO: Only emit the signal when the property actually changes.
    // if (component->properties[name] == value) return;
    component->properties[name] = value;
    emit propertyChanged(name, value);
    std::cout << "hello " << value << std::endl;
mayah commented 8 years ago

Added operator==, operator!=, and operator[]. So, closing.

Currently I don't have any plan to add something like std::compare, since there is no natural order for toml::Value. If you have a strong demand for compare() method, feel free to file a new issue.

RobertBColton commented 8 years ago

Sounds great, it's working perfect here, thanks again!