|
| 1 | +// Copyright David Abrahams 2002. Permission to copy, use, |
| 2 | +// modify, sell and distribute this software is granted provided this |
| 3 | +// copyright notice appears in all copies. This software is provided |
| 4 | +// "as is" without express or implied warranty, and with no claim as |
| 5 | +// to its suitability for any purpose. |
| 6 | +#include <boost/python.hpp> |
| 7 | +#include <string> |
| 8 | + |
| 9 | +struct Foo |
| 10 | +{ |
| 11 | + Foo( |
| 12 | + int a = 0 |
| 13 | + , double b = 0 |
| 14 | + , const std::string &n = std::string() |
| 15 | + ) : |
| 16 | + a_(a) |
| 17 | + , b_(b) |
| 18 | + , n_(n) |
| 19 | + {} |
| 20 | + |
| 21 | + void set(int a=0, double b=0, const std::string &n=std::string()) |
| 22 | + { |
| 23 | + a_ = a; |
| 24 | + b_ = b; |
| 25 | + n_ = n; |
| 26 | + } |
| 27 | + |
| 28 | + int geta() const { return a_; } |
| 29 | + |
| 30 | + double getb() const { return b_; } |
| 31 | + |
| 32 | + std::string getn() const { return n_; } |
| 33 | + |
| 34 | +private: |
| 35 | + int a_; |
| 36 | + double b_; |
| 37 | + std::string n_; |
| 38 | +}; |
| 39 | + |
| 40 | +struct Bar |
| 41 | +{ |
| 42 | + Bar( |
| 43 | + int a = 0 |
| 44 | + , double b = 0 |
| 45 | + , const std::string &n = std::string() |
| 46 | + ) : |
| 47 | + a_(a) |
| 48 | + , b_(b) |
| 49 | + , n_(n) |
| 50 | + {} |
| 51 | + |
| 52 | + void set(int a=0, double b=0, const std::string &n=std::string()) |
| 53 | + { |
| 54 | + a_ = a; |
| 55 | + b_ = b; |
| 56 | + n_ = n; |
| 57 | + } |
| 58 | + |
| 59 | + int geta() const { return a_; } |
| 60 | + |
| 61 | + double getb() const { return b_; } |
| 62 | + |
| 63 | + std::string getn() const { return n_; } |
| 64 | + |
| 65 | +private: |
| 66 | + int a_; |
| 67 | + double b_; |
| 68 | + std::string n_; |
| 69 | +}; |
| 70 | + |
| 71 | +BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(bar_set, Bar::set, 0,3) |
| 72 | + |
| 73 | +using namespace boost::python; |
| 74 | +#if BOOST_WORKAROUND(__GNUC__, == 2) |
| 75 | +using boost::python::arg; |
| 76 | +#endif |
| 77 | +BOOST_PYTHON_MODULE(keywords) |
| 78 | +{ |
| 79 | + class_<Foo>("Foo" , init< |
| 80 | + int |
| 81 | + , double |
| 82 | + , const std::string & |
| 83 | + >( |
| 84 | + ( arg("a") = 0 |
| 85 | + , arg("b") = 0.0 |
| 86 | + , arg("n") = std::string() |
| 87 | + ) |
| 88 | + )) |
| 89 | + |
| 90 | + .def("set", &Foo::set, (arg("a") = 0, arg("b") = 0.0, arg("n") = std::string()) ) |
| 91 | + .def("a", &Foo::geta) |
| 92 | + .def("b", &Foo::getb) |
| 93 | + .def("n", &Foo::getn) |
| 94 | + ; |
| 95 | + |
| 96 | + class_<Bar>("Bar" , init<optional< |
| 97 | + int |
| 98 | + , double |
| 99 | + , const std::string & |
| 100 | + > |
| 101 | + >() |
| 102 | + ) |
| 103 | + |
| 104 | + .def("set", &Bar::set, bar_set()) |
| 105 | + .def("a", &Bar::geta) |
| 106 | + .def("b", &Bar::getb) |
| 107 | + .def("n", &Bar::getn) |
| 108 | + ; |
| 109 | + |
| 110 | +} |
| 111 | + |
| 112 | + |
| 113 | + |
| 114 | +#include "module_tail.cpp" |
0 commit comments