forked from boostorg/python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdefaults.cpp
More file actions
157 lines (128 loc) · 4.3 KB
/
defaults.cpp
File metadata and controls
157 lines (128 loc) · 4.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright David Abrahams 2002. Permission to copy, use,
// modify, sell and distribute this software is granted provided this
// copyright notice appears in all copies. This software is provided
// "as is" without express or implied warranty, and with no claim as
// to its suitability for any purpose.
#include <boost/python/def.hpp>
#include <boost/python/module_init.hpp>
#include <boost/python/class.hpp>
#include <boost/python/tuple.hpp>
#include <boost/python/list.hpp>
#include <boost/python/module.hpp>
#if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
# include <iostream> // works around a KCC intermediate code generation bug
#endif
using namespace boost::python;
using namespace std;
char const* const format = "int(%s); char(%s); string(%s); double(%s); ";
///////////////////////////////////////////////////////////////////////////////
//
// Overloaded functions
//
///////////////////////////////////////////////////////////////////////////////
object
bar(int a, char b, std::string c, double d)
{
return format % make_tuple(a, b, c, d);
}
object
bar(int a, char b, std::string c)
{
return format % make_tuple(a, b, c, 0.0);
}
object
bar(int a, char b)
{
return format % make_tuple(a, b, "default", 0.0);
}
object
bar(int a)
{
return format % make_tuple(a, 'D', "default", 0.0);
}
BOOST_PYTHON_FUNCTION_GENERATOR(bar_stubs, bar, 1, 4)
///////////////////////////////////////////////////////////////////////////////
//
// Functions with default arguments
//
///////////////////////////////////////////////////////////////////////////////
object
foo(int a, char b = 'D', std::string c = "default", double d = 0.0)
{
return format % make_tuple(a, b, c, d);
}
BOOST_PYTHON_FUNCTION_GENERATOR(foo_stubs, foo, 1, 4)
///////////////////////////////////////////////////////////////////////////////
//
// Overloaded member functions with default arguments
//
///////////////////////////////////////////////////////////////////////////////
struct X {
X() {}
X(int a, char b = 'D', std::string c = "constructor", double d = 0.0)
: state(format % make_tuple(a, b, c, d))
{}
object
bar(int a, char b = 'D', std::string c = "default", double d = 0.0) const
{
return format % make_tuple(a, b, c, d);
}
object
bar2(int a = 0, char b = 'D', std::string c = "default", double d = 0.0) const
{
// tests zero arg member function
return format % make_tuple(a, b, c, d);
}
object
foo(int a, bool b=false) const
{
return "int(%s); bool(%s); " % make_tuple(a, b);
}
object
foo(std::string a, bool b=false) const
{
return "string(%s); bool(%s); " % make_tuple(a, b);
}
object
foo(list a, list b, bool c=false) const
{
return "list(%s); list(%s); bool(%s); " % make_tuple(a, b, c);
}
object
get_state() const
{
return state;
}
object state;
};
BOOST_PYTHON_MEM_FUN_GENERATOR(X_bar_stubs, bar, 1, 4)
BOOST_PYTHON_MEM_FUN_GENERATOR(X_bar2_stubs, bar2, 0, 4) // tests zero arg member function
BOOST_PYTHON_MEM_FUN_GENERATOR(X_foo_2_stubs, foo, 1, 2)
BOOST_PYTHON_MEM_FUN_GENERATOR(X_foo_3_stubs, foo, 2, 3)
///////////////////////////////////////////////////////////////////////////////
BOOST_PYTHON_MODULE_INIT(defaults_ext)
{
def("foo", foo, foo_stubs());
def("bar", (object(*)(int, char, std::string, double))0, bar_stubs());
// Show that this works with the old obsolete module version of def().
module("defaults_ext")
.def("foobar", foo, foo_stubs())
.def("barfoo", (object(*)(int, char, std::string, double))0, bar_stubs())
;
class_<X>("X")
# if (!defined(BOOST_INTEL_CXX_VERSION) || BOOST_INTEL_CXX_VERSION > 600)
.def(init<int, optional<char, std::string, double> >())
# else
.def_init(args<int>())
.def_init(args<int, char>())
.def_init(args<int, char, std::string>())
.def_init(args<int, char, std::string, double>())
# endif
.def("get_state", &X::get_state)
.def("bar", &X::bar, X_bar_stubs())
.def("foo", (object(X::*)(std::string, bool) const)0, X_foo_2_stubs())
.def("foo", (object(X::*)(int, bool) const)0, X_foo_2_stubs())
.def("foo", (object(X::*)(list, list, bool) const)0, X_foo_3_stubs())
;
}
#include "module_tail.cpp"