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
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
27 hash += (uint64_t)Py_TYPE(pyobj);
28#if PY_VERSION_HEX >= 0x030e0000
30#else
31 hash += (uint64_t)(Py_REFCNT(pyobj) == 1 ? 1 : 0);
32#endif
33 hash += (hash << 10); hash ^= (hash >> 6);
34 }
35
36 hash += (hash << 3); hash ^= (hash >> 11); hash += (hash << 15);
37
38 return hash;
39}
40
42public:
43 typedef std::vector<std::pair<uint64_t, PyCallable*>> DispatchMap_t;
44 typedef std::vector<PyCallable*> Methods_t;
45
63
64public:
65 void Set(const std::string& name, std::vector<PyCallable*>& methods);
66 void AdoptMethod(PyCallable* pc);
68
69 const std::string& GetName() const { return fMethodInfo->fName; }
70 bool HasMethods() const { return !fMethodInfo->fMethods.empty(); }
71
72// find a method based on the provided signature
73 PyObject* FindOverload(const std::string& signature, int want_const = -1);
75
76public: // public, as the python C-API works with C structs
78 CPPInstance* fSelf; // must be first (same layout as TemplateProxy)
80 uint32_t fFlags;
81#if PY_VERSION_HEX >= 0x03080000
83#endif
84
85private:
86 CPPOverload() = delete;
87};
88
89
90//- method proxy type and type verification ----------------------------------
92
93template<typename T>
94inline bool CPPOverload_Check(T* object)
95{
96 return object && PyObject_TypeCheck(object, &CPPOverload_Type);
97}
98
99template<typename T>
100inline bool CPPOverload_CheckExact(T* object)
101{
102 return object && Py_TYPE(object) == &CPPOverload_Type;
103}
104
105//- creation -----------------------------------------------------------------
107 const std::string& name, std::vector<PyCallable*>& methods)
108{
109// Create and initialize a new method proxy from the overloads.
110 CPPOverload* pymeth = (CPPOverload*)CPPOverload_Type.tp_new(&CPPOverload_Type, nullptr, nullptr);
111 pymeth->Set(name, methods);
112 return pymeth;
113}
114
115inline CPPOverload* CPPOverload_New(const std::string& name, PyCallable* method)
116{
117// Create and initialize a new method proxy from the method.
118 std::vector<PyCallable*> p;
119 p.push_back(method);
120 return CPPOverload_New(name, p);
121}
122
123} // namespace CPyCppyy
124
125#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
bool PyUnstable_Object_IsUniqueReferencedTemporary(PyObject *pyobject)
_object PyObject
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
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:79
PyObject * FindOverload(const std::string &signature, int want_const=-1)
PyObject_HEAD CPPInstance * fSelf
Definition CPPOverload.h:78
const std::string & GetName() const
Definition CPPOverload.h:69
std::vector< PyCallable * > Methods_t
Definition CPPOverload.h:44
std::vector< std::pair< uint64_t, PyCallable * > > DispatchMap_t
Definition CPPOverload.h:43
bool HasMethods() const
Definition CPPOverload.h:70
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:94
uint64_t HashSignature(CPyCppyy_PyArgs_t args, size_t nargsf)
Definition CPPOverload.h:17
PyTypeObject CPPOverload_Type
bool CPPOverload_CheckExact(T *object)
CPPOverload::DispatchMap_t fDispatchMap
Definition CPPOverload.h:52
MethodInfo_t(const MethodInfo_t &)=delete
MethodInfo_t & operator=(const MethodInfo_t &)=delete
CPPOverload::Methods_t fMethods
Definition CPPOverload.h:53