2
0
mirror of https://github.com/boostorg/python.git synced 2026-01-24 06:02:14 +00:00

bug fix for a single use of arg with no comma operator

[SVN r20533]
This commit is contained in:
Dave Abrahams
2003-10-29 00:46:08 +00:00
parent 2b9d29a0fc
commit ef7d675d67
2 changed files with 70 additions and 56 deletions

View File

@@ -32,10 +32,12 @@
namespace boost { namespace python {
typedef detail::keywords<1> arg;
namespace detail
{
template <std::size_t nkeywords>
struct keywords
struct keywords_base
{
BOOST_STATIC_CONSTANT(std::size_t, size = nkeywords);
@@ -44,19 +46,45 @@ namespace detail
return keyword_range(elements, elements + nkeywords);
}
keywords<nkeywords+1> operator,(const keywords<1> &k) const
{
python::detail::keywords<size+1> res;
std::copy(elements, elements+size, res.elements);
res.elements[size] = k.elements[0];
return res;
}
keywords<nkeywords+1> operator,(const char *name) const;
keyword elements[nkeywords];
};
template <std::size_t nkeywords>
struct keywords : keywords_base<nkeywords>
{
};
template <>
struct keywords<1> : keywords_base<1>
{
explicit keywords(char const *name)
{
elements[0].name = name;
}
template <class T>
arg& operator=(T const& value)
{
object z(value);
elements[0].default_value = handle<>(python::borrowed(object(value).ptr()));
return *this;
}
operator detail::keyword const&() const
{
return elements[0];
}
};
template <std::size_t nkeywords>
keywords<nkeywords+1> operator,(keywords<nkeywords> const& l, const keywords<1> &k)
{
python::detail::keywords<nkeywords+1> res;
std::copy(l.elements, l.elements+nkeywords, res.elements);
res.elements[nkeywords] = k.elements[0];
return res;
}
# ifndef BOOST_NO_TEMPLATE_PARTIAL_SPECIALIZATION
template<typename T>
struct is_keywords
@@ -112,37 +140,21 @@ namespace detail
class old_edg_workaround_for_arg { friend class arg; };
#endif
struct arg : detail::keywords<1>
{
explicit arg(char const *name)
{
elements[0].name = name;
}
template <class T>
arg& operator=(T const& value)
{
object z(value);
elements[0].default_value = handle<>(python::borrowed(object(value).ptr()));
return *this;
}
operator detail::keyword const&() const
{
return elements[0];
}
};
namespace detail
{
template <std::size_t nkeywords>
inline keywords<nkeywords + 1>
keywords<nkeywords>::operator,(const char *name) const
operator,(keywords<nkeywords> const& l, char *name)
{
return this->operator,(arg(name));
return l.operator,(arg(name));
}
}
inline detail::keywords<1> args(char const* name)
{
return detail::keywords<1>(name);
}
# define BOOST_PYTHON_ASSIGN_NAME(z, n, _) result.elements[n].name = name##n;
# define BOOST_PP_LOCAL_MACRO(n) \
inline detail::keywords<n> args(BOOST_PP_ENUM_PARAMS_Z(1, n, char const* name)) \
@@ -151,7 +163,7 @@ inline detail::keywords<n> args(BOOST_PP_ENUM_PARAMS_Z(1, n, char const* name))
BOOST_PP_REPEAT_1(n, BOOST_PYTHON_ASSIGN_NAME, _) \
return result; \
}
# define BOOST_PP_LOCAL_LIMITS (1, BOOST_PYTHON_MAX_ARITY)
# define BOOST_PP_LOCAL_LIMITS (2, BOOST_PYTHON_MAX_ARITY)
# include BOOST_PP_LOCAL_ITERATE()
}} // namespace boost::python

View File

@@ -56,6 +56,11 @@ struct Bar
n_ = n;
}
void seta(int a)
{
a_ = a;
}
int geta() const { return a_; }
double getb() const { return b_; }
@@ -71,37 +76,34 @@ private:
BOOST_PYTHON_MEMBER_FUNCTION_OVERLOADS(bar_set, Bar::set, 0,3)
using namespace boost::python;
#if BOOST_WORKAROUND(__GNUC__, == 2)
using boost::python::arg;
#endif
BOOST_PYTHON_MODULE(keywords)
{
class_<Foo>("Foo" , init<
int
, double
, const std::string &
>(
( arg("a") = 0
, arg("b") = 0.0
, arg("n") = std::string()
)
))
#if BOOST_WORKAROUND(__GNUC__, == 2)
using boost::python::arg;
#endif
class_<Foo>(
"Foo"
, init<int, double, const std::string&>(
( arg("a") = 0
, arg("b") = 0.0
, arg("n") = std::string()
)
))
.def("set", &Foo::set, (arg("a") = 0, arg("b") = 0.0, arg("n") = std::string()) )
.def("a", &Foo::geta)
.def("b", &Foo::getb)
.def("n", &Foo::getn)
;
class_<Bar>("Bar" , init<optional<
int
, double
, const std::string &
>
>()
)
class_<Bar>("Bar"
, init<optional<int, double, const std::string &> >()
)
.def("set", &Bar::set, bar_set())
.def("seta", &Bar::seta, arg("a"))
.def("a", &Bar::geta)
.def("b", &Bar::getb)
.def("n", &Bar::getn)