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