Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
P
pistache
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Libraries
pistache
Commits
aa845e40
Commit
aa845e40
authored
Nov 17, 2015
by
octal
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
async: added static functions to directly resolve or reject a promise
parent
0b87248e
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
49 additions
and
0 deletions
+49
-0
src/async.h
src/async.h
+41
-0
tests/async_test.cc
tests/async_test.cc
+8
-0
No files found.
src/async.h
View file @
aa845e40
...
@@ -458,6 +458,7 @@ namespace Async {
...
@@ -458,6 +458,7 @@ namespace Async {
typedef
std
::
function
<
void
(
Resolver
&
,
Rejection
&
)
>
ResolveFunc
;
typedef
std
::
function
<
void
(
Resolver
&
,
Rejection
&
)
>
ResolveFunc
;
typedef
Private
::
CoreT
<
T
>
Core
;
typedef
Private
::
CoreT
<
T
>
Core
;
Promise
(
ResolveFunc
func
)
Promise
(
ResolveFunc
func
)
:
core_
(
std
::
make_shared
<
Core
>
())
:
core_
(
std
::
make_shared
<
Core
>
())
,
resolver_
(
core_
)
,
resolver_
(
core_
)
...
@@ -476,6 +477,40 @@ namespace Async {
...
@@ -476,6 +477,40 @@ namespace Async {
{
{
}
}
template
<
typename
U
>
static
Promise
<
T
>
resolved
(
U
&&
value
)
{
static_assert
(
!
std
::
is_void
<
T
>::
value
,
"Can not resolve a void promise with parameters"
);
static_assert
(
std
::
is_same
<
T
,
U
>::
value
||
std
::
is_convertible
<
U
,
T
>::
value
,
"Incompatible value type"
);
auto
core
=
std
::
make_shared
<
Core
>
();
core
->
template
construct
<
T
>(
std
::
forward
<
U
>
(
value
));
return
Promise
<
T
>
(
std
::
move
(
core
));
}
static
Promise
<
void
>
resolved
()
{
static_assert
(
std
::
is_void
<
T
>::
value
,
"Resolving a non-void promise requires parameters"
);
auto
core
=
std
::
make_shared
<
Core
>
();
core
->
state
=
State
::
Fulfilled
;
return
Promise
<
T
>
(
std
::
move
(
core
));
}
template
<
typename
Exc
>
static
Promise
<
T
>
rejected
(
Exc
exc
)
{
auto
core
=
std
::
make_shared
<
Core
>
();
core
->
exc
=
std
::
make_exception_ptr
(
exc
);
core
->
state
=
State
::
Rejected
;
return
Promise
<
T
>
(
std
::
move
(
core
));
}
bool
isPending
()
const
{
return
core_
->
state
==
State
::
Pending
;
}
bool
isPending
()
const
{
return
core_
->
state
==
State
::
Pending
;
}
bool
isFulfilled
()
const
{
return
core_
->
state
==
State
::
Fulfilled
;
}
bool
isFulfilled
()
const
{
return
core_
->
state
==
State
::
Fulfilled
;
}
bool
isRejected
()
const
{
return
core_
->
state
==
State
::
Rejected
;
}
bool
isRejected
()
const
{
return
core_
->
state
==
State
::
Rejected
;
}
...
@@ -528,6 +563,12 @@ namespace Async {
...
@@ -528,6 +563,12 @@ namespace Async {
{
{
}
}
Promise
(
std
::
shared_ptr
<
Core
>&&
core
)
:
core_
(
core
)
,
resolver_
(
core_
)
,
rejection_
(
core_
)
{
}
template
<
typename
ResolveFunc
>
template
<
typename
ResolveFunc
>
void
thenStaticChecks
(
std
::
true_type
/* is_void */
)
{
void
thenStaticChecks
(
std
::
true_type
/* is_void */
)
{
static_assert
(
detail
::
FunctionTrait
<
ResolveFunc
>::
ArgsCount
==
0
,
static_assert
(
detail
::
FunctionTrait
<
ResolveFunc
>::
ArgsCount
==
0
,
...
...
tests/async_test.cc
View file @
aa845e40
...
@@ -49,6 +49,14 @@ TEST(async_test, basic_test) {
...
@@ -49,6 +49,14 @@ TEST(async_test, basic_test) {
ASSERT_THROW
(
std
::
rethrow_exception
(
eptr
),
std
::
runtime_error
);
ASSERT_THROW
(
std
::
rethrow_exception
(
eptr
),
std
::
runtime_error
);
});
});
auto
p4
=
Async
::
Promise
<
int
>::
resolved
(
10
);
ASSERT_TRUE
(
p4
.
isFulfilled
());
auto
p5
=
Async
::
Promise
<
void
>::
resolved
();
ASSERT_TRUE
(
p5
.
isFulfilled
());
auto
p6
=
Async
::
Promise
<
int
>::
rejected
(
std
::
invalid_argument
(
"Invalid"
));
ASSERT_TRUE
(
p6
.
isRejected
());
}
}
TEST
(
async_test
,
void_promise
)
{
TEST
(
async_test
,
void_promise
)
{
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment