Logo ROOT  
Reference Guide
TemplateProxy.h
Go to the documentation of this file.
1#ifndef CPYCPPYY_TEMPLATEPROXY_H
2#define CPYCPPYY_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
15
16namespace CPyCppyy {
17
18class PyCallable;
19class CPPOverload;
20
21/** Template proxy object to return functions and methods
22 */
23
24typedef std::pair<uint64_t, CPPOverload*> TP_DispatchEntry_t;
25typedef std::map<std::string, std::vector<TP_DispatchEntry_t>> TP_DispatchMap_t;
26
28public:
30 TemplateInfo(const TemplateInfo&) = delete;
33
34public:
38 CPPOverload* fNonTemplated; // holder for non-template overloads
39 CPPOverload* fTemplated; // holder for templated overloads
40 CPPOverload* fLowPriority; // low priority overloads such as void*/void**
42
44
45 uint64_t fFlags; // collective for all methods
46};
47
48typedef std::shared_ptr<TemplateInfo> TP_TInfo_t;
49
51private:
53 const std::string& cppname, const std::string& pyname, PyObject* pyclass);
54 void Set(const std::string& cppname, const std::string& pyname, PyObject* pyclass);
55
56public: // public, as the python C-API works with C structs
57 PyObject_HEAD
58 PyObject* fSelf; // must be first (same layout as CPPOverload)
62
63public:
64 void MergeOverload(CPPOverload* mp);
67 PyObject* Instantiate(const std::string& fname,
68 PyObject* tmplArgs, Utility::ArgPreference, int* pcnt = nullptr);
69
70private: // private, as the python C-API will handle creation
71 TemplateProxy() = delete;
72};
73
74
75//- template proxy type and type verification --------------------------------
76extern PyTypeObject TemplateProxy_Type;
77
78template<typename T>
79inline bool TemplateProxy_Check(T* object)
80{
81 return object && PyObject_TypeCheck(object, &TemplateProxy_Type);
82}
83
84template<typename T>
85inline bool TemplateProxy_CheckExact(T* object)
86{
87 return object && Py_TYPE(object) == &TemplateProxy_Type;
88}
89
90//- creation -----------------------------------------------------------------
92 const std::string& cppname, const std::string& pyname, PyObject* pyclass)
93{
94// Create and initialize a new template method proxy for the class.
95 if (!CPPScope_Check(pyclass)) return nullptr;
96
97 TemplateProxy* pytmpl =
98 (TemplateProxy*)TemplateProxy_Type.tp_new(&TemplateProxy_Type, nullptr, nullptr);
99 pytmpl->Set(cppname, pyname, pyclass);
100 return pytmpl;
101}
102
103} // namespace CPyCppyy
104
105#endif // !CPYCPPYY_TEMPLATEPROXY_H
#define Py_TYPE(ob)
Definition: CPyCppyy.h:217
_object PyObject
Definition: PyMethodBase.h:42
#define pyname
Definition: TMCParticle.cxx:19
CPPOverload * fTemplated
Definition: TemplateProxy.h:39
TemplateInfo & operator=(const TemplateInfo &)=delete
TemplateInfo(const TemplateInfo &)=delete
CPPOverload * fLowPriority
Definition: TemplateProxy.h:40
TP_DispatchMap_t fDispatchMap
Definition: TemplateProxy.h:43
CPPOverload * fNonTemplated
Definition: TemplateProxy.h:38
PyObject * Instantiate(const std::string &fname, PyObject *tmplArgs, Utility::ArgPreference, int *pcnt=nullptr)
void Set(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
PyObject_HEAD PyObject * fSelf
Definition: TemplateProxy.h:58
void AdoptTemplate(PyCallable *pc)
friend TemplateProxy * TemplateProxy_New(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
Definition: TemplateProxy.h:91
void AdoptMethod(PyCallable *pc)
void MergeOverload(CPPOverload *mp)
Set of helper functions that are invoked from the pythonizors, on the Python side.
std::pair< uint64_t, CPPOverload * > TP_DispatchEntry_t
Template proxy object to return functions and methods.
Definition: TemplateProxy.h:19
bool CPPScope_Check(T *object)
Definition: CPPScope.h:76
bool TemplateProxy_CheckExact(T *object)
Definition: TemplateProxy.h:85
PyTypeObject TemplateProxy_Type
std::map< std::string, std::vector< TP_DispatchEntry_t > > TP_DispatchMap_t
Definition: TemplateProxy.h:25
bool TemplateProxy_Check(T *object)
Definition: TemplateProxy.h:79
TemplateProxy * TemplateProxy_New(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
Definition: TemplateProxy.h:91
std::shared_ptr< TemplateInfo > TP_TInfo_t
Definition: TemplateProxy.h:48
double T(double x)
Definition: ChebyshevPol.h:34
static constexpr double pc