2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-23 05:42:30 +00:00

use BOOST_PYTHON_MODULE_INIT, update getting_started2 for better documentation

[SVN r9472]
This commit is contained in:
Dave Abrahams
2001-03-07 03:53:14 +00:00
parent 00cea4ff83
commit 98b31ed073
6 changed files with 38 additions and 37 deletions

View File

@@ -1,29 +1,28 @@
#include <iostream>
#include <string>
#include <boost/python/class_builder.hpp>
namespace python = boost::python;
namespace { // Avoid cluttering the global namespace.
// A friendly class.
class world
class hello
{
public:
hello(const std::string& country) { this->country = country; }
std::string greet() const { return "Hello from " + country; }
private:
std::string country;
public:
world(const std::string& country) { this->country = country; }
std::string greet() const { return "Hello from " + country + "!"; }
};
// A function taking a world object as an argument.
std::string invite(const world& w) {
return w.greet() + " Please come soon!";
// A function taking a hello object as an argument.
std::string invite(const hello& w) {
return w.greet() + "! Please come soon!";
}
}
extern "C"
DL_EXPORT(void)
initgetting_started2()
#include <boost/python/class_builder.hpp>
namespace python = boost::python;
BOOST_PYTHON_MODULE_INIT(getting_started2)
{
try
{
@@ -31,18 +30,18 @@ initgetting_started2()
python::module_builder this_module("getting_started2");
// Create the Python type object for our extension class.
python::class_builder<world> world_class(this_module, "world");
python::class_builder<hello> hello_class(this_module, "hello");
// Add the __init__ function.
world_class.def(python::constructor<std::string>());
hello_class.def(python::constructor<std::string>());
// Add a regular member function.
world_class.def(&world::greet, "greet");
hello_class.def(&hello::greet, "greet");
// Add invite() as a regular function to the module.
this_module.def(invite, "invite");
// Even better, invite() can also be made a member of world_class!!!
world_class.def(invite, "invite");
// Even better, invite() can also be made a member of hello_class!!!
hello_class.def(invite, "invite");
}
catch(...)
{