Oboe
A library for creating real-time audio apps on Android
Loading...
Searching...
No Matches
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
24namespace oboe {
25
46template <typename T>
48public:
49
55 ResultWithValue(oboe::Result error)
56 : mValue{}
57 , mError(error) {}
58
65 : mValue(value)
66 , mError(oboe::Result::OK) {}
67
68 ResultWithValue(T value, oboe::Result error)
69 : mValue(value)
70 , mError(error) {}
71
77 oboe::Result error() const {
78 return mError;
79 }
80
85 T value() const {
86 return mValue;
87 }
88
92 explicit operator bool() const { return mError == oboe::Result::OK; }
93
104 bool operator !() const { return mError != oboe::Result::OK; }
105
114 operator Result() const {
115 return mError;
116 }
117
125 static ResultWithValue<T> createBasedOnSign(T numericResult){
126
127 // Ensure that the type is either an integer or float
128 static_assert(std::is_arithmetic<T>::value,
129 "createBasedOnSign can only be called for numeric types (int or float)");
130
131 if (numericResult >= 0){
132 return ResultWithValue<T>(numericResult);
133 } else {
134 return ResultWithValue<T>(static_cast<Result>(numericResult));
135 }
136 }
137
138private:
139 const T mValue;
140 const oboe::Result mError;
141};
142
146template <typename T>
147std::ostream& operator<<(std::ostream &strm, const ResultWithValue<T> &result) {
148 if (!result) {
149 strm << convertToText(result.error());
150 } else {
151 strm << result.value();
152 }
153 return strm;
154}
155
156} // namespace oboe
157
158
159#endif //OBOE_RESULT_WITH_VALUE_H
Definition ResultWithValue.h:47
bool operator!() const
Definition ResultWithValue.h:104
T value() const
Definition ResultWithValue.h:85
ResultWithValue(T value)
Definition ResultWithValue.h:64
static ResultWithValue< T > createBasedOnSign(T numericResult)
Definition ResultWithValue.h:125
ResultWithValue(oboe::Result error)
Definition ResultWithValue.h:55
oboe::Result error() const
Definition ResultWithValue.h:77