Skip to content

Commit

Permalink
oboe: cleaned up ErrorOrValue
Browse files Browse the repository at this point in the history
Inverted boolean operators.
More generic constructors.
  • Loading branch information
Phil Burk authored and dturner committed Mar 27, 2018
1 parent 4cf25f1 commit 7c19352
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 11 deletions.
23 changes: 16 additions & 7 deletions include/oboe/ErrorOrValue.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (C) 2016 The Android Open Source Project
* 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.
Expand Down Expand Up @@ -29,8 +29,8 @@ class ErrorOrValue {
, mError(error) {}

explicit ErrorOrValue(T value)
: mValue(value < 0 ? 0 : value)
, mError(value < 0 ? static_cast<Result>(value) : oboe::Result::OK) {}
: mValue(value)
, mError(oboe::Result::OK) {}

oboe::Result error() const {
return mError;
Expand All @@ -41,12 +41,21 @@ class ErrorOrValue {
}

/**
* Quick way to check for an error.
* @return true if an error occurred // TODO does this seem backwards?
* @return true if OK
*/
explicit operator bool() const { return mError != oboe::Result::OK; }
explicit operator bool() const { return mError == oboe::Result::OK; }

bool operator !() const { return mError == oboe::Result::OK; }
/**
* Quick way to check for an error.
*
* The caller could write something like this:
* <code>
* if (!result) { printf("Got error %s\n", convertToText(result.error())); }
* </code>
*
* @return true if an error occurred
*/
bool operator !() const { return mError != oboe::Result::OK; }

private:
const T mValue;
Expand Down
13 changes: 10 additions & 3 deletions src/aaudio/AudioStreamAAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,15 +264,18 @@ Result AudioStreamAAudio::requestStop() {
}
}

// TODO: Update to return tuple of Result and framesWritten (avoids cast)
ErrorOrValue<int32_t> AudioStreamAAudio::write(const void *buffer,
int32_t numFrames,
int64_t timeoutNanoseconds) {
AAudioStream *stream = mAAudioStream.load();
if (stream != nullptr) {
int32_t result = mLibLoader->stream_write(mAAudioStream, buffer,
numFrames, timeoutNanoseconds);
return ErrorOrValue<int32_t>(result);
if (result < 0) {
return ErrorOrValue<int32_t>(static_cast<Result>(result));
} else {
return ErrorOrValue<int32_t>(result);
}
} else {
return ErrorOrValue<int32_t>(Result::ErrorNull);
}
Expand All @@ -285,7 +288,11 @@ ErrorOrValue<int32_t> AudioStreamAAudio::read(void *buffer,
if (stream != nullptr) {
int32_t result = mLibLoader->stream_read(mAAudioStream, buffer,
numFrames, timeoutNanoseconds);
return ErrorOrValue<int32_t>(result);
if (result < 0) {
return ErrorOrValue<int32_t>(static_cast<Result>(result));
} else {
return ErrorOrValue<int32_t>(result);
}
} else {
return ErrorOrValue<int32_t>(Result::ErrorNull);
}
Expand Down
2 changes: 1 addition & 1 deletion src/opensles/AudioStreamBuffered.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ ErrorOrValue<int32_t> AudioStreamBuffered::transfer(void *buffer,
LOGE("AudioStreamBuffered::%s(): numFrames is negative", __func__);
return ErrorOrValue<int32_t>(Result::ErrorOutOfRange);
} else if (numFrames == 0) {
return ErrorOrValue<int32_t>(0);
return ErrorOrValue<int32_t>(numFrames);
}
if (timeoutNanoseconds < 0) {
LOGE("AudioStreamBuffered::%s(): timeoutNanoseconds is negative", __func__);
Expand Down

0 comments on commit 7c19352

Please sign in to comment.