diff --git a/test/maybe.cpp b/test/maybe.cpp new file mode 100755 index 0000000..b9fbc4d --- /dev/null +++ b/test/maybe.cpp @@ -0,0 +1,34 @@ +// Copyright Daniel Wallin 2006. Use, modification and distribution is +// subject to the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include + +namespace test { + +BOOST_PARAMETER_KEYWORD(tag, kw) +BOOST_PARAMETER_KEYWORD(tag, unused) + +template +int f(Args const& args) +{ + return args[kw | 1.f]; +} + +} // namespace test + +int main() +{ + using test::kw; + using test::unused; + using test::f; + using boost::parameter::aux::maybe; + + assert(f((kw = 0, unused = 0)) == 0); + assert(f(unused = 0) == 1); + assert(f((kw = maybe(), unused = 0)) == 1); + assert(f((kw = maybe(2), unused = 0)) == 2); +} + diff --git a/test/python.cpp b/test/python.cpp new file mode 100755 index 0000000..3d9b5b1 --- /dev/null +++ b/test/python.cpp @@ -0,0 +1,86 @@ +// Copyright Daniel Wallin 2006. Use, modification and distribution is +// subject to the Boost Software License, Version 1.0. (See accompanying +// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) + +#include +#include +#include +#include + +namespace test { + +BOOST_PARAMETER_KEYWORD(tag, x) +BOOST_PARAMETER_KEYWORD(tag, y) +BOOST_PARAMETER_KEYWORD(tag, z) + +struct X +{ + BOOST_PARAMETER_MEMBER_FUNCTION((int), f, tag, + (required + (x, *) + (y, *) + ) + (optional + (z, *) + ) + ) + { + return args[x] + args[y] + args[z | 0]; + } + + BOOST_PARAMETER_MEMBER_FUNCTION((std::string), g, tag, + (optional + (x, *) + (y, *) + ) + ) + { + return std::string(args[x | "foo"]) + args[y | "bar"]; + } +}; + +} // namespace test + +struct f_fwd +{ + template + R operator()(boost::type, T& self, A0 const& a0, A1 const& a1, A2 const& a2) + { + return self.f(a0,a1,a2); + } +}; + +struct g_fwd +{ + template + R operator()(boost::type, T& self, A0 const& a0, A1 const& a1) + { + return self.g(a0,a1); + } +}; + +BOOST_PYTHON_MODULE(python_parameter) +{ + namespace mpl = boost::mpl; + using namespace test; + using namespace boost::python; + + class_("X") + .def( + "f" + , boost::parameter::python::function< + f_fwd + , mpl::vector3 + , mpl::vector4 + >() + ) + .def( + "g" + , boost::parameter::python::function< + g_fwd + , mpl::vector2 + , mpl::vector3 + >() + ); +} + diff --git a/test/python.py b/test/python.py new file mode 100644 index 0000000..cd5540c --- /dev/null +++ b/test/python.py @@ -0,0 +1,31 @@ +''' +>>> from python_parameter import X +>>> x = X() +>>> x.f(1,2) +3 +>>> x.f(1,2,3) +6 +>>> x.f(1,2, z = 3) +6 +>>> x.f(z = 3, y = 2, x = 1) +6 +>>> x.g() +'foobar' +>>> x.g(y = "baz") +'foobaz' +>>> x.g(x = "baz") +'bazbar' +>>> x.g(y = "foo", x = "bar") +'barfoo' +''' + +def run(args = None): + if args is not None: + import sys + sys.argv = args + import doctest, python + return doctest.testmod(python) + +if __name__ == '__main__': + import sys + sys.exit(run()[0])