Store single string value from node view array into string variable
HealthyPear opened this issue · 4 comments
Is your feature request related to a problem? Please describe.
I have something like this,
[geometry]
size = ["3 m", "1 m", "1 m"]
This issue is likely related to the discussion in #45 and it's for sure related to my poor understanding of C++ :)
Describe the solution you'd like
I tried to play a bit like this (nevermind the type prints, I was trying to navigate the API docs),
#include <iostream>
#include <string>
#include <typeinfo>
#include <toml++/toml.h>
using namespace std::string_view_literals;
int main()
{
toml::table tbl = toml::parse(R"(
[world]
size = ["3 m", "1 m", "1 m"]
)"sv);
auto world = tbl["world"];
std::cout << typeid(world).name() << std::endl;
auto size = world["size"];
std::cout << typeid(size).name() << std::endl;
if (size.is_homogeneous<std::string>()){
std::cout << "world[size] is a homogeneous array of strings!" << std::endl;
std::cout << tbl["world"]["size"][1] << std::endl;
std::cout << typeid(tbl["world"]["size"][1]).name() << std::endl;
std::string size_along_y = *tbl["world"]["size"][1].as_string();
}
return 0;
}
Is this not possible or am I (very likely) missing something?
Additional context
value_or() should do the trick:
toml::table tbl = toml::parse(R"(
[world]
size = ["3 m", "1 m", "1 m"]
)"sv);
std::vector<std::string> size{
tbl["world"][0].value_or("0 m"),
tbl["world"][1].value_or("0 m"),
tbl["world"][2].value_or("0 m")
};
this works but you need to add ["size"]
between ["world"]
and the index
maybe this small example is worth adding to the docs, not sure if I have overlooked it
There is an example of value_or() in the docs already. It's on the main page, under "Working with TOML data".
Anyways, glad it works for you.
There is an example of value_or() in the docs already. It's on the main page, under "Working with TOML data".
I saw that but it refers to a simple string, not to an array which was for me less straightforward (but again, it might be that I am the problem since I never really worked with C++)