• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright Ralf W. Grosse-Kunstleve 2002-2004. Distributed under the Boost
2 // Software License, Version 1.0. (See accompanying
3 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
4 
5 #include <boost/python/class.hpp>
6 #include <boost/python/module.hpp>
7 #include <boost/python/def.hpp>
8 #include <iostream>
9 #include <string>
10 
11 namespace { // Avoid cluttering the global namespace.
12 
13   // A friendly class.
14   class hello
15   {
16     public:
hello(const std::string & country)17       hello(const std::string& country) { this->country = country; }
greet() const18       std::string greet() const { return "Hello from " + country; }
19     private:
20       std::string country;
21   };
22 
23   // A function taking a hello object as an argument.
invite(const hello & w)24   std::string invite(const hello& w) {
25     return w.greet() + "! Please come soon!";
26   }
27 }
28 
BOOST_PYTHON_MODULE(extending)29 BOOST_PYTHON_MODULE(extending)
30 {
31     using namespace boost::python;
32     class_<hello>("hello", init<std::string>())
33         // Add a regular member function.
34         .def("greet", &hello::greet)
35         // Add invite() as a member of hello!
36         .def("invite", invite)
37         ;
38 
39     // Also add invite() as a regular function to the module.
40     def("invite", invite);
41 }
42