# Oboe: include/oboe/ResultWithValue.h Source File

ResultWithValue.h
/*
* Copyright (C) 2018 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OBOE_RESULT_WITH_VALUE_H
#define OBOE_RESULT_WITH_VALUE_H
#include "oboe/Definitions.h"
#include <iostream>
#include <sstream>
namespace oboe {
template <typename T>
class ResultWithValue {
public:
ResultWithValue(oboe::Result error)
: mValue{}
, mError(error) {}
explicit ResultWithValue(T value)
: mValue(value)
, mError(oboe::Result::OK) {}
oboe::Result error() const {
return mError;
}
T value() const {
return mValue;
}
explicit operator bool() const { return mError == oboe::Result::OK; }
bool operator !() const { return mError != oboe::Result::OK; }
operator Result() const {
return mError;
}
static ResultWithValue<T> createBasedOnSign(T numericResult){
// Ensure that the type is either an integer or float
static_assert(std::is_arithmetic<T>::value,
"createBasedOnSign can only be called for numeric types (int or float)");
if (numericResult >= 0){
return ResultWithValue<T>(numericResult);
} else {
return ResultWithValue<T>(static_cast<Result>(numericResult));
}
}
private:
const T mValue;
const oboe::Result mError;
};
template <typename T>
std::ostream& operator<<(std::ostream &strm, const ResultWithValue<T> &result) {
if (!result) {
strm << convertToText(result.error());
} else {
strm << result.value();
}
return strm;
}
} // namespace oboe
#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