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;
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
#define pyname
Definition: TMCParticle.cxx:19
TemplateProxy * TemplateProxy_New(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
Definition: TemplateProxy.h:91
TP_DispatchMap_t fDispatchMap
Definition: TemplateProxy.h:43
double T(double x)
Definition: ChebyshevPol.h:34
CPPOverload * fLowPriority
Definition: TemplateProxy.h:40
std::map< std::string, std::vector< TP_DispatchEntry_t > > TP_DispatchMap_t
Definition: TemplateProxy.h:25
std::pair< uint64_t, CPPOverload * > TP_DispatchEntry_t
Template proxy object to return functions and methods.
Definition: TemplateProxy.h:19
PyObject_HEAD PyObject * fSelf
Definition: TemplateProxy.h:58
#define Py_TYPE(ob)
Definition: CPyCppyy.h:209
void AdoptTemplate(PyCallable *pc)
bool TemplateProxy_CheckExact(T *object)
Definition: TemplateProxy.h:85
PyTypeObject TemplateProxy_Type
std::shared_ptr< TemplateInfo > TP_TInfo_t
Definition: TemplateProxy.h:48
static constexpr double pc
_object PyObject
Definition: PyMethodBase.h:41
void AdoptMethod(PyCallable *pc)
TemplateInfo & operator=(const TemplateInfo &)=delete
void MergeOverload(CPPOverload *mp)
void Set(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
bool CPPScope_Check(T *object)
Definition: CPPScope.h:76
bool TemplateProxy_Check(T *object)
Definition: TemplateProxy.h:79
friend TemplateProxy * TemplateProxy_New(const std::string &cppname, const std::string &pyname, PyObject *pyclass)
Definition: TemplateProxy.h:91
CPPOverload * fTemplated
Definition: TemplateProxy.h:39
PyObject * Instantiate(const std::string &fname, PyObject *tmplArgs, Utility::ArgPreference, int *pcnt=nullptr)
CPPOverload * fNonTemplated
Definition: TemplateProxy.h:38