mirror of
https://github.com/kidfromjupiter/nearby.git
synced 2026-09-14 22:56:12 -04:00
Roll forward to cl/338482889
Signed-off-by: Alexey Polyudov <apolyudov@google.com> Change-Id: Ic2bdb234e89f3c5860d1b483dd4bce689f13d057
This commit is contained in:
@@ -15,65 +15,34 @@
|
||||
#ifndef PLATFORM_API_SETTABLE_FUTURE_H_
|
||||
#define PLATFORM_API_SETTABLE_FUTURE_H_
|
||||
|
||||
#include "platform/api/platform.h"
|
||||
#include "platform/api/settable_future_def.h"
|
||||
#include "platform/exception.h"
|
||||
#include "platform/ptr.h"
|
||||
#include "platform/runnable.h"
|
||||
#include "absl/types/any.h"
|
||||
#include "platform/api/listenable_future.h"
|
||||
#include "platform/base/exception.h"
|
||||
|
||||
namespace location {
|
||||
namespace nearby {
|
||||
namespace api {
|
||||
|
||||
// "Common" part of implementation.
|
||||
// Placed here for textual compatibility to minimize scope of changes.
|
||||
// Can be (and should be) moved to a separate file outside "api" folder.
|
||||
// TODO(apolyudov): for API v2.0
|
||||
namespace platform {
|
||||
namespace impl {
|
||||
|
||||
// A SettableFuture is a type of Future whose result can be set.
|
||||
//
|
||||
// https://google.github.io/guava/releases/20.0/api/docs/com/google/common/util/concurrent/SettableFuture.html
|
||||
template <typename T>
|
||||
class SettableFutureImpl : public SettableFuture<T> {
|
||||
class SettableFuture : public ListenableFuture<T> {
|
||||
public:
|
||||
SettableFutureImpl() {
|
||||
future_ = platform::ImplementationPlatform::createSettableFutureAny();
|
||||
}
|
||||
~SettableFuture() override = default;
|
||||
|
||||
~SettableFutureImpl() override = default;
|
||||
// Completes the future successfully. The value is returned to any waiters.
|
||||
// Returns true, if value was set.
|
||||
// Returns false, if Future is already in "done" state.
|
||||
virtual bool Set(T value) = 0;
|
||||
|
||||
bool set(T value) override { return future_->set(absl::any(value)); }
|
||||
|
||||
bool setException(Exception exception) override {
|
||||
return future_->setException(exception);
|
||||
}
|
||||
|
||||
void addListener(Ptr<Runnable> runnable, Executor* executor) override {
|
||||
future_->addListener(runnable, executor);
|
||||
}
|
||||
|
||||
ExceptionOr<T> get() override { return CommonGet(future_->get()); }
|
||||
ExceptionOr<T> get(std::int64_t timeout_ms) override {
|
||||
return CommonGet(future_->get(timeout_ms));
|
||||
}
|
||||
|
||||
private:
|
||||
ExceptionOr<T> CommonGet(ExceptionOr<absl::any> ret_val) {
|
||||
if (ret_val.exception() != Exception::kSuccess) {
|
||||
return ExceptionOr<T>{ret_val.exception()};
|
||||
}
|
||||
return ExceptionOr<T>{absl::any_cast<T>(ret_val.result())};
|
||||
}
|
||||
|
||||
Ptr<SettableFuture<absl::any>> future_;
|
||||
// Completes the future unsuccessfully. The exception value is returned to any
|
||||
// waiters.
|
||||
// Returns true, if exception was set.
|
||||
// Returns false, if Future is already in "done" state.
|
||||
virtual bool SetException(Exception exception) = 0;
|
||||
};
|
||||
} // namespace impl
|
||||
|
||||
template <typename T>
|
||||
Ptr<SettableFuture<T>> ImplementationPlatform::createSettableFuture() {
|
||||
return Ptr<SettableFuture<T>>(new impl::SettableFutureImpl<T>{});
|
||||
}
|
||||
|
||||
} // namespace platform
|
||||
} // namespace api
|
||||
} // namespace nearby
|
||||
} // namespace location
|
||||
|
||||
|
||||
Reference in New Issue
Block a user