summaryrefslogtreecommitdiff
path: root/src/mongo/base/status_with.h
diff options
context:
space:
mode:
authorJacob Evans <jacob.evans@10gen.com>2020-04-03 15:06:52 -0400
committerEvergreen Agent <no-reply@evergreen.mongodb.com>2020-09-29 02:41:46 +0000
commit684894759e5bf26d07aa5fe458aa6bed38907d03 (patch)
tree3439e7dadb8bfcb617fcc5348362b8eae0dec747 /src/mongo/base/status_with.h
parent875e21edd28047c6f6d78431a092271be7a74af7 (diff)
downloadmongo-684894759e5bf26d07aa5fe458aa6bed38907d03.tar.gz
SERVER-47327 Add composition member functions to StatusWith
Diffstat (limited to 'src/mongo/base/status_with.h')
-rw-r--r--src/mongo/base/status_with.h104
1 files changed, 104 insertions, 0 deletions
diff --git a/src/mongo/base/status_with.h b/src/mongo/base/status_with.h
index 79e5f1339af..b4e2654f815 100644
--- a/src/mongo/base/status_with.h
+++ b/src/mongo/base/status_with.h
@@ -163,6 +163,110 @@ public:
}
/**
+ * For any type U returned by a function f, transform creates a StatusWith<U> by either applying
+ * the function to the _t member or forwarding the _status. This is the lvalue overload.
+ */
+ template <typename F>
+ StatusWith<std::invoke_result_t<F&&, T&>> transform(F&& f) & {
+ if (_t)
+ return {std::forward<F>(f)(*_t)};
+ else
+ return {_status};
+ }
+
+ /**
+ * For any type U returned by a function f, transform creates a StatusWith<U> by either applying
+ * the function to the _t member or forwarding the _status. This is the const overload.
+ */
+ template <typename F>
+ StatusWith<std::invoke_result_t<F&&, const T&>> transform(F&& f) const& {
+ if (_t)
+ return {std::forward<F>(f)(*_t)};
+ else
+ return {_status};
+ }
+
+ /**
+ * For any type U returned by a function f, transform creates a StatusWith<U> by either applying
+ * the function to the _t member or forwarding the _status. This is the rvalue overload.
+ */
+ template <typename F>
+ StatusWith<std::invoke_result_t<F&&, T&&>> transform(F&& f) && {
+ if (_t)
+ return {std::forward<F>(f)(*std::move(_t))};
+ else
+ return {std::move(_status)};
+ }
+
+ /**
+ * For any type U returned by a function f, transform creates a StatusWith<U> by either applying
+ * the function to the _t member or forwarding the _status. This is the const rvalue overload.
+ */
+ template <typename F>
+ StatusWith<std::invoke_result_t<F&&, const T&&>> transform(F&& f) const&& {
+ if (_t)
+ return {std::forward<F>(f)(*std::move(_t))};
+ else
+ return {std::move(_status)};
+ }
+
+ /**
+ * For any type U returned inside a StatusWith<U> by a function f, andThen directly produces a
+ * StatusWith<U> by applying the function to the _t member or creates one by forwarding the
+ * _status. andThen performs the same function as transform but for a function f with a return
+ * type of StatusWith. This is the lvalue overload.
+ */
+ template <typename F>
+ StatusWith<typename std::invoke_result_t<F&&, T&>::value_type> andThen(F&& f) & {
+ if (_t)
+ return {std::forward<F>(f)(*_t)};
+ else
+ return {_status};
+ }
+
+ /**
+ * For any type U returned inside a StatusWith<U> by a function f, andThen directly produces a
+ * StatusWith<U> by applying the function to the _t member or creates one by forwarding the
+ * _status. andThen performs the same function as transform but for a function f with a return
+ * type of StatusWith. This is the const overload.
+ */
+ template <typename F>
+ StatusWith<typename std::invoke_result_t<F&&, const T&>::value_type> andThen(F&& f) const& {
+ if (_t)
+ return {std::forward<F>(f)(*_t)};
+ else
+ return {_status};
+ }
+
+ /**
+ * For any type U returned inside a StatusWith<U> by a function f, andThen directly produces a
+ * StatusWith<U> by applying the function to the _t member or creates one by forwarding the
+ * _status. andThen performs the same function as transform but for a function f with a return
+ * type of StatusWith. This is the rvalue overload.
+ */
+ template <typename F>
+ StatusWith<typename std::invoke_result_t<F&&, T&&>::value_type> andThen(F&& f) && {
+ if (_t)
+ return {std::forward<F>(f)(*std::move(_t))};
+ else
+ return {std::move(_status)};
+ }
+
+ /**
+ * For any type U returned inside a StatusWith<U> by a function f, andThen directly produces a
+ * StatusWith<U> by applying the function to the _t member or creates one by forwarding the
+ * _status. andThen performs the same function as transform but for a function f with a return
+ * type of StatusWith. This is the const rvalue overload.
+ */
+ template <typename F>
+ StatusWith<typename std::invoke_result_t<F&&, const T&&>::value_type> andThen(F&& f) const&& {
+ if (_t)
+ return {std::forward<F>(f)(*std::move(_t))};
+ else
+ return {std::move(_status)};
+ }
+
+ /**
* This method is a transitional tool, to facilitate transition to compile-time enforced status
* checking.
*