File: raw_constructor.hpp

package info (click to toggle)
yade 2025.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 33,308 kB
  • sloc: cpp: 93,298; python: 50,409; sh: 577; makefile: 162
file content (30 lines) | stat: -rw-r--r-- 935 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#pragma once
#include <boost/python/raw_function.hpp>
// many thanks to http://markmail.org/message/s4ksg6nfspw2wxwd
namespace boost {
namespace python {
	namespace detail {
		template <class F> struct raw_constructor_dispatcher {
			raw_constructor_dispatcher(F ff)
			        : f(make_constructor(ff))
			{
			}
			PyObject* operator()(PyObject* args, PyObject* keywords)
			{
				borrowed_reference_t* ra = borrowed_reference(args);
				object                a(ra);
				return incref(
				        object(f(object(a[0]), object(a.slice(1, len(a))), keywords ? dict(borrowed_reference(keywords)) : dict())).ptr());
			}

		private:
			object f;
		};
	}
	template <class F> object raw_constructor(F ff, std::size_t min_args = 0)
	{
		return detail::make_raw_function(objects::py_function(
		        detail::raw_constructor_dispatcher<F>(ff), mpl::vector2<void, object>(), min_args + 1, (std::numeric_limits<unsigned>::max)()));
	}
}
}