Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11
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
Commits
d3349af4
Commit
d3349af4
authored
Mar 26, 2016
by
Wenzel Jakob
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
modularized logic in preceding change, fixed issue with char (fixes #150)
parent
0772967e
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
29 additions
and
17 deletions
+29
-17
example/issues.cpp
+3
-0
example/issues.py
+2
-1
example/issues.ref
+1
-0
include/pybind11/cast.h
+23
-16
No files found.
example/issues.cpp
View file @
d3349af4
...
@@ -15,4 +15,7 @@ void init_issues(py::module &m) {
...
@@ -15,4 +15,7 @@ void init_issues(py::module &m) {
// #137: const char* isn't handled properly
// #137: const char* isn't handled properly
m2
.
def
(
"print_cchar"
,
[](
const
char
*
string
)
{
std
::
cout
<<
string
<<
std
::
endl
;
});
m2
.
def
(
"print_cchar"
,
[](
const
char
*
string
)
{
std
::
cout
<<
string
<<
std
::
endl
;
});
// #150: char bindings broken
m2
.
def
(
"print_char"
,
[](
char
c
)
{
std
::
cout
<<
c
<<
std
::
endl
;
});
}
}
example/issues.py
View file @
d3349af4
...
@@ -3,6 +3,7 @@ from __future__ import print_function
...
@@ -3,6 +3,7 @@ from __future__ import print_function
import
sys
import
sys
sys
.
path
.
append
(
'.'
)
sys
.
path
.
append
(
'.'
)
from
example.issues
import
print_cchar
from
example.issues
import
print_cchar
,
print_char
print_cchar
(
"const char *"
)
print_cchar
(
"const char *"
)
print_char
(
'c'
)
example/issues.ref
View file @
d3349af4
const char *
const char *
c
include/pybind11/cast.h
View file @
d3349af4
...
@@ -197,6 +197,12 @@ protected:
...
@@ -197,6 +197,12 @@ protected:
object
temp
;
object
temp
;
};
};
/* Determine suitable casting operator */
template
<
typename
T
>
using
cast_op_type
=
typename
std
::
conditional
<
std
::
is_pointer
<
T
>::
value
,
typename
std
::
add_pointer
<
typename
intrinsic_type
<
T
>::
type
>::
type
,
typename
std
::
add_lvalue_reference
<
typename
intrinsic_type
<
T
>::
type
>::
type
>::
type
;
/// Generic type caster for objects stored on the heap
/// Generic type caster for objects stored on the heap
template
<
typename
type
,
typename
Enable
=
void
>
class
type_caster
:
public
type_caster_generic
{
template
<
typename
type
,
typename
Enable
=
void
>
class
type_caster
:
public
type_caster_generic
{
public
:
public
:
...
@@ -214,6 +220,8 @@ public:
...
@@ -214,6 +220,8 @@ public:
return
type_caster_generic
::
cast
(
src
,
policy
,
parent
,
&
typeid
(
type
),
&
copy_constructor
);
return
type_caster_generic
::
cast
(
src
,
policy
,
parent
,
&
typeid
(
type
),
&
copy_constructor
);
}
}
template
<
typename
T
>
using
cast_op_type
=
pybind11
::
detail
::
cast_op_type
<
T
>
;
operator
type
*
()
{
return
(
type
*
)
value
;
}
operator
type
*
()
{
return
(
type
*
)
value
;
}
operator
type
&
()
{
return
*
((
type
*
)
value
);
}
operator
type
&
()
{
return
*
((
type
*
)
value
);
}
protected
:
protected
:
...
@@ -234,7 +242,8 @@ protected:
...
@@ -234,7 +242,8 @@ protected:
return cast(*src, policy, parent); \
return cast(*src, policy, parent); \
} \
} \
operator type*() { return &value; } \
operator type*() { return &value; } \
operator type&() { return value; }
operator type&() { return value; } \
template <typename _T> using cast_op_type = pybind11::detail::cast_op_type<_T>;
#define PYBIND11_DECLARE_HOLDER_TYPE(type, holder_type) \
#define PYBIND11_DECLARE_HOLDER_TYPE(type, holder_type) \
namespace pybind11 { namespace detail { \
namespace pybind11 { namespace detail { \
...
@@ -310,6 +319,7 @@ public:
...
@@ -310,6 +319,7 @@ public:
operator
T
*
()
{
return
&
value
;
}
operator
T
*
()
{
return
&
value
;
}
operator
T
&
()
{
return
value
;
}
operator
T
&
()
{
return
value
;
}
template
<
typename
T2
>
using
cast_op_type
=
pybind11
::
detail
::
cast_op_type
<
T2
>
;
protected
:
protected
:
T
value
;
T
value
;
};
};
...
@@ -348,7 +358,7 @@ public:
...
@@ -348,7 +358,7 @@ public:
operator
void
*
()
{
return
value
;
}
operator
void
*
()
{
return
value
;
}
private
:
private
:
void
*
value
;
void
*
value
=
nullptr
;
};
};
template
<>
class
type_caster
<
std
::
nullptr_t
>
:
public
type_caster
<
void_type
>
{
};
template
<>
class
type_caster
<
std
::
nullptr_t
>
:
public
type_caster
<
void_type
>
{
};
...
@@ -442,6 +452,9 @@ public:
...
@@ -442,6 +452,9 @@ public:
operator
char
*
()
{
return
(
char
*
)
value
.
c_str
();
}
operator
char
*
()
{
return
(
char
*
)
value
.
c_str
();
}
operator
char
()
{
if
(
value
.
length
()
>
0
)
return
value
[
0
];
else
return
'\0'
;
}
operator
char
()
{
if
(
value
.
length
()
>
0
)
return
value
[
0
];
else
return
'\0'
;
}
template
<
typename
T
>
using
cast_op_type
=
typename
std
::
conditional
<
std
::
is_pointer
<
T
>::
value
,
char
*
,
char
>::
type
;
static
PYBIND11_DESCR
name
()
{
return
type_descr
(
_
(
PYBIND11_STRING_NAME
));
}
static
PYBIND11_DESCR
name
()
{
return
type_descr
(
_
(
PYBIND11_STRING_NAME
));
}
};
};
...
@@ -489,6 +502,8 @@ public:
...
@@ -489,6 +502,8 @@ public:
_
(
", "
)
+
type_caster
<
typename
intrinsic_type
<
T2
>::
type
>::
name
()
+
_
(
")"
));
_
(
", "
)
+
type_caster
<
typename
intrinsic_type
<
T2
>::
type
>::
name
()
+
_
(
")"
));
}
}
template
<
typename
T
>
using
cast_op_type
=
type
;
operator
type
()
{
operator
type
()
{
return
type
(
first
,
second
);
return
type
(
first
,
second
);
}
}
...
@@ -526,29 +541,21 @@ public:
...
@@ -526,29 +541,21 @@ public:
return
void_type
();
return
void_type
();
}
}
template
<
typename
T
>
using
cast_op_type
=
type
;
operator
type
()
{
operator
type
()
{
return
cast
(
typename
make_index_sequence
<
sizeof
...(
Tuple
)
>::
type
());
return
cast
(
typename
make_index_sequence
<
sizeof
...(
Tuple
)
>::
type
());
}
}
protected
:
protected
:
template
<
typename
T
>
/* Used to select the right casting operator in the two functions below */
using
cast_target
=
typename
std
::
conditional
<
is_specialization_of
<
typename
intrinsic_type
<
T
>::
type
,
std
::
tuple
>::
value
||
is_specialization_of
<
typename
intrinsic_type
<
T
>::
type
,
std
::
pair
>::
value
,
typename
intrinsic_type
<
T
>::
type
,
/* special case: tuple/pair -> pass by value */
typename
std
::
conditional
<
std
::
is_pointer
<
T
>::
value
,
typename
std
::
add_pointer
<
typename
intrinsic_type
<
T
>::
type
>::
type
,
/* pass using pointer */
typename
std
::
add_lvalue_reference
<
typename
intrinsic_type
<
T
>::
type
>::
type
/* pass using reference */
>::
type
>
;
template
<
typename
ReturnValue
,
typename
Func
,
size_t
...
Index
>
ReturnValue
call
(
Func
&&
f
,
index_sequence
<
Index
...
>
)
{
template
<
typename
ReturnValue
,
typename
Func
,
size_t
...
Index
>
ReturnValue
call
(
Func
&&
f
,
index_sequence
<
Index
...
>
)
{
return
f
(
std
::
get
<
Index
>
(
value
).
operator
typename
cast_target
<
Tuple
>::
type
()...);
return
f
(
std
::
get
<
Index
>
(
value
)
.
operator
typename
type_caster
<
typename
intrinsic_type
<
Tuple
>::
type
>::
template
cast_op_type
<
Tuple
>
()...);
}
}
template
<
size_t
...
Index
>
type
cast
(
index_sequence
<
Index
...
>
)
{
template
<
size_t
...
Index
>
type
cast
(
index_sequence
<
Index
...
>
)
{
return
type
(
std
::
get
<
Index
>
(
value
).
operator
typename
cast_target
<
Tuple
>::
type
()...);
return
type
(
std
::
get
<
Index
>
(
value
)
.
operator
typename
type_caster
<
typename
intrinsic_type
<
Tuple
>::
type
>::
template
cast_op_type
<
Tuple
>
()...);
}
}
template
<
size_t
...
Indices
>
bool
load
(
handle
src
,
bool
convert
,
index_sequence
<
Indices
...
>
)
{
template
<
size_t
...
Indices
>
bool
load
(
handle
src
,
bool
convert
,
index_sequence
<
Indices
...
>
)
{
...
...
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