예제 #1
0
 // Extract an object x of the Target type from the first Python
 // argument, and invoke get_start(x)/get_finish(x) to produce
 // iterators, which are used to construct a new iterator_range<>
 // object that gets wrapped into a Python iterator.
 iterator_range<NextPolicies,Iterator>
 operator()(back_reference<Target&> x) const
 {
     // Make sure the Python class is instantiated.
     detail::demand_iterator_class("iterator", (Iterator*)0, NextPolicies());
     
     return iterator_range<NextPolicies,Iterator>(
         x.source()
       , m_get_start(x.get())
       , m_get_finish(x.get())
     );
 }
예제 #2
0
      // Extract an object x of the Target type from the first Python
      // argument, and invoke get_start(x)/get_finish(x) to produce
      // iterators, which are used to construct a new iterator_range<>
      // object that gets wrapped into a Python iterator.
      static PyObject* create(
          Accessor1 const& get_start, Accessor2 const& get_finish
          , PyObject* args_, PyObject* /*kw*/)
      {
          // Make sure the Python class is instantiated.
          detail::demand_iterator_class("iterator", (Iterator*)0, NextPolicies());

          to_python_value<iterator_range<NextPolicies,Iterator> > cr;

          // Extract x from the first argument
          PyObject* arg0 = PyTuple_GET_ITEM(args_, 0);
          arg_from_python<Target> c0(arg0);
          if (!c0.convertible()) return 0;
          typename arg_from_python<Target>::result_type x = c0(arg0);

          // Build and convert the iterator_range<>.
          return cr(
              iterator_range<NextPolicies,Iterator>(
                  object((python::detail::borrowed_reference)arg0)
                  , get_start(x), get_finish(x)));
      }
예제 #3
0
  object demand_iterator_class(char const* name, Iterator* = 0, NextPolicies const& policies = NextPolicies())
  {
      typedef iterator_range<NextPolicies,Iterator> range_;

      // Check the registry. If one is already registered, return it.
      handle<> class_obj(
          objects::registered_class_object(python::type_id<range_>()));
        
      if (class_obj.get() != 0)
          return object(class_obj);

      typedef typename range_::next_fn next_fn;
      typedef typename next_fn::result_type result_type;
      
      return class_<range_>(name, no_init)
          .def("__iter__", identity_function())
          .def(
#if PY_VERSION_HEX >= 0x03000000
              "__next__"
#else
              "next"
#endif
            , make_function(
                next_fn()
              , policies
              , mpl::vector2<result_type,range_&>()
            ));
  }
예제 #4
0
  object demand_iterator_class(char const* name, Iterator* = 0, NextPolicies const& policies = NextPolicies())
  {
      typedef iterator_range<NextPolicies,Iterator> range_;

      // Check the registry. If one is already registered, return it.
      handle<> class_obj(
          objects::registered_class_object(python::type_id<range_>()));
        
      if (class_obj.get() != 0)
          return object(class_obj);

      // Make a callable object which can be used as the iterator's next() function.
      object next_function = 
          objects::function_object(
                  bind(&detail::iterator_next<Iterator,NextPolicies>::execute, _1, _2, policies)
              , 1);
    
      return class_<range_>(name, no_init)
          .def("__iter__", identity_function())
          .setattr("next", next_function)
          ;
  }