Continue to bolt-down MgFuture impl

This commit is contained in:
Tyler Neely 2022-07-04 18:54:09 +00:00
parent 20839b0ae0
commit 73719b2120
4 changed files with 38 additions and 22 deletions

View File

@ -43,8 +43,8 @@ class Shared {
public:
Shared() = default;
Shared(Shared &&) = default;
Shared &operator=(Shared &&) = default;
Shared(Shared &&) = delete;
Shared &operator=(Shared &&) = delete;
Shared(const Shared &) = delete;
Shared &operator=(const Shared &) = delete;
~Shared() = default;
@ -121,19 +121,33 @@ class MgFuture {
friend std::pair<MgFuture<T>, MgPromise<T>> FuturePromisePair<T>(SimulatorHandle);
public:
MgFuture(MgFuture &&) = default;
MgFuture &operator=(MgFuture &&) = default;
MgFuture(MgFuture &&old) {
shared_ = std::move(old.shared_);
consumed_or_moved_ = old.consumed_or_moved_;
old.consumed_or_moved_ = true;
}
MgFuture &operator=(MgFuture &&old) {
shared_ = std::move(old.shared_);
consumed_or_moved_ = old.consumed_or_moved_;
old.consumed_or_moved_ = true;
}
MgFuture(const MgFuture &) = delete;
MgFuture &operator=(const MgFuture &) = delete;
~MgFuture() = default;
// Block on the corresponding promise to be filled,
// returning the inner item when ready.
T Wait() { return shared_->Wait(); }
T Wait() {
MG_ASSERT(!consumed_or_moved_, "MgFuture should only be consumed with Wait once!");
T ret = shared_->Wait();
consumed_or_moved_ = true;
return ret;
}
private:
MgFuture(std::shared_ptr<Shared<T>> shared) : shared_(shared) {}
bool consumed_or_moved_;
std::shared_ptr<Shared<T>> shared_;
};
@ -143,23 +157,26 @@ class MgPromise {
friend std::pair<MgFuture<T>, MgPromise<T>> FuturePromisePair<T>(SimulatorHandle);
public:
MgPromise(MgPromise &&) = default;
MgPromise &operator=(MgPromise &&) = default;
MgPromise(MgPromise &&old) {
shared_ = std::move(old.shared_);
filled_or_moved_ = old.filled_or_moved_;
old.filled_or_moved_ = true;
}
MgPromise &operator=(MgPromise &&old) {
shared_ = std::move(old.shared_);
filled_or_moved_ = old.filled_or_moved_;
old.filled_or_moved_ = true;
}
MgPromise(const MgPromise &) = delete;
MgPromise &operator=(const MgPromise &) = delete;
~MgPromise() {
/*
MG_ASSERT(filled_,
"MgPromise destroyed before its \
associated MgFuture was filled!");
*/
}
~MgPromise() { MG_ASSERT(filled_or_moved_, "MgPromise destroyed before its associated MgFuture was filled!"); }
// Fill the expected item into the Future.
void Fill(T item) {
MG_ASSERT(!filled_or_moved_, "MgPromise::Fill called twice on the same promise!");
shared_->Fill(item);
filled_ = true;
filled_or_moved_ = true;
}
bool IsAwaited() { return shared_->IsAwaited(); }
@ -168,7 +185,7 @@ class MgPromise {
MgPromise(std::shared_ptr<Shared<T>> shared) : shared_(shared) {}
std::shared_ptr<Shared<T>> shared_;
bool filled_;
bool filled_or_moved_;
};
template <typename T>

View File

@ -11,6 +11,8 @@
#pragma once
#include "address.hpp"
class SimulatorHandle {
public:
void NotifySimulator() {

View File

@ -19,5 +19,8 @@ function(add_simulation_test test_cpp)
add_dependencies(memgraph__simulation ${target_name})
endfunction(add_simulation_test)
add_simulation_test(future.cpp)
target_link_libraries(${test_prefix}future mg-utils mg-io-v3)
add_simulation_test(basic_request.cpp)
target_link_libraries(${test_prefix}basic_request mg-utils mg-io-v3)

View File

@ -18,11 +18,6 @@
#include "utils/logging.hpp"
int main() {
auto [future, promise] = FuturePromisePair<std::string>();
promise.Fill("yo");
MG_ASSERT(future.Wait() == "yo");
/*
auto simulator = Simulator();
auto addr_1 = Address();
auto addr_2 = Address();
@ -31,7 +26,6 @@ int main() {
auto sim_transport_1 = simulator.Register(addr_1, true);
auto sim_transport_2 = simulator.Register(addr_2, true);
auto sim_transport_3 = simulator.Register(addr_3, true);
*/
return 0;
}