ecto
raw_constructor.hpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2011, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 #pragma once
30 
31 #include <Python.h>
32 #include <boost/python.hpp>
33 #include <boost/python/raw_function.hpp>
34 #include <boost/python/detail/api_placeholder.hpp>
35 
36 namespace boost { namespace python {
37 
38 namespace detail {
39 
40  template <class F>
42  {
44  : f(make_constructor(f)) {}
45 
46  PyObject* operator()(PyObject* args, PyObject* keywords)
47  {
48  borrowed_reference_t* ra = borrowed_reference(args);
49  object a(ra);
50  return incref(
51  object(
52  f(
53  object(a[0])
54  , object(a.slice(1, len(a)))
55  , keywords ? dict(borrowed_reference(keywords)) : dict()
56  )
57  ).ptr()
58  );
59  }
60 
61  private:
62  object f;
63  };
64 
65 } // namespace detail
66 
67 template <class F>
68 object raw_constructor(F f, std::size_t min_args = 0)
69 {
70  return detail::make_raw_function(
71  objects::py_function(
73  , mpl::vector2<void, object>()
74  , min_args+1
75  , (std::numeric_limits<unsigned>::max)()
76  )
77  );
78 }
79 
80 }} // namespace boost::python
81 
Definition: std_map_indexing_suite.hpp:20
object raw_constructor(F f, std::size_t min_args=0)
Definition: raw_constructor.hpp:68
object f
Definition: raw_constructor.hpp:62
raw_constructor_dispatcher(F f)
Definition: raw_constructor.hpp:43
PyObject * operator()(PyObject *args, PyObject *keywords)
Definition: raw_constructor.hpp:46
Definition: raw_constructor.hpp:41