Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
gaoqiong
MIGraphX
Commits
13a9d276
Commit
13a9d276
authored
May 05, 2018
by
Paul
Browse files
Add support for any_cast
parent
5f8d091d
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
86 additions
and
1 deletion
+86
-1
include/rtg/operation.hpp
include/rtg/operation.hpp
+61
-0
test/operation.cpp
test/operation.cpp
+18
-1
test/test.hpp
test/test.hpp
+7
-0
No files found.
include/rtg/operation.hpp
View file @
13a9d276
...
@@ -49,6 +49,29 @@ struct operation
...
@@ -49,6 +49,29 @@ struct operation
return
*
this
;
return
*
this
;
}
}
// Cast
template
<
typename
PrivateDetailTypeErasedT
>
PrivateDetailTypeErasedT
*
any_cast
()
{
return
private_detail_te_get_handle
().
type
()
==
typeid
(
PrivateDetailTypeErasedT
)
?
std
::
addressof
(
static_cast
<
private_detail_te_handle_type
<
typename
std
::
remove_cv
<
PrivateDetailTypeErasedT
>::
type
>&>
(
private_detail_te_get_handle
())
.
private_detail_te_value
)
:
nullptr
;
}
template
<
typename
PrivateDetailTypeErasedT
>
const
typename
std
::
remove_cv
<
PrivateDetailTypeErasedT
>::
type
*
any_cast
()
const
{
return
private_detail_te_get_handle
().
type
()
==
typeid
(
PrivateDetailTypeErasedT
)
?
std
::
addressof
(
static_cast
<
const
private_detail_te_handle_type
<
typename
std
::
remove_cv
<
PrivateDetailTypeErasedT
>::
type
>&>
(
private_detail_te_get_handle
())
.
private_detail_te_value
)
:
nullptr
;
}
std
::
string
name
()
const
std
::
string
name
()
const
{
{
assert
(
private_detail_te_handle_mem_var
);
assert
(
private_detail_te_handle_mem_var
);
...
@@ -72,6 +95,7 @@ struct operation
...
@@ -72,6 +95,7 @@ struct operation
{
{
virtual
~
private_detail_te_handle_base_type
()
{}
virtual
~
private_detail_te_handle_base_type
()
{}
virtual
std
::
shared_ptr
<
private_detail_te_handle_base_type
>
clone
()
const
=
0
;
virtual
std
::
shared_ptr
<
private_detail_te_handle_base_type
>
clone
()
const
=
0
;
virtual
const
std
::
type_info
&
type
()
const
=
0
;
virtual
std
::
string
name
()
const
=
0
;
virtual
std
::
string
name
()
const
=
0
;
virtual
shape
compute_shape
(
std
::
vector
<
shape
>
input
)
const
=
0
;
virtual
shape
compute_shape
(
std
::
vector
<
shape
>
input
)
const
=
0
;
...
@@ -104,6 +128,11 @@ struct operation
...
@@ -104,6 +128,11 @@ struct operation
return
std
::
make_shared
<
private_detail_te_handle_type
>
(
private_detail_te_value
);
return
std
::
make_shared
<
private_detail_te_handle_type
>
(
private_detail_te_value
);
}
}
const
std
::
type_info
&
type
()
const
override
{
return
typeid
(
private_detail_te_value
);
}
std
::
string
name
()
const
override
{
return
private_detail_te_value
.
name
();
}
std
::
string
name
()
const
override
{
return
private_detail_te_value
.
name
();
}
shape
compute_shape
(
std
::
vector
<
shape
>
input
)
const
override
shape
compute_shape
(
std
::
vector
<
shape
>
input
)
const
override
...
@@ -144,6 +173,38 @@ struct operation
...
@@ -144,6 +173,38 @@ struct operation
std
::
shared_ptr
<
private_detail_te_handle_base_type
>
private_detail_te_handle_mem_var
;
std
::
shared_ptr
<
private_detail_te_handle_base_type
>
private_detail_te_handle_mem_var
;
};
};
template
<
typename
ValueType
>
inline
const
ValueType
*
any_cast
(
const
operation
*
x
)
{
return
x
->
any_cast
<
ValueType
>
();
}
template
<
typename
ValueType
>
inline
ValueType
*
any_cast
(
operation
*
x
)
{
return
x
->
any_cast
<
ValueType
>
();
}
template
<
typename
ValueType
>
inline
ValueType
&
any_cast
(
operation
&
x
)
{
using
type
=
typename
std
::
remove_reference
<
ValueType
>::
type
;
auto
*
y
=
x
.
any_cast
<
type
>
();
if
(
y
==
nullptr
)
throw
std
::
bad_cast
();
return
*
y
;
}
template
<
typename
ValueType
>
inline
const
ValueType
&
any_cast
(
const
operation
&
x
)
{
using
type
=
typename
std
::
remove_reference
<
ValueType
>::
type
;
const
auto
*
y
=
x
.
any_cast
<
type
>
();
if
(
y
==
nullptr
)
throw
std
::
bad_cast
();
return
*
y
;
}
}
// namespace rtg
}
// namespace rtg
#endif
#endif
test/operation.cpp
View file @
13a9d276
...
@@ -6,6 +6,7 @@
...
@@ -6,6 +6,7 @@
struct
simple_operation
struct
simple_operation
{
{
int
data
=
1
;
std
::
string
name
()
const
{
return
"simple"
;
}
std
::
string
name
()
const
{
return
"simple"
;
}
rtg
::
shape
compute_shape
(
std
::
vector
<
rtg
::
shape
>
)
const
{
RTG_THROW
(
"not computable"
);
}
rtg
::
shape
compute_shape
(
std
::
vector
<
rtg
::
shape
>
)
const
{
RTG_THROW
(
"not computable"
);
}
rtg
::
argument
compute
(
std
::
vector
<
rtg
::
argument
>
)
const
{
RTG_THROW
(
"not computable"
);
}
rtg
::
argument
compute
(
std
::
vector
<
rtg
::
argument
>
)
const
{
RTG_THROW
(
"not computable"
);
}
...
@@ -20,4 +21,20 @@ void operation_copy_test()
...
@@ -20,4 +21,20 @@ void operation_copy_test()
EXPECT
(
op2
.
name
()
==
op1
.
name
());
EXPECT
(
op2
.
name
()
==
op1
.
name
());
}
}
int
main
()
{
operation_copy_test
();
}
struct
not_operation
{};
void
operation_any_cast
()
{
rtg
::
operation
op1
=
simple_operation
{};
EXPECT
(
rtg
::
any_cast
<
simple_operation
>
(
op1
).
data
==
1
);
EXPECT
(
rtg
::
any_cast
<
not_operation
*>
(
&
op1
)
==
nullptr
);
EXPECT
(
test
::
throws
([
&
]{
rtg
::
any_cast
<
not_operation
&>
(
op1
);
}));
rtg
::
operation
op2
=
simple_operation
{
2
};
EXPECT
(
rtg
::
any_cast
<
simple_operation
>
(
op2
).
data
==
2
);
EXPECT
(
rtg
::
any_cast
<
not_operation
*>
(
&
op2
)
==
nullptr
);
}
int
main
()
{
operation_copy_test
();
operation_any_cast
();
}
test/test.hpp
View file @
13a9d276
...
@@ -27,6 +27,13 @@ namespace test {
...
@@ -27,6 +27,13 @@ namespace test {
TEST_FOREACH_OPERATOR
(
TEST_EACH_OPERATOR_OBJECT
)
TEST_FOREACH_OPERATOR
(
TEST_EACH_OPERATOR_OBJECT
)
inline
std
::
ostream
&
operator
<<
(
std
::
ostream
&
s
,
std
::
nullptr_t
)
{
s
<<
"nullptr"
;
return
s
;
}
template
<
class
T
,
class
U
,
class
Operator
>
template
<
class
T
,
class
U
,
class
Operator
>
struct
expression
struct
expression
{
{
...
...
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