extending.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. #include <boost/python/class.hpp>
  5. #include <boost/python/module.hpp>
  6. #include <boost/python/def.hpp>
  7. #include <iostream>
  8. #include <string>
  9. namespace { // Avoid cluttering the global namespace.
  10. // A friendly class.
  11. class hello
  12. {
  13. public:
  14. hello(const std::string& country) { this->country = country; }
  15. std::string greet() const { return "Hello from " + country; }
  16. private:
  17. std::string country;
  18. };
  19. // A function taking a hello object as an argument.
  20. std::string invite(const hello& w) {
  21. return w.greet() + "! Please come soon!";
  22. }
  23. }
  24. BOOST_PYTHON_MODULE(extending)
  25. {
  26. using namespace boost::python;
  27. class_<hello>("hello", init<std::string>())
  28. // Add a regular member function.
  29. .def("greet", &hello::greet)
  30. // Add invite() as a member of hello!
  31. .def("invite", invite)
  32. ;
  33. // Also add invite() as a regular function to the module.
  34. def("invite", invite);
  35. }