Skip to content

Commit e1099e9

Browse files
committed
Added make_tuple
[SVN r15052]
1 parent 68c8901 commit e1099e9

File tree

3 files changed

+48
-49
lines changed

3 files changed

+48
-49
lines changed
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
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+
7+
#if !defined(BOOST_PP_IS_ITERATING)
8+
# error Boost.Python - do not include this file!
9+
#endif
10+
11+
#define N BOOST_PP_ITERATION()
12+
13+
#define BOOST_PYTHON_MAKE_TUPLE_ARG(N, ignored) \
14+
PyTuple_SET_ITEM( \
15+
result.ptr() \
16+
, N \
17+
, python::incref(python::object(a##N).ptr()) \
18+
);
19+
20+
template <BOOST_PYTHON_UNARY_ENUM(N, class A)>
21+
tuple
22+
make_tuple(BOOST_PYTHON_BINARY_ENUM(N, A, const& a))
23+
{
24+
tuple result((detail::new_reference)::PyTuple_New(N));
25+
BOOST_PP_REPEAT(N, BOOST_PYTHON_MAKE_TUPLE_ARG, _)
26+
return result;
27+
}
28+
29+
#undef BOOST_PYTHON_MAKE_TUPLE_ARG
30+
31+
#undef N

include/boost/python/tuple.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,11 @@ class tuple : public object
2828
static BOOST_PYTHON_DECL detail::new_reference call(object const&);
2929
};
3030

31+
// for completeness
32+
inline tuple make_tuple() { return tuple(); }
33+
34+
# define BOOST_PP_ITERATION_PARAMS_1 (3, (1, BOOST_PYTHON_MAX_ARITY, <boost/python/detail/make_tuple.hpp>))
35+
# include BOOST_PP_ITERATE()
3136

3237
//
3338
// Converter Specializations

test/defaults.cpp

Lines changed: 12 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
// to its suitability for any purpose.
66
#include <boost/python/module.hpp>
77
#include <boost/python/class.hpp>
8+
#include <boost/python/tuple.hpp>
89
#include <boost/python/list.hpp>
910

1011
#if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
@@ -14,49 +15,31 @@
1415
using namespace boost::python;
1516
using namespace std;
1617

18+
char const* const format = "int(%s); char(%s); string(%s); double(%s); ";
19+
1720
///////////////////////////////////////////////////////////////////////////////
1821
object
1922
bar(int a, char b, std::string c, double d)
2023
{
21-
list abcd;
22-
abcd.append(a);
23-
abcd.append(b);
24-
abcd.append(c);
25-
abcd.append(d);
26-
return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd);
24+
return format % make_tuple(a, b, c, d);
2725
}
2826

2927
object
3028
bar(int a, char b, std::string c)
3129
{
32-
list abcd;
33-
abcd.append(a);
34-
abcd.append(b);
35-
abcd.append(c);
36-
abcd.append(0.0);
37-
return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd);
30+
return format % make_tuple(a, b, c, 0.0);
3831
}
3932

4033
object
4134
bar(int a, char b)
4235
{
43-
list abcd;
44-
abcd.append(a);
45-
abcd.append(b);
46-
abcd.append("default");
47-
abcd.append(0.0);
48-
return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd);
36+
return format % make_tuple(a, b, "default", 0.0);
4937
}
5038

5139
object
5240
bar(int a)
5341
{
54-
list abcd;
55-
abcd.append(a);
56-
abcd.append('D');
57-
abcd.append("default");
58-
abcd.append(0.0);
59-
return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd);
42+
return format % make_tuple(a, 'D', "default", 0.0);
6043
}
6144

6245
BOOST_PYTHON_FUNCTION_GENERATOR(bar_stubs, bar, 1, 4)
@@ -65,12 +48,7 @@ BOOST_PYTHON_FUNCTION_GENERATOR(bar_stubs, bar, 1, 4)
6548
object
6649
foo(int a, char b = 'D', std::string c = "default", double d = 0.0)
6750
{
68-
list abcd;
69-
abcd.append(a);
70-
abcd.append(b);
71-
abcd.append(c);
72-
abcd.append(d);
73-
return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd);
51+
return format % make_tuple(a, b, c, d);
7452
}
7553

7654
BOOST_PYTHON_FUNCTION_GENERATOR(foo_stubs, foo, 1, 4)
@@ -82,40 +60,25 @@ struct X {
8260
object
8361
bar(int a, char b = 'D', std::string c = "default", double d = 0.0) const
8462
{
85-
list abcd;
86-
abcd.append(a);
87-
abcd.append(b);
88-
abcd.append(c);
89-
abcd.append(d);
90-
return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd);
63+
return format % make_tuple(a, b, c, d);
9164
}
9265

9366
object
9467
foo(int a, bool b=false) const
9568
{
96-
list ab;
97-
ab.append(a);
98-
ab.append(b);
99-
return "int(%s); bool(%s); " % tuple(ab);
69+
return "int(%s); bool(%s); " % make_tuple(a, b);
10070
}
10171

10272
object
10373
foo(std::string a, bool b=false) const
10474
{
105-
list ab;
106-
ab.append(a);
107-
ab.append(b);
108-
return "string(%s); bool(%s); " % tuple(ab);
75+
return "string(%s); bool(%s); " % make_tuple(a, b);
10976
}
11077

11178
object
11279
foo(list a, list b, bool c=false) const
11380
{
114-
list abc;
115-
abc.append(a);
116-
abc.append(b);
117-
abc.append(c);
118-
return "list(%s); list(%s); bool(%s); " % tuple(abc);
81+
return "list(%s); list(%s); bool(%s); " % make_tuple(a, b, c);
11982
}
12083
};
12184

0 commit comments

Comments
 (0)