knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
with.h
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #ifndef KNOWROB_PY_WITH_H
7 #define KNOWROB_PY_WITH_H
8 
9 #include <boost/python.hpp>
10 #include <boost/function.hpp>
11 #include <boost/function_types/components.hpp>
12 #include <boost/function_types/function_type.hpp>
13 #include <boost/function_types/result_type.hpp>
14 
15 namespace knowrob::py {
20  template<typename Signature, typename Guard>
21  class guarded_function {
22  public:
24 
25  template<typename Fn>
26  explicit guarded_function(Fn fn)
27  : fn_(fn) {}
28 
30  Guard g;
31  return fn_();
32  }
33 
34  template<typename A1>
35  result_type operator()(const A1 &a1) {
36  Guard g;
37  return fn_(a1);
38  }
39 
40  template<typename A1, typename A2>
41  result_type operator()(const A1 &a1, const A2 &a2) {
42  Guard g;
43  return fn_(a1, a2);
44  }
45 
46  template<typename A1, typename A2, typename A3>
47  result_type operator()(const A1 &a1, const A2 &a2, const A3 &a3) {
48  Guard g;
49  return fn_(a1, a2, a3);
50  }
51 
52  private:
53  boost::function<Signature> fn_;
54  };
55 
57  template<typename Signature>
58  struct mpl_signature {
59  typedef typename boost::function_types::components<Signature,
60  boost::add_pointer<boost::mpl::placeholders::_> >::type type;
61  };
62 
64  template<typename Signature>
65  struct mpl_signature<boost::function<Signature> > {
67  };
68 
70  template<typename Guard, typename Fn, typename Policy>
71  boost::python::object with_aux(Fn fn, const Policy &policy) {
72  // Obtain the components of the Fn. This will decompose non-member
73  // and member functions into an mpl sequence.
74  // R (*)(A1) => R, A1
75  // R (C::*)(A1) => R, C*, A1
76  typedef typename mpl_signature<Fn>::type mpl_signature_type;
77 
78  // Synthesize the components into a function type. This process
79  // causes member functions to require the instance argument.
80  // This is necessary because member functions will be explicitly
81  // provided the 'self' argument.
82  // R, A1 => R (*)(A1)
83  // R, C*, A1 => R (*)(C*, A1)
84  typedef typename boost::function_types::function_type<
85  mpl_signature_type>::type signature_type;
86 
87  // Create a callable boost::python::object that delegates to the
88  // guarded_function.
89  return boost::python::make_function(
91  policy, mpl_signature_type());
92  }
93 
95  template<typename Guard, typename Fn, typename Policy>
96  boost::python::object with(const Fn &fn, const Policy &policy) {
97  return with_aux<Guard>(fn, policy);
98  }
99 
101  template<typename Guard, typename Fn>
102  boost::python::object with(const Fn &fn) {
103  return with<Guard>(fn, boost::python::default_call_policies());
104  }
105 }
106 
107 #endif //KNOWROB_PY_WITH_H
result_type operator()(const A1 &a1)
Definition: with.h:35
result_type operator()(const A1 &a1, const A2 &a2, const A3 &a3)
Definition: with.h:47
result_type operator()(const A1 &a1, const A2 &a2)
Definition: with.h:41
result_type operator()()
Definition: with.h:29
boost::function_types::result_type< Signature >::type result_type
Definition: with.h:23
FunctionRule & function()
Definition: terms.cpp:140
boost::python::object with_aux(Fn fn, const Policy &policy)
Definition: with.h:71
boost::python::object with(const Fn &fn, const Policy &policy)
Definition: with.h:96
const IRIAtomPtr type
Definition: rdf.h:15
boost::function_types::components< Signature >::type type
Definition: with.h:66
boost::function_types::components< Signature, boost::add_pointer< boost::mpl::placeholders::_ > >::type type
Definition: with.h:60