diff --git a/doc/system/changes.adoc b/doc/system/changes.adoc index 1799005..8c2a4fa 100644 --- a/doc/system/changes.adoc +++ b/doc/system/changes.adoc @@ -13,6 +13,7 @@ https://www.boost.org/LICENSE_1_0.txt * Added support for `result`. * Added `operator|` for `result`. * Added `operator&` for `result`. +* Added `operator&=` for `result`. ## Changes in Boost 1.81 diff --git a/doc/system/reference.adoc b/doc/system/reference.adoc index 65c5a47..445ddcc 100644 --- a/doc/system/reference.adoc +++ b/doc/system/reference.adoc @@ -1542,6 +1542,14 @@ template template R operator&( result const& r, F&& f ); template R operator&( result&& r, F&& f ); +// operator&= + +template + result& operator&=( result& r, F&& f ); + +template + result& operator&=( result& r, F&& f ); + } // namespace system } // namespace boost ``` @@ -2683,14 +2691,15 @@ struct JsonValue namespace helpers { - inline auto at( std::size_t i ) { - return [=](JsonValue const& jv){ return jv.at( i ); }; } - inline auto at( std::string_view key ) { - return [=](JsonValue const& jv){ return jv.at( key ); }; } +inline auto at( std::size_t i ) { + return [=](JsonValue const& jv){ return jv.at( i ); }; } - template inline auto to_number() { - return [](JsonValue const& jv){ return jv.to_number(); }; } +inline auto at( std::string_view key ) { + return [=](JsonValue const& jv){ return jv.at( key ); }; } + +template inline auto to_number() { + return [](JsonValue const& jv){ return jv.to_number(); }; } } // namespace helpers @@ -2701,6 +2710,71 @@ int get_port( JsonValue const& config, int def ) } ``` +#### operator&= + +``` +template + result& operator&=( result& r, F&& f ); +``` +[none] +* {blank} ++ +If `r` contains a value, replaces it with the result of invoking the function `f` on the value in `r`. ++ +Let `U` be the type of `f(*r)`. ++ +Effects: :: If `r.has_value()` is `true`, assigns `f(*std::move(r))` to `r`. +Returns: :: `r`. +Remarks: :: + Only enabled when `U` is not an instance of `result` and is convertible to `T`. + +``` +template + result& operator&=( result& r, F&& f ); +``` +[none] +* {blank} ++ +If `r` contains a value, replaces `r` with the result of invoking the function `f` on the value in `r`. ++ +Let `R` be the type of `f(*r)`. ++ +Effects: :: If `r.has_value()` is `true`, assigns `f(*std::move(r))` to `r`. +Returns: :: `r`. +Remarks: :: + Only enabled when `R` is an instance of `result` and is convertible to `result`. +Example: :: ++ +``` +struct JsonValue +{ + result at( std::string_view key ) const noexcept; +}; + +namespace helpers +{ + +inline auto at( std::string_view key ) { + return [=](JsonValue const& jv){ return jv.at( key ); }; } + +} // namespace helpers + +result at_path( JsonValue const& jv, + std::initializer_list path ) +{ + result r( jv ); + + using namespace helpers; + + for( auto key: path ) + { + r &= at( key ); + } + + return r; +} +``` + ## This convenience header includes all the headers previously described.