Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11_abseil
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11_abseil
Commits
3132b38e
Commit
3132b38e
authored
Dec 22, 2020
by
Jean-Baptiste Lespiau
Committed by
Copybara-Service
Dec 22, 2020
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add absl::variant type_caster.
PiperOrigin-RevId: 348646910
parent
22974484
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
65 additions
and
0 deletions
+65
-0
pybind11_abseil/absl_casters.h
+10
-0
pybind11_abseil/tests/absl_example.cc
+34
-0
pybind11_abseil/tests/absl_test.py
+21
-0
No files found.
pybind11_abseil/absl_casters.h
View file @
3132b38e
...
@@ -38,6 +38,7 @@
...
@@ -38,6 +38,7 @@
#include <stdexcept>
#include <stdexcept>
#include <type_traits>
#include <type_traits>
#include <typeinfo>
#include <typeinfo>
#include <vector>
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "absl/container/flat_hash_set.h"
...
@@ -337,6 +338,15 @@ template <>
...
@@ -337,6 +338,15 @@ template <>
struct
type_caster
<
absl
::
nullopt_t
>
:
public
void_caster
<
absl
::
nullopt_t
>
{};
struct
type_caster
<
absl
::
nullopt_t
>
:
public
void_caster
<
absl
::
nullopt_t
>
{};
#endif
#endif
// This is a simple port of the pybind11 std::variant type_caster, applied to
// absl::variant. See pybind11 stl.h.
#ifndef ABSL_HAVE_STD_VARIANT
template
<
typename
...
Ts
>
struct
type_caster
<
absl
::
variant
<
Ts
...
>>
:
variant_caster
<
absl
::
variant
<
Ts
...
>>
{};
#endif
}
// namespace detail
}
// namespace detail
}
// namespace pybind11
}
// namespace pybind11
...
...
pybind11_abseil/tests/absl_example.cc
View file @
3132b38e
...
@@ -196,6 +196,33 @@ void DefineNonConstSpan(module* py_m, absl::string_view type_name) {
...
@@ -196,6 +196,33 @@ void DefineNonConstSpan(module* py_m, absl::string_view type_name) {
&
FillNonConstSpan
<
T
>
,
arg
(
"value"
),
arg
(
"output_span"
).
noconvert
());
&
FillNonConstSpan
<
T
>
,
arg
(
"value"
),
arg
(
"output_span"
).
noconvert
());
}
}
// absl::variant
struct
A
{
int
a
;
};
struct
B
{
int
b
;
};
typedef
absl
::
variant
<
A
,
B
>
AOrB
;
int
VariantToInt
(
AOrB
value
)
{
if
(
absl
::
holds_alternative
<
A
>
(
value
))
{
return
absl
::
get
<
A
>
(
value
).
a
;
}
else
if
(
absl
::
holds_alternative
<
B
>
(
value
))
{
return
absl
::
get
<
B
>
(
value
).
b
;
}
else
{
throw
std
::
exception
();
}
}
std
::
vector
<
AOrB
>
IdentityWithCopy
(
const
std
::
vector
<
AOrB
>&
value
)
{
return
value
;
}
std
::
vector
<
absl
::
variant
<
A
*
,
B
*>>
Identity
(
const
std
::
vector
<
absl
::
variant
<
A
*
,
B
*>>&
value
)
{
return
value
;
}
PYBIND11_MODULE
(
absl_example
,
m
)
{
PYBIND11_MODULE
(
absl_example
,
m
)
{
// absl::Time/Duration bindings.
// absl::Time/Duration bindings.
m
.
def
(
"make_duration"
,
&
MakeDuration
,
arg
(
"secs"
));
m
.
def
(
"make_duration"
,
&
MakeDuration
,
arg
(
"secs"
));
...
@@ -257,6 +284,13 @@ PYBIND11_MODULE(absl_example, m) {
...
@@ -257,6 +284,13 @@ PYBIND11_MODULE(absl_example, m) {
// absl::flat_hash_set bindings
// absl::flat_hash_set bindings
m
.
def
(
"make_set"
,
&
MakeSet
,
arg
(
"values"
));
m
.
def
(
"make_set"
,
&
MakeSet
,
arg
(
"values"
));
m
.
def
(
"check_set"
,
&
CheckSet
,
arg
(
"set"
),
arg
(
"values"
));
m
.
def
(
"check_set"
,
&
CheckSet
,
arg
(
"set"
),
arg
(
"values"
));
// absl::variant
class_
<
A
>
(
m
,
"A"
).
def
(
init
<
int
>
()).
def_readonly
(
"a"
,
&
A
::
a
);
class_
<
B
>
(
m
,
"B"
).
def
(
init
<
int
>
()).
def_readonly
(
"b"
,
&
B
::
b
);
m
.
def
(
"VariantToInt"
,
&
VariantToInt
);
m
.
def
(
"Identity"
,
&
Identity
);
m
.
def
(
"IdentityWithCopy"
,
&
IdentityWithCopy
);
}
}
}
// namespace test
}
// namespace test
...
...
pybind11_abseil/tests/absl_test.py
View file @
3132b38e
...
@@ -365,5 +365,26 @@ class AbslOptionalTest(absltest.TestCase):
...
@@ -365,5 +365,26 @@ class AbslOptionalTest(absltest.TestCase):
self
.
assertIsNone
(
absl_example
.
make_optional
())
self
.
assertIsNone
(
absl_example
.
make_optional
())
class
AbslVariantTest
(
absltest
.
TestCase
):
def
test_variant
(
self
):
assert
absl_example
.
VariantToInt
(
absl_example
.
A
(
3
))
==
3
assert
absl_example
.
VariantToInt
(
absl_example
.
B
(
5
))
==
5
for
identity_f
,
should_be_equal
in
[(
absl_example
.
Identity
,
True
),
(
absl_example
.
IdentityWithCopy
,
False
)]:
objs
=
[
absl_example
.
A
(
3
),
absl_example
.
B
(
5
)]
vector
=
identity_f
(
objs
)
self
.
assertLen
(
vector
,
2
)
self
.
assertIsInstance
(
vector
[
0
],
absl_example
.
A
)
self
.
assertEqual
(
vector
[
0
]
.
a
,
3
)
self
.
assertIsInstance
(
vector
[
1
],
absl_example
.
B
)
self
.
assertEqual
(
vector
[
1
]
.
b
,
5
)
if
should_be_equal
:
self
.
assertEqual
(
objs
,
vector
)
else
:
self
.
assertNotEqual
(
objs
,
vector
)
if
__name__
==
'__main__'
:
if
__name__
==
'__main__'
:
absltest
.
main
()
absltest
.
main
()
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