Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
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#ifndef Py_LIMITED_API
14#include "CPPScope.h"
15#endif
16#include "Cppyy.h"
17#include "CallContext.h" // for Parameter
18
19// Standard
20#include <utility>
21#include <vector>
22
23
24namespace CPyCppyy {
25
26typedef std::vector<std::pair<ptrdiff_t, PyObject*>> CI_DatamemberCache_t;
27
29public:
30 enum EFlags {
31 kDefault = 0x0000,
32 kNoWrapConv = 0x0001, // use type as-is (eg. no smart ptr wrap)
33 kIsOwner = 0x0002, // Python instance owns C++ object/memory
34 kIsExtended = 0x0004, // has extended data
35 kIsValue = 0x0008, // was created from a by-value return
36 kIsReference = 0x0010, // represents one indirection
37 kIsArray = 0x0020, // represents an array of objects
38 kIsSmartPtr = 0x0040, // is or embeds a smart pointer
39 kIsPtrPtr = 0x0080, // represents two indirections
40 kIsRValue = 0x0100, // can be used as an r-value
41 kIsLValue = 0x0200, // can be used as an l-value
42 kNoMemReg = 0x0400, // do not register with memory regulator
43 kIsRegulated = 0x0800, // is registered with memory regulator
44 kIsActual = 0x1000, // has been downcasted to actual type
45 kHasLifeLine = 0x2000, // has a life line set
46 };
47
48public: // public, as the python C-API works with C structs
50 void* fObject;
51 uint32_t fFlags;
52
53public:
54// construction (never done directly)
55 CPPInstance() = delete;
56
57 void Set(void* address, EFlags flags = kDefault);
58 CPPInstance* Copy(void* cppinst, PyTypeObject* target = nullptr);
59
60// state checking
61 bool IsExtended() const { return fFlags & kIsExtended; }
62 bool IsSmart() const { return fFlags & kIsSmartPtr; }
63
64// access to C++ pointer and type
65 void* GetObject();
66 void*& GetObjectRaw() { return IsExtended() ? *(void**) fObject : fObject; }
67 Cppyy::TCppType_t ObjectIsA(bool check_smart = true) const;
68
69// memory management: ownership of the underlying C++ object
70 void PythonOwns();
71 void CppOwns();
72
73// data member cache
75
76// smart pointer management
78 void* GetSmartObject() { return GetObjectRaw(); }
80
81// cross-inheritance dispatch
82 void SetDispatchPtr(void*);
83
84// redefine pointer to object as fixed-size array
87
88// implementation of the __reduce__ method: doesn't wrap any function by
89// default but can be re-assigned by libraries that add C++ object
90// serialization support, like ROOT
91 static PyCFunction &ReduceMethod();
92
93private:
94 void CreateExtension();
95 void* GetExtendedObject();
96};
97
98
99//- public methods -----------------------------------------------------------
100inline void CPPInstance::Set(void* address, EFlags flags)
101{
102// Initialize the proxy with the pointer value 'address.'
103 if (flags != kDefault) fFlags = flags;
104 GetObjectRaw() = address;
105}
106
107//----------------------------------------------------------------------------
109{
110// Retrieve a pointer to the held C++ object.
111 if (!IsExtended()) {
112 if (fObject && (fFlags & kIsReference))
113 return *(reinterpret_cast<void**>(fObject));
114 else
115 return fObject; // may be null
116 } else
117 return GetExtendedObject();
118}
119
120//----------------------------------------------------------------------------
121#ifndef Py_LIMITED_API
123{
124// Retrieve the C++ type identifier (or raw type if smart).
125 if (check_smart || !IsSmart()) return ((CPPClass*)Py_TYPE(this))->fCppType;
126 return GetSmartIsA();
127}
128#endif
129
130
131//- object proxy type and type verification ----------------------------------
132// Needs to be extern because the libROOTPythonizations is secretly using it
133#ifdef _MSC_VER
135#else
137#endif
138
139#ifndef Py_LIMITED_API
140template<typename T>
141inline bool CPPInstance_Check(T* object)
142{
143// Short-circuit the type check by checking tp_new which all generated subclasses
144// of CPPInstance inherit.
145 return object && \
146 (Py_TYPE(object)->tp_new == CPPInstance_Type.tp_new || \
148}
149#endif
150
151template<typename T>
152inline bool CPPInstance_CheckExact(T* object)
153{
154 return object && Py_TYPE(object) == &CPPInstance_Type;
155}
156
157
158//- helper for memory regulation (no PyTypeObject equiv. member in p2.2) -----
159void op_dealloc_nofree(CPPInstance*);
160
161} // namespace CPyCppyy
162
163#endif // !CPYCPPYY_CPPINSTANCE_H
#define Py_TYPE(ob)
Definition CPyCppyy.h:196
int Py_ssize_t
Definition CPyCppyy.h:215
_object PyObject
ROOT::Detail::TRangeCast< T, true > TRangeDynCast
TRangeDynCast is an adapter class that allows the typed iteration through a TCollection.
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t target
Cppyy::TCppType_t GetSmartIsA() const
bool IsSmart() const
Definition CPPInstance.h:62
void CastToArray(Py_ssize_t sz)
CPPInstance * Copy(void *cppinst, PyTypeObject *target=nullptr)
void Set(void *address, EFlags flags=kDefault)
CI_DatamemberCache_t & GetDatamemberCache()
void SetSmart(PyObject *smart_type)
PyObject_HEAD void * fObject
Definition CPPInstance.h:50
static PyCFunction & ReduceMethod()
Cppyy::TCppType_t ObjectIsA(bool check_smart=true) const
bool IsExtended() const
Definition CPPInstance.h:61
void SetDispatchPtr(void *)
PyTypeObject CPPInstance_Type
std::vector< std::pair< ptrdiff_t, PyObject * > > CI_DatamemberCache_t
Definition CPPInstance.h:26
void op_dealloc_nofree(CPPInstance *)
bool CPPInstance_Check(T *object)
bool CPPInstance_CheckExact(T *object)
TCppScope_t TCppType_t
Definition cpp_cppyy.h:35