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#include "CPPScope.h"
14#include "Cppyy.h"
15#include "CallContext.h" // for Parameter
16
17// Standard
18#include <utility>
19#include <vector>
20
21
22namespace CPyCppyy {
23
24typedef std::vector<std::pair<ptrdiff_t, PyObject*>> CI_DatamemberCache_t;
25
27public:
28 enum EFlags {
29 kDefault = 0x0000,
30 kNoWrapConv = 0x0001, // use type as-is (eg. no smart ptr wrap)
31 kIsOwner = 0x0002, // Python instance owns C++ object/memory
32 kIsExtended = 0x0004, // has extended data
33 kIsValue = 0x0008, // was created from a by-value return
34 kIsReference = 0x0010, // represents one indirection
35 kIsArray = 0x0020, // represents an array of objects
36 kIsSmartPtr = 0x0040, // is or embeds a smart pointer
37 kIsPtrPtr = 0x0080, // represents two indirections
38 kIsRValue = 0x0100, // can be used as an r-value
39 kIsLValue = 0x0200, // can be used as an l-value
40 kNoMemReg = 0x0400, // do not register with memory regulator
41 kIsRegulated = 0x0800, // is registered with memory regulator
42 kIsActual = 0x1000, // has been downcasted to actual type
43 kHasLifeLine = 0x2000, // has a life line set
44 };
45
46public: // public, as the python C-API works with C structs
47 PyObject_HEAD
48 void* fObject;
49 uint32_t fFlags;
50
51public:
52// construction (never done directly)
53 CPPInstance() = delete;
54
55 void Set(void* address, EFlags flags = kDefault);
56 CPPInstance* Copy(void* cppinst, PyTypeObject* target = nullptr);
57
58// state checking
59 bool IsExtended() const { return fFlags & kIsExtended; }
60 bool IsSmart() const { return fFlags & kIsSmartPtr; }
61
62// access to C++ pointer and type
63 void* GetObject();
64 void*& GetObjectRaw() { return IsExtended() ? *(void**) fObject : fObject; }
65 Cppyy::TCppType_t ObjectIsA(bool check_smart = true) const;
66
67// memory management: ownership of the underlying C++ object
68 void PythonOwns();
69 void CppOwns();
70
71// data member cache
73
74// smart pointer management
75 void SetSmart(PyObject* smart_type);
76 void* GetSmartObject() { return GetObjectRaw(); }
78
79// cross-inheritance dispatch
80 void SetDispatchPtr(void*);
81
82// redefine pointer to object as fixed-size array
83 void CastToArray(Py_ssize_t sz);
85
86private:
87 void CreateExtension();
88 void* GetExtendedObject();
89};
90
91
92//- public methods -----------------------------------------------------------
93inline void CPPInstance::Set(void* address, EFlags flags)
94{
95// Initialize the proxy with the pointer value 'address.'
96 if (flags != kDefault) fFlags = flags;
97 GetObjectRaw() = address;
98}
99
100//----------------------------------------------------------------------------
102{
103// Retrieve a pointer to the held C++ object.
104 if (!IsExtended()) {
105 if (fObject && (fFlags & kIsReference))
106 return *(reinterpret_cast<void**>(fObject));
107 else
108 return fObject; // may be null
109 } else
110 return GetExtendedObject();
111}
112
113//----------------------------------------------------------------------------
114inline Cppyy::TCppType_t CPPInstance::ObjectIsA(bool check_smart) const
115{
116// Retrieve the C++ type identifier (or raw type if smart).
117 if (check_smart || !IsSmart()) return ((CPPClass*)Py_TYPE(this))->fCppType;
118 return GetSmartIsA();
119}
120
121
122//- object proxy type and type verification ----------------------------------
124
125template<typename T>
126inline bool CPPInstance_Check(T* object)
127{
128// Short-circuit the type check by checking tp_new which all generated subclasses
129// of CPPInstance inherit.
130 return object && \
131 (Py_TYPE(object)->tp_new == CPPInstance_Type.tp_new || \
132 PyObject_TypeCheck(object, &CPPInstance_Type));
133}
134
135template<typename T>
136inline bool CPPInstance_CheckExact(T* object)
137{
138 return object && Py_TYPE(object) == &CPPInstance_Type;
139}
140
141
142//- helper for memory regulation (no PyTypeObject equiv. member in p2.2) -----
143void op_dealloc_nofree(CPPInstance*);
144
145} // namespace CPyCppyy
146
147#endif // !CPYCPPYY_CPPINSTANCE_H
#define Py_TYPE(ob)
Definition CPyCppyy.h:196
int Py_ssize_t
Definition CPyCppyy.h:215
_object PyObject
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
#define CPYCPPYY_IMPORT
Definition CommonDefs.h:26
Cppyy::TCppType_t GetSmartIsA() const
bool IsSmart() const
Definition CPPInstance.h:60
void CastToArray(Py_ssize_t sz)
CPPInstance * Copy(void *cppinst, PyTypeObject *target=nullptr)
void Set(void *address, EFlags flags=kDefault)
Definition CPPInstance.h:93
CI_DatamemberCache_t & GetDatamemberCache()
void SetSmart(PyObject *smart_type)
PyObject_HEAD void * fObject
Definition CPPInstance.h:48
Cppyy::TCppType_t ObjectIsA(bool check_smart=true) const
bool IsExtended() const
Definition CPPInstance.h:59
void SetDispatchPtr(void *)
PyTypeObject CPPInstance_Type
std::vector< std::pair< ptrdiff_t, PyObject * > > CI_DatamemberCache_t
Definition CPPInstance.h:24
void op_dealloc_nofree(CPPInstance *)
bool CPPInstance_Check(T *object)
bool CPPInstance_CheckExact(T *object)
TCppScope_t TCppType_t
Definition cpp_cppyy.h:19