/proc/self/cwd/internal/status_builder.h
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2021 Google LLC |
2 | | // |
3 | | // Licensed under the Apache License, Version 2.0 (the "License"); |
4 | | // you may not use this file except in compliance with the License. |
5 | | // You may obtain a copy of the License at |
6 | | // |
7 | | // https://www.apache.org/licenses/LICENSE-2.0 |
8 | | // |
9 | | // Unless required by applicable law or agreed to in writing, software |
10 | | // distributed under the License is distributed on an "AS IS" BASIS, |
11 | | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
12 | | // See the License for the specific language governing permissions and |
13 | | // limitations under the License. |
14 | | |
15 | | #ifndef THIRD_PARTY_CEL_CPP_INTERNAL_STATUS_BUILDER_H_ |
16 | | #define THIRD_PARTY_CEL_CPP_INTERNAL_STATUS_BUILDER_H_ |
17 | | |
18 | | #include <type_traits> |
19 | | #include <utility> |
20 | | |
21 | | #include "absl/base/attributes.h" |
22 | | #include "absl/status/status.h" |
23 | | |
24 | | namespace cel::internal { |
25 | | |
26 | | class StatusBuilder; |
27 | | |
28 | | template <typename Invocable, typename Argument, typename Expected> |
29 | | inline constexpr bool kResultMatches = |
30 | | std::is_same_v<std::decay_t<std::invoke_result_t<Invocable, Argument>>, |
31 | | Expected>; |
32 | | |
33 | | template <typename Adaptor, typename Builder> |
34 | | using EnableIfStatusBuilder = |
35 | | std::enable_if_t<kResultMatches<Adaptor, Builder, StatusBuilder>, |
36 | | std::invoke_result_t<Adaptor, Builder>>; |
37 | | |
38 | | template <typename Adaptor, typename Builder> |
39 | | using EnableIfStatus = |
40 | | std::enable_if_t<kResultMatches<Adaptor, Builder, absl::Status>, |
41 | | std::invoke_result_t<Adaptor, Builder>>; |
42 | | |
43 | | class StatusBuilder final { |
44 | | public: |
45 | | StatusBuilder() = default; |
46 | | |
47 | 0 | explicit StatusBuilder(const absl::Status& status) : status_(status) {} |
48 | | |
49 | | StatusBuilder(const StatusBuilder&) = default; |
50 | | |
51 | | StatusBuilder(StatusBuilder&&) = default; |
52 | | |
53 | | ~StatusBuilder() = default; |
54 | | |
55 | | StatusBuilder& operator=(const StatusBuilder&) = default; |
56 | | |
57 | | StatusBuilder& operator=(StatusBuilder&&) = default; |
58 | | |
59 | 0 | bool ok() const { return status_.ok(); } |
60 | | |
61 | 0 | absl::StatusCode code() const { return status_.code(); } |
62 | | |
63 | 0 | operator absl::Status() const& { return status_; } // NOLINT |
64 | | |
65 | 0 | operator absl::Status() && { return std::move(status_); } // NOLINT |
66 | | |
67 | | template <typename Adaptor> |
68 | | auto With( |
69 | | Adaptor&& adaptor) & -> EnableIfStatusBuilder<Adaptor, StatusBuilder&> { |
70 | | return std::forward<Adaptor>(adaptor)(*this); |
71 | | } |
72 | | |
73 | | template <typename Adaptor> |
74 | | ABSL_MUST_USE_RESULT auto With( |
75 | | Adaptor&& adaptor) && -> EnableIfStatusBuilder<Adaptor, StatusBuilder&&> { |
76 | | return std::forward<Adaptor>(adaptor)(std::move(*this)); |
77 | | } |
78 | | |
79 | | template <typename Adaptor> |
80 | | auto With(Adaptor&& adaptor) & -> EnableIfStatus<Adaptor, StatusBuilder&> { |
81 | | return std::forward<Adaptor>(adaptor)(*this); |
82 | | } |
83 | | |
84 | | template <typename Adaptor> |
85 | | ABSL_MUST_USE_RESULT auto With( |
86 | | Adaptor&& adaptor) && -> EnableIfStatus<Adaptor, StatusBuilder&&> { |
87 | | return std::forward<Adaptor>(adaptor)(std::move(*this)); |
88 | | } |
89 | | |
90 | | private: |
91 | | absl::Status status_; |
92 | | }; |
93 | | |
94 | | } // namespace cel::internal |
95 | | |
96 | | #endif // THIRD_PARTY_CEL_CPP_INTERNAL_STATUS_BUILDER_H_ |