1 /*
2  * Copyright (C) 2018 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef IDMAP2_INCLUDE_IDMAP2_RESULT_H_
18 #define IDMAP2_INCLUDE_IDMAP2_RESULT_H_
19 
20 #include <string>
21 #include <utility>
22 #include <variant>
23 
24 #include "android-base/logging.h"  // CHECK
25 
26 namespace android::idmap2 {
27 
28 using Unit = std::monostate;
29 
30 class Error {
31  public:
32   explicit Error(const Error& parent) = default;
33 
34   // NOLINTNEXTLINE(cert-dcl50-cpp)
35   explicit Error(const char* fmt, ...) __attribute__((__format__(printf, 2, 3)));
36 
37   // NOLINTNEXTLINE(cert-dcl50-cpp)
38   explicit Error(const Error& parent, const char* fmt, ...)
39       __attribute__((__format__(printf, 3, 4)));
40 
GetMessage()41   inline std::string GetMessage() const {
42     return msg_;
43   }
44 
45  private:
46   std::string msg_;
47 };
48 
49 template <typename T>
50 class Result {
51  public:
52   Result(const T& value);      // NOLINT(runtime/explicit)
53   Result(T&& value) noexcept;  // NOLINT(runtime/explicit)
54 
55   Result(const Error& error);      // NOLINT(runtime/explicit)
56   Result(Error&& error) noexcept;  // NOLINT(runtime/explicit)
57 
58   Result(const Result& error) = default;
59 
60   Result& operator=(const Result& rhs) = default;
61   Result& operator=(Result&& rhs) noexcept = default;
62 
63   explicit operator bool() const;
64 
65   constexpr const T& operator*() const&;
66   T& operator*() &;
67 
68   constexpr const T* operator->() const&;
69   T* operator->() &;
70 
71   std::string GetErrorMessage() const;
72   Error GetError() const;
73 
74  private:
75   bool is_ok() const;
76 
77   std::variant<T, Error> data_;
78 };
79 
80 template <typename T>
Result(const T & value)81 Result<T>::Result(const T& value) : data_(std::in_place_type<T>, value) {
82 }
83 
84 template <typename T>
Result(T && value)85 Result<T>::Result(T&& value) noexcept : data_(std::in_place_type<T>, std::forward<T>(value)) {
86 }
87 
88 template <typename T>
Result(const Error & error)89 Result<T>::Result(const Error& error) : data_(std::in_place_type<Error>, error) {
90 }
91 
92 template <typename T>
Result(Error && error)93 Result<T>::Result(Error&& error) noexcept
94     : data_(std::in_place_type<Error>, std::forward<Error>(error)) {
95 }
96 
97 template <typename T>
98 Result<T>::operator bool() const {
99   return is_ok();
100 }
101 
102 template <typename T>
103 constexpr const T& Result<T>::operator*() const& {
104   CHECK(is_ok()) << "Result<T>::operator* called in ERROR state";
105   return std::get<T>(data_);
106 }
107 
108 template <typename T>
109 T& Result<T>::operator*() & {
110   CHECK(is_ok()) << "Result<T>::operator* called in ERROR state";
111   return std::get<T>(data_);
112 }
113 
114 template <typename T>
115 constexpr const T* Result<T>::operator->() const& {
116   CHECK(is_ok()) << "Result<T>::operator-> called in ERROR state";
117   return &std::get<T>(data_);
118 }
119 
120 template <typename T>
121 T* Result<T>::operator->() & {
122   CHECK(is_ok()) << "Result<T>::operator-> called in ERROR state";
123   return &std::get<T>(data_);
124 }
125 
126 template <typename T>
GetErrorMessage()127 inline std::string Result<T>::GetErrorMessage() const {
128   CHECK(!is_ok()) << "Result<T>::GetErrorMessage called in OK state";
129   return std::get<Error>(data_).GetMessage();
130 }
131 
132 template <typename T>
GetError()133 inline Error Result<T>::GetError() const {
134   CHECK(!is_ok()) << "Result<T>::GetError called in OK state";
135   return Error(std::get<Error>(data_));
136 }
137 
138 template <typename T>
is_ok()139 inline bool Result<T>::is_ok() const {
140   return std::holds_alternative<T>(data_);
141 }
142 
143 }  // namespace android::idmap2
144 
145 #endif  // IDMAP2_INCLUDE_IDMAP2_RESULT_H_
146