Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TemplateProxy.h
Go to the documentation of this file.
1#ifndef CPYRT_TEMPLATEPROXY_H
2#define CPYRT_TEMPLATEPROXY_H
3
4// Bindings
5#include "CPPScope.h"
6#include "Utility.h"
7
8// Standard
9#include <map>
10#include <memory>
11#include <string>
12#include <utility>
13#include <vector>
14
15namespace cppjit::cpyrt {
16
17class PyCallable;
18class CPPOverload;
19
20/** Template proxy object to return functions and methods
21 */
22
23typedef std::pair<uint64_t, CPPOverload*> TP_DispatchEntry_t;
24typedef std::unordered_map<std::string, std::vector<TP_DispatchEntry_t>>
26
28public:
30 TemplateInfo(const TemplateInfo&) = delete;
33
34public:
35 std::string fCppName;
37 CPPOverload* fNonTemplated; // holder for non-template overloads
38 CPPOverload* fTemplated; // holder for templated overloads
39 CPPOverload* fLowPriority; // low priority overloads such as void*/void**
40
43};
44
45typedef std::shared_ptr<TemplateInfo> TP_TInfo_t;
46
48private:
49 friend TemplateProxy* TemplateProxy_New(const std::string& cppname,
50 const std::string& pyname,
51 PyObject* pyclass);
52 void Set(const std::string& cppname, const std::string& pyname,
53 PyObject* pyclass);
54
55public: // public, as the python C-API works with C structs
56 PyObject_HEAD PyObject* fSelf; // must be first (same layout as CPPOverload)
59 vectorcallfunc fVectorCall;
61
62public:
63 void MergeOverload(CPPOverload* mp);
64 void AdoptMethod(PyCallable* pc);
65 void AdoptTemplate(PyCallable* pc);
66 PyObject* Instantiate(const std::string& fname, cpyrt_PyArgs_t tmplArgs,
67 size_t nargsf, Utility::ArgPreference,
68 int* pcnt = nullptr);
69
70private: // private, as the python C-API will handle creation
71 TemplateProxy() = delete;
72};
73
74//- template proxy type and type verification --------------------------------
75extern PyTypeObject TemplateProxy_Type;
76
77template <typename T> inline bool TemplateProxy_Check(T* object) {
78 return object && PyObject_TypeCheck(object, &TemplateProxy_Type);
79}
80
81template <typename T> inline bool TemplateProxy_CheckExact(T* object) {
82 return object && Py_TYPE(object) == &TemplateProxy_Type;
83}
84
85//- creation -----------------------------------------------------------------
86inline TemplateProxy* TemplateProxy_New(const std::string& cppname,
87 const std::string& pyname,
88 PyObject* pyclass) {
89 // Create and initialize a new template method proxy for the class.
90 if (!CPPScope_Check(pyclass))
91 return nullptr;
92
94 &TemplateProxy_Type, nullptr, nullptr);
95 pytmpl->Set(cppname, pyname, pyclass);
96 return pytmpl;
97}
98
99} // namespace cppjit::cpyrt
100
101#endif // !CPYRT_TEMPLATEPROXY_H
_object PyObject
TemplateInfo & operator=(const TemplateInfo &)=delete
TemplateInfo(const TemplateInfo &)=delete
TP_DispatchMap_t fDispatchMap
void AdoptTemplate(PyCallable *pc)
void Set(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
PyObject * Instantiate(const std::string &fname, cpyrt_PyArgs_t tmplArgs, size_t nargsf, Utility::ArgPreference, int *pcnt=nullptr)
void AdoptMethod(PyCallable *pc)
PyObject_HEAD PyObject * fSelf
void MergeOverload(CPPOverload *mp)
friend TemplateProxy * TemplateProxy_New(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
PyObject *const * cpyrt_PyArgs_t
Definition cpyrt.h:81
bool TemplateProxy_Check(T *object)
TemplateProxy * TemplateProxy_New(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
PyTypeObject TemplateProxy_Type
bool TemplateProxy_CheckExact(T *object)
std::shared_ptr< TemplateInfo > TP_TInfo_t
bool CPPScope_Check(T *object)
Definition CPPScope.h:86
std::unordered_map< std::string, std::vector< TP_DispatchEntry_t > > TP_DispatchMap_t
std::pair< uint64_t, CPPOverload * > TP_DispatchEntry_t
Template proxy object to return functions and methods.