blob: baa680dfee4c8da8185dfffa818d0affa7f77c0c [file] [log] [blame]
Tom Cherry11a3aee2017-08-03 12:54:07 -07001/*
2 * Copyright (C) 2017 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// This file contains classes for returning a successful result along with an optional
18// arbitrarily typed return value or for returning a failure result along with an optional string
19// indicating why the function failed.
20
21// There are 3 classes that implement this functionality and one additional helper type.
22//
23// Result<T> either contains a member of type T that can be accessed using similar semantics as
Tom Cherry130e3d72017-08-22 16:07:15 -070024// std::optional<T> or it contains a ResultError describing an error, which can be accessed via
Tom Cherry11a3aee2017-08-03 12:54:07 -070025// Result<T>::error().
26//
Tom Cherry130e3d72017-08-22 16:07:15 -070027// ResultError is a type that contains both a std::string describing the error and a copy of errno
28// from when the error occurred. ResultError can be used in an ostream directly to print its
29// string value.
30//
Tom Cherry11a3aee2017-08-03 12:54:07 -070031// Success is a typedef that aids in creating Result<T> that do not contain a return value.
32// Result<Success> is the correct return type for a function that either returns successfully or
33// returns an error value. Returning Success() from a function that returns Result<Success> is the
34// correct way to indicate that a function without a return type has completed successfully.
35//
36// A successful Result<T> is constructed implicitly from any type that can be implicitly converted
37// to T or from the constructor arguments for T. This allows you to return a type T directly from
38// a function that returns Result<T>.
39//
Tom Cherry130e3d72017-08-22 16:07:15 -070040// Error and ErrnoError are used to construct a Result<T> that has failed. The Error class takes
41// an ostream as an input and are implicitly cast to a Result<T> containing that failure.
42// ErrnoError() is a helper function to create an Error class that appends ": " + strerror(errno)
43// to the end of the failure string to aid in interacting with C APIs. Alternatively, an errno
44// value can be directly specified via the Error() constructor.
45//
46// ResultError can be used in the ostream when using Error to construct a Result<T>. In this case,
47// the string that the ResultError takes is passed through the stream normally, but the errno is
48// passed to the Result<T>. This can be used to pass errno from a failing C function up multiple
49// callers.
50//
51// ResultError can also directly construct a Result<T>. This is particularly useful if you have a
52// function that return Result<T> but you have a Result<U> and want to return its error. In this
53// case, you can return the .error() from the Result<U> to construct the Result<T>.
Tom Cherry11a3aee2017-08-03 12:54:07 -070054
55// An example of how to use these is below:
56// Result<U> CalculateResult(const T& input) {
57// U output;
58// if (!SomeOtherCppFunction(input, &output)) {
59// return Error() << "SomeOtherCppFunction(" << input << ") failed";
60// }
61// if (!c_api_function(output)) {
62// return ErrnoError() << "c_api_function(" << output << ") failed";
63// }
64// return output;
65// }
66//
67// auto output = CalculateResult(input);
68// if (!output) return Error() << "CalculateResult failed: " << output.error();
69// UseOutput(*output);
70
Tom Cherry9949ec52019-05-16 16:54:49 -070071#pragma once
Tom Cherry11a3aee2017-08-03 12:54:07 -070072
73#include <errno.h>
74
75#include <sstream>
76#include <string>
Tom Cherry9949ec52019-05-16 16:54:49 -070077
78#include <android-base/expected.h>
Tom Cherry11a3aee2017-08-03 12:54:07 -070079
80namespace android {
81namespace init {
82
Tom Cherry130e3d72017-08-22 16:07:15 -070083struct ResultError {
84 template <typename T>
85 ResultError(T&& error_string, int error_errno)
Tom Cherry9949ec52019-05-16 16:54:49 -070086 : as_string(std::forward<T>(error_string)), as_errno(error_errno) {}
Tom Cherry130e3d72017-08-22 16:07:15 -070087
Tom Cherry9949ec52019-05-16 16:54:49 -070088 template <typename T>
89 operator android::base::expected<T, ResultError>() {
90 return android::base::unexpected(ResultError(as_string, as_errno));
91 }
92
93 std::string as_string;
94 int as_errno;
Tom Cherry130e3d72017-08-22 16:07:15 -070095};
96
97inline std::ostream& operator<<(std::ostream& os, const ResultError& t) {
Tom Cherry9949ec52019-05-16 16:54:49 -070098 os << t.as_string;
Tom Cherry130e3d72017-08-22 16:07:15 -070099 return os;
100}
101
102inline std::ostream& operator<<(std::ostream& os, ResultError&& t) {
Tom Cherry9949ec52019-05-16 16:54:49 -0700103 os << std::move(t.as_string);
Tom Cherry130e3d72017-08-22 16:07:15 -0700104 return os;
105}
106
Tom Cherry11a3aee2017-08-03 12:54:07 -0700107class Error {
108 public:
Tom Cherry130e3d72017-08-22 16:07:15 -0700109 Error() : errno_(0), append_errno_(false) {}
110 Error(int errno_to_append) : errno_(errno_to_append), append_errno_(true) {}
Tom Cherry11a3aee2017-08-03 12:54:07 -0700111
112 template <typename T>
Tom Cherry9949ec52019-05-16 16:54:49 -0700113 operator android::base::expected<T, ResultError>() {
114 return android::base::unexpected(ResultError(str(), errno_));
115 }
116
117 template <typename T>
Tom Cherry11a3aee2017-08-03 12:54:07 -0700118 Error&& operator<<(T&& t) {
119 ss_ << std::forward<T>(t);
120 return std::move(*this);
121 }
122
Tom Cherry130e3d72017-08-22 16:07:15 -0700123 Error&& operator<<(const ResultError& result_error) {
Tom Cherry9949ec52019-05-16 16:54:49 -0700124 ss_ << result_error.as_string;
125 errno_ = result_error.as_errno;
Tom Cherry130e3d72017-08-22 16:07:15 -0700126 return std::move(*this);
Tom Cherry11a3aee2017-08-03 12:54:07 -0700127 }
128
Tom Cherry130e3d72017-08-22 16:07:15 -0700129 Error&& operator<<(ResultError&& result_error) {
Tom Cherry9949ec52019-05-16 16:54:49 -0700130 ss_ << std::move(result_error.as_string);
131 errno_ = result_error.as_errno;
Tom Cherry130e3d72017-08-22 16:07:15 -0700132 return std::move(*this);
133 }
134
135 const std::string str() const {
136 std::string str = ss_.str();
137 if (append_errno_) {
138 if (str.empty()) {
139 return strerror(errno_);
140 }
141 return str + ": " + strerror(errno_);
142 }
143 return str;
144 }
145
146 int get_errno() const { return errno_; }
147
Tom Cherry11a3aee2017-08-03 12:54:07 -0700148 Error(const Error&) = delete;
149 Error(Error&&) = delete;
150 Error& operator=(const Error&) = delete;
151 Error& operator=(Error&&) = delete;
152
Tom Cherry11a3aee2017-08-03 12:54:07 -0700153 private:
154 std::stringstream ss_;
Tom Cherry130e3d72017-08-22 16:07:15 -0700155 int errno_;
156 bool append_errno_;
Tom Cherry11a3aee2017-08-03 12:54:07 -0700157};
158
Tom Cherry130e3d72017-08-22 16:07:15 -0700159inline Error ErrnoError() {
160 return Error(errno);
161}
Tom Cherry11a3aee2017-08-03 12:54:07 -0700162
163template <typename T>
Tom Cherry9949ec52019-05-16 16:54:49 -0700164using Result = android::base::expected<T, ResultError>;
Tom Cherry11a3aee2017-08-03 12:54:07 -0700165
166using Success = std::monostate;
167
168} // namespace init
169} // namespace android
170