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
65// find a method based on the provided signature
66 PyObject* FindOverload(const std::string& signature, int want_const = -1);
67 PyObject* FindOverload(PyObject *args_tuple, int want_const = -1);
68
69public: // public, as the python C-API works with C structs
70 PyObject_HEAD
71 CPPInstance* fSelf; // must be first (same layout as TemplateProxy)
73
74private:
75 CPPOverload() = delete;
76};
77
78
79//- method proxy type and type verification ----------------------------------
81
82template<typename T>
83inline bool CPPOverload_Check(T* object)
84{
85 return object && PyObject_TypeCheck(object, &CPPOverload_Type);
86}
87
88template<typename T>
89inline bool CPPOverload_CheckExact(T* object)
90{
91 return object && Py_TYPE(object) == &CPPOverload_Type;
92}
93
94//- creation -----------------------------------------------------------------
96 const std::string& name, std::vector<PyCallable*>& methods)
97{
98// Create and initialize a new method proxy from the overloads.
99 CPPOverload* pymeth = (CPPOverload*)CPPOverload_Type.tp_new(&CPPOverload_Type, nullptr, nullptr);
100 pymeth->Set(name, methods);
101 return pymeth;
102}
103
104inline CPPOverload* CPPOverload_New(const std::string& name, PyCallable* method)
105{
106// Create and initialize a new method proxy from the method.
107 std::vector<PyCallable*> p;
108 p.push_back(method);
109 return CPPOverload_New(name, p);
110}
111
112} // namespace CPyCppyy
113
114#endif // !CPYCPPYY_CPPOVERLOAD_H
#define Py_TYPE(ob)
Definition CPyCppyy.h:217
_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:72
PyObject * FindOverload(const std::string &signature, int want_const=-1)
PyObject_HEAD CPPInstance * fSelf
Definition CPPOverload.h:71
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)
CPPOverload * CPPOverload_New(const std::string &name, std::vector< PyCallable * > &methods)
Definition CPPOverload.h:95
bool CPPOverload_Check(T *object)
Definition CPPOverload.h:83
PyTypeObject CPPOverload_Type
uint64_t HashSignature(PyObject *args)
Definition CPPOverload.h:16
bool CPPOverload_CheckExact(T *object)
Definition CPPOverload.h:89
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