Merge pull request #2 from BlackMATov/dev

Dev
This commit is contained in:
BlackMat MATov
2018-12-10 08:27:02 +07:00
committed by GitHub
3 changed files with 426 additions and 63 deletions

View File

@@ -1,6 +1,10 @@
cmake_minimum_required(VERSION 3.5 FATAL_ERROR)
project(promise)
if(MSVC)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /bigobj")
endif(MSVC)
file(GLOB test_sources "*.cpp" "*.hpp")
add_executable(${PROJECT_NAME} ${test_sources})
set_target_properties(${PROJECT_NAME} PROPERTIES

View File

@@ -5,9 +5,11 @@
#include <new>
#include <mutex>
#include <atomic>
#include <memory>
#include <vector>
#include <utility>
#include <iterator>
#include <exception>
#include <stdexcept>
#include <functional>
@@ -185,6 +187,30 @@ namespace promise_hpp
return next;
}
template < typename ResolveF >
auto then_all(ResolveF&& on_resolve) {
return then([
f = std::forward<ResolveF>(on_resolve)
](const T& v) mutable {
auto r = invoke_hpp::invoke(
std::forward<decltype(f)>(f),
v);
return make_all_promise(std::move(r));
});
}
template < typename ResolveF >
auto then_any(ResolveF&& on_resolve) {
return then([
f = std::forward<ResolveF>(on_resolve)
](const T& v) mutable {
auto r = invoke_hpp::invoke(
std::forward<decltype(f)>(f),
v);
return make_any_promise(std::move(r));
});
}
template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF,T> >
std::enable_if_t<
@@ -210,20 +236,17 @@ namespace promise_hpp
}
template < typename U >
promise& resolve(U&& value) {
state_->resolve(std::forward<U>(value));
return *this;
bool resolve(U&& value) {
return state_->resolve(std::forward<U>(value));
}
promise& reject(std::exception_ptr e) {
state_->reject(e);
return *this;
bool reject(std::exception_ptr e) {
return state_->reject(e);
}
template < typename E >
promise& reject(E&& e) {
state_->reject(std::make_exception_ptr(std::forward<E>(e)));
return *this;
bool reject(E&& e) {
return state_->reject(std::make_exception_ptr(std::forward<E>(e)));
}
private:
class state;
@@ -234,24 +257,26 @@ namespace promise_hpp
state() = default;
template < typename U >
void resolve(U&& value) {
bool resolve(U&& value) {
std::lock_guard<std::mutex> guard(mutex_);
if ( status_ != status::pending ) {
throw std::logic_error("do not try to resolve a resolved or rejected promise");
return false;
}
storage_.set(std::forward<U>(value));
status_ = status::resolved;
invoke_resolve_handlers_();
return true;
}
void reject(std::exception_ptr e) {
bool reject(std::exception_ptr e) {
std::lock_guard<std::mutex> guard(mutex_);
if ( status_ != status::pending ) {
throw std::logic_error("do not try to reject a resolved or rejected promise");
return false;
}
exception_ = e;
status_ = status::rejected;
invoke_reject_handlers_();
return true;
}
template < typename U, typename ResolveF, typename RejectF >
@@ -458,6 +483,28 @@ namespace promise_hpp
return next;
}
template < typename ResolveF >
auto then_all(ResolveF&& on_resolve) {
return then([
f = std::forward<ResolveF>(on_resolve)
]() mutable {
auto r = invoke_hpp::invoke(
std::forward<decltype(f)>(f));
return make_all_promise(std::move(r));
});
}
template < typename ResolveF >
auto then_any(ResolveF&& on_resolve) {
return then([
f = std::forward<ResolveF>(on_resolve)
]() mutable {
auto r = invoke_hpp::invoke(
std::forward<decltype(f)>(f));
return make_any_promise(std::move(r));
});
}
template < typename ResolveF
, typename ResolveFR = invoke_hpp::invoke_result_t<ResolveF> >
std::enable_if_t<
@@ -482,20 +529,17 @@ namespace promise_hpp
return next;
}
promise& resolve() {
state_->resolve();
return *this;
bool resolve() {
return state_->resolve();
}
promise& reject(std::exception_ptr e) {
state_->reject(e);
return *this;
bool reject(std::exception_ptr e) {
return state_->reject(e);
}
template < typename E >
promise& reject(E&& e) {
state_->reject(std::make_exception_ptr(std::forward<E>(e)));
return *this;
bool reject(E&& e) {
return state_->reject(std::make_exception_ptr(std::forward<E>(e)));
}
private:
class state;
@@ -505,23 +549,25 @@ namespace promise_hpp
public:
state() = default;
void resolve() {
bool resolve() {
std::lock_guard<std::mutex> guard(mutex_);
if ( status_ != status::pending ) {
throw std::logic_error("do not try to resolve a resolved or rejected promise");
return false;
}
status_ = status::resolved;
invoke_resolve_handlers_();
return true;
}
void reject(std::exception_ptr e) {
bool reject(std::exception_ptr e) {
std::lock_guard<std::mutex> guard(mutex_);
if ( status_ != status::pending ) {
throw std::logic_error("do not try to reject a resolved or rejected promise");
return false;
}
exception_ = e;
status_ = status::rejected;
invoke_reject_handlers_();
return true;
}
template < typename U, typename ResolveF, typename RejectF >
@@ -656,6 +702,11 @@ namespace promise_hpp
// make_promise
//
template < typename R >
promise<R> make_promise() {
return promise<R>();
}
template < typename R, typename F >
promise<R> make_promise(F&& f) {
promise<R> result;
@@ -663,13 +714,13 @@ namespace promise_hpp
auto resolver = [
p = result
](auto&& v) mutable {
p.resolve(std::forward<decltype(v)>(v));
return p.resolve(std::forward<decltype(v)>(v));
};
auto rejector = [
p = result
](auto&& e) mutable {
p.reject(std::forward<decltype(e)>(e));
return p.reject(std::forward<decltype(e)>(e));
};
try {
@@ -684,6 +735,10 @@ namespace promise_hpp
return result;
}
//
// make_resolved_promise
//
inline promise<void> make_resolved_promise() {
promise<void> result;
result.resolve();
@@ -697,6 +752,10 @@ namespace promise_hpp
return result;
}
//
// make_rejected_promise
//
template < typename E >
promise<void> make_rejected_promise(E&& e) {
promise<void> result;
@@ -710,4 +769,88 @@ namespace promise_hpp
result.reject(std::forward<E>(e));
return result;
}
//
// make_all_promise
//
template < typename Iter >
auto make_all_promise(Iter begin, Iter end) {
using child_promise_t = typename Iter::value_type;
using child_promise_value_t = typename child_promise_t::value_type;
using promise_out_container_t = std::vector<child_promise_value_t>;
struct context_t {
promise_out_container_t results;
std::atomic_size_t counter = ATOMIC_VAR_INIT(0);
context_t(std::size_t count)
: results(count) {}
bool apply_result(std::size_t index, const child_promise_value_t& value) {
results[index] = value;
return ++counter == results.size();
}
};
if ( begin == end ) {
return make_resolved_promise(promise_out_container_t());
}
return make_promise<promise_out_container_t>([begin, end](auto&& resolver, auto&& rejector){
std::size_t result_index = 0;
auto context = std::make_shared<context_t>(std::distance(begin, end));
for ( auto iter = begin; iter != end; ++iter, ++result_index ) {
(*iter).then([
context,
resolver,
result_index
](const child_promise_value_t& v) mutable {
if ( context->apply_result(result_index, v) ) {
resolver(std::move(context->results));
}
}).fail([rejector](std::exception_ptr e) mutable {
rejector(e);
});
}
});
}
template < typename Container >
auto make_all_promise(Container&& container) {
return make_all_promise(
std::begin(container),
std::end(container));
}
//
// make_any_promise
//
template < typename Iter >
auto make_any_promise(Iter begin, Iter end) {
using child_promise_t = typename Iter::value_type;
using child_promise_value_t = typename child_promise_t::value_type;
if ( begin == end ) {
throw std::logic_error("at least one input promise must be provided for make_any_promise");
}
return make_promise<child_promise_value_t>([begin, end](auto&& resolver, auto&& rejector){
for ( auto iter = begin; iter != end; ++iter ) {
(*iter).then([resolver](const child_promise_value_t& v) mutable {
resolver(v);
}).fail([rejector](std::exception_ptr e) mutable {
rejector(e);
});
}
});
}
template < typename Container >
auto make_any_promise(Container&& container) {
return make_any_promise(
std::begin(container),
std::end(container));
}
}

286
tests.cpp
View File

@@ -74,7 +74,7 @@ TEST_CASE("is_promise_r") {
pr::is_promise_r<int, const pr::promise<int>>::value,
"unit test fail");
static_assert(
pr::is_promise_r<long, const pr::promise<int>>::value,
pr::is_promise_r<double, const pr::promise<int>>::value,
"unit test fail");
}
SECTION("negative") {
@@ -88,7 +88,7 @@ TEST_CASE("is_promise_r") {
!pr::is_promise_r<int, pr::promise<obj_t>>::value,
"unit test fail");
static_assert(
!pr::is_promise_r<long, int>::value,
!pr::is_promise_r<double, int>::value,
"unit test fail");
}
}
@@ -97,18 +97,18 @@ TEST_CASE("promise") {
SECTION("resolved") {
{
int check_42_int = 0;
pr::promise<int>()
.resolve(42)
.then([&check_42_int](int value){
auto p = pr::promise<int>();
p.resolve(42);
p.then([&check_42_int](int value){
check_42_int = value;
});
REQUIRE(check_42_int == 42);
}
{
int check_42_int = 0;
pr::promise<int>()
.resolve(42)
.fail([](std::exception_ptr){
auto p = pr::promise<int>();
p.resolve(42);
p.fail([](std::exception_ptr){
}).then([&check_42_int](int value){
check_42_int = value;
});
@@ -118,9 +118,9 @@ TEST_CASE("promise") {
int check_84_int = 0;
bool check_void_call = false;
int check_100500_transform = 0;
pr::promise<int>()
.resolve(42)
.then([](int value){
auto p = pr::promise<int>();
p.resolve(42);
p.then([](int value){
return value * 2;
}).then([&check_84_int](int value){
check_84_int = value;
@@ -140,9 +140,9 @@ TEST_CASE("promise") {
{
bool call_fail_with_logic_error = false;
bool not_call_then_on_reject = true;
pr::promise<int>()
.reject(std::logic_error("hello fail"))
.then([&not_call_then_on_reject](int value) {
auto p = pr::promise<int>();
p.reject(std::logic_error("hello fail"));
p.then([&not_call_then_on_reject](int value) {
(void)value;
not_call_then_on_reject = false;
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
@@ -154,9 +154,9 @@ TEST_CASE("promise") {
{
std::logic_error ee("hello fail");
bool call_fail_with_logic_error = false;
pr::promise<int>()
.reject(ee)
.then([](int){
auto p = pr::promise<int>();
p.reject(ee);
p.then([](int){
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
@@ -165,9 +165,9 @@ TEST_CASE("promise") {
{
std::logic_error ee("hello fail");
bool call_fail_with_logic_error = false;
pr::promise<int>()
.reject(std::make_exception_ptr(ee))
.then([](int){
auto p = pr::promise<int>();
p.reject(std::make_exception_ptr(ee));
p.then([](int){
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
@@ -175,9 +175,9 @@ TEST_CASE("promise") {
}
{
int check_multi_fail = 0;
pr::promise<>()
.reject(std::logic_error("hello fail"))
.fail([&check_multi_fail](std::exception_ptr){
auto p = pr::promise<>();
p.reject(std::logic_error("hello fail"));
p.fail([&check_multi_fail](std::exception_ptr){
++check_multi_fail;
}).fail([&check_multi_fail](std::exception_ptr){
++check_multi_fail;
@@ -285,7 +285,7 @@ TEST_CASE("promise") {
REQUIRE(call_fail_with_logic_error);
}
{
bool call_fail_with_logic_error = 0;
bool call_fail_with_logic_error = false;
pr::make_rejected_promise(std::logic_error("hello fail"))
.fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
@@ -297,9 +297,9 @@ TEST_CASE("promise") {
{
bool not_call_then_on_reject = true;
bool call_fail_with_logic_error = false;
pr::promise<int>()
.resolve(42)
.then([](int){
auto p = pr::promise<int>();
p.resolve(42);
p.then([](int){
throw std::logic_error("hello fail");
}).then([&not_call_then_on_reject](){
not_call_then_on_reject = false;
@@ -379,6 +379,7 @@ TEST_CASE("promise") {
auto p2 = pr::make_resolved_promise(84);
p1.then([&p2](int v){
(void)v;
return p2;
}).then([&check_84_int](int v2){
check_84_int = v2;
@@ -405,6 +406,7 @@ TEST_CASE("promise") {
auto p2 = pr::make_resolved_promise();
p1.then([&p2](int v){
(void)v;
return p2;
}).then([&check_84_int](){
check_84_int = 84;
@@ -426,6 +428,26 @@ TEST_CASE("promise") {
REQUIRE(check_84_int == 84);
}
}
SECTION("lazy_chaining") {
{
int check_84_int = 0;
auto p1 = pr::make_promise<int>();
auto p2 = pr::make_promise<int>();
p1.then([&p2](int v){
(void)v;
return p2;
}).then([&check_84_int](int v2){
check_84_int = v2;
});
REQUIRE(check_84_int == 0);
p1.resolve(42);
REQUIRE(check_84_int == 0);
p2.resolve(84);
REQUIRE(check_84_int == 84);
}
}
SECTION("typed_chaining_fails") {
{
bool call_fail_with_logic_error = false;
@@ -436,7 +458,7 @@ TEST_CASE("promise") {
(void)v;
throw std::logic_error("hello fail");
return p2;
}).then([&call_fail_with_logic_error](int v2){
}).then([](int v2){
(void)v2;
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
@@ -452,7 +474,7 @@ TEST_CASE("promise") {
p1.then([&p2](int v){
(void)v;
return p2;
}).then([&call_fail_with_logic_error](int v2){
}).then([](int v2){
(void)v2;
throw std::logic_error("hello fail");
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
@@ -469,7 +491,7 @@ TEST_CASE("promise") {
p1.then([&p2](int v){
(void)v;
return p2;
}).then([&call_fail_with_logic_error](int v2){
}).then([](int v2){
(void)v2;
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
@@ -485,7 +507,7 @@ TEST_CASE("promise") {
p1.then([&p2](int v){
(void)v;
return p2;
}).then([&call_fail_with_logic_error](int v2){
}).then([](int v2){
(void)v2;
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
@@ -503,7 +525,7 @@ TEST_CASE("promise") {
p1.then([&p2](){
throw std::logic_error("hello fail");
return p2;
}).then([&call_fail_with_logic_error](){
}).then([](){
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
@@ -517,7 +539,7 @@ TEST_CASE("promise") {
p1.then([&p2](){
return p2;
}).then([&call_fail_with_logic_error](){
}).then([](){
throw std::logic_error("hello fail");
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
@@ -532,7 +554,7 @@ TEST_CASE("promise") {
p1.then([&p2](){
return p2;
}).then([&call_fail_with_logic_error](){
}).then([](){
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
@@ -546,7 +568,7 @@ TEST_CASE("promise") {
p1.then([&p2](){
return p2;
}).then([&call_fail_with_logic_error](){
}).then([](){
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
@@ -554,4 +576,198 @@ TEST_CASE("promise") {
REQUIRE(call_fail_with_logic_error);
}
}
SECTION("make_all_promise") {
{
bool all_is_ok = false;
pr::make_all_promise(std::vector<pr::promise<int>>())
.then([&all_is_ok](const std::vector<int>& c){
all_is_ok = c.empty();
});
REQUIRE(all_is_ok);
}
{
bool all_is_ok = false;
auto p = pr::make_all_promise(std::vector<pr::promise<int>>{
pr::make_resolved_promise(32),
pr::make_resolved_promise(10)
}).then([&all_is_ok](const std::vector<int>& c){
all_is_ok = (2 == c.size())
&& c[0] == 32
&& c[1] == 10;
});
REQUIRE(all_is_ok);
}
{
auto p1 = pr::promise<int>();
auto p2 = pr::promise<int>();
int call_then_only_once = 0;
pr::make_all_promise(std::vector<pr::promise<int>>{p1, p2})
.then([&call_then_only_once](const std::vector<int>& c){
(void)c;
++call_then_only_once;
});
p1.resolve(1);
p2.resolve(2);
REQUIRE(call_then_only_once == 1);
}
}
SECTION("make_any_promise") {
{
auto p1 = pr::promise<int>();
auto p2 = pr::promise<int>();
int check_42_int = 0;
int call_then_only_once = 0;
pr::make_any_promise(std::vector<pr::promise<int>>{p1, p2})
.then([&check_42_int, &call_then_only_once](const int& v){
check_42_int = v;
++call_then_only_once;
});
p1.resolve(42);
REQUIRE(check_42_int == 42);
REQUIRE(call_then_only_once == 1);
p2.resolve(84);
REQUIRE(check_42_int == 42);
REQUIRE(call_then_only_once == 1);
}
{
auto p1 = pr::promise<int>();
auto p2 = pr::promise<int>();
int check_42_int = 0;
int call_then_only_once = 0;
pr::make_any_promise(std::vector<pr::promise<int>>{p1, p2})
.then([&check_42_int, &call_then_only_once](const int& v){
check_42_int = v;
++call_then_only_once;
});
p2.resolve(42);
REQUIRE(check_42_int == 42);
REQUIRE(call_then_only_once == 1);
p1.resolve(84);
REQUIRE(check_42_int == 42);
REQUIRE(call_then_only_once == 1);
}
}
SECTION("make_all_promise_fail") {
{
bool call_fail_with_logic_error = false;
bool not_call_then_on_reject = true;
auto p = pr::make_all_promise(std::vector<pr::promise<int>>{
pr::make_rejected_promise<int>(std::logic_error("hello fail")),
pr::make_resolved_promise(10)
}).then([&not_call_then_on_reject](const std::vector<int>& c){
(void)c;
not_call_then_on_reject = false;
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(not_call_then_on_reject);
REQUIRE(call_fail_with_logic_error);
}
}
SECTION("make_any_promise_fail") {
REQUIRE_THROWS_AS(
pr::make_any_promise(std::vector<pr::promise<int>>{}),
std::logic_error);
{
bool call_fail_with_logic_error = false;
bool not_call_then_on_reject = true;
auto p = pr::make_any_promise(std::vector<pr::promise<int>>{
pr::make_rejected_promise<int>(std::logic_error("hello fail")),
pr::make_resolved_promise(10)
}).then([&not_call_then_on_reject](const int& c){
(void)c;
not_call_then_on_reject = false;
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(not_call_then_on_reject);
REQUIRE(call_fail_with_logic_error);
}
}
SECTION("then_all") {
{
int check_42_int = 0;
pr::make_resolved_promise()
.then_all([](){
return std::vector<pr::promise<int>>{
pr::make_resolved_promise(32),
pr::make_resolved_promise(10)};
}).then([&check_42_int](const std::vector<int>& v){
if ( v.size() == 2) {
check_42_int = v[0] + v[1];
}
});
REQUIRE(check_42_int == 42);
}
{
int check_42_int = 0;
int check_42_int2 = 0;
pr::make_resolved_promise(42)
.then_all([&check_42_int](int v){
check_42_int = v;
return std::vector<pr::promise<int>>{
pr::make_resolved_promise(32),
pr::make_resolved_promise(10)};
}).then([&check_42_int2](const std::vector<int>& v){
if ( v.size() == 2) {
check_42_int2 = v[0] + v[1];
}
});
REQUIRE(check_42_int == 42);
REQUIRE(check_42_int2 == 42);
}
}
SECTION("then_any") {
{
int check_42_int = 0;
pr::make_resolved_promise()
.then_any([](){
return std::vector<pr::promise<int>>{
pr::make_resolved_promise(42),
pr::make_rejected_promise<int>(std::logic_error("hello fail"))};
}).then([&check_42_int](const int& v){
check_42_int = v;
});
REQUIRE(check_42_int == 42);
}
{
bool call_fail_with_logic_error = false;
pr::make_resolved_promise()
.then_any([](){
return std::vector<pr::promise<int>>{
pr::make_rejected_promise<int>(std::logic_error("hello fail")),
pr::make_resolved_promise(42)};
}).fail([&call_fail_with_logic_error](std::exception_ptr e){
call_fail_with_logic_error = check_hello_fail_exception(e);
});
REQUIRE(call_fail_with_logic_error);
}
{
int check_42_int = 0;
int check_42_int2 = 0;
int call_then_only_once = 0;
pr::make_resolved_promise(42)
.then_any([&check_42_int](int v){
check_42_int = v;
return std::vector<pr::promise<int>>{
pr::make_resolved_promise(42),
pr::make_resolved_promise(10)};
}).then([&call_then_only_once, &check_42_int2](const int& v){
++call_then_only_once;
check_42_int2 = v;
});
REQUIRE(check_42_int == 42);
REQUIRE(check_42_int2 == 42);
REQUIRE(call_then_only_once == 1);
}
}
}