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
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
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){
129 } else {
130 return ResultWithValue<T>(static_cast<Result>(numericResult));
131 }
132 }
133
134private:
135 const T mValue;
136 const oboe::Result mError;
137};
138
142template <typename T>
143std::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
Definition ResultWithValue.h:47
bool operator!() const
Definition ResultWithValue.h:100
T value() const
Definition ResultWithValue.h:81
ResultWithValue(T value)
Definition ResultWithValue.h:64
static ResultWithValue< T > createBasedOnSign(T numericResult)
Definition ResultWithValue.h:121
ResultWithValue(oboe::Result error)
Definition ResultWithValue.h:55
oboe::Result error() const
Definition ResultWithValue.h:73