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
61352e50
Commit
61352e50
authored
Jul 18, 2016
by
Wenzel Jakob
Committed by
GitHub
Jul 18, 2016
Browse files
Merge pull request #289 from jagerman/example-renaming
Rename examples files, as per #288
parents
fb6aed21
3e2e44f5
Changes
65
Hide whitespace changes
Inline
Side-by-side
Showing
20 changed files
with
153 additions
and
104 deletions
+153
-104
example/example-operator-overloading.py
example/example-operator-overloading.py
+0
-0
example/example-operator-overloading.ref
example/example-operator-overloading.ref
+0
-0
example/example-pickling.cpp
example/example-pickling.cpp
+2
-2
example/example-pickling.py
example/example-pickling.py
+0
-0
example/example-pickling.ref
example/example-pickling.ref
+0
-0
example/example-python-types.cpp
example/example-python-types.cpp
+30
-30
example/example-python-types.py
example/example-python-types.py
+12
-12
example/example-python-types.ref
example/example-python-types.ref
+27
-27
example/example-sequences-and-iterators.cpp
example/example-sequences-and-iterators.cpp
+2
-2
example/example-sequences-and-iterators.py
example/example-sequences-and-iterators.py
+0
-0
example/example-sequences-and-iterators.ref
example/example-sequences-and-iterators.ref
+0
-0
example/example-smart-ptr.cpp
example/example-smart-ptr.cpp
+2
-2
example/example-smart-ptr.py
example/example-smart-ptr.py
+0
-0
example/example-smart-ptr.ref
example/example-smart-ptr.ref
+0
-0
example/example-stl-binder-vector.cpp
example/example-stl-binder-vector.cpp
+2
-2
example/example-stl-binder-vector.py
example/example-stl-binder-vector.py
+0
-0
example/example-stl-binder-vector.ref
example/example-stl-binder-vector.ref
+0
-0
example/example-virtual-functions.cpp
example/example-virtual-functions.cpp
+27
-27
example/example-virtual-functions.py
example/example-virtual-functions.py
+36
-0
example/example-virtual-functions.ref
example/example-virtual-functions.ref
+13
-0
No files found.
example/example
3
.py
→
example/example
-operator-overloading
.py
View file @
61352e50
File moved
example/example
3
.ref
→
example/example
-operator-overloading
.ref
View file @
61352e50
File moved
example/example
15
.cpp
→
example/example
-pickling
.cpp
View file @
61352e50
/*
/*
example/example
15
.cpp -- pickle support
example/example
-pickling
.cpp -- pickle support
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
...
@@ -24,7 +24,7 @@ private:
...
@@ -24,7 +24,7 @@ private:
int
m_extra2
=
0
;
int
m_extra2
=
0
;
};
};
void
init_ex
15
(
py
::
module
&
m
)
{
void
init_ex
_pickling
(
py
::
module
&
m
)
{
py
::
class_
<
Pickleable
>
(
m
,
"Pickleable"
)
py
::
class_
<
Pickleable
>
(
m
,
"Pickleable"
)
.
def
(
py
::
init
<
std
::
string
>
())
.
def
(
py
::
init
<
std
::
string
>
())
.
def
(
"value"
,
&
Pickleable
::
value
)
.
def
(
"value"
,
&
Pickleable
::
value
)
...
...
example/example
15
.py
→
example/example
-pickling
.py
View file @
61352e50
File moved
example/example
15
.ref
→
example/example
-pickling
.ref
View file @
61352e50
File moved
example/example
2
.cpp
→
example/example
-python-types
.cpp
View file @
61352e50
/*
/*
example/example
2
.cpp2 -- singleton design pattern, static functions and
example/example
-python-types
.cpp2 -- singleton design pattern, static functions and
variables, passing and interacting with Python types
variables, passing and interacting with Python types
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
...
@@ -16,13 +16,13 @@
...
@@ -16,13 +16,13 @@
# include <fcntl.h>
# include <fcntl.h>
#endif
#endif
class
Example
2
{
class
Example
PythonTypes
{
public:
public:
static
Example
2
*
new_instance
()
{
static
Example
PythonTypes
*
new_instance
()
{
return
new
Example
2
();
return
new
Example
PythonTypes
();
}
}
~
Example
2
()
{
~
Example
PythonTypes
()
{
std
::
cout
<<
"Destructing Example
2
"
<<
std
::
endl
;
std
::
cout
<<
"Destructing Example
PythonTypes
"
<<
std
::
endl
;
}
}
/* Create and return a Python dictionary */
/* Create and return a Python dictionary */
...
@@ -142,31 +142,31 @@ public:
...
@@ -142,31 +142,31 @@ public:
static
const
int
value2
;
static
const
int
value2
;
};
};
int
Example
2
::
value
=
0
;
int
Example
PythonTypes
::
value
=
0
;
const
int
Example
2
::
value2
=
5
;
const
int
Example
PythonTypes
::
value2
=
5
;
void
init_ex
2
(
py
::
module
&
m
)
{
void
init_ex
_python_types
(
py
::
module
&
m
)
{
/* No constructor is explicitly defined below. An exception is raised when
/* No constructor is explicitly defined below. An exception is raised when
trying to construct it directly from Python */
trying to construct it directly from Python */
py
::
class_
<
Example
2
>
(
m
,
"Example
2
"
,
"Example 2 documentation"
)
py
::
class_
<
Example
PythonTypes
>
(
m
,
"Example
PythonTypes
"
,
"Example 2 documentation"
)
.
def
(
"get_dict"
,
&
Example
2
::
get_dict
,
"Return a Python dictionary"
)
.
def
(
"get_dict"
,
&
Example
PythonTypes
::
get_dict
,
"Return a Python dictionary"
)
.
def
(
"get_dict_2"
,
&
Example
2
::
get_dict_2
,
"Return a C++ dictionary"
)
.
def
(
"get_dict_2"
,
&
Example
PythonTypes
::
get_dict_2
,
"Return a C++ dictionary"
)
.
def
(
"get_list"
,
&
Example
2
::
get_list
,
"Return a Python list"
)
.
def
(
"get_list"
,
&
Example
PythonTypes
::
get_list
,
"Return a Python list"
)
.
def
(
"get_list_2"
,
&
Example
2
::
get_list_2
,
"Return a C++ list"
)
.
def
(
"get_list_2"
,
&
Example
PythonTypes
::
get_list_2
,
"Return a C++ list"
)
.
def
(
"get_set"
,
&
Example
2
::
get_set
,
"Return a Python set"
)
.
def
(
"get_set"
,
&
Example
PythonTypes
::
get_set
,
"Return a Python set"
)
.
def
(
"get_set2"
,
&
Example
2
::
get_set_2
,
"Return a C++ set"
)
.
def
(
"get_set2"
,
&
Example
PythonTypes
::
get_set_2
,
"Return a C++ set"
)
.
def
(
"get_array"
,
&
Example
2
::
get_array
,
"Return a C++ array"
)
.
def
(
"get_array"
,
&
Example
PythonTypes
::
get_array
,
"Return a C++ array"
)
.
def
(
"print_dict"
,
&
Example
2
::
print_dict
,
"Print entries of a Python dictionary"
)
.
def
(
"print_dict"
,
&
Example
PythonTypes
::
print_dict
,
"Print entries of a Python dictionary"
)
.
def
(
"print_dict_2"
,
&
Example
2
::
print_dict_2
,
"Print entries of a C++ dictionary"
)
.
def
(
"print_dict_2"
,
&
Example
PythonTypes
::
print_dict_2
,
"Print entries of a C++ dictionary"
)
.
def
(
"print_set"
,
&
Example
2
::
print_set
,
"Print entries of a Python set"
)
.
def
(
"print_set"
,
&
Example
PythonTypes
::
print_set
,
"Print entries of a Python set"
)
.
def
(
"print_set_2"
,
&
Example
2
::
print_set_2
,
"Print entries of a C++ set"
)
.
def
(
"print_set_2"
,
&
Example
PythonTypes
::
print_set_2
,
"Print entries of a C++ set"
)
.
def
(
"print_list"
,
&
Example
2
::
print_list
,
"Print entries of a Python list"
)
.
def
(
"print_list"
,
&
Example
PythonTypes
::
print_list
,
"Print entries of a Python list"
)
.
def
(
"print_list_2"
,
&
Example
2
::
print_list_2
,
"Print entries of a C++ list"
)
.
def
(
"print_list_2"
,
&
Example
PythonTypes
::
print_list_2
,
"Print entries of a C++ list"
)
.
def
(
"print_array"
,
&
Example
2
::
print_array
,
"Print entries of a C++ array"
)
.
def
(
"print_array"
,
&
Example
PythonTypes
::
print_array
,
"Print entries of a C++ array"
)
.
def
(
"pair_passthrough"
,
&
Example
2
::
pair_passthrough
,
"Return a pair in reversed order"
)
.
def
(
"pair_passthrough"
,
&
Example
PythonTypes
::
pair_passthrough
,
"Return a pair in reversed order"
)
.
def
(
"tuple_passthrough"
,
&
Example
2
::
tuple_passthrough
,
"Return a triple in reversed order"
)
.
def
(
"tuple_passthrough"
,
&
Example
PythonTypes
::
tuple_passthrough
,
"Return a triple in reversed order"
)
.
def
(
"throw_exception"
,
&
Example
2
::
throw_exception
,
"Throw an exception"
)
.
def
(
"throw_exception"
,
&
Example
PythonTypes
::
throw_exception
,
"Throw an exception"
)
.
def_static
(
"new_instance"
,
&
Example
2
::
new_instance
,
"Return an instance"
)
.
def_static
(
"new_instance"
,
&
Example
PythonTypes
::
new_instance
,
"Return an instance"
)
.
def_readwrite_static
(
"value"
,
&
Example
2
::
value
,
"Static value member"
)
.
def_readwrite_static
(
"value"
,
&
Example
PythonTypes
::
value
,
"Static value member"
)
.
def_readonly_static
(
"value2"
,
&
Example
2
::
value2
,
"Static value member (readonly)"
);
.
def_readonly_static
(
"value2"
,
&
Example
PythonTypes
::
value2
,
"Static value member (readonly)"
);
}
}
example/example
2
.py
→
example/example
-python-types
.py
View file @
61352e50
...
@@ -4,23 +4,23 @@ import sys, pydoc
...
@@ -4,23 +4,23 @@ import sys, pydoc
sys
.
path
.
append
(
'.'
)
sys
.
path
.
append
(
'.'
)
import
example
import
example
from
example
import
Example
2
from
example
import
Example
PythonTypes
Example
2
.
value
=
15
Example
PythonTypes
.
value
=
15
print
(
Example
2
.
value
)
print
(
Example
PythonTypes
.
value
)
print
(
Example
2
.
value2
)
print
(
Example
PythonTypes
.
value2
)
try
:
try
:
Example
2
()
Example
PythonTypes
()
except
Exception
as
e
:
except
Exception
as
e
:
print
(
e
)
print
(
e
)
try
:
try
:
Example
2
.
value2
=
15
Example
PythonTypes
.
value2
=
15
except
Exception
as
e
:
except
Exception
as
e
:
print
(
e
)
print
(
e
)
instance
=
Example
2
.
new_instance
()
instance
=
Example
PythonTypes
.
new_instance
()
dict_result
=
instance
.
get_dict
()
dict_result
=
instance
.
get_dict
()
dict_result
[
'key2'
]
=
'value2'
dict_result
[
'key2'
]
=
'value2'
...
@@ -58,10 +58,10 @@ except Exception as e:
...
@@ -58,10 +58,10 @@ except Exception as e:
print
(
instance
.
pair_passthrough
((
True
,
"test"
)))
print
(
instance
.
pair_passthrough
((
True
,
"test"
)))
print
(
instance
.
tuple_passthrough
((
True
,
"test"
,
5
)))
print
(
instance
.
tuple_passthrough
((
True
,
"test"
,
5
)))
print
(
pydoc
.
render_doc
(
Example
2
,
"Help on %s"
))
print
(
pydoc
.
render_doc
(
Example
PythonTypes
,
"Help on %s"
))
print
(
"__name__(example) = %s"
%
example
.
__name__
)
print
(
"__name__(example) = %s"
%
example
.
__name__
)
print
(
"__name__(example.Example
2
) = %s"
%
Example
2
.
__name__
)
print
(
"__name__(example.Example
PythonTypes
) = %s"
%
Example
PythonTypes
.
__name__
)
print
(
"__module__(example.Example
2
) = %s"
%
Example
2
.
__module__
)
print
(
"__module__(example.Example
PythonTypes
) = %s"
%
Example
PythonTypes
.
__module__
)
print
(
"__name__(example.Example
2
.get_set) = %s"
%
Example
2
.
get_set
.
__name__
)
print
(
"__name__(example.Example
PythonTypes
.get_set) = %s"
%
Example
PythonTypes
.
get_set
.
__name__
)
print
(
"__module__(example.Example
2
.get_set) = %s"
%
Example
2
.
get_set
.
__module__
)
print
(
"__module__(example.Example
PythonTypes
.get_set) = %s"
%
Example
PythonTypes
.
get_set
.
__module__
)
example/example
2
.ref
→
example/example
-python-types
.ref
View file @
61352e50
15
15
5
5
example.Example
2
: No constructor defined!
example.Example
PythonTypes
: No constructor defined!
can't set attribute
can't set attribute
key: key2, value=value2
key: key2, value=value2
key: key, value=value
key: key, value=value
...
@@ -23,9 +23,9 @@ array item 1: array entry 2
...
@@ -23,9 +23,9 @@ array item 1: array entry 2
This exception was intentionally thrown.
This exception was intentionally thrown.
(u'test', True)
(u'test', True)
(5L, u'test', True)
(5L, u'test', True)
Help on class Example
2
in module example
Help on class Example
PythonTypes
in module example
class EExxaammppllee
22
(__builtin__.object)
class EExxaammppllee
PPyytthhoonnTTyyppeess
(__builtin__.object)
| Example 2 documentation
| Example 2 documentation
|
|
| Methods defined here:
| Methods defined here:
...
@@ -34,104 +34,104 @@ class EExxaammppllee22(__builtin__.object)
...
@@ -34,104 +34,104 @@ class EExxaammppllee22(__builtin__.object)
| x.__init__(...) initializes x; see help(type(x)) for signature
| x.__init__(...) initializes x; see help(type(x)) for signature
|
|
| ggeett__aarrrraayy(...)
| ggeett__aarrrraayy(...)
| Signature : (example.Example
2
) -> list<unicode>[2]
| Signature : (example.Example
PythonTypes
) -> list<unicode>[2]
|
|
| Return a C++ array
| Return a C++ array
|
|
| ggeett__ddiicctt(...)
| ggeett__ddiicctt(...)
| Signature : (example.Example
2
) -> dict
| Signature : (example.Example
PythonTypes
) -> dict
|
|
| Return a Python dictionary
| Return a Python dictionary
|
|
| ggeett__ddiicctt__22(...)
| ggeett__ddiicctt__22(...)
| Signature : (example.Example
2
) -> dict<unicode, unicode>
| Signature : (example.Example
PythonTypes
) -> dict<unicode, unicode>
|
|
| Return a C++ dictionary
| Return a C++ dictionary
|
|
| ggeett__lliisstt(...)
| ggeett__lliisstt(...)
| Signature : (example.Example
2
) -> list
| Signature : (example.Example
PythonTypes
) -> list
|
|
| Return a Python list
| Return a Python list
|
|
| ggeett__lliisstt__22(...)
| ggeett__lliisstt__22(...)
| Signature : (example.Example
2
) -> list<unicode>
| Signature : (example.Example
PythonTypes
) -> list<unicode>
|
|
| Return a C++ list
| Return a C++ list
|
|
| ggeett__sseett(...)
| ggeett__sseett(...)
| Signature : (example.Example
2
) -> set
| Signature : (example.Example
PythonTypes
) -> set
|
|
| Return a Python set
| Return a Python set
|
|
| ggeett__sseett22(...)
| ggeett__sseett22(...)
| Signature : (example.Example
2
) -> set
| Signature : (example.Example
PythonTypes
) -> set
|
|
| Return a C++ set
| Return a C++ set
|
|
| ppaaiirr__ppaasssstthhrroouugghh(...)
| ppaaiirr__ppaasssstthhrroouugghh(...)
| Signature : (example.Example
2
, (bool, unicode)) -> (unicode, bool)
| Signature : (example.Example
PythonTypes
, (bool, unicode)) -> (unicode, bool)
|
|
| Return a pair in reversed order
| Return a pair in reversed order
|
|
| pprriinntt__aarrrraayy(...)
| pprriinntt__aarrrraayy(...)
| Signature : (example.Example
2
, list<unicode>[2]) -> NoneType
| Signature : (example.Example
PythonTypes
, list<unicode>[2]) -> NoneType
|
|
| Print entries of a C++ array
| Print entries of a C++ array
|
|
| pprriinntt__ddiicctt(...)
| pprriinntt__ddiicctt(...)
| Signature : (example.Example
2
, dict) -> NoneType
| Signature : (example.Example
PythonTypes
, dict) -> NoneType
|
|
| Print entries of a Python dictionary
| Print entries of a Python dictionary
|
|
| pprriinntt__ddiicctt__22(...)
| pprriinntt__ddiicctt__22(...)
| Signature : (example.Example
2
, dict<unicode, unicode>) -> NoneType
| Signature : (example.Example
PythonTypes
, dict<unicode, unicode>) -> NoneType
|
|
| Print entries of a C++ dictionary
| Print entries of a C++ dictionary
|
|
| pprriinntt__lliisstt(...)
| pprriinntt__lliisstt(...)
| Signature : (example.Example
2
, list) -> NoneType
| Signature : (example.Example
PythonTypes
, list) -> NoneType
|
|
| Print entries of a Python list
| Print entries of a Python list
|
|
| pprriinntt__lliisstt__22(...)
| pprriinntt__lliisstt__22(...)
| Signature : (example.Example
2
, list<unicode>) -> NoneType
| Signature : (example.Example
PythonTypes
, list<unicode>) -> NoneType
|
|
| Print entries of a C++ list
| Print entries of a C++ list
|
|
| pprriinntt__sseett(...)
| pprriinntt__sseett(...)
| Signature : (example.Example
2
, set) -> NoneType
| Signature : (example.Example
PythonTypes
, set) -> NoneType
|
|
| Print entries of a Python set
| Print entries of a Python set
|
|
| pprriinntt__sseett__22(...)
| pprriinntt__sseett__22(...)
| Signature : (example.Example
2
, set<unicode>) -> NoneType
| Signature : (example.Example
PythonTypes
, set<unicode>) -> NoneType
|
|
| Print entries of a C++ set
| Print entries of a C++ set
|
|
| tthhrrooww__eexxcceeppttiioonn(...)
| tthhrrooww__eexxcceeppttiioonn(...)
| Signature : (example.Example
2
) -> NoneType
| Signature : (example.Example
PythonTypes
) -> NoneType
|
|
| Throw an exception
| Throw an exception
|
|
| ttuuppllee__ppaasssstthhrroouugghh(...)
| ttuuppllee__ppaasssstthhrroouugghh(...)
| Signature : (example.Example
2
, (bool, unicode, int)) -> (int, unicode, bool)
| Signature : (example.Example
PythonTypes
, (bool, unicode, int)) -> (int, unicode, bool)
|
|
| Return a triple in reversed order
| Return a triple in reversed order
|
|
| ----------------------------------------------------------------------
| ----------------------------------------------------------------------
| Data and other attributes defined here:
| Data and other attributes defined here:
|
|
| ____nneeww____ = <built-in method __new__ of example.Example
2
__Meta object>
| ____nneeww____ = <built-in method __new__ of example.Example
PythonTypes
__Meta object>
| T.__new__(S, ...) -> a new object with type S, a subtype of T
| T.__new__(S, ...) -> a new object with type S, a subtype of T
|
|
| nneeww__iinnssttaannccee = <built-in method new_instance of PyCapsule object>
| nneeww__iinnssttaannccee = <built-in method new_instance of PyCapsule object>
| Signature : () -> example.Example
2
| Signature : () -> example.Example
PythonTypes
|
|
| Return an instance
| Return an instance
__name__(example) = example
__name__(example) = example
__name__(example.Example
2) = Example2
__name__(example.Example
PythonTypes) = ExamplePythonTypes
__module__(example.Example
2
) = example
__module__(example.Example
PythonTypes
) = example
__name__(example.Example
2
.get_set) = get_set
__name__(example.Example
PythonTypes
.get_set) = get_set
__module__(example.Example
2
.get_set) = example
__module__(example.Example
PythonTypes
.get_set) = example
Destructing Example
2
Destructing Example
PythonTypes
example/example
6
.cpp
→
example/example
-sequences-and-iterators
.cpp
View file @
61352e50
/*
/*
example/example
6
.cpp -- supporting Pythons' sequence protocol, iterators,
example/example
-sequences-and-iterators
.cpp -- supporting Pythons' sequence protocol, iterators,
etc.
etc.
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
...
@@ -109,7 +109,7 @@ private:
...
@@ -109,7 +109,7 @@ private:
float
*
m_data
;
float
*
m_data
;
};
};
void
init_ex
6
(
py
::
module
&
m
)
{
void
init_ex
_sequences_and_iterators
(
py
::
module
&
m
)
{
py
::
class_
<
Sequence
>
seq
(
m
,
"Sequence"
);
py
::
class_
<
Sequence
>
seq
(
m
,
"Sequence"
);
seq
.
def
(
py
::
init
<
size_t
>
())
seq
.
def
(
py
::
init
<
size_t
>
())
...
...
example/example
6
.py
→
example/example
-sequences-and-iterators
.py
View file @
61352e50
File moved
example/example
6
.ref
→
example/example
-sequences-and-iterators
.ref
View file @
61352e50
File moved
example/example
8
.cpp
→
example/example
-smart-ptr
.cpp
View file @
61352e50
/*
/*
example/example
8
.cpp -- binding classes with custom reference counting,
example/example
-smart-ptr
.cpp -- binding classes with custom reference counting,
implicit conversions between types
implicit conversions between types
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
...
@@ -105,7 +105,7 @@ void print_myobject3_2(std::shared_ptr<MyObject3> obj) { std::cout << obj->toStr
...
@@ -105,7 +105,7 @@ void print_myobject3_2(std::shared_ptr<MyObject3> obj) { std::cout << obj->toStr
void
print_myobject3_3
(
const
std
::
shared_ptr
<
MyObject3
>
&
obj
)
{
std
::
cout
<<
obj
->
toString
()
<<
std
::
endl
;
}
void
print_myobject3_3
(
const
std
::
shared_ptr
<
MyObject3
>
&
obj
)
{
std
::
cout
<<
obj
->
toString
()
<<
std
::
endl
;
}
void
print_myobject3_4
(
const
std
::
shared_ptr
<
MyObject3
>
*
obj
)
{
std
::
cout
<<
(
*
obj
)
->
toString
()
<<
std
::
endl
;
}
void
print_myobject3_4
(
const
std
::
shared_ptr
<
MyObject3
>
*
obj
)
{
std
::
cout
<<
(
*
obj
)
->
toString
()
<<
std
::
endl
;
}
void
init_ex
8
(
py
::
module
&
m
)
{
void
init_ex
_smart_ptr
(
py
::
module
&
m
)
{
py
::
class_
<
Object
,
ref
<
Object
>>
obj
(
m
,
"Object"
);
py
::
class_
<
Object
,
ref
<
Object
>>
obj
(
m
,
"Object"
);
obj
.
def
(
"getRefCount"
,
&
Object
::
getRefCount
);
obj
.
def
(
"getRefCount"
,
&
Object
::
getRefCount
);
...
...
example/example
8
.py
→
example/example
-smart-ptr
.py
View file @
61352e50
File moved
example/example
8
.ref
→
example/example
-smart-ptr
.ref
View file @
61352e50
File moved
example/example
17
.cpp
→
example/example
-stl-binder-vector
.cpp
View file @
61352e50
/*
/*
example/example
17
.cpp -- Usage of stl_binders functions
example/example
-stl-binder-vector
.cpp -- Usage of stl_binders functions
Copyright (c) 2016 Sergey Lyskov
Copyright (c) 2016 Sergey Lyskov
...
@@ -24,7 +24,7 @@ std::ostream & operator<<(std::ostream &s, El const&v) {
...
@@ -24,7 +24,7 @@ std::ostream & operator<<(std::ostream &s, El const&v) {
return
s
;
return
s
;
}
}
void
init_ex
17
(
py
::
module
&
m
)
{
void
init_ex
_stl_binder_vector
(
py
::
module
&
m
)
{
pybind11
::
class_
<
El
>
(
m
,
"El"
)
pybind11
::
class_
<
El
>
(
m
,
"El"
)
.
def
(
pybind11
::
init
<
int
>
());
.
def
(
pybind11
::
init
<
int
>
());
...
...
example/example
17
.py
→
example/example
-stl-binder-vector
.py
View file @
61352e50
File moved
example/example
17
.ref
→
example/example
-stl-binder-vector
.ref
View file @
61352e50
File moved
example/example
12
.cpp
→
example/example
-virtual-functions
.cpp
View file @
61352e50
/*
/*
example/example
12
.cpp -- overriding virtual functions from Python
example/example
-virtual-functions
.cpp -- overriding virtual functions from Python
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
Copyright (c) 2016 Wenzel Jakob <wenzel.jakob@epfl.ch>
...
@@ -11,18 +11,18 @@
...
@@ -11,18 +11,18 @@
#include <pybind11/functional.h>
#include <pybind11/functional.h>
/* This is an example class that we'll want to be able to extend from Python */
/* This is an example class that we'll want to be able to extend from Python */
class
Example
12
{
class
Example
Virt
{
public:
public:
Example
12
(
int
state
)
:
state
(
state
)
{
Example
Virt
(
int
state
)
:
state
(
state
)
{
cout
<<
"Constructing Example
12
.."
<<
endl
;
cout
<<
"Constructing Example
Virt
.."
<<
endl
;
}
}
~
Example
12
()
{
~
Example
Virt
()
{
cout
<<
"Destructing Example
12
.."
<<
endl
;
cout
<<
"Destructing Example
Virt
.."
<<
endl
;
}
}
virtual
int
run
(
int
value
)
{
virtual
int
run
(
int
value
)
{
std
::
cout
<<
"Original implementation of Example
12
::run(state="
<<
state
std
::
cout
<<
"Original implementation of Example
Virt
::run(state="
<<
state
<<
", value="
<<
value
<<
")"
<<
std
::
endl
;
<<
", value="
<<
value
<<
")"
<<
std
::
endl
;
return
state
+
value
;
return
state
+
value
;
}
}
...
@@ -34,24 +34,24 @@ private:
...
@@ -34,24 +34,24 @@ private:
};
};
/* This is a wrapper class that must be generated */
/* This is a wrapper class that must be generated */
class
PyExample
12
:
public
Example
12
{
class
PyExample
Virt
:
public
Example
Virt
{
public:
public:
using
Example
12
::
Example
12
;
/* Inherit constructors */
using
Example
Virt
::
Example
Virt
;
/* Inherit constructors */
virtual
int
run
(
int
value
)
{
virtual
int
run
(
int
value
)
{
/* Generate wrapping code that enables native function overloading */
/* Generate wrapping code that enables native function overloading */
PYBIND11_OVERLOAD
(
PYBIND11_OVERLOAD
(
int
,
/* Return type */
int
,
/* Return type */
Example
12
,
/* Parent class */
Example
Virt
,
/* Parent class */
run
,
/* Name of function */
run
,
/* Name of function */
value
/* Argument(s) */
value
/* Argument(s) */
);
);
}
}
virtual
bool
run_bool
()
{
virtual
bool
run_bool
()
{
PYBIND11_OVERLOAD_PURE
(
PYBIND11_OVERLOAD_PURE
(
bool
,
/* Return type */
bool
,
/* Return type */
Example
12
,
/* Parent class */
Example
Virt
,
/* Parent class */
run_bool
,
/* Name of function */
run_bool
,
/* Name of function */
/* This function has no arguments. The trailing comma
/* This function has no arguments. The trailing comma
in the previous line is needed for some compilers */
in the previous line is needed for some compilers */
...
@@ -61,7 +61,7 @@ public:
...
@@ -61,7 +61,7 @@ public:
virtual
void
pure_virtual
()
{
virtual
void
pure_virtual
()
{
PYBIND11_OVERLOAD_PURE
(
PYBIND11_OVERLOAD_PURE
(
void
,
/* Return type */
void
,
/* Return type */
Example
12
,
/* Parent class */
Example
Virt
,
/* Parent class */
pure_virtual
,
/* Name of function */
pure_virtual
,
/* Name of function */
/* This function has no arguments. The trailing comma
/* This function has no arguments. The trailing comma
in the previous line is needed for some compilers */
in the previous line is needed for some compilers */
...
@@ -69,30 +69,30 @@ public:
...
@@ -69,30 +69,30 @@ public:
}
}
};
};
int
runExample
12
(
Example
12
*
ex
,
int
value
)
{
int
runExample
Virt
(
Example
Virt
*
ex
,
int
value
)
{
return
ex
->
run
(
value
);
return
ex
->
run
(
value
);
}
}
bool
runExample
12
Bool
(
Example
12
*
ex
)
{
bool
runExample
Virt
Bool
(
Example
Virt
*
ex
)
{
return
ex
->
run_bool
();
return
ex
->
run_bool
();
}
}
void
runExample
12
Virtual
(
Example
12
*
ex
)
{
void
runExample
Virt
Virtual
(
Example
Virt
*
ex
)
{
ex
->
pure_virtual
();
ex
->
pure_virtual
();
}
}
void
init_ex
12
(
py
::
module
&
m
)
{
void
init_ex
_virtual_functions
(
py
::
module
&
m
)
{
/* Important: indicate the trampoline class PyExample
12
using the third
/* Important: indicate the trampoline class PyExample
Virt
using the third
argument to py::class_. The second argument with the unique pointer
argument to py::class_. The second argument with the unique pointer
is simply the default holder type used by pybind11. */
is simply the default holder type used by pybind11. */
py
::
class_
<
Example
12
,
std
::
unique_ptr
<
Example
12
>
,
PyExample
12
>
(
m
,
"Example
12
"
)
py
::
class_
<
Example
Virt
,
std
::
unique_ptr
<
Example
Virt
>
,
PyExample
Virt
>
(
m
,
"Example
Virt
"
)
.
def
(
py
::
init
<
int
>
())
.
def
(
py
::
init
<
int
>
())
/* Reference original class in function definitions */
/* Reference original class in function definitions */
.
def
(
"run"
,
&
Example
12
::
run
)
.
def
(
"run"
,
&
Example
Virt
::
run
)
.
def
(
"run_bool"
,
&
Example
12
::
run_bool
)
.
def
(
"run_bool"
,
&
Example
Virt
::
run_bool
)
.
def
(
"pure_virtual"
,
&
Example
12
::
pure_virtual
);
.
def
(
"pure_virtual"
,
&
Example
Virt
::
pure_virtual
);
m
.
def
(
"runExample
12
"
,
&
runExample
12
);
m
.
def
(
"runExample
Virt
"
,
&
runExample
Virt
);
m
.
def
(
"runExample
12
Bool"
,
&
runExample
12
Bool
);
m
.
def
(
"runExample
Virt
Bool"
,
&
runExample
Virt
Bool
);
m
.
def
(
"runExample
12
Virtual"
,
&
runExample
12
Virtual
);
m
.
def
(
"runExample
Virt
Virtual"
,
&
runExample
Virt
Virtual
);
}
}
example/example
12
.py
→
example/example
-virtual-functions
.py
View file @
61352e50
...
@@ -3,34 +3,34 @@ from __future__ import print_function
...
@@ -3,34 +3,34 @@ from __future__ import print_function
import
sys
import
sys
sys
.
path
.
append
(
'.'
)
sys
.
path
.
append
(
'.'
)
from
example
import
Example
12
,
runExample
12
,
runExample
12
Virtual
,
runExample
12
Bool
from
example
import
Example
Virt
,
runExample
Virt
,
runExample
Virt
Virtual
,
runExample
Virt
Bool
class
ExtendedExample
12
(
Example
12
):
class
ExtendedExample
Virt
(
Example
Virt
):
def
__init__
(
self
,
state
):
def
__init__
(
self
,
state
):
super
(
ExtendedExample
12
,
self
).
__init__
(
state
+
1
)
super
(
ExtendedExample
Virt
,
self
).
__init__
(
state
+
1
)
self
.
data
=
"Hello world"
self
.
data
=
"Hello world"
def
run
(
self
,
value
):
def
run
(
self
,
value
):
print
(
'ExtendedExample
12
::run(%i), calling parent..'
%
value
)
print
(
'ExtendedExample
Virt
::run(%i), calling parent..'
%
value
)
return
super
(
ExtendedExample
12
,
self
).
run
(
value
+
1
)
return
super
(
ExtendedExample
Virt
,
self
).
run
(
value
+
1
)
def
run_bool
(
self
):
def
run_bool
(
self
):
print
(
'ExtendedExample
12
::run_bool()'
)
print
(
'ExtendedExample
Virt
::run_bool()'
)
return
False
return
False
def
pure_virtual
(
self
):
def
pure_virtual
(
self
):
print
(
'ExtendedExample
12
::pure_virtual(): %s'
%
self
.
data
)
print
(
'ExtendedExample
Virt
::pure_virtual(): %s'
%
self
.
data
)
ex12
=
Example
12
(
10
)
ex12
=
Example
Virt
(
10
)
print
(
runExample
12
(
ex12
,
20
))
print
(
runExample
Virt
(
ex12
,
20
))
try
:
try
:
runExample
12
Virtual
(
ex12
)
runExample
Virt
Virtual
(
ex12
)
except
Exception
as
e
:
except
Exception
as
e
:
print
(
"Caught expected exception: "
+
str
(
e
))
print
(
"Caught expected exception: "
+
str
(
e
))
ex12p
=
ExtendedExample
12
(
10
)
ex12p
=
ExtendedExample
Virt
(
10
)
print
(
runExample
12
(
ex12p
,
20
))
print
(
runExample
Virt
(
ex12p
,
20
))
print
(
runExample
12
Bool
(
ex12p
))
print
(
runExample
Virt
Bool
(
ex12p
))
runExample
12
Virtual
(
ex12p
)
runExample
Virt
Virtual
(
ex12p
)
example/example-virtual-functions.ref
0 → 100644
View file @
61352e50
Constructing ExampleVirt..
Original implementation of ExampleVirt::run(state=10, value=20)
30
Caught expected exception: Tried to call pure virtual function "ExampleVirt::pure_virtual"
Constructing ExampleVirt..
ExtendedExampleVirt::run(20), calling parent..
Original implementation of ExampleVirt::run(state=11, value=21)
32
ExtendedExampleVirt::run_bool()
False
ExtendedExampleVirt::pure_virtual(): Hello world
Destructing ExampleVirt..
Destructing ExampleVirt..
Prev
1
2
3
4
Next
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