00001 #ifndef SCOPEDDYNOBJ_HPP
00002 #define SCOPEDDYNOBJ_HPP
00003
00004 #include "DynObjPtr.hpp"
00005 #include "DoNew.h"
00006
00007 #if DO_USE_RUNTIME==1
00008 #include "DoNew.h"
00009 #endif
00010
00011
00012 #define LocalDynObj ScopedDynObj
00013
00023 template<class T>
00024 struct ScopedDynObj : public DynObjPtr<T> {
00028 explicit ScopedDynObj( T* pt=NULL ) {
00029 this->m_flag_unused = false;
00030 if( !pt ){
00031 pt = do_new<T>( (DynI*)NULL );
00032 if( !pt ) return;
00033 }
00034 DoAssign( pt );
00035 }
00036
00039 template<class A>
00040 explicit ScopedDynObj( A arg ) {
00041 this->m_flag_unused = false;
00042 T* pt = do_new<T>( arg );
00043 if( !pt ) return;
00044 DoAssign( pt );
00045 }
00046
00050 #if DO_USE_RUNTIME==1
00051 template<class A>
00052 explicit ScopedDynObj( const char* lib_name, A arg ) {
00053 this->m_flag_unused = false;
00054 T* pt = do_new<T>( lib_name, arg );
00055 if( !pt ) return;
00056 DoAssign( pt );
00057 }
00058 #endif
00059
00060 ~ScopedDynObj(){
00061 if( !this->m_pt ) return;
00062
00063 ::doReleaseDestroy(this->m_pt);
00064 this->m_pt = NULL;
00065 }
00066
00068 T& operator () () { return *this->m_pt; }
00069
00071 bool IsOk(){ return this->m_pt!=NULL; }
00072
00073 protected:
00074 void DoAssign( T* pt ){
00075 if( !pt ) return;
00076
00077 if( !this->InternalAssign(pt,false) ){
00078
00079 this->m_flag_unused = true;
00080 this->m_pt = pt;
00081
00082
00083 if( ::doIsMainObject(pt)<=0 )
00084 DO_LOG1( "ScopedDynObj - Ownership of plugin object without weak ref: %p", pt );
00085 }
00086 }
00087
00088 private:
00089
00090 ScopedDynObj& operator = (T* pt){
00091 DO_ASSERT_MSG(0,"Should not assign to ScopedDynObj");
00092 return *this;
00093 }
00094 ScopedDynObj& operator = (const DynObjPtr<T>& dop){
00095 DO_ASSERT_MSG(0,"Should not assign to ScopedDynObj");
00096 return operator = ((T*)dop);
00097 }
00098 };
00099
00100 #endif // SCOPEDDYNOBJ_HPP