Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/python/example/quickstart/extending.cpp @ 29

Last change on this file since 29 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 1.1 KB
Line 
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
11namespace { // Avoid cluttering the global namespace.
12
13  // A friendly class.
14  class hello
15  {
16    public:
17      hello(const std::string& country) { this->country = country; }
18      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.
24  std::string invite(const hello& w) {
25    return w.greet() + "! Please come soon!";
26  }
27}
28
29BOOST_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}
Note: See TracBrowser for help on using the repository browser.