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
pybind11
Commits
42ad3284
Commit
42ad3284
authored
Jun 19, 2016
by
Ivan Smirnov
Browse files
Change format_descriptor::value to a static func
parent
a7e62e1c
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
35 additions
and
27 deletions
+35
-27
docs/advanced.rst
docs/advanced.rst
+8
-8
example/example-buffers.cpp
example/example-buffers.cpp
+7
-7
include/pybind11/common.h
include/pybind11/common.h
+13
-5
include/pybind11/eigen.h
include/pybind11/eigen.h
+6
-6
include/pybind11/numpy.h
include/pybind11/numpy.h
+1
-1
No files found.
docs/advanced.rst
View file @
42ad3284
...
@@ -1224,12 +1224,12 @@ completely avoid copy operations with Python expressions like
...
@@ -1224,12 +1224,12 @@ completely avoid copy operations with Python expressions like
py::class_<Matrix>(m, "Matrix")
py::class_<Matrix>(m, "Matrix")
.def_buffer([](Matrix &m) -> py::buffer_info {
.def_buffer([](Matrix &m) -> py::buffer_info {
return py::buffer_info(
return py::buffer_info(
m.data(), /* Pointer to buffer */
m.data(),
/* Pointer to buffer */
sizeof(float), /* Size of one scalar */
sizeof(float),
/* Size of one scalar */
py::format_descriptor<float>::value, /* Python struct-style format descriptor */
py::format_descriptor<float>::value
()
, /* Python struct-style format descriptor */
2, /* Number of dimensions */
2,
/* Number of dimensions */
{ m.rows(), m.cols() }, /* Buffer dimensions */
{ m.rows(), m.cols() },
/* Buffer dimensions */
{ sizeof(float) * m.rows(), /* Strides (in bytes) for each index */
{ sizeof(float) * m.rows(),
/* Strides (in bytes) for each index */
sizeof(float) }
sizeof(float) }
);
);
});
});
...
@@ -1273,7 +1273,7 @@ buffer objects (e.g. a NumPy matrix).
...
@@ -1273,7 +1273,7 @@ buffer objects (e.g. a NumPy matrix).
py::buffer_info info = b.request();
py::buffer_info info = b.request();
/* Some sanity checks ... */
/* Some sanity checks ... */
if (info.format != py::format_descriptor<Scalar>::value)
if (info.format != py::format_descriptor<Scalar>::value
()
)
throw std::runtime_error("Incompatible format: expected a double array!");
throw std::runtime_error("Incompatible format: expected a double array!");
if (info.ndim != 2)
if (info.ndim != 2)
...
@@ -1299,7 +1299,7 @@ as follows:
...
@@ -1299,7 +1299,7 @@ as follows:
m.data(), /* Pointer to buffer */
m.data(), /* Pointer to buffer */
sizeof(Scalar), /* Size of one scalar */
sizeof(Scalar), /* Size of one scalar */
/* Python struct-style format descriptor */
/* Python struct-style format descriptor */
py::format_descriptor<Scalar>::value,
py::format_descriptor<Scalar>::value
()
,
/* Number of dimensions */
/* Number of dimensions */
2,
2,
/* Buffer dimensions */
/* Buffer dimensions */
...
...
example/example-buffers.cpp
View file @
42ad3284
...
@@ -81,7 +81,7 @@ void init_ex_buffers(py::module &m) {
...
@@ -81,7 +81,7 @@ void init_ex_buffers(py::module &m) {
/// Construct from a buffer
/// Construct from a buffer
.
def
(
"__init__"
,
[](
Matrix
&
v
,
py
::
buffer
b
)
{
.
def
(
"__init__"
,
[](
Matrix
&
v
,
py
::
buffer
b
)
{
py
::
buffer_info
info
=
b
.
request
();
py
::
buffer_info
info
=
b
.
request
();
if
(
info
.
format
!=
py
::
format_descriptor
<
float
>::
value
||
info
.
ndim
!=
2
)
if
(
info
.
format
!=
py
::
format_descriptor
<
float
>::
value
()
||
info
.
ndim
!=
2
)
throw
std
::
runtime_error
(
"Incompatible buffer format!"
);
throw
std
::
runtime_error
(
"Incompatible buffer format!"
);
new
(
&
v
)
Matrix
(
info
.
shape
[
0
],
info
.
shape
[
1
]);
new
(
&
v
)
Matrix
(
info
.
shape
[
0
],
info
.
shape
[
1
]);
memcpy
(
v
.
data
(),
info
.
ptr
,
sizeof
(
float
)
*
v
.
rows
()
*
v
.
cols
());
memcpy
(
v
.
data
(),
info
.
ptr
,
sizeof
(
float
)
*
v
.
rows
()
*
v
.
cols
());
...
@@ -104,12 +104,12 @@ void init_ex_buffers(py::module &m) {
...
@@ -104,12 +104,12 @@ void init_ex_buffers(py::module &m) {
/// Provide buffer access
/// Provide buffer access
.
def_buffer
([](
Matrix
&
m
)
->
py
::
buffer_info
{
.
def_buffer
([](
Matrix
&
m
)
->
py
::
buffer_info
{
return
py
::
buffer_info
(
return
py
::
buffer_info
(
m
.
data
(),
/* Pointer to buffer */
m
.
data
(),
/* Pointer to buffer */
sizeof
(
float
),
/* Size of one scalar */
sizeof
(
float
),
/* Size of one scalar */
py
::
format_descriptor
<
float
>::
value
,
/* Python struct-style format descriptor */
py
::
format_descriptor
<
float
>::
value
()
,
/* Python struct-style format descriptor */
2
,
/* Number of dimensions */
2
,
/* Number of dimensions */
{
m
.
rows
(),
m
.
cols
()
},
/* Buffer dimensions */
{
m
.
rows
(),
m
.
cols
()
},
/* Buffer dimensions */
{
sizeof
(
float
)
*
m
.
rows
(),
/* Strides (in bytes) for each index */
{
sizeof
(
float
)
*
m
.
rows
(),
/* Strides (in bytes) for each index */
sizeof
(
float
)
}
sizeof
(
float
)
}
);
);
})
})
...
...
include/pybind11/common.h
View file @
42ad3284
...
@@ -204,7 +204,7 @@ struct buffer_info {
...
@@ -204,7 +204,7 @@ struct buffer_info {
void
*
ptr
;
// Pointer to the underlying storage
void
*
ptr
;
// Pointer to the underlying storage
size_t
itemsize
;
// Size of individual items in bytes
size_t
itemsize
;
// Size of individual items in bytes
size_t
size
;
// Total number of entries
size_t
size
;
// Total number of entries
std
::
string
format
;
// For homogeneous buffers, this should be set to format_descriptor<T>::value
std
::
string
format
;
// For homogeneous buffers, this should be set to format_descriptor<T>::value
()
size_t
ndim
;
// Number of dimensions
size_t
ndim
;
// Number of dimensions
std
::
vector
<
size_t
>
shape
;
// Shape of the tensor (1 entry per dimension)
std
::
vector
<
size_t
>
shape
;
// Shape of the tensor (1 entry per dimension)
std
::
vector
<
size_t
>
strides
;
// Number of entries between adjacent entries (for each per dimension)
std
::
vector
<
size_t
>
strides
;
// Number of entries between adjacent entries (for each per dimension)
...
@@ -348,14 +348,22 @@ PYBIND11_RUNTIME_EXCEPTION(reference_cast_error) /// Used internally
...
@@ -348,14 +348,22 @@ PYBIND11_RUNTIME_EXCEPTION(reference_cast_error) /// Used internally
[[
noreturn
]]
PYBIND11_NOINLINE
inline
void
pybind11_fail
(
const
std
::
string
&
reason
)
{
throw
std
::
runtime_error
(
reason
);
}
[[
noreturn
]]
PYBIND11_NOINLINE
inline
void
pybind11_fail
(
const
std
::
string
&
reason
)
{
throw
std
::
runtime_error
(
reason
);
}
/// Format strings for basic number types
/// Format strings for basic number types
#define PYBIND11_DECL_FMT(t, v) template<> struct format_descriptor<t> { static constexpr const char *value = v; }
#define PYBIND11_DECL_FMT(t, v) template<> struct format_descriptor<t> \
{ static constexpr const char* value() { return v; } };
template
<
typename
T
,
typename
SFINAE
=
void
>
struct
format_descriptor
{
};
template
<
typename
T
,
typename
SFINAE
=
void
>
struct
format_descriptor
{
};
template
<
typename
T
>
struct
format_descriptor
<
T
,
typename
std
::
enable_if
<
std
::
is_integral
<
T
>::
value
>::
type
>
{
template
<
typename
T
>
struct
format_descriptor
<
T
,
typename
std
::
enable_if
<
std
::
is_integral
<
T
>::
value
>::
type
>
{
static
constexpr
const
char
value
[
2
]
=
static
constexpr
const
char
*
value
()
{
return
format
;
}
static
constexpr
const
char
format
[
2
]
=
{
"bBhHiIqQ"
[
detail
::
log2
(
sizeof
(
T
))
*
2
+
(
std
::
is_unsigned
<
T
>::
value
?
1
:
0
)],
'\0'
};
{
"bBhHiIqQ"
[
detail
::
log2
(
sizeof
(
T
))
*
2
+
(
std
::
is_unsigned
<
T
>::
value
?
1
:
0
)],
'\0'
};
};
};
template
<
typename
T
>
constexpr
const
char
format_descriptor
<
template
<
typename
T
>
constexpr
const
char
format_descriptor
<
T
,
typename
std
::
enable_if
<
std
::
is_integral
<
T
>::
value
>::
type
>::
value
[
2
];
T
,
typename
std
::
enable_if
<
std
::
is_integral
<
T
>::
value
>::
type
>::
format
[
2
];
PYBIND11_DECL_FMT
(
float
,
"f"
);
PYBIND11_DECL_FMT
(
double
,
"d"
);
PYBIND11_DECL_FMT
(
bool
,
"?"
);
PYBIND11_DECL_FMT
(
float
,
"f"
);
PYBIND11_DECL_FMT
(
double
,
"d"
);
PYBIND11_DECL_FMT
(
bool
,
"?"
);
NAMESPACE_END
(
pybind11
)
NAMESPACE_END
(
pybind11
)
include/pybind11/eigen.h
View file @
42ad3284
...
@@ -133,7 +133,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_dense<Type>::value &&
...
@@ -133,7 +133,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_dense<Type>::value &&
/* Size of one scalar */
/* Size of one scalar */
sizeof
(
Scalar
),
sizeof
(
Scalar
),
/* Python struct-style format descriptor */
/* Python struct-style format descriptor */
format_descriptor
<
Scalar
>::
value
,
format_descriptor
<
Scalar
>::
value
()
,
/* Number of dimensions */
/* Number of dimensions */
1
,
1
,
/* Buffer dimensions */
/* Buffer dimensions */
...
@@ -148,7 +148,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_dense<Type>::value &&
...
@@ -148,7 +148,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_dense<Type>::value &&
/* Size of one scalar */
/* Size of one scalar */
sizeof
(
Scalar
),
sizeof
(
Scalar
),
/* Python struct-style format descriptor */
/* Python struct-style format descriptor */
format_descriptor
<
Scalar
>::
value
,
format_descriptor
<
Scalar
>::
value
()
,
/* Number of dimensions */
/* Number of dimensions */
isVector
?
1
:
2
,
isVector
?
1
:
2
,
/* Buffer dimensions */
/* Buffer dimensions */
...
@@ -233,7 +233,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_sparse<Type>::value>::
...
@@ -233,7 +233,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_sparse<Type>::value>::
try
{
try
{
obj
=
matrix_type
(
obj
);
obj
=
matrix_type
(
obj
);
}
catch
(
const
error_already_set
&
)
{
}
catch
(
const
error_already_set
&
)
{
PyErr_Clear
();
PyErr_Clear
();
return
false
;
return
false
;
}
}
}
}
...
@@ -276,7 +276,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_sparse<Type>::value>::
...
@@ -276,7 +276,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_sparse<Type>::value>::
// Size of one scalar
// Size of one scalar
sizeof
(
Scalar
),
sizeof
(
Scalar
),
// Python struct-style format descriptor
// Python struct-style format descriptor
format_descriptor
<
Scalar
>::
value
,
format_descriptor
<
Scalar
>::
value
()
,
// Number of dimensions
// Number of dimensions
1
,
1
,
// Buffer dimensions
// Buffer dimensions
...
@@ -291,7 +291,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_sparse<Type>::value>::
...
@@ -291,7 +291,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_sparse<Type>::value>::
// Size of one scalar
// Size of one scalar
sizeof
(
StorageIndex
),
sizeof
(
StorageIndex
),
// Python struct-style format descriptor
// Python struct-style format descriptor
format_descriptor
<
StorageIndex
>::
value
,
format_descriptor
<
StorageIndex
>::
value
()
,
// Number of dimensions
// Number of dimensions
1
,
1
,
// Buffer dimensions
// Buffer dimensions
...
@@ -306,7 +306,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_sparse<Type>::value>::
...
@@ -306,7 +306,7 @@ struct type_caster<Type, typename std::enable_if<is_eigen_sparse<Type>::value>::
// Size of one scalar
// Size of one scalar
sizeof
(
StorageIndex
),
sizeof
(
StorageIndex
),
// Python struct-style format descriptor
// Python struct-style format descriptor
format_descriptor
<
StorageIndex
>::
value
,
format_descriptor
<
StorageIndex
>::
value
()
,
// Number of dimensions
// Number of dimensions
1
,
1
,
// Buffer dimensions
// Buffer dimensions
...
...
include/pybind11/numpy.h
View file @
42ad3284
...
@@ -351,7 +351,7 @@ struct vectorize_helper {
...
@@ -351,7 +351,7 @@ struct vectorize_helper {
return
cast
(
f
(
*
((
Args
*
)
buffers
[
Index
].
ptr
)...));
return
cast
(
f
(
*
((
Args
*
)
buffers
[
Index
].
ptr
)...));
array
result
(
buffer_info
(
nullptr
,
sizeof
(
Return
),
array
result
(
buffer_info
(
nullptr
,
sizeof
(
Return
),
format_descriptor
<
Return
>::
value
,
format_descriptor
<
Return
>::
value
()
,
ndim
,
shape
,
strides
));
ndim
,
shape
,
strides
));
buffer_info
buf
=
result
.
request
();
buffer_info
buf
=
result
.
request
();
...
...
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