Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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 <map>
9#include <string>
10#include <utility>
11#include <vector>
12
13
14namespace CPyCppyy {
15
16// signature hashes are also used by TemplateProxy
17inline uint64_t HashSignature(CPyCppyy_PyArgs_t args, size_t nargsf)
18{
19// Build a hash from the types of the given python function arguments.
20 uint64_t hash = 0;
21
22 Py_ssize_t nargs = CPyCppyy_PyArgs_GET_SIZE(args, nargsf);
23 for (Py_ssize_t i = 0; i < nargs; ++i) {
24 // TODO: hashing in the ref-count is for moves; resolve this together with the
25 // improved overloads for implicit conversions
26 PyObject* pyobj = CPyCppyy_PyArgs_GET_ITEM(args, i);
27 hash += (uint64_t)Py_TYPE(pyobj);
28 hash += (uint64_t)(pyobj->ob_refcnt == 1 ? 1 : 0);
29 hash += (hash << 10); hash ^= (hash >> 6);
30 }
31
32 hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15);
33
34 return hash;
35}
36
38public:
39 typedef std::vector<std::pair<uint64_t, PyCallable*>> DispatchMap_t;
40 typedef std::vector<PyCallable*> Methods_t;
41
42 struct MethodInfo_t {
44 { fRefCount = new int(1); }
46
47 std::string fName;
51 uint32_t fFlags;
52
54
55 private:
56 MethodInfo_t(const MethodInfo_t&) = delete;
58 };
59
60public:
61 void Set(const std::string& name, std::vector<PyCallable*>& methods);
62 void AdoptMethod(PyCallable* pc);
63 void MergeOverload(CPPOverload* meth);
64
65 const std::string& GetName() const { return fMethodInfo->fName; }
66 bool HasMethods() const { return !fMethodInfo->fMethods.empty(); }
67
68// find a method based on the provided signature
69 PyObject* FindOverload(const std::string& signature, int want_const = -1);
70 PyObject* FindOverload(PyObject *args_tuple, int want_const = -1);
71
72public: // public, as the python C-API works with C structs
73 PyObject_HEAD
74 CPPInstance* fSelf; // must be first (same layout as TemplateProxy)
76 uint32_t fFlags;
77#if PY_VERSION_HEX >= 0x03080000
78 vectorcallfunc fVectorCall;
79#endif
80
81private:
82 CPPOverload() = delete;
83};
84
85
86//- method proxy type and type verification ----------------------------------
88
89template<typename T>
90inline bool CPPOverload_Check(T* object)
91{
92 return object && PyObject_TypeCheck(object, &CPPOverload_Type);
93}
94
95template<typename T>
96inline bool CPPOverload_CheckExact(T* object)
97{
98 return object && Py_TYPE(object) == &CPPOverload_Type;
99}
100
101//- creation -----------------------------------------------------------------
103 const std::string& name, std::vector<PyCallable*>& methods)
104{
105// Create and initialize a new method proxy from the overloads.
106 CPPOverload* pymeth = (CPPOverload*)CPPOverload_Type.tp_new(&CPPOverload_Type, nullptr, nullptr);
107 pymeth->Set(name, methods);
108 return pymeth;
109}
110
111inline CPPOverload* CPPOverload_New(const std::string& name, PyCallable* method)
112{
113// Create and initialize a new method proxy from the method.
114 std::vector<PyCallable*> p;
115 p.push_back(method);
116 return CPPOverload_New(name, p);
117}
118
119} // namespace CPyCppyy
120
121#endif // !CPYCPPYY_CPPOVERLOAD_H
#define Py_TYPE(ob)
Definition CPyCppyy.h:196
int Py_ssize_t
Definition CPyCppyy.h:215
static Py_ssize_t CPyCppyy_PyArgs_GET_SIZE(CPyCppyy_PyArgs_t args, size_t)
Definition CPyCppyy.h:337
PyObject * CPyCppyy_PyArgs_t
Definition CPyCppyy.h:330
static PyObject * CPyCppyy_PyArgs_GET_ITEM(CPyCppyy_PyArgs_t args, Py_ssize_t i)
Definition CPyCppyy.h:331
_object PyObject
winID h TVirtualViewer3D TVirtualGLPainter p
char name[80]
Definition TGX11.cxx:110
#define CPYCPPYY_IMPORT
Definition CommonDefs.h:26
void MergeOverload(CPPOverload *meth)
void AdoptMethod(PyCallable *pc)
MethodInfo_t * fMethodInfo
Definition CPPOverload.h:75
PyObject * FindOverload(const std::string &signature, int want_const=-1)
PyObject_HEAD CPPInstance * fSelf
Definition CPPOverload.h:74
const std::string & GetName() const
Definition CPPOverload.h:65
std::vector< PyCallable * > Methods_t
Definition CPPOverload.h:40
std::vector< std::pair< uint64_t, PyCallable * > > DispatchMap_t
Definition CPPOverload.h:39
bool HasMethods() const
Definition CPPOverload.h:66
void Set(const std::string &name, std::vector< PyCallable * > &methods)
CPPOverload * CPPOverload_New(const std::string &name, std::vector< PyCallable * > &methods)
bool CPPOverload_Check(T *object)
Definition CPPOverload.h:90
uint64_t HashSignature(CPyCppyy_PyArgs_t args, size_t nargsf)
Definition CPPOverload.h:17
PyTypeObject CPPOverload_Type
bool CPPOverload_CheckExact(T *object)
Definition CPPOverload.h:96
CPPOverload::DispatchMap_t fDispatchMap
Definition CPPOverload.h:48
MethodInfo_t(const MethodInfo_t &)=delete
MethodInfo_t & operator=(const MethodInfo_t &)=delete
CPPOverload::Methods_t fMethods
Definition CPPOverload.h:49