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 <string>
9#include <utility>
10#include <vector>
11
12
13namespace CPyCppyy {
14
15// signature hashes are also used by TemplateProxy
16inline 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
37public:
38 typedef std::vector<std::pair<uint64_t, PyCallable*>> DispatchMap_t;
39 typedef std::vector<PyCallable*> Methods_t;
40
41 struct MethodInfo_t {
44
45 std::string fName;
48 uint64_t fFlags;
49
51
52 private:
53 MethodInfo_t(const MethodInfo_t&) = delete;
55 };
56
57public:
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
65public: // public, as the python C-API works with C structs
66 PyObject_HEAD
67 CPPInstance* fSelf; // must be first (same layout as TemplateProxy)
69
70private:
71 CPPOverload() = delete;
72};
73
74
75//- method proxy type and type verification ----------------------------------
77
78template<typename T>
79inline bool CPPOverload_Check(T* object)
80{
81 return object && PyObject_TypeCheck(object, &CPPOverload_Type);
82}
83
84template<typename T>
85inline 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
100inline 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
#define Py_TYPE(ob)
Definition CPyCppyy.h:217
_object PyObject
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:68
PyObject_HEAD CPPInstance * fSelf
Definition CPPOverload.h:67
const std::string & GetName() const
Definition CPPOverload.h:62
std::vector< PyCallable * > Methods_t
Definition CPPOverload.h:39
std::vector< std::pair< uint64_t, PyCallable * > > DispatchMap_t
Definition CPPOverload.h:38
bool HasMethods() const
Definition CPPOverload.h:63
void Set(const std::string &name, std::vector< PyCallable * > &methods)
Set of helper functions that are invoked from the pythonizors, on the Python side.
CPPOverload * CPPOverload_New(const std::string &name, std::vector< PyCallable * > &methods)
Definition CPPOverload.h:91
bool CPPOverload_Check(T *object)
Definition CPPOverload.h:79
PyTypeObject CPPOverload_Type
uint64_t HashSignature(PyObject *args)
Definition CPPOverload.h:16
bool CPPOverload_CheckExact(T *object)
Definition CPPOverload.h:85
CPPOverload::DispatchMap_t fDispatchMap
Definition CPPOverload.h:46
MethodInfo_t(const MethodInfo_t &)=delete
MethodInfo_t & operator=(const MethodInfo_t &)=delete
CPPOverload::Methods_t fMethods
Definition CPPOverload.h:47