Logo ROOT  
Reference Guide
CPPOverload.h
Go to the documentation of this file.
1 #ifndef CPYCPPYY_CPPOVERLOAD_H
2 #define CPYCPPYY_CPPOVERLOAD_H
3 
4 // Bindings
5 #include "PyCallable.h"
6 
7 // Standard
8 #include <string>
9 #include <utility>
10 #include <vector>
11 
12 
13 namespace CPyCppyy {
14 
15 // signature hashes are also used by TemplateProxy
16 inline uint64_t HashSignature(PyObject* args)
17 {
18 // Build a hash from the types of the given python function arguments.
19  uint64_t hash = 0;
20 
21  int nargs = (int)PyTuple_GET_SIZE(args);
22  for (int i = 0; i < nargs; ++i) {
23  // TODO: hashing in the ref-count is for moves; resolve this together with the
24  // improved overloads for implicit conversions
25  PyObject* pyobj = PyTuple_GET_ITEM(args, i);
26  hash += (uint64_t)Py_TYPE(pyobj);
27  hash += (uint64_t)(pyobj->ob_refcnt == 1 ? 1 : 0);
28  hash += (hash << 10); hash ^= (hash >> 6);
29  }
30 
31  hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15);
32 
33  return hash;
34 }
35 
36 class CPPOverload {
37 public:
38  typedef std::vector<std::pair<uint64_t, PyCallable*>> DispatchMap_t;
39  typedef std::vector<PyCallable*> Methods_t;
40 
41  struct MethodInfo_t {
43  ~MethodInfo_t();
44 
45  std::string fName;
48  uint64_t fFlags;
49 
50  int* fRefCount;
51 
52  private:
53  MethodInfo_t(const MethodInfo_t&) = delete;
54  MethodInfo_t& operator=(const MethodInfo_t&) = delete;
55  };
56 
57 public:
58  void Set(const std::string& name, std::vector<PyCallable*>& methods);
59  void AdoptMethod(PyCallable* pc);
60  void MergeOverload(CPPOverload* meth);
61 
62  const std::string& GetName() const { return fMethodInfo->fName; }
63  bool HasMethods() const { return !fMethodInfo->fMethods.empty(); }
64 
65 public: // public, as the python C-API works with C structs
66  PyObject_HEAD
67  CPPInstance* fSelf; // must be first (same layout as TemplateProxy)
69 
70 private:
71  CPPOverload() = delete;
72 };
73 
74 
75 //- method proxy type and type verification ----------------------------------
77 
78 template<typename T>
79 inline bool CPPOverload_Check(T* object)
80 {
81  return object && PyObject_TypeCheck(object, &CPPOverload_Type);
82 }
83 
84 template<typename T>
85 inline bool CPPOverload_CheckExact(T* object)
86 {
87  return object && Py_TYPE(object) == &CPPOverload_Type;
88 }
89 
90 //- creation -----------------------------------------------------------------
92  const std::string& name, std::vector<PyCallable*>& methods)
93 {
94 // Create and initialize a new method proxy from the overloads.
95  CPPOverload* pymeth = (CPPOverload*)CPPOverload_Type.tp_new(&CPPOverload_Type, nullptr, nullptr);
96  pymeth->Set(name, methods);
97  return pymeth;
98 }
99 
100 inline CPPOverload* CPPOverload_New(const std::string& name, PyCallable* method)
101 {
102 // Create and initialize a new method proxy from the method.
103  std::vector<PyCallable*> p;
104  p.push_back(method);
105  return CPPOverload_New(name, p);
106 }
107 
108 } // namespace CPyCppyy
109 
110 #endif // !CPYCPPYY_CPPOVERLOAD_H
CPyCppyy
Definition: TPython.cxx:99
PyCallable.h
CPyCppyy::CPPOverload_Type
PyTypeObject CPPOverload_Type
Definition: CPPOverload.cxx:863
CPyCppyy::CPPOverload::MethodInfo_t::MethodInfo_t
MethodInfo_t(const MethodInfo_t &)=delete
CPyCppyy::CPPOverload::MethodInfo_t::~MethodInfo_t
~MethodInfo_t()
Definition: CPPOverload.cxx:962
CPyCppyy::CallContext
Definition: CallContext.h:45
CPyCppyy::HashSignature
uint64_t HashSignature(PyObject *args)
Definition: CPPOverload.h:16
CPyCppyy::CPPOverload::HasMethods
bool HasMethods() const
Definition: CPPOverload.h:63
PyObject
_object PyObject
Definition: PyMethodBase.h:41
CPyCppyy::CPPOverload::Set
void Set(const std::string &name, std::vector< PyCallable * > &methods)
Definition: CPPOverload.cxx:924
CPyCppyy::CPPOverload::CPPOverload
CPPOverload()=delete
CPYCPPYY_IMPORT
#define CPYCPPYY_IMPORT
Definition: CommonDefs.h:26
CPyCppyy::CPPInstance
Definition: CPPInstance.h:26
CPyCppyy::CPPOverload::MergeOverload
void MergeOverload(CPPOverload *meth)
Definition: CPPOverload.cxx:950
CPyCppyy::PyCallable
Definition: PyCallable.h:12
TGeant4Unit::pc
static constexpr double pc
Definition: TGeant4SystemOfUnits.h:130
CPyCppyy::CPPOverload::DispatchMap_t
std::vector< std::pair< uint64_t, PyCallable * > > DispatchMap_t
Definition: CPPOverload.h:38
CPyCppyy::CPPOverload::fSelf
PyObject_HEAD CPPInstance * fSelf
Definition: CPPOverload.h:67
CPyCppyy::CPPOverload_New
CPPOverload * CPPOverload_New(const std::string &name, std::vector< PyCallable * > &methods)
Definition: CPPOverload.h:91
CPyCppyy::kNone
@ kNone
Definition: CPPDataMember.cxx:19
CPyCppyy::CPPOverload::GetName
const std::string & GetName() const
Definition: CPPOverload.h:62
CPyCppyy::CPPOverload_Check
bool CPPOverload_Check(T *object)
Definition: CPPOverload.h:79
CPyCppyy::CPPOverload::MethodInfo_t::fRefCount
int * fRefCount
Definition: CPPOverload.h:50
CPyCppyy::CPPOverload_CheckExact
bool CPPOverload_CheckExact(T *object)
Definition: CPPOverload.h:85
CPyCppyy::CPPOverload::MethodInfo_t::operator=
MethodInfo_t & operator=(const MethodInfo_t &)=delete
CPyCppyy::CPPOverload::MethodInfo_t::fDispatchMap
CPPOverload::DispatchMap_t fDispatchMap
Definition: CPPOverload.h:46
CPyCppyy::CPPOverload::AdoptMethod
void AdoptMethod(PyCallable *pc)
Definition: CPPOverload.cxx:942
CPyCppyy::CPPOverload::Methods_t
std::vector< PyCallable * > Methods_t
Definition: CPPOverload.h:39
CPyCppyy::CPPOverload::MethodInfo_t::MethodInfo_t
MethodInfo_t()
Definition: CPPOverload.h:42
CPyCppyy::CPPOverload::MethodInfo_t::fName
std::string fName
Definition: CPPOverload.h:45
name
char name[80]
Definition: TGX11.cxx:109
CPyCppyy::CPPOverload::MethodInfo_t::fFlags
uint64_t fFlags
Definition: CPPOverload.h:48
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
CPyCppyy::CPPOverload::MethodInfo_t
Definition: CPPOverload.h:41
CPyCppyy::CPPOverload
Definition: CPPOverload.h:36
Py_TYPE
#define Py_TYPE(ob)
Definition: CPyCppyy.h:209
CPyCppyy::CPPOverload::MethodInfo_t::fMethods
CPPOverload::Methods_t fMethods
Definition: CPPOverload.h:47
CPyCppyy::CPPOverload::fMethodInfo
MethodInfo_t * fMethodInfo
Definition: CPPOverload.h:68
int