|
1 | 1 | #include <iostream> |
2 | 2 | #include <string> |
3 | | -#include <boost/python/class_builder.hpp> |
4 | | -namespace python = boost::python; |
5 | 3 |
|
6 | 4 | namespace { // Avoid cluttering the global namespace. |
7 | 5 |
|
8 | 6 | // A friendly class. |
9 | | - class world |
| 7 | + class hello |
10 | 8 | { |
| 9 | + public: |
| 10 | + hello(const std::string& country) { this->country = country; } |
| 11 | + std::string greet() const { return "Hello from " + country; } |
11 | 12 | private: |
12 | 13 | std::string country; |
13 | | - public: |
14 | | - world(const std::string& country) { this->country = country; } |
15 | | - std::string greet() const { return "Hello from " + country + "!"; } |
16 | 14 | }; |
17 | 15 |
|
18 | | - // A function taking a world object as an argument. |
19 | | - std::string invite(const world& w) { |
20 | | - return w.greet() + " Please come soon!"; |
| 16 | + // A function taking a hello object as an argument. |
| 17 | + std::string invite(const hello& w) { |
| 18 | + return w.greet() + "! Please come soon!"; |
21 | 19 | } |
22 | 20 | } |
23 | 21 |
|
24 | | -extern "C" |
25 | | -DL_EXPORT(void) |
26 | | -initgetting_started2() |
| 22 | +#include <boost/python/class_builder.hpp> |
| 23 | +namespace python = boost::python; |
| 24 | + |
| 25 | +BOOST_PYTHON_MODULE_INIT(getting_started2) |
27 | 26 | { |
28 | 27 | try |
29 | 28 | { |
30 | 29 | // Create an object representing this extension module. |
31 | 30 | python::module_builder this_module("getting_started2"); |
32 | 31 |
|
33 | 32 | // Create the Python type object for our extension class. |
34 | | - python::class_builder<world> world_class(this_module, "world"); |
| 33 | + python::class_builder<hello> hello_class(this_module, "hello"); |
35 | 34 |
|
36 | 35 | // Add the __init__ function. |
37 | | - world_class.def(python::constructor<std::string>()); |
| 36 | + hello_class.def(python::constructor<std::string>()); |
38 | 37 | // Add a regular member function. |
39 | | - world_class.def(&world::greet, "greet"); |
| 38 | + hello_class.def(&hello::greet, "greet"); |
40 | 39 |
|
41 | 40 | // Add invite() as a regular function to the module. |
42 | 41 | this_module.def(invite, "invite"); |
43 | 42 |
|
44 | | - // Even better, invite() can also be made a member of world_class!!! |
45 | | - world_class.def(invite, "invite"); |
| 43 | + // Even better, invite() can also be made a member of hello_class!!! |
| 44 | + hello_class.def(invite, "invite"); |
46 | 45 | } |
47 | 46 | catch(...) |
48 | 47 | { |
|
0 commit comments