Skip to content
GitLab
Menu
Projects
Groups
Snippets
Loading...
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
gaoqiong
pybind11
Commits
7ca27558
Commit
7ca27558
authored
Apr 22, 2016
by
Wenzel Jakob
Browse files
new type wrapper for iterable objects
parent
54b6fdd4
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
17 additions
and
3 deletions
+17
-3
include/pybind11/pytypes.h
include/pybind11/pytypes.h
+17
-3
No files found.
include/pybind11/pytypes.h
View file @
7ca27558
...
@@ -212,6 +212,16 @@ private:
...
@@ -212,6 +212,16 @@ private:
ssize_t
pos
=
0
;
ssize_t
pos
=
0
;
};
};
inline
bool
iterable_check
(
PyObject
*
obj
)
{
PyObject
*
iter
=
PyObject_GetIter
(
obj
);
if
(
iter
)
{
Py_DECREF
(
iter
);
return
true
;
}
else
{
PyErr_Clear
();
return
false
;
}
}
NAMESPACE_END
(
detail
)
NAMESPACE_END
(
detail
)
...
@@ -233,7 +243,6 @@ NAMESPACE_END(detail)
...
@@ -233,7 +243,6 @@ NAMESPACE_END(detail)
class
iterator
:
public
object
{
class
iterator
:
public
object
{
public:
public:
PYBIND11_OBJECT_DEFAULT
(
iterator
,
object
,
PyIter_Check
)
PYBIND11_OBJECT_DEFAULT
(
iterator
,
object
,
PyIter_Check
)
iterator
(
handle
obj
,
bool
borrowed
=
false
)
:
object
(
obj
,
borrowed
)
{
}
iterator
&
operator
++
()
{
iterator
&
operator
++
()
{
if
(
ptr
())
if
(
ptr
())
value
=
object
(
PyIter_Next
(
m_ptr
),
false
);
value
=
object
(
PyIter_Next
(
m_ptr
),
false
);
...
@@ -250,12 +259,17 @@ private:
...
@@ -250,12 +259,17 @@ private:
mutable
object
value
;
mutable
object
value
;
};
};
class
iterable
:
public
object
{
public:
PYBIND11_OBJECT_DEFAULT
(
iterable
,
object
,
detail
::
iterable_check
)
};
inline
detail
::
accessor
handle
::
operator
[](
handle
key
)
const
{
return
detail
::
accessor
(
ptr
(),
key
.
ptr
(),
false
);
}
inline
detail
::
accessor
handle
::
operator
[](
handle
key
)
const
{
return
detail
::
accessor
(
ptr
(),
key
.
ptr
(),
false
);
}
inline
detail
::
accessor
handle
::
operator
[](
const
char
*
key
)
const
{
return
detail
::
accessor
(
ptr
(),
key
,
false
);
}
inline
detail
::
accessor
handle
::
operator
[](
const
char
*
key
)
const
{
return
detail
::
accessor
(
ptr
(),
key
,
false
);
}
inline
detail
::
accessor
handle
::
attr
(
handle
key
)
const
{
return
detail
::
accessor
(
ptr
(),
key
.
ptr
(),
true
);
}
inline
detail
::
accessor
handle
::
attr
(
handle
key
)
const
{
return
detail
::
accessor
(
ptr
(),
key
.
ptr
(),
true
);
}
inline
detail
::
accessor
handle
::
attr
(
const
char
*
key
)
const
{
return
detail
::
accessor
(
ptr
(),
key
,
true
);
}
inline
detail
::
accessor
handle
::
attr
(
const
char
*
key
)
const
{
return
detail
::
accessor
(
ptr
(),
key
,
true
);
}
inline
iterator
handle
::
begin
()
const
{
return
iterator
(
PyObject_GetIter
(
ptr
()));
}
inline
iterator
handle
::
begin
()
const
{
return
iterator
(
PyObject_GetIter
(
ptr
())
,
false
);
}
inline
iterator
handle
::
end
()
const
{
return
iterator
(
nullptr
);
}
inline
iterator
handle
::
end
()
const
{
return
iterator
(
nullptr
,
false
);
}
class
str
:
public
object
{
class
str
:
public
object
{
public:
public:
...
...
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