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
4b72479b
Commit
4b72479b
authored
Nov 30, 2015
by
Mathieu Stefani
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Async: Added a TypeId helper to throw an exception when types are incompatible
parent
06b76563
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
97 additions
and
3 deletions
+97
-3
src/async.h
src/async.h
+11
-3
src/typeid.h
src/typeid.h
+60
-0
tests/CMakeLists.txt
tests/CMakeLists.txt
+4
-0
tests/async_test.cc
tests/async_test.cc
+7
-0
tests/typeid_test.cc
tests/typeid_test.cc
+15
-0
No files found.
src/async.h
View file @
4b72479b
...
...
@@ -12,6 +12,7 @@
#include <memory>
#include <atomic>
#include "optional.h"
#include "typeid.h"
namespace
Async
{
...
...
@@ -119,13 +120,15 @@ namespace Async {
};
struct
Core
{
Core
(
State
state
)
Core
(
State
state
,
TypeId
id
)
:
state
(
state
)
,
id
(
id
)
{
}
State
state
;
std
::
exception_ptr
exc
;
std
::
vector
<
std
::
shared_ptr
<
Request
>>
requests
;
TypeId
id
;
virtual
void
*
memory
()
=
0
;
...
...
@@ -136,16 +139,21 @@ namespace Async {
if
(
isVoid
())
throw
Error
(
"Can not construct a void core"
);
if
(
id
!=
TypeId
::
of
<
T
>
())
{
throw
Error
(
"Bad value type"
);
}
void
*
mem
=
memory
();
new
(
mem
)
T
(
std
::
forward
<
Args
>
(
args
)...);
state
=
State
::
Fulfilled
;
}
};
template
<
typename
T
>
struct
CoreT
:
public
Core
{
CoreT
()
:
Core
(
State
::
Pending
)
:
Core
(
State
::
Pending
,
TypeId
::
of
<
T
>
()
)
{
}
template
<
class
Other
>
...
...
@@ -173,7 +181,7 @@ namespace Async {
template
<
>
struct
CoreT
<
void
>
:
public
Core
{
CoreT
()
:
Core
(
State
::
Pending
)
:
Core
(
State
::
Pending
,
TypeId
::
of
<
void
>
()
)
{
}
bool
isVoid
()
const
{
return
true
;
}
...
...
src/typeid.h
0 → 100644
View file @
4b72479b
/* typeid.h
Mathieu Stefani, 30 novembre 2015
Copyright (c) 2015 Datacratic. All rights reserved.
This header provides a TypeId type that holds an unique identifier
for a given type. Basically equivalent to std::type_info except that
it does not rely on RTTI. The identifier is determined at compile-time.
Inspired by Rust's std::TypeId
*/
#pragma once
class
TypeId
{
public:
template
<
typename
T
>
static
TypeId
of
()
{
static
char
const
id_
{};
return
TypeId
(
&
id_
);
}
operator
size_t
()
const
{
return
reinterpret_cast
<
size_t
>
(
id_
);
}
private:
typedef
void
const
*
Id
;
TypeId
(
Id
id
)
:
id_
(
id
)
{
}
Id
id_
;
};
#define APPLY_OP(lhs, rhs, op) \
static_cast<size_t>(lhs) op static_cast<size_t>(rhs);
inline
bool
operator
==
(
const
TypeId
&
lhs
,
const
TypeId
&
rhs
)
{
return
APPLY_OP
(
lhs
,
rhs
,
==
);
}
inline
bool
operator
!=
(
const
TypeId
&
lhs
,
const
TypeId
&
rhs
)
{
return
APPLY_OP
(
lhs
,
rhs
,
!=
);
}
inline
bool
operator
<
(
const
TypeId
&
lhs
,
const
TypeId
&
rhs
)
{
return
APPLY_OP
(
lhs
,
rhs
,
<
);
}
#undef APPLY_OP
namespace
std
{
template
<
>
struct
hash
<
TypeId
>
{
size_t
operator
()(
const
TypeId
&
id
)
{
return
static_cast
<
size_t
>
(
id
);
}
};
}
tests/CMakeLists.txt
View file @
4b72479b
...
...
@@ -9,3 +9,7 @@ add_test( headers_test run_headers_test )
add_executable
(
run_async_test async_test.cc
)
target_link_libraries
(
run_async_test gtest gtest_main net
)
add_test
(
async_test run_async_test
)
add_executable
(
run_typeid_test typeid_test.cc
)
target_link_libraries
(
run_typeid_test gtest gtest_main
)
add_test
(
typeid_test run_typeid_test
)
tests/async_test.cc
View file @
4b72479b
...
...
@@ -58,6 +58,13 @@ TEST(async_test, basic_test) {
ASSERT_TRUE
(
p6
.
isRejected
());
}
TEST
(
async_test
,
error_test
)
{
Async
::
Promise
<
int
>
p1
(
[](
Async
::
Resolver
&
resolve
,
Async
::
Rejection
&
reject
)
{
ASSERT_THROW
(
resolve
(
10.5
),
Async
::
Error
);
});
}
TEST
(
async_test
,
void_promise
)
{
Async
::
Promise
<
void
>
p1
(
[](
Async
::
Resolver
&
resolve
,
Async
::
Rejection
&
reject
)
{
...
...
tests/typeid_test.cc
0 → 100644
View file @
4b72479b
#include "gtest/gtest.h"
#include "typeid.h"
TEST
(
type_id_test
,
basic_test
)
{
ASSERT_EQ
(
TypeId
::
of
<
int
>
(),
TypeId
::
of
<
int
>
());
ASSERT_NE
(
TypeId
::
of
<
int
>
(),
TypeId
::
of
<
int
*>
());
ASSERT_NE
(
TypeId
::
of
<
int
>
(),
TypeId
::
of
<
int
&>
());
ASSERT_NE
(
TypeId
::
of
<
int
&>
(),
TypeId
::
of
<
int
&&>
());
ASSERT_EQ
(
TypeId
::
of
<
int
&>
(),
TypeId
::
of
<
int
&>
());
ASSERT_NE
(
TypeId
::
of
<
int
&>
(),
TypeId
::
of
<
const
int
&>
());
ASSERT_NE
(
TypeId
::
of
<
int
*
const
>
(),
TypeId
::
of
<
int
const
*>
());
}
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