Logo ROOT  
Reference Guide
CPPInstance.h
Go to the documentation of this file.
1 #ifndef CPYCPPYY_CPPINSTANCE_H
2 #define CPYCPPYY_CPPINSTANCE_H
3 
4 //////////////////////////////////////////////////////////////////////////////
5 // //
6 // CpyCppyy::CPPInstance //
7 // //
8 // Python-side proxy, encapsulaties a C++ object. //
9 // //
10 //////////////////////////////////////////////////////////////////////////////
11 
12 // Bindings
13 #include "CPPScope.h"
14 #include "Cppyy.h"
15 #include "CallContext.h" // for Parameter
16 
17 // Standard
18 #include <utility>
19 #include <vector>
20 
21 
22 namespace CPyCppyy {
23 
24 typedef std::vector<std::pair<ptrdiff_t, PyObject*>> CI_DatamemberCache_t;
25 
26 class CPPInstance {
27 public:
28  enum EFlags {
29  kDefault = 0x0000,
30  kNoWrapConv = 0x0001,
31  kIsOwner = 0x0002,
32  kIsExtended = 0x0004,
33  kIsReference = 0x0008,
34  kIsRValue = 0x0010,
35  kIsValue = 0x0020,
36  kIsPtrPtr = 0x0040,
37  kIsSmartPtr = 0x0080,
38  kNoMemReg = 0x0100,
39  kHasLifeline = 0x0200,
40  kIsRegulated = 0x0400 };
41 
42 public: // public, as the python C-API works with C structs
43  PyObject_HEAD
44  void* fObject;
45  int fFlags;
46 
47 public:
48 // construction (never done directly)
49  CPPInstance() = delete;
50 
51  void Set(void* address, EFlags flags = kDefault);
52  CPPInstance* Copy(void* cppinst);
53 
54 // state checking
55  bool IsExtended() const { return fFlags & kIsExtended; }
56  bool IsSmart() const { return fFlags & kIsSmartPtr; }
57 
58 // access to C++ pointer and type
59  void* GetObject();
60  void*& GetObjectRaw() { return IsExtended() ? *(void**) fObject : fObject; }
61  Cppyy::TCppType_t ObjectIsA(bool check_smart = true) const;
62 
63 // memory management: ownership of the underlying C++ object
64  void PythonOwns();
65  void CppOwns();
66 
67 // smart pointer management
68  void SetSmart(PyObject* smart_type);
69  void* GetSmartObject() { return GetObjectRaw(); }
71 
72 // data member cache
74 
75 // cross-inheritence dispatch
76  void SetDispatchPtr(void*);
77 
78 private:
79  void CreateExtension();
80  void* GetExtendedObject();
81 };
82 
83 
84 //- public methods -----------------------------------------------------------
85 inline void CPPInstance::Set(void* address, EFlags flags)
86 {
87 // Initialize the proxy with the pointer value 'address.'
88  if (flags != kDefault) fFlags = flags;
89  GetObjectRaw() = address;
90 }
91 
92 //----------------------------------------------------------------------------
93 inline void* CPPInstance::GetObject()
94 {
95 // Retrieve a pointer to the held C++ object.
96  if (!IsExtended()) {
97  if (fObject && (fFlags & kIsReference))
98  return *(reinterpret_cast<void**>(fObject));
99  else
100  return fObject; // may be null
101  } else
102  return GetExtendedObject();
103 }
104 
105 //----------------------------------------------------------------------------
106 inline Cppyy::TCppType_t CPPInstance::ObjectIsA(bool check_smart) const
107 {
108 // Retrieve the C++ type identifier (or raw type if smart).
109  if (check_smart || !IsSmart()) return ((CPPClass*)Py_TYPE(this))->fCppType;
110  return GetSmartIsA();
111 }
112 
113 
114 //- object proxy type and type verification ----------------------------------
115 CPYCPPYY_IMPORT PyTypeObject CPPInstance_Type;
116 
117 template<typename T>
118 inline bool CPPInstance_Check(T* object)
119 {
120 // Short-circuit the type check by checking tp_new which all generated subclasses
121 // of CPPInstance inherit.
122  return object && \
123  (Py_TYPE(object)->tp_new == CPPInstance_Type.tp_new || \
124  PyObject_TypeCheck(object, &CPPInstance_Type));
125 }
126 
127 template<typename T>
128 inline bool CPPInstance_CheckExact(T* object)
129 {
130  return object && Py_TYPE(object) == &CPPInstance_Type;
131 }
132 
133 
134 //- helper for memory regulation (no PyTypeObject equiv. member in p2.2) -----
135 void op_dealloc_nofree(CPPInstance*);
136 
137 } // namespace CPyCppyy
138 
139 #endif // !CPYCPPYY_CPPINSTANCE_H
CPyCppyy
Definition: TPython.cxx:99
CPyCppyy::CPPInstance::kIsRegulated
@ kIsRegulated
Definition: CPPInstance.h:40
CPyCppyy::CPPInstance::kNoWrapConv
@ kNoWrapConv
Definition: CPPInstance.h:30
CPyCppyy::CPPInstance::SetDispatchPtr
void SetDispatchPtr(void *)
Definition: CPPInstance.cxx:192
CPyCppyy::CPPInstance::CreateExtension
void CreateExtension()
Definition: CPPInstance.cxx:79
PyObject
_object PyObject
Definition: PyMethodBase.h:41
CPyCppyy::CPPInstance::fFlags
int fFlags
Definition: CPPInstance.h:45
CPyCppyy::CPPInstance::Set
void Set(void *address, EFlags flags=kDefault)
Definition: CPPInstance.h:85
CPyCppyy::CI_DatamemberCache_t
std::vector< std::pair< ptrdiff_t, PyObject * > > CI_DatamemberCache_t
Definition: CPPInstance.h:24
CPyCppyy::CPPInstance_CheckExact
bool CPPInstance_CheckExact(T *object)
Definition: CPPInstance.h:128
CPyCppyy::CPPInstance::GetObjectRaw
void *& GetObjectRaw()
Definition: CPPInstance.h:60
CPyCppyy::CPPInstance::kIsExtended
@ kIsExtended
Definition: CPPInstance.h:32
Cppyy.h
CPYCPPYY_IMPORT
#define CPYCPPYY_IMPORT
Definition: CommonDefs.h:26
CPyCppyy::CPPInstance
Definition: CPPInstance.h:26
CPyCppyy::CPPInstance::kIsSmartPtr
@ kIsSmartPtr
Definition: CPPInstance.h:37
CPyCppyy::CPPInstance::kNoMemReg
@ kNoMemReg
Definition: CPPInstance.h:38
CPyCppyy::CPPInstance::GetDatamemberCache
CI_DatamemberCache_t & GetDatamemberCache()
Definition: CPPInstance.cxx:184
CPyCppyy::CPPInstance::GetObject
void * GetObject()
Definition: CPPInstance.h:93
CPyCppyy::CPPInstance::PythonOwns
void PythonOwns()
Definition: CPPInstance.cxx:152
CPyCppyy::CPPInstance::GetExtendedObject
void * GetExtendedObject()
Definition: CPPInstance.cxx:88
CPyCppyy::CPPInstance::fObject
PyObject_HEAD void * fObject
Definition: CPPInstance.h:44
CPyCppyy::CPPInstance::kIsRValue
@ kIsRValue
Definition: CPPInstance.h:34
CPyCppyy::CPPInstance::EFlags
EFlags
Definition: CPPInstance.h:28
CallContext.h
CPyCppyy::CPPInstance::kIsOwner
@ kIsOwner
Definition: CPPInstance.h:31
CPyCppyy::CPPInstance::kIsPtrPtr
@ kIsPtrPtr
Definition: CPPInstance.h:36
CPyCppyy::CPPInstance::kDefault
@ kDefault
Definition: CPPInstance.h:29
CPyCppyy::CPPInstance::GetSmartObject
void * GetSmartObject()
Definition: CPPInstance.h:69
CPyCppyy::CPPScope
Definition: CPPScope.h:37
CPyCppyy::CPPInstance::IsExtended
bool IsExtended() const
Definition: CPPInstance.h:55
Cppyy::TCppType_t
TCppScope_t TCppType_t
Definition: cpp_cppyy.h:19
CPyCppyy::CPPInstance::Copy
CPPInstance * Copy(void *cppinst)
Definition: CPPInstance.cxx:100
CPPScope.h
CPyCppyy::CPPInstance::kHasLifeline
@ kHasLifeline
Definition: CPPInstance.h:39
CPyCppyy::CPPInstance::kIsValue
@ kIsValue
Definition: CPPInstance.h:35
ROOT::Math::Chebyshev::T
double T(double x)
Definition: ChebyshevPol.h:34
CPyCppyy::CPPInstance_Type
PyTypeObject CPPInstance_Type
Definition: CPPInstance.cxx:745
CPyCppyy::CPPInstance::SetSmart
void SetSmart(PyObject *smart_type)
Definition: CPPInstance.cxx:168
CPyCppyy::CPPInstance::GetSmartIsA
Cppyy::TCppType_t GetSmartIsA() const
Definition: CPPInstance.cxx:177
CPyCppyy::CPPInstance::ObjectIsA
Cppyy::TCppType_t ObjectIsA(bool check_smart=true) const
Definition: CPPInstance.h:106
CPyCppyy::CPPInstance::CPPInstance
CPPInstance()=delete
CPyCppyy::CPPInstance::CppOwns
void CppOwns()
Definition: CPPInstance.cxx:160
Py_TYPE
#define Py_TYPE(ob)
Definition: CPyCppyy.h:209
CPyCppyy::CPPInstance::kIsReference
@ kIsReference
Definition: CPPInstance.h:33
CPyCppyy::CPPInstance_Check
bool CPPInstance_Check(T *object)
Definition: CPPInstance.h:118
CPyCppyy::op_dealloc_nofree
void op_dealloc_nofree(CPPInstance *)
Definition: CPPInstance.cxx:201
CPyCppyy::CPPInstance::IsSmart
bool IsSmart() const
Definition: CPPInstance.h:56