diff --git a/docs/content.en/docs/features/value.md b/docs/content.en/docs/features/value.md index e3913249..3107688b 100644 --- a/docs/content.en/docs/features/value.md +++ b/docs/content.en/docs/features/value.md @@ -544,6 +544,17 @@ This is useful when you want to provide a fallback value instead of handling exc const auto a = toml::find_or(input, "a", 42); ``` +## Using `toml::find_or_default` to Search and Use the Default Value on Failure + +`toml::find_or_default` works similarly to `toml::find_or` but returns the default constructor result if the search or conversion fails. + +This is useful when you want to use the default value instead of handling exceptions, especially when the default construction is costly, as this delays it until the actual failure happens. + +```cpp +const auto a = toml::find_or(input, "a", expensive()); // ctor is called before the function call +const auto a = toml::find_or_default(input, "a"); // ctor will be called only on failure +``` + ## `toml::find>` If `std::optional` is available, you can specify `std::optional` as a template argument of `toml::find`. diff --git a/docs/content.ja/docs/features/value.md b/docs/content.ja/docs/features/value.md index 9de44900..950efcf9 100644 --- a/docs/content.ja/docs/features/value.md +++ b/docs/content.ja/docs/features/value.md @@ -569,6 +569,17 @@ const auto a = toml::find_or(input, "a", 42); 型変換の失敗だけでなく、キーが見つからなかった場合もデフォルト値を返します。 +## `toml::find_or_default`を使って失敗時の値を指定する + +`toml::find_or_default` は、 `toml::find_or` と同様に、失敗時にデフォルトコンストラクタの結果を返します。デフォルトコンストラクタは失敗時のみに呼ばれるため、特にそれが高価な時に有効です。 + +```cpp +const auto a = toml::find_or(input, "a", expensive()); // 関数呼出前にコンストラクタ呼出 +const auto a = toml::find_or_default(input, "a"); // 失敗時にのみコンストラクタ呼出 +``` + +型変換の失敗だけでなく、キーが見つからなかった場合もデフォルトコンストラクタの結果を返します。 + ## `toml::find>` C++17以降の場合、`std::optional`を`toml::find`に指定することができます。 diff --git a/include/toml11/find.hpp b/include/toml11/find.hpp index 1d0658d4..d1f70bbf 100644 --- a/include/toml11/find.hpp +++ b/include/toml11/find.hpp @@ -544,5 +544,36 @@ T find_or(const basic_value& v, const K1& k1, const K2& k2, const K3& k3, co } } +// =========================================================================== +// find_or_default(value, key) + +template +cxx::enable_if_t::value, T> +find_or_default(const basic_value& v, K&& k) noexcept(std::is_nothrow_default_constructible::value) +{ + try + { + return ::toml::get(v.at(detail::key_cast(std::forward(k)))); + } + catch(...) + { + return T(); + } +} + +template +cxx::enable_if_t::value, T> +find_or_default(const basic_value& v, K1&& k1, Ks&& ... keys) noexcept(std::is_nothrow_default_constructible::value) +{ + try + { + return find_or_default(v.at(std::forward(k1)), std::forward(keys)...); + } + catch(...) + { + return T(); + } +} + } // toml #endif // TOML11_FIND_HPP diff --git a/tests/test_find_or.cpp b/tests/test_find_or.cpp index 9b0725fc..14772b95 100644 --- a/tests/test_find_or.cpp +++ b/tests/test_find_or.cpp @@ -673,3 +673,86 @@ TEST_CASE("testing find_or(val, keys..., opt)") CHECK_EQ(v2, 3.14); } } + +TEST_CASE("testing find_or_default(val, keys...)") +{ + // explicitly specify type, doing type conversion + { + const toml::value v( + toml::table{ {"foo", + toml::table{ {"bar", + toml::table{ {"baz", + 42 + } } + } } + } } + ); + auto v1 = toml::find_or_default(v, "foo", "bar", "baz"); + auto v2 = toml::find_or_default(v, "foo", "bar", "qux"); + + CHECK_EQ(v1, 42); + CHECK_EQ(v2, 0); + } + { + toml::value v( + toml::table{ {"A", + toml::table{ {"B", + toml::table{ {"C", + toml::table{ {"D", + toml::table{ {"E", + toml::table{ {"F", + 42 + } } + } } + } } + } } + } } + } } + ); + auto v1 = toml::find_or_default(v, "A", "B", "C", "D", "E", "F"); + auto v2 = toml::find_or_default(v, "A", "B", "C", "D", "E", "G"); + + CHECK_EQ(v1, 42); + CHECK_EQ(v2, 0); + } + + // the value exists, but type is different from the expected. + { + const toml::value v( + toml::table{ {"foo", + toml::table{ {"bar", + toml::table{ {"baz", + 42 + } } + } } + } } + ); + auto v1 = toml::find_or_default(v, "foo", "bar", "baz"); + auto v2 = toml::find_or_default(v, "foo", "bar", "baz"); + + CHECK_EQ(v1, ""); + CHECK_EQ(v2, 0.); + } + { + const toml::value v( + toml::table{ {"A", + toml::table{ {"B", + toml::table{ {"C", + toml::table{ {"D", + toml::table{ {"E", + toml::table{ {"F", + 42 + } } + } } + } } + } } + } } + } } + ); + auto v1 = toml::find_or_default(v, "A", "B", "C", "D", "E", "F"); + auto v2 = toml::find_or_default(v, "A", "B", "C", "D", "E", "F"); + + CHECK_EQ(v1, ""); + CHECK_EQ(v2, 0.); + } +}