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