Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_33_1/libs/python/test/defaults.cpp @ 14

Last change on this file since 14 was 12, checked in by landauf, 18 years ago

added boost

File size: 4.4 KB
Line 
1// Copyright David Abrahams 2002.
2// Distributed under the Boost Software License, Version 1.0. (See
3// accompanying file LICENSE_1_0.txt or copy at
4// http://www.boost.org/LICENSE_1_0.txt)
5
6#include <boost/python/def.hpp>
7#include <boost/python/module.hpp>
8#include <boost/python/class.hpp>
9#include <boost/python/tuple.hpp>
10#include <boost/python/list.hpp>
11#include <boost/python/overloads.hpp>
12#include <boost/python/return_internal_reference.hpp>
13
14#if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
15# include <iostream> // works around a KCC intermediate code generation bug
16#endif
17
18using namespace boost::python;
19
20char const* const format = "int(%s); char(%s); string(%s); double(%s); ";
21
22///////////////////////////////////////////////////////////////////////////////
23//
24//  Overloaded functions
25//
26///////////////////////////////////////////////////////////////////////////////
27object
28bar(int a, char b, std::string c, double d)
29{
30    return format % make_tuple(a, b, c, d);
31}
32
33object
34bar(int a, char b, std::string c)
35{
36    return format % make_tuple(a, b, c, 0.0);
37}
38
39object
40bar(int a, char b)
41{
42    return format % make_tuple(a, b, "default", 0.0);
43}
44
45object
46bar(int a)
47{
48    return format % make_tuple(a, 'D', "default", 0.0);
49}
50
51BOOST_PYTHON_FUNCTION_OVERLOADS(bar_stubs, bar, 1, 4)
52
53///////////////////////////////////////////////////////////////////////////////
54//
55//  Functions with default arguments
56//
57///////////////////////////////////////////////////////////////////////////////
58object
59foo(int a, char b = 'D', std::string c = "default", double d = 0.0)
60{
61    return format % make_tuple(a, b, c, d);
62}
63
64BOOST_PYTHON_FUNCTION_OVERLOADS(foo_stubs, foo, 1, 4)
65
66///////////////////////////////////////////////////////////////////////////////
67//
68//  Overloaded member functions with default arguments
69//
70///////////////////////////////////////////////////////////////////////////////
71struct Y {
72
73    Y() {}
74
75    object
76    get_state() const
77    {
78        return format % make_tuple(a, b, c, d);
79    }
80
81    int a; char b; std::string c; double d;
82};
83
84
85struct X {
86
87    X() {}
88
89    X(int a, char b = 'D', std::string c = "constructor", double d = 0.0)
90    : state(format % make_tuple(a, b, c, d))
91    {}
92
93    X(std::string s, bool b)
94    : state("Got exactly two arguments from constructor: string(%s); bool(%s); " % make_tuple(s, b*1))
95    {}
96
97    object
98    bar(int a, char b = 'D', std::string c = "default", double d = 0.0) const
99    {
100        return format % make_tuple(a, b, c, d);
101    }
102
103    Y const&
104    bar2(int a = 0, char b = 'D', std::string c = "default", double d = 0.0)
105    {
106        // tests zero arg member function and return_internal_reference policy
107        y.a = a;
108        y.b = b;
109        y.c = c;
110        y.d = d;
111        return y;
112    }
113
114    object
115    foo(int a, bool b=false) const
116    {
117        return "int(%s); bool(%s); " % make_tuple(a, b*1);
118    }
119
120    object
121    foo(std::string a, bool b=false) const
122    {
123        return "string(%s); bool(%s); " % make_tuple(a, b*1);
124    }
125
126    object
127    foo(list a, list b, bool c=false) const
128    {
129        return "list(%s); list(%s); bool(%s); " % make_tuple(a, b, c*1);
130    }
131
132    object
133    get_state() const
134    {
135        return state;
136    }
137
138    Y y;
139    object state;
140};
141
142BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs, bar, 1, 4)
143BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_bar_stubs2, bar2, 0, 4)
144BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_2_stubs, foo, 1, 2)
145BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(X_foo_3_stubs, foo, 2, 3)
146
147///////////////////////////////////////////////////////////////////////////////
148
149BOOST_PYTHON_MODULE(defaults_ext)
150{
151    def("foo", foo, foo_stubs());
152    def("bar", (object(*)(int, char, std::string, double))0, bar_stubs());
153
154    class_<Y>("Y", init<>("doc of Y init")) // this should work
155        .def("get_state", &Y::get_state)
156        ;
157
158    class_<X>("X")
159
160        .def(init<int, optional<char, std::string, double> >("doc of init"))
161        .def(init<std::string, bool>()[default_call_policies()]) // what's a good policy here?
162        .def("get_state", &X::get_state)
163        .def("bar", &X::bar, X_bar_stubs())
164        .def("bar2", &X::bar2, X_bar_stubs2("doc of X::bar2")[return_internal_reference<>()])
165        .def("foo", (object(X::*)(std::string, bool) const)0, X_foo_2_stubs())
166        .def("foo", (object(X::*)(int, bool) const)0, X_foo_2_stubs())
167        .def("foo", (object(X::*)(list, list, bool) const)0, X_foo_3_stubs())
168        ;
169}
170
171#include "module_tail.cpp"
172
Note: See TracBrowser for help on using the repository browser.