mirror of
https://github.com/boostorg/python.git
synced 2026-01-23 05:42:30 +00:00
Added make_tuple
[SVN r15052]
This commit is contained in:
31
include/boost/python/detail/make_tuple.hpp
Normal file
31
include/boost/python/detail/make_tuple.hpp
Normal file
@@ -0,0 +1,31 @@
|
||||
# // 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.
|
||||
|
||||
#if !defined(BOOST_PP_IS_ITERATING)
|
||||
# error Boost.Python - do not include this file!
|
||||
#endif
|
||||
|
||||
#define N BOOST_PP_ITERATION()
|
||||
|
||||
#define BOOST_PYTHON_MAKE_TUPLE_ARG(N, ignored) \
|
||||
PyTuple_SET_ITEM( \
|
||||
result.ptr() \
|
||||
, N \
|
||||
, python::incref(python::object(a##N).ptr()) \
|
||||
);
|
||||
|
||||
template <BOOST_PYTHON_UNARY_ENUM(N, class A)>
|
||||
tuple
|
||||
make_tuple(BOOST_PYTHON_BINARY_ENUM(N, A, const& a))
|
||||
{
|
||||
tuple result((detail::new_reference)::PyTuple_New(N));
|
||||
BOOST_PP_REPEAT(N, BOOST_PYTHON_MAKE_TUPLE_ARG, _)
|
||||
return result;
|
||||
}
|
||||
|
||||
#undef BOOST_PYTHON_MAKE_TUPLE_ARG
|
||||
|
||||
#undef N
|
||||
@@ -28,6 +28,11 @@ class tuple : public object
|
||||
static BOOST_PYTHON_DECL detail::new_reference call(object const&);
|
||||
};
|
||||
|
||||
// for completeness
|
||||
inline tuple make_tuple() { return tuple(); }
|
||||
|
||||
# define BOOST_PP_ITERATION_PARAMS_1 (3, (1, BOOST_PYTHON_MAX_ARITY, <boost/python/detail/make_tuple.hpp>))
|
||||
# include BOOST_PP_ITERATE()
|
||||
|
||||
//
|
||||
// Converter Specializations
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
// to its suitability for any purpose.
|
||||
#include <boost/python/module.hpp>
|
||||
#include <boost/python/class.hpp>
|
||||
#include <boost/python/tuple.hpp>
|
||||
#include <boost/python/list.hpp>
|
||||
|
||||
#if defined(_AIX) && defined(__EDG_VERSION__) && __EDG_VERSION__ < 245
|
||||
@@ -14,49 +15,31 @@
|
||||
using namespace boost::python;
|
||||
using namespace std;
|
||||
|
||||
char const* const format = "int(%s); char(%s); string(%s); double(%s); ";
|
||||
|
||||
///////////////////////////////////////////////////////////////////////////////
|
||||
object
|
||||
bar(int a, char b, std::string c, double d)
|
||||
{
|
||||
list abcd;
|
||||
abcd.append(a);
|
||||
abcd.append(b);
|
||||
abcd.append(c);
|
||||
abcd.append(d);
|
||||
return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd);
|
||||
return format % make_tuple(a, b, c, d);
|
||||
}
|
||||
|
||||
object
|
||||
bar(int a, char b, std::string c)
|
||||
{
|
||||
list abcd;
|
||||
abcd.append(a);
|
||||
abcd.append(b);
|
||||
abcd.append(c);
|
||||
abcd.append(0.0);
|
||||
return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd);
|
||||
return format % make_tuple(a, b, c, 0.0);
|
||||
}
|
||||
|
||||
object
|
||||
bar(int a, char b)
|
||||
{
|
||||
list abcd;
|
||||
abcd.append(a);
|
||||
abcd.append(b);
|
||||
abcd.append("default");
|
||||
abcd.append(0.0);
|
||||
return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd);
|
||||
return format % make_tuple(a, b, "default", 0.0);
|
||||
}
|
||||
|
||||
object
|
||||
bar(int a)
|
||||
{
|
||||
list abcd;
|
||||
abcd.append(a);
|
||||
abcd.append('D');
|
||||
abcd.append("default");
|
||||
abcd.append(0.0);
|
||||
return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd);
|
||||
return format % make_tuple(a, 'D', "default", 0.0);
|
||||
}
|
||||
|
||||
BOOST_PYTHON_FUNCTION_GENERATOR(bar_stubs, bar, 1, 4)
|
||||
@@ -65,12 +48,7 @@ BOOST_PYTHON_FUNCTION_GENERATOR(bar_stubs, bar, 1, 4)
|
||||
object
|
||||
foo(int a, char b = 'D', std::string c = "default", double d = 0.0)
|
||||
{
|
||||
list abcd;
|
||||
abcd.append(a);
|
||||
abcd.append(b);
|
||||
abcd.append(c);
|
||||
abcd.append(d);
|
||||
return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd);
|
||||
return format % make_tuple(a, b, c, d);
|
||||
}
|
||||
|
||||
BOOST_PYTHON_FUNCTION_GENERATOR(foo_stubs, foo, 1, 4)
|
||||
@@ -82,40 +60,25 @@ struct X {
|
||||
object
|
||||
bar(int a, char b = 'D', std::string c = "default", double d = 0.0) const
|
||||
{
|
||||
list abcd;
|
||||
abcd.append(a);
|
||||
abcd.append(b);
|
||||
abcd.append(c);
|
||||
abcd.append(d);
|
||||
return "int(%s); char(%s); string(%s); double(%s); " % tuple(abcd);
|
||||
return format % make_tuple(a, b, c, d);
|
||||
}
|
||||
|
||||
object
|
||||
foo(int a, bool b=false) const
|
||||
{
|
||||
list ab;
|
||||
ab.append(a);
|
||||
ab.append(b);
|
||||
return "int(%s); bool(%s); " % tuple(ab);
|
||||
return "int(%s); bool(%s); " % make_tuple(a, b);
|
||||
}
|
||||
|
||||
object
|
||||
foo(std::string a, bool b=false) const
|
||||
{
|
||||
list ab;
|
||||
ab.append(a);
|
||||
ab.append(b);
|
||||
return "string(%s); bool(%s); " % tuple(ab);
|
||||
return "string(%s); bool(%s); " % make_tuple(a, b);
|
||||
}
|
||||
|
||||
object
|
||||
foo(list a, list b, bool c=false) const
|
||||
{
|
||||
list abc;
|
||||
abc.append(a);
|
||||
abc.append(b);
|
||||
abc.append(c);
|
||||
return "list(%s); list(%s); bool(%s); " % tuple(abc);
|
||||
return "list(%s); list(%s); bool(%s); " % make_tuple(a, b, c);
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user