args.hpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. // Copyright David Abrahams 2002.
  2. // Distributed under the Boost Software License, Version 1.0. (See
  3. // accompanying file LICENSE_1_0.txt or copy at
  4. // http://www.boost.org/LICENSE_1_0.txt)
  5. #ifndef KEYWORDS_DWA2002323_HPP
  6. # define KEYWORDS_DWA2002323_HPP
  7. # include <boost/python/detail/prefix.hpp>
  8. # include <boost/python/args_fwd.hpp>
  9. # include <boost/config.hpp>
  10. # include <boost/python/detail/preprocessor.hpp>
  11. # include <boost/python/detail/type_list.hpp>
  12. # include <boost/python/detail/type_traits.hpp>
  13. # include <boost/preprocessor/enum_params.hpp>
  14. # include <boost/preprocessor/repeat.hpp>
  15. # include <boost/preprocessor/facilities/intercept.hpp>
  16. # include <boost/preprocessor/iteration/local.hpp>
  17. # include <boost/python/detail/mpl_lambda.hpp>
  18. # include <boost/python/object_core.hpp>
  19. # include <boost/mpl/bool.hpp>
  20. # include <cstddef>
  21. # include <algorithm>
  22. namespace boost { namespace python {
  23. typedef detail::keywords<1> arg;
  24. typedef arg arg_; // gcc 2.96 workaround
  25. namespace detail
  26. {
  27. template <std::size_t nkeywords>
  28. struct keywords_base
  29. {
  30. BOOST_STATIC_CONSTANT(std::size_t, size = nkeywords);
  31. keyword_range range() const
  32. {
  33. return keyword_range(elements, elements + nkeywords);
  34. }
  35. keyword elements[nkeywords];
  36. keywords<nkeywords+1>
  37. operator,(python::arg const &k) const;
  38. keywords<nkeywords + 1>
  39. operator,(char const *name) const;
  40. };
  41. template <std::size_t nkeywords>
  42. struct keywords : keywords_base<nkeywords>
  43. {
  44. };
  45. template <>
  46. struct keywords<1> : keywords_base<1>
  47. {
  48. explicit keywords(char const *name)
  49. {
  50. elements[0].name = name;
  51. }
  52. template <class T>
  53. python::arg& operator=(T const& value)
  54. {
  55. object z(value);
  56. elements[0].default_value = handle<>(python::borrowed(object(value).ptr()));
  57. return *this;
  58. }
  59. operator detail::keyword const&() const
  60. {
  61. return elements[0];
  62. }
  63. };
  64. template <std::size_t nkeywords>
  65. inline
  66. keywords<nkeywords+1>
  67. keywords_base<nkeywords>::operator,(python::arg const &k) const
  68. {
  69. keywords<nkeywords> const& l = *static_cast<keywords<nkeywords> const*>(this);
  70. python::detail::keywords<nkeywords+1> res;
  71. std::copy(l.elements, l.elements+nkeywords, res.elements);
  72. res.elements[nkeywords] = k.elements[0];
  73. return res;
  74. }
  75. template <std::size_t nkeywords>
  76. inline
  77. keywords<nkeywords + 1>
  78. keywords_base<nkeywords>::operator,(char const *name) const
  79. {
  80. return this->operator,(python::arg(name));
  81. }
  82. template<typename T>
  83. struct is_keywords
  84. {
  85. BOOST_STATIC_CONSTANT(bool, value = false);
  86. };
  87. template<std::size_t nkeywords>
  88. struct is_keywords<keywords<nkeywords> >
  89. {
  90. BOOST_STATIC_CONSTANT(bool, value = true);
  91. };
  92. template <class T>
  93. struct is_reference_to_keywords
  94. {
  95. BOOST_STATIC_CONSTANT(bool, is_ref = detail::is_reference<T>::value);
  96. typedef typename detail::remove_reference<T>::type deref;
  97. typedef typename detail::remove_cv<deref>::type key_t;
  98. BOOST_STATIC_CONSTANT(bool, is_key = is_keywords<key_t>::value);
  99. BOOST_STATIC_CONSTANT(bool, value = (is_ref & is_key));
  100. typedef mpl::bool_<value> type;
  101. BOOST_PYTHON_MPL_LAMBDA_SUPPORT(1,is_reference_to_keywords,(T))
  102. };
  103. }
  104. inline detail::keywords<1> args(char const* name)
  105. {
  106. return detail::keywords<1>(name);
  107. }
  108. # define BOOST_PYTHON_ASSIGN_NAME(z, n, _) result.elements[n].name = name##n;
  109. # define BOOST_PP_LOCAL_MACRO(n) \
  110. inline detail::keywords<n> args(BOOST_PP_ENUM_PARAMS_Z(1, n, char const* name)) \
  111. { \
  112. detail::keywords<n> result; \
  113. BOOST_PP_REPEAT_1(n, BOOST_PYTHON_ASSIGN_NAME, _) \
  114. return result; \
  115. }
  116. # define BOOST_PP_LOCAL_LIMITS (2, BOOST_PYTHON_MAX_ARITY)
  117. # include BOOST_PP_LOCAL_ITERATE()
  118. }} // namespace boost::python
  119. # endif // KEYWORDS_DWA2002323_HPP