00001 // @(#)root/pyroot:$Id: ObjectProxy.h 35563 2010-09-22 00:32:31Z wlav $ 00002 // Author: Wim Lavrijsen, Jan 2005 00003 00004 #ifndef PYROOT_OBJECTPROXY_H 00005 #define PYROOT_OBJECTPROXY_H 00006 00007 // Bindings 00008 #include "PyRootType.h" 00009 00010 // ROOT 00011 #include "DllImport.h" 00012 #include "TClassRef.h" 00013 class TClass; 00014 00015 00016 namespace PyROOT { 00017 00018 /** Object proxy object to hold ROOT instance 00019 @author WLAV 00020 @date 01/04/2005 00021 @date 03/31/2008 00022 @version 2.0 00023 */ 00024 00025 class ObjectProxy { 00026 public: 00027 enum EFlags { kNone = 0x0, kIsOwner = 0x0001, kIsReference = 0x0002 }; 00028 00029 public: 00030 void Set( void** address, EFlags flags = kNone ) 00031 { 00032 fObject = (void*) address; 00033 fFlags = flags | kIsReference; 00034 } 00035 00036 void Set( void* object, EFlags flags = kNone ) 00037 { 00038 fObject = object; 00039 fFlags = flags & ~kIsReference; 00040 } 00041 00042 void* GetObject() const 00043 { 00044 if ( fObject && ( fFlags & kIsReference ) ) 00045 return *(reinterpret_cast< void** >( const_cast< void* >( fObject ) )); 00046 else 00047 return const_cast< void* >( fObject ); // may be null 00048 } 00049 00050 TClass* ObjectIsA() const 00051 { 00052 // the following may return null 00053 return ((PyRootClass*)Py_TYPE(this))->fClass.GetClass(); 00054 } 00055 00056 void HoldOn() { fFlags |= kIsOwner; } 00057 void Release() { fFlags &= ~kIsOwner; } 00058 00059 public: // public, as the python C-API works with C structs 00060 PyObject_HEAD 00061 void* fObject; 00062 int fFlags; 00063 00064 private: // private, as the python C-API will handle creation 00065 ObjectProxy() {} 00066 }; 00067 00068 00069 //- object proxy type and type verification ---------------------------------- 00070 R__EXTERN PyTypeObject ObjectProxy_Type; 00071 00072 template< typename T > 00073 inline Bool_t ObjectProxy_Check( T* object ) 00074 { 00075 return object && PyObject_TypeCheck( object, &ObjectProxy_Type ); 00076 } 00077 00078 template< typename T > 00079 inline Bool_t ObjectProxy_CheckExact( T* object ) 00080 { 00081 return object && Py_TYPE(object) == &ObjectProxy_Type; 00082 } 00083 00084 00085 //- helper for memory regulation (no PyTypeObject equiv. member in p2.2) ----- 00086 void op_dealloc_nofree( ObjectProxy* ); 00087 00088 } // namespace PyROOT 00089 00090 #endif // !PYROOT_OBJECTPROXY_H