pickle4.cpp 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. // Copyright Ralf W. Grosse-Kunstleve 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. This example shows how to enable pickling without using the
  6. pickle_suite. The pickling interface (__getinitargs__) is
  7. implemented in Python.
  8. For more information refer to boost/libs/python/doc/pickle.html.
  9. */
  10. #include <boost/python/module.hpp>
  11. #include <boost/python/class.hpp>
  12. #include <string>
  13. namespace boost_python_test {
  14. // A friendly class.
  15. class world
  16. {
  17. private:
  18. std::string country;
  19. public:
  20. world(const std::string& _country) {
  21. this->country = _country;
  22. }
  23. std::string greet() const { return "Hello from " + country + "!"; }
  24. std::string get_country() const { return country; }
  25. };
  26. }
  27. BOOST_PYTHON_MODULE(pickle4_ext)
  28. {
  29. using namespace boost::python;
  30. using namespace boost_python_test;
  31. class_<world>("world", init<const std::string&>())
  32. .enable_pickling()
  33. .def("greet", &world::greet)
  34. .def("get_country", &world::get_country)
  35. ;
  36. }