Commit 5455a471 authored by octal's avatar octal

A promise is not attached to its core anymore

parent c9a8e990
......@@ -76,28 +76,12 @@ namespace Async {
struct Core {
Core(State state)
: state(state)
, promise(nullptr)
{ }
State state;
std::exception_ptr exc;
PromiseBase* promise;
std::vector<std::shared_ptr<Request>> requests;
void attach(PromiseBase* p) {
if (promise)
throw Error("Trying to double-attach a Promise");
promise = p;
}
void detach() {
if (!promise)
throw Error("Trying to detach a non attached Promise");
promise = nullptr;
}
virtual void* memory() = 0;
};
......@@ -113,9 +97,6 @@ namespace Async {
bool operator()(Arg&& arg) {
typedef typename std::remove_reference<Arg>::type Type;
auto promise = core_->promise;
if (!promise) return false;
if (core_->state != State::Pending)
throw Error("Attempt to resolve a fulfilled promise");
......@@ -142,10 +123,6 @@ namespace Async {
template<typename Exc>
bool operator()(Exc exc) {
auto promise = core_->promise;
if (!promise)
return false;
if (core_->state != State::Pending)
throw Error("Attempt to reject a fulfilled promise");
......@@ -196,7 +173,6 @@ namespace Async {
, resolver_(core_)
, rejection_(core_)
{
core_->attach(this);
func(resolver_, rejection_);
}
......@@ -208,7 +184,6 @@ namespace Async {
~Promise()
{
core_->detach();
}
bool isPending() const { return core_->state == State::Pending; }
......@@ -273,7 +248,6 @@ namespace Async {
, resolver_(core)
, rejection_(core)
{
core_->attach(this);
}
......
......@@ -369,8 +369,7 @@ Response::send(Code code) {
Async::Promise<ssize_t>
Response::send(Code code, const std::string& body, const Mime::MediaType& mime)
{
char buffer[Const::MaxBuffer];
Io::OutArrayBuf obuf(buffer, Io::Init::ZeroOut);
Io::OutArrayBuf obuf(buffer_, Io::Init::ZeroOut);
std::ostream stream(&obuf);
stream << "HTTP/1.1 ";
......@@ -403,7 +402,7 @@ Response::send(Code code, const std::string& body, const Mime::MediaType& mime)
stream << crlf;
}
return peer()->send(buffer, obuf.len());
return peer()->send(buffer_, obuf.len());
}
void
......
......@@ -107,6 +107,7 @@ private:
void associatePeer(const std::shared_ptr<Tcp::Peer>& peer);
std::weak_ptr<Tcp::Peer> peer_;
char buffer_[Const::MaxBuffer];
};
namespace Private {
......
......@@ -30,9 +30,11 @@ TEST(async_test, basic_test) {
p1.then([&](int v) { val = v; }, Async::NoExcept);
ASSERT_EQ(val, 10);
{
Async::Promise<int> p2 = doAsync(10);
p2.then([](int result) { ASSERT_EQ(result, 20); },
Async::NoExcept);
}
std::this_thread::sleep_for(std::chrono::seconds(2));
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment