mayah / tinytoml

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

Add convenience function for array #7

Closed zmb3 closed 8 years ago

zmb3 commented 8 years ago

It would be nice if there were an easier way to get the elements from a toml::Array. Since an Array is just a std::vector<toml::Value>, you have to manually convert each Value element to the underlying type.

I would like to be able to do something like this:

toml::Value v = ...;
toml::Array arr = v.get<toml::Array>("foo");
std::vector<int> int_vec = toml::array_convert<int>(arr);

// or maybe add an additional template argument:
std::vector<int> arr = v.get<toml::Array, int>("foo");

Would something like this be a good candidate for the library?

Perhaps another option would be to make Array a class and not an alias for vector, and then you could add is<T>() and as<T>() methods similar to toml::Value.

mayah commented 8 years ago

Sounds reasonable. However, I don't like to change toml::Array type.

I would like to avoid adding the second type to get() template, because the second type makes sense only when the first type is toml::Array. Probably, we would like to have something like v.get<std::vector<int>>("foo")? Isn't it more sophisticated?

zmb3 commented 8 years ago

Yeah that sounds great! On Feb 8, 2016 9:31 PM, "MAYAH" notifications@github.com wrote:

Sounds reasonable. However, I don't like to change toml::Array type.

I would like to avoid adding the second type to get() template, because the second type makes sense only when the first type is toml::Array. Probably, we would like to have something like v.getstd::vector("foo")? Isn't it more sophisticated?

— Reply to this email directly or view it on GitHub https://github.com/mayah/tinytoml/issues/7#issuecomment-181674492.