From 77b237c53a67d23fe49f1f0f98ad6024c7144412 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Thu, 13 Dec 2018 02:00:42 +0900 Subject: [PATCH] update README --- README.md | 78 +++++++++++++++++++++++++++---------------------------- 1 file changed, 39 insertions(+), 39 deletions(-) diff --git a/README.md b/README.md index e07a85a..fa4a5b8 100644 --- a/README.md +++ b/README.md @@ -96,7 +96,7 @@ std::cout << toml::get(data.at("answer")) << std::endl; // 54 ``` If the specified type requires conversion, you can't take a reference to the -return value. See also [underlying types](#underlying-types) +return value. See also [underlying types](#underlying-types). #### passing invalid type to toml::get @@ -112,44 +112,6 @@ terminate called after throwing an instance of 'toml::type_error' NOTE: In order to show this kind of error message, all the toml values has 1 shared_ptr that points the corresponding byte sequence and 2 iterator that points the range. It is recommended to destruct all the `toml::value` classes after configuring your application to save the memory resource. -#### finding value from table - -toml11 provides overloads to find a value from `toml::table`. Of course, you can do this in your own way with `toml::get` because it just searches and returns a value. - -```cpp -const auto data = toml::parse("example.toml"); -const auto num = toml::get(data, "num", /*optional*/"example.toml"); -``` - -If the value does not exists, it throws `std::out_of_range` with informative error message. - -```console -terminate called after throwing an instance of 'std::out_of_range' - what(): [error] key "num" not found in example.toml -``` - -You can use this with a `toml::value` that is expected to be a `toml::table`. It automatically casts the value to table. - -```cpp -const auto data = toml::parse("example.toml"); -const auto num = toml::get(data.at("table"), "num"); -// expecting the following example.toml -// [table] -// num = 42 -``` - -In this case, because the value `data.at("table")` knows the locatoin of itself, you don't need to pass where you find the value. `toml::get` will show you a great error message. - -```console -terminate called after throwing an instance of 'std::out_of_range' - what(): [error] key "num" not found - --> example.toml - 3 | [table] - | ~~~~~~~ in this table -``` - -If it's not a `toml::table`, error like "invalid type" would be thrown. - ### getting arrays You can set any kind of `container` class to obtain `toml::array` except for `map`-like classes. @@ -331,6 +293,44 @@ const auto value = toml::expect(data.at("number")) }).unwrap_or(/*default value =*/ 3.14); ``` +### finding value from table + +toml11 provides utility function to find a value from `toml::table`. Of course, you can do this in your own way with `toml::get` because it just searches `unordered_map` and returns a value if exists. + +```cpp +const auto data = toml::parse("example.toml"); +const auto num = toml::find(data, "num", /*for err msg*/"example.toml"); +``` + +If the value does not exist, it throws `std::out_of_range` with informative error message. + +```console +terminate called after throwing an instance of 'std::out_of_range' + what(): [error] key "num" not found in example.toml +``` + +You can use this with a `toml::value` that is expected to be a `toml::table`. It automatically casts the value to table. + +```cpp +const auto data = toml::parse("example.toml"); +const auto num = toml::find(data.at("table"), "num"); +// expecting the following example.toml +// [table] +// num = 42 +``` + +In this case, because the value `data.at("table")` knows the locatoin of itself, you don't need to pass where you find the value. `toml::find` will show you a great error message. + +```console +terminate called after throwing an instance of 'std::out_of_range' + what(): [error] key "num" not found + --> example.toml + 3 | [table] + | ~~~~~~~ in this table +``` + +If it's not a `toml::table`, the same error as "invalid type" would be thrown. + ### checking value type When you don't know the exact type of toml-value, you can get `enum` type from `toml::value`.