Oboe 1.2 A library for creating real-time audio apps on Android

ResultWithValue.h
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 OBOE_RESULT_WITH_VALUE_H
18 #define OBOE_RESULT_WITH_VALUE_H
19
20 #include "oboe/Definitions.h"
21 #include <iostream>
22 #include <sstream>
23
24 namespace oboe {
25
46 template <typename T>
47 class ResultWithValue {
48 public:
49
55 ResultWithValue(oboe::Result error)
56 : mValue{}
57 , mError(error) {}
58
64 explicit ResultWithValue(T value)
65 : mValue(value)
66 , mError(oboe::Result::OK) {}
67
73 oboe::Result error() const {
74 return mError;
75 }
76
81 T value() const {
82 return mValue;
83 }
84
88 explicit operator bool() const { return mError == oboe::Result::OK; }
89
100 bool operator !() const { return mError != oboe::Result::OK; }
101
110 operator Result() const {
111 return mError;
112 }
113
121 static ResultWithValue<T> createBasedOnSign(T numericResult){
122
123 // Ensure that the type is either an integer or float
124 static_assert(std::is_arithmetic<T>::value,
125 "createBasedOnSign can only be called for numeric types (int or float)");
126
127 if (numericResult >= 0){
128 return ResultWithValue<T>(numericResult);
129 } else {
130 return ResultWithValue<T>(static_cast<Result>(numericResult));
131 }
132 }
133
134 private:
135 const T mValue;
136 const oboe::Result mError;
137 };
138
142 template <typename T>
143 std::ostream& operator<<(std::ostream &strm, const ResultWithValue<T> &result) {
144 if (!result) {
145 strm << convertToText(result.error());
146 } else {
147 strm << result.value();
148 }
149 return strm;
150 }
151
152 } // namespace oboe
153
154
155 #endif //OBOE_RESULT_WITH_VALUE_H
oboe::ResultWithValue::value
T value() const
Definition: ResultWithValue.h:81
oboe::ResultWithValue::ResultWithValue
ResultWithValue(oboe::Result error)
Definition: ResultWithValue.h:55
oboe::ResultWithValue::createBasedOnSign
static ResultWithValue< T > createBasedOnSign(T numericResult)
Definition: ResultWithValue.h:121
oboe::operator<<
std::ostream & operator<<(std::ostream &strm, const ResultWithValue< T > &result)
Definition: ResultWithValue.h:143
oboe::convertToText
const char * convertToText(FromType input)
oboe::ResultWithValue::error
oboe::Result error() const
Definition: ResultWithValue.h:73
oboe::ResultWithValue::operator !
bool operator !() const
Definition: ResultWithValue.h:100
oboe::Result
Result
Definition: Definitions.h:131
oboe::ResultWithValue::ResultWithValue
ResultWithValue(T value)
Definition: ResultWithValue.h:64
oboe
Definition: AudioStream.h:31
oboe::ResultWithValue
Definition: ResultWithValue.h:47