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,
31 kIsOwner = 0x0002,
32 kIsExtended = 0x0004,
33 kIsReference = 0x0008,
34 kIsRValue = 0x0010,
35 kIsLValue = 0x0020,
36 kIsValue = 0x0040,
37 kIsPtrPtr = 0x0080,
38 kIsArray = 0x0100,
39 kIsSmartPtr = 0x0200,
40 kNoMemReg = 0x0400,
41 kHasLifeline = 0x0800,
42 kIsRegulated = 0x1000,
43 kIsActual = 0x2000 };
44
45public: // public, as the python C-API works with C structs
46 PyObject_HEAD
47 void* fObject;
48 int fFlags;
49
50public:
51// construction (never done directly)
52 CPPInstance() = delete;
53
54 void Set(void* address, EFlags flags = kDefault);
55 CPPInstance* Copy(void* cppinst);
56
57// state checking
58 bool IsExtended() const { return fFlags & kIsExtended; }
59 bool IsSmart() const { return fFlags & kIsSmartPtr; }
60
61// access to C++ pointer and type
62 void* GetObject();
63 void*& GetObjectRaw() { return IsExtended() ? *(void**) fObject : fObject; }
64 Cppyy::TCppType_t ObjectIsA(bool check_smart = true) const;
65
66// memory management: ownership of the underlying C++ object
67 void PythonOwns();
68 void CppOwns();
69
70// smart pointer management
71 void SetSmart(PyObject* smart_type);
72 void* GetSmartObject() { return GetObjectRaw(); }
74
75// data member cache
77
78// cross-inheritence dispatch
79 void SetDispatchPtr(void*);
80
81private:
82 void CreateExtension();
83 void* GetExtendedObject();
84};
85
86
87//- public methods -----------------------------------------------------------
88inline void CPPInstance::Set(void* address, EFlags flags)
89{
90// Initialize the proxy with the pointer value 'address.'
91 if (flags != kDefault) fFlags = flags;
92 GetObjectRaw() = address;
93}
94
95//----------------------------------------------------------------------------
97{
98// Retrieve a pointer to the held C++ object.
99 if (!IsExtended()) {
100 if (fObject && (fFlags & kIsReference))
101 return *(reinterpret_cast<void**>(fObject));
102 else
103 return fObject; // may be null
104 } else
105 return GetExtendedObject();
106}
107
108//----------------------------------------------------------------------------
109inline Cppyy::TCppType_t CPPInstance::ObjectIsA(bool check_smart) const
110{
111// Retrieve the C++ type identifier (or raw type if smart).
112 if (check_smart || !IsSmart()) return ((CPPClass*)Py_TYPE(this))->fCppType;
113 return GetSmartIsA();
114}
115
116
117//- object proxy type and type verification ----------------------------------
119
120template<typename T>
121inline bool CPPInstance_Check(T* object)
122{
123// Short-circuit the type check by checking tp_new which all generated subclasses
124// of CPPInstance inherit.
125 return object && \
126 (Py_TYPE(object)->tp_new == CPPInstance_Type.tp_new || \
127 PyObject_TypeCheck(object, &CPPInstance_Type));
128}
129
130template<typename T>
131inline bool CPPInstance_CheckExact(T* object)
132{
133 return object && Py_TYPE(object) == &CPPInstance_Type;
134}
135
136
137//- helper for memory regulation (no PyTypeObject equiv. member in p2.2) -----
138void op_dealloc_nofree(CPPInstance*);
139
140} // namespace CPyCppyy
141
142#endif // !CPYCPPYY_CPPINSTANCE_H
#define Py_TYPE(ob)
Definition CPyCppyy.h:217
_object PyObject
#define CPYCPPYY_IMPORT
Definition CommonDefs.h:26
Cppyy::TCppType_t GetSmartIsA() const
bool IsSmart() const
Definition CPPInstance.h:59
void Set(void *address, EFlags flags=kDefault)
Definition CPPInstance.h:88
CPPInstance * Copy(void *cppinst)
CI_DatamemberCache_t & GetDatamemberCache()
void SetSmart(PyObject *smart_type)
PyObject_HEAD void * fObject
Definition CPPInstance.h:47
Cppyy::TCppType_t ObjectIsA(bool check_smart=true) const
bool IsExtended() const
Definition CPPInstance.h:58
void SetDispatchPtr(void *)
Set of helper functions that are invoked from the pythonizors, on the Python side.
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