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 
16 namespace CPyCppyy {
17 
18 class PyCallable;
19 class CPPOverload;
20 
21 /** Template proxy object to return functions and methods
22  */
23 
24 typedef std::pair<uint64_t, CPPOverload*> TP_DispatchEntry_t;
25 typedef std::map<std::string, std::vector<TP_DispatchEntry_t>> TP_DispatchMap_t;
26 
27 class TemplateInfo {
28 public:
29  TemplateInfo();
30  TemplateInfo(const TemplateInfo&) = delete;
31  TemplateInfo& operator=(const TemplateInfo&) = delete;
32  ~TemplateInfo();
33 
34 public:
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 
48 typedef std::shared_ptr<TemplateInfo> TP_TInfo_t;
49 
51 private:
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 
56 public: // public, as the python C-API works with C structs
57  PyObject_HEAD
58  PyObject* fSelf; // must be first (same layout as CPPOverload)
62 
63 public:
64  void MergeOverload(CPPOverload* mp);
65  void AdoptMethod(PyCallable* pc);
67  PyObject* Instantiate(const std::string& fname,
68  PyObject* tmplArgs, Utility::ArgPreference, int* pcnt = nullptr);
69 
70 private: // private, as the python C-API will handle creation
71  TemplateProxy() = delete;
72 };
73 
74 
75 //- template proxy type and type verification --------------------------------
76 extern PyTypeObject TemplateProxy_Type;
77 
78 template<typename T>
79 inline bool TemplateProxy_Check(T* object)
80 {
81  return object && PyObject_TypeCheck(object, &TemplateProxy_Type);
82 }
83 
84 template<typename T>
85 inline 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
CPyCppyy
Definition: TPython.cxx:99
CPyCppyy::TP_DispatchEntry_t
std::pair< uint64_t, CPPOverload * > TP_DispatchEntry_t
Template proxy object to return functions and methods.
Definition: TemplateProxy.h:19
CPyCppyy::TemplateProxy_Type
PyTypeObject TemplateProxy_Type
Definition: TemplateProxy.cxx:727
CPyCppyy::TemplateProxy::TemplateProxy
TemplateProxy()=delete
PyObject
_object PyObject
Definition: PyMethodBase.h:41
CPyCppyy::Utility::ArgPreference
ArgPreference
Definition: Utility.h:36
CPyCppyy::TemplateProxy::fWeakrefList
PyObject * fWeakrefList
Definition: TemplateProxy.h:60
Utility.h
pyname
#define pyname
Definition: TMCParticle.cxx:19
CPyCppyy::TemplateInfo::fLowPriority
CPPOverload * fLowPriority
Definition: TemplateProxy.h:40
CPyCppyy::PyCallable
Definition: PyCallable.h:12
TGeant4Unit::pc
static constexpr double pc
Definition: TGeant4SystemOfUnits.h:130
CPyCppyy::TemplateInfo::fDispatchMap
TP_DispatchMap_t fDispatchMap
Definition: TemplateProxy.h:43
CPyCppyy::TemplateInfo::TemplateInfo
TemplateInfo()
Definition: TemplateProxy.cxx:51
CPyCppyy::TemplateInfo::fFlags
uint64_t fFlags
Definition: TemplateProxy.h:45
CPyCppyy::TemplateProxy::MergeOverload
void MergeOverload(CPPOverload *mp)
Definition: TemplateProxy.cxx:94
CPyCppyy::TemplateInfo::operator=
TemplateInfo & operator=(const TemplateInfo &)=delete
CPyCppyy::TemplateProxy_CheckExact
bool TemplateProxy_CheckExact(T *object)
Definition: TemplateProxy.h:85
CPyCppyy::TP_TInfo_t
std::shared_ptr< TemplateInfo > TP_TInfo_t
Definition: TemplateProxy.h:48
CPyCppyy::TemplateInfo::fPyClass
PyObject * fPyClass
Definition: TemplateProxy.h:37
CPyCppyy::TemplateProxy
Definition: TemplateProxy.h:50
CPyCppyy::TemplateProxy::AdoptTemplate
void AdoptTemplate(PyCallable *pc)
Definition: TemplateProxy.cxx:114
CPyCppyy::CPPScope_Check
bool CPPScope_Check(T *object)
Definition: CPPScope.h:76
CPyCppyy::TemplateProxy::Set
void Set(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
Definition: TemplateProxy.cxx:76
CPyCppyy::TemplateProxy_New
TemplateProxy * TemplateProxy_New(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
Definition: TemplateProxy.h:91
CPyCppyy::TemplateProxy::TemplateProxy_New
friend TemplateProxy * TemplateProxy_New(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
Definition: TemplateProxy.h:91
CPyCppyy::TemplateProxy::AdoptMethod
void AdoptMethod(PyCallable *pc)
Definition: TemplateProxy.cxx:108
CPyCppyy::TemplateInfo::fCppName
PyObject * fCppName
Definition: TemplateProxy.h:35
CPyCppyy::TP_DispatchMap_t
std::map< std::string, std::vector< TP_DispatchEntry_t > > TP_DispatchMap_t
Definition: TemplateProxy.h:25
CPPScope.h
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
CPyCppyy::TemplateInfo::~TemplateInfo
~TemplateInfo()
Definition: TemplateProxy.cxx:58
CPyCppyy::TemplateProxy::fTI
TP_TInfo_t fTI
Definition: TemplateProxy.h:61
CPyCppyy::TemplateInfo
Definition: TemplateProxy.h:27
CPyCppyy::CPPOverload
Definition: CPPOverload.h:36
CPyCppyy::TemplateProxy::fTemplateArgs
PyObject * fTemplateArgs
Definition: TemplateProxy.h:59
CPyCppyy::TemplateInfo::fWeakrefList
PyObject * fWeakrefList
Definition: TemplateProxy.h:41
Py_TYPE
#define Py_TYPE(ob)
Definition: CPyCppyy.h:209
CPyCppyy::TemplateProxy::Instantiate
PyObject * Instantiate(const std::string &fname, PyObject *tmplArgs, Utility::ArgPreference, int *pcnt=nullptr)
Definition: TemplateProxy.cxx:121
CPyCppyy::TemplateProxy_Check
bool TemplateProxy_Check(T *object)
Definition: TemplateProxy.h:79
CPyCppyy::TemplateProxy::fSelf
PyObject_HEAD PyObject * fSelf
Definition: TemplateProxy.h:58
CPyCppyy::TemplateInfo::fNonTemplated
CPPOverload * fNonTemplated
Definition: TemplateProxy.h:38
CPyCppyy::TemplateInfo::fTemplated
CPPOverload * fTemplated
Definition: TemplateProxy.h:39
CPyCppyy::TemplateInfo::fPyName
PyObject * fPyName
Definition: TemplateProxy.h:36
CPyCppyy::TemplateInfo::TemplateInfo
TemplateInfo(const TemplateInfo &)=delete