Skip to content

Commit e508842

Browse files
author
Ralf W. Grosse-Kunstleve
committed
BPL support for exporting/importing class wrappers.
[SVN r8788]
1 parent b0d9bbc commit e508842

25 files changed

Lines changed: 935 additions & 133 deletions

example/abstract.cpp

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
// Example by Ullrich Koethe
2+
#include "boost/python/class_builder.hpp"
3+
#include <string>
4+
5+
struct Abstract
6+
{
7+
virtual std::string test() = 0;
8+
};
9+
10+
struct Abstract_callback: Abstract
11+
{
12+
Abstract_callback(PyObject * self)
13+
: m_self(self)
14+
{}
15+
16+
std::string test()
17+
{
18+
return boost::python::callback<std::string>::call_method(m_self, "test");
19+
}
20+
21+
PyObject * m_self;
22+
};
23+
24+
extern "C"
25+
DL_EXPORT(void)
26+
initabstract()
27+
{
28+
boost::python::module_builder a("abstract");
29+
30+
boost::python::class_builder<Abstract, Abstract_callback>
31+
a_class(a, "Abstract");
32+
a_class.def(boost::python::constructor<>()); // wrap a constructor
33+
a_class.def(&Abstract::test, "test");
34+
}

example/dvect.cpp

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
#include "ivect.h"
2+
#include "dvect.h"
3+
#include <boost/python/class_builder.hpp>
4+
#include <boost/python/detail/import_extension_class.hpp>
5+
namespace python = boost::python;
6+
7+
namespace {
8+
9+
vects::ivect dvect_as_ivect(const vects::dvect& dv)
10+
{
11+
vects::ivect iv(dv.size());
12+
vects::ivect::iterator iviter = iv.begin();
13+
for (int i = 0; i < dv.size(); i++) iviter[i] = static_cast<int>(dv[i]);
14+
return iv;
15+
}
16+
17+
boost::python::tuple ivect_as_tuple(const vects::ivect& iv)
18+
{
19+
return iv.as_tuple();
20+
}
21+
22+
std::auto_ptr<vects::ivect> auto_ptr_ivect(const vects::dvect& dv)
23+
{
24+
return std::auto_ptr<vects::ivect>(new vects::ivect(dvect_as_ivect(dv)));
25+
}
26+
27+
boost::shared_ptr<vects::ivect> shared_ptr_ivect(const vects::dvect& dv)
28+
{
29+
return boost::shared_ptr<vects::ivect>(new vects::ivect(dvect_as_ivect(dv)));
30+
}
31+
32+
boost::python::tuple auto_ptr_ivect_as_tuple(std::auto_ptr<vects::ivect>& iv)
33+
{
34+
return iv->as_tuple();
35+
}
36+
37+
boost::python::tuple shared_ptr_ivect_as_tuple(boost::shared_ptr<vects::ivect>& iv)
38+
{
39+
return iv->as_tuple();
40+
}
41+
}
42+
43+
extern "C"
44+
DL_EXPORT(void)
45+
initdvect()
46+
{
47+
try
48+
{
49+
python::module_builder this_module("dvect");
50+
51+
python::x_class_builder<vects::dvect> dvect_class(this_module, "dvect");
52+
53+
python::import_class_builder<vects::ivect> ivect_class("ivect", "ivect");
54+
55+
dvect_class.def(python::constructor<python::tuple>());
56+
dvect_class.def(&vects::dvect::as_tuple, "as_tuple");
57+
dvect_class.def(dvect_as_ivect, "as_ivect");
58+
59+
this_module.def(ivect_as_tuple, "ivect_as_tuple");
60+
dvect_class.def(auto_ptr_ivect, "auto_ptr_ivect");
61+
dvect_class.def(shared_ptr_ivect, "shared_ptr_ivect");
62+
this_module.def(auto_ptr_ivect_as_tuple, "auto_ptr_ivect_as_tuple");
63+
this_module.def(shared_ptr_ivect_as_tuple, "shared_ptr_ivect_as_tuple");
64+
}
65+
catch(...)
66+
{
67+
python::handle_exception(); // Deal with the exception for Python
68+
}
69+
}

example/dvect.h

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#ifndef DVECT_H
2+
#define DVECT_H
3+
4+
#include <vector>
5+
#include <boost/python/class_builder.hpp>
6+
7+
namespace vects {
8+
9+
struct dvect : public std::vector<double>
10+
{
11+
dvect() : std::vector<double>() {}
12+
dvect(size_t n) : std::vector<double>(n) {}
13+
dvect(boost::python::tuple tuple) : std::vector<double>(tuple.size())
14+
{
15+
std::vector<double>::iterator v_it = begin();
16+
for (int i = 0; i < tuple.size(); i++)
17+
v_it[i] = BOOST_PYTHON_CONVERSION::from_python(tuple[i].get(),
18+
boost::python::type<double>());
19+
}
20+
21+
boost::python::tuple as_tuple() const
22+
{
23+
boost::python::tuple t(size());
24+
for (int i = 0; i < size(); i++)
25+
t.set_item(i,
26+
boost::python::ref(BOOST_PYTHON_CONVERSION::to_python((*this)[i])));
27+
return t;
28+
}
29+
};
30+
}
31+
32+
#endif // DVECT_H

example/example1.cpp

Lines changed: 0 additions & 54 deletions
This file was deleted.
Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,18 @@ namespace { // Avoid cluttering the global namespace.
88
}
99

1010
#include <boost/python/class_builder.hpp>
11-
1211
namespace python = boost::python;
1312

1413
// Python requires an exported function called init<module-name> in every
1514
// extension module. This is where we build the module contents.
1615
extern "C"
17-
#ifdef _WIN32
18-
__declspec(dllexport)
19-
#endif
20-
void initrwgk1()
16+
DL_EXPORT(void)
17+
initgetting_started1()
2118
{
2219
try
2320
{
2421
// Create an object representing this extension module.
25-
python::module_builder this_module("rwgk1");
22+
python::module_builder this_module("getting_started1");
2623

2724
// Add regular functions to the module.
2825
this_module.def(greet, "greet");
@@ -33,9 +30,3 @@ void initrwgk1()
3330
python::handle_exception(); // Deal with the exception for Python
3431
}
3532
}
36-
37-
// Win32 DLL boilerplate
38-
#if defined(_WIN32)
39-
#include <windows.h>
40-
extern "C" BOOL WINAPI DllMain(HINSTANCE, DWORD, LPVOID) { return 1; }
41-
#endif // _WIN32

example/getting_started2.cpp

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

0 commit comments

Comments
 (0)