Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / third_party / pigweed / repo / pw_result / public / pw_result / result.h
1 // Copyright 2020 The Pigweed Authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
5 // 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, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
13 // the License.
14 #pragma once
15
16 #include <algorithm>
17
18 #include "pw_assert/assert.h"
19 #include "pw_status/status.h"
20
21 namespace pw {
22
23 // A Result represents the result of an operation which can fail. It is a
24 // convenient wrapper around returning a Status alongside some data when the
25 // status is OK.
26 template <typename T>
27 class Result {
28  public:
29   constexpr Result(T&& value) : value_(std::move(value)), status_(OkStatus()) {}
30   constexpr Result(const T& value) : value_(value), status_(OkStatus()) {}
31
32   template <typename... Args>
33   constexpr Result(std::in_place_t, Args&&... args)
34       : value_(std::forward<Args>(args)...), status_(OkStatus()) {}
35
36   // TODO(pwbug/246): This can be constexpr when tokenized asserts are fixed.
37   Result(Status status) : status_(status) { PW_CHECK(status_ != OkStatus()); }
38   // TODO(pwbug/246): This can be constexpr when tokenized asserts are fixed.
39   Result(Status::Code code) : status_(code) { PW_CHECK(status_ != OkStatus()); }
40
41   constexpr Result(const Result&) = default;
42   constexpr Result& operator=(const Result&) = default;
43
44   constexpr Result(Result&&) = default;
45   constexpr Result& operator=(Result&&) = default;
46
47   constexpr Status status() const { return status_; }
48   constexpr bool ok() const { return status_.ok(); }
49
50   // TODO(pwbug/246): This can be constexpr when tokenized asserts are fixed.
51   T& value() & {
52     PW_CHECK_OK(status_);
53     return value_;
54   }
55
56   // TODO(pwbug/246): This can be constexpr when tokenized asserts are fixed.
57   const T& value() const& {
58     PW_CHECK_OK(status_);
59     return value_;
60   }
61
62   // TODO(pwbug/246): This can be constexpr when tokenized asserts are fixed.
63   T&& value() && {
64     PW_CHECK_OK(status_);
65     return std::move(value_);
66   }
67
68   template <typename U>
69   constexpr T value_or(U&& default_value) const& {
70     if (ok()) {
71       return value_;
72     }
73     return std::forward<U>(default_value);
74   }
75
76   template <typename U>
77   constexpr T value_or(U&& default_value) && {
78     if (ok()) {
79       return std::move(value_);
80     }
81     return std::forward<U>(default_value);
82   }
83
84  private:
85   union {
86     T value_;
87   };
88   Status status_;
89 };
90
91 }  // namespace pw