Use anonymous namespace to avoid friend declarations in future.hpp

This commit is contained in:
Tyler Neely 2022-07-21 10:49:19 +00:00
parent 498ae97ae9
commit f6c2202772
2 changed files with 19 additions and 37 deletions
src/io/v3
tests/simulation

View File

@ -22,18 +22,7 @@
#include "io/v3/errors.hpp"
template <typename T>
class MgPromise;
template <typename T>
class MgFuture;
template <typename T>
std::pair<MgFuture<T>, MgPromise<T>> FuturePromisePair();
template <typename T>
std::pair<MgFuture<T>, MgPromise<T>> FuturePromisePairWithNotifier(std::function<bool()>);
namespace {
template <typename T>
class Shared {
std::condition_variable cv_;
@ -43,10 +32,14 @@ class Shared {
bool waiting_ = false;
std::optional<std::function<bool()>> simulator_notifier_;
friend std::pair<MgFuture<T>, MgPromise<T>> FuturePromisePair<T>();
friend std::pair<MgFuture<T>, MgPromise<T>> FuturePromisePairWithNotifier<T>(std::function<bool()>);
friend MgPromise<T>;
friend MgFuture<T>;
public:
explicit Shared(std::function<bool()> simulator_notifier) : simulator_notifier_(simulator_notifier) {}
Shared() = default;
Shared(Shared &&) = delete;
Shared &operator=(Shared &&) = delete;
Shared(const Shared &) = delete;
Shared &operator=(const Shared &) = delete;
~Shared() = default;
T Wait() {
std::unique_lock<std::mutex> lock(mu_);
@ -128,27 +121,18 @@ class Shared {
std::unique_lock<std::mutex> lock(mu_);
return waiting_;
}
public:
explicit Shared(std::function<bool()> simulator_notifier) : simulator_notifier_(simulator_notifier) {}
Shared() = default;
Shared(Shared &&) = delete;
Shared &operator=(Shared &&) = delete;
Shared(const Shared &) = delete;
Shared &operator=(const Shared &) = delete;
~Shared() = default;
};
} // namespace
template <typename T>
class MgFuture {
explicit MgFuture(std::shared_ptr<Shared<T>> shared) : shared_(shared) {}
bool consumed_or_moved_ = false;
std::shared_ptr<Shared<T>> shared_;
friend std::pair<MgFuture<T>, MgPromise<T>> FuturePromisePair<T>();
friend std::pair<MgFuture<T>, MgPromise<T>> FuturePromisePairWithNotifier<T>(std::function<bool()>);
public:
explicit MgFuture(std::shared_ptr<Shared<T>> shared) : shared_(shared) {}
MgFuture() = delete;
MgFuture(MgFuture &&old) {
shared_ = std::move(old.shared_);
consumed_or_moved_ = old.consumed_or_moved_;
@ -206,12 +190,10 @@ class MgPromise {
std::shared_ptr<Shared<T>> shared_;
bool filled_or_moved_ = false;
friend std::pair<MgFuture<T>, MgPromise<T>> FuturePromisePair<T>();
friend std::pair<MgFuture<T>, MgPromise<T>> FuturePromisePairWithNotifier<T>(std::function<bool()>);
public:
explicit MgPromise(std::shared_ptr<Shared<T>> shared) : shared_(shared) {}
MgPromise() = delete;
MgPromise(MgPromise &&old) {
shared_ = std::move(old.shared_);
MG_ASSERT(!old.filled_or_moved_, "MgPromise moved from after already being moved from or filled.");

View File

@ -14,11 +14,11 @@
#include "io/v3/simulator.hpp"
struct CounterRequest {
uint64_t proposal_;
uint64_t proposal;
};
struct CounterResponse {
uint64_t highest_seen_;
uint64_t highest_seen;
};
void run_server(Io<SimulatorTransport> io) {
@ -34,7 +34,7 @@ void run_server(Io<SimulatorTransport> io) {
auto request_envelope = request_result.GetValue();
auto req = std::get<CounterRequest>(request_envelope.message);
highest_seen = std::max(highest_seen, req.proposal_);
highest_seen = std::max(highest_seen, req.proposal);
auto srv_res = CounterResponse{highest_seen};
request_envelope.Reply(srv_res, io);
@ -62,13 +62,13 @@ int main() {
for (int i = 1; i < 3; ++i) {
// send request
CounterRequest cli_req;
cli_req.proposal_ = i;
cli_req.proposal = i;
auto res_f = cli_io.RequestWithTimeout<CounterRequest, CounterResponse>(srv_addr, cli_req, 1000);
auto res_rez = res_f.Wait();
if (!res_rez.HasError()) {
std::cout << "[CLIENT] Got a valid response" << std::endl;
auto env = res_rez.GetValue();
MG_ASSERT(env.message.highest_seen_ == i);
MG_ASSERT(env.message.highest_seen == i);
} else {
std::cout << "[CLIENT] Got an error" << std::endl;
}