Logo ROOT  
Reference Guide
ObjectProxy.h
Go to the documentation of this file.
1// @(#)root/pyroot:$Id$
2// Author: Wim Lavrijsen, Jan 2005
3
4#ifndef PYROOT_OBJECTPROXY_H
5#define PYROOT_OBJECTPROXY_H
6
7//////////////////////////////////////////////////////////////////////////////
8// //
9// PyROOT::ObjectProxy //
10// //
11// Python-side proxy, encapsulaties a C++ object. //
12// //
13//////////////////////////////////////////////////////////////////////////////
14
15
16// Bindings
17#include "PyRootType.h"
18#include "Cppyy.h"
19#include "TCallContext.h"
20
21// ROOT
22#include "DllImport.h"
23
24// TODO: have an ObjectProxy derived or alternative type for smart pointers
25
26namespace PyROOT {
27
29 public:
30 enum EFlags { kNone = 0x0, kIsOwner = 0x0001, kIsReference = 0x0002, kIsValue = 0x0004, kIsSmartPtr = 0x0008 };
31
32 public:
33 void Set( void* address, EFlags flags = kNone )
34 {
35 // Initialize the proxy with the pointer value 'address.'
36 fObject = address;
37 fFlags = flags;
38 }
39
40 void SetSmartPtr ( void* address, Cppyy::TCppType_t ptrType )
41 {
43 fSmartPtr = address;
44 fSmartPtrType = ptrType;
45 }
46
47 void* GetObject() const
48 {
49 // Retrieve a pointer to the held C++ object.
50
51 // We get the raw pointer from the smart pointer each time, in case
52 // it has changed or has been freed.
53 if ( fFlags & kIsSmartPtr ) {
54 // TODO: this is icky and slow
55 std::vector< Cppyy::TCppMethod_t > methods = Cppyy::GetMethodsFromName( fSmartPtrType, "operator->", /*bases?*/ true);
56 std::vector<TParameter> args;
57 return Cppyy::CallR( methods[0], fSmartPtr, &args );
58 }
59
60 if ( fObject && ( fFlags & kIsReference ) )
61 return *(reinterpret_cast< void** >( const_cast< void* >( fObject ) ));
62 else
63 return const_cast< void* >( fObject ); // may be null
64 }
65
67 {
68 // Retrieve a pointer to the C++ type; may return NULL.
69 return ((PyRootClass*)Py_TYPE(this))->fCppType;
70 }
71
72 void HoldOn() { fFlags |= kIsOwner; }
73 void Release() { fFlags &= ~kIsOwner; }
74
75 public: // public, as the python C-API works with C structs
76 PyObject_HEAD
77 void* fObject;
78 int fFlags;
79 void* fSmartPtr;
81
82 private: // private, as the python C-API will handle creation
84 };
85
86
87//- object proxy type and type verification ----------------------------------
88 R__EXTERN PyTypeObject ObjectProxy_Type;
89
90 template< typename T >
91 inline Bool_t ObjectProxy_Check( T* object )
92 {
93 return object && PyObject_TypeCheck( object, &ObjectProxy_Type );
94 }
95
96 template< typename T >
98 {
99 return object && Py_TYPE(object) == &ObjectProxy_Type;
100 }
101
102
103//- helper for memory regulation (no PyTypeObject equiv. member in p2.2) -----
104 void op_dealloc_nofree( ObjectProxy* );
105
106} // namespace PyROOT
107
108#endif // !PYROOT_OBJECTPROXY_H
#define R__EXTERN
Definition: DllImport.h:27
#define Py_TYPE(ob)
Definition: PyROOT.h:166
bool Bool_t
Definition: RtypesCore.h:59
Cppyy::TCppType_t ObjectIsA() const
Definition: ObjectProxy.h:66
PyObject_HEAD void * fObject
Definition: ObjectProxy.h:77
void * GetObject() const
Definition: ObjectProxy.h:47
void SetSmartPtr(void *address, Cppyy::TCppType_t ptrType)
Definition: ObjectProxy.h:40
void Set(void *address, EFlags flags=kNone)
Definition: ObjectProxy.h:33
Cppyy::TCppType_t fSmartPtrType
Definition: ObjectProxy.h:80
Type object to hold TClassRef instance (this is only semantically a presentation of PyRootType instan...
Definition: PyRootType.h:37
void * CallR(TCppMethod_t method, TCppObject_t self, void *args)
Definition: Cppyy.cxx:489
TCppScope_t TCppType_t
Definition: Cppyy.h:16
std::vector< TCppMethod_t > GetMethodsFromName(TCppScope_t scope, const std::string &name, bool alsoInBases=false)
Definition: Cppyy.cxx:714
Bool_t ObjectProxy_CheckExact(T *object)
Definition: ObjectProxy.h:97
Bool_t ObjectProxy_Check(T *object)
Definition: ObjectProxy.h:91
PyTypeObject ObjectProxy_Type
void op_dealloc_nofree(ObjectProxy *)
Destroy the held C++ object, if owned; does not deallocate the proxy.
Definition: ObjectProxy.cxx:46
double T(double x)
Definition: ChebyshevPol.h:34