00001
00002
00003
00004 #ifndef DOBASE_I
00005 #define DOBASE_I
00006
00007
00008
00009
00010
00011 #ifdef DO_IMPLEMENT_DOBASEI
00012 #define DO_IMPLEMENTING // If app has not defined it already
00013 #endif
00014 #include "dynobj/DynObj.h"
00015
00016
00017 #define MUTEXI_TYPE_ID 0xD98ED000
00018 #define MALLOCI_TYPE_ID 0x9F46B000
00019 #define TEXTIOI_TYPE_ID 0xB5C14000
00020 #define NAMEDREFI_TYPE_ID 0xAABF9000
00021
00022
00023 class NotifDataI;
00024 class NotifierI;
00025 class NotifiableI;
00026 class MutexI;
00027 class MallocI;
00028 class TextIoI;
00029 class NotifDataWithPtr;
00030 class NamedRefI;
00031 class DynObjLibI;
00032 class DoRunTimeI;
00033
00034
00035 DO_DECL_TYPE_INFO(NotifDataI,NOTIFDATAI_TYPE_ID);
00036 DO_DECL_TYPE_INFO(NotifierI,NOTIFIERI_TYPE_ID);
00037 DO_DECL_TYPE_INFO(NotifiableI,NOTIFIABLEI_TYPE_ID);
00038 DO_DECL_TYPE_INFO(MutexI,MUTEXI_TYPE_ID);
00039 DO_DECL_TYPE_INFO(MallocI,MALLOCI_TYPE_ID);
00040 DO_DECL_TYPE_INFO(TextIoI,TEXTIOI_TYPE_ID);
00041 DO_DECL_TYPE_INFO(NotifDataWithPtr,NOTIFDATAWITHPTR_TYPE_ID);
00042 DO_DECL_TYPE_INFO(NamedRefI,NAMEDREFI_TYPE_ID);
00043 DO_DECL_TYPE_INFO(DynObjLibI,DYNOBJLIBI_TYPE_ID);
00044 DO_DECL_TYPE_INFO(DoRunTimeI,DORUNTIMEI_TYPE_ID);
00045
00046
00047
00048
00049
00050
00051 #include "utils/NotifierI.h"
00052
00053
00054
00055
00056
00057
00063
00064 class MutexI {
00065 public:
00069 virtual bool docall Lock() = 0;
00070 virtual bool docall Unlock() = 0;
00071 virtual bool docall TryLock() { return false; }
00073 };
00074
00078 class MutexLocker {
00079 public:
00080 MutexLocker(MutexI *pmi, bool try_only=false) : m_pmi(pmi), m_locked(false) {
00081 if( m_pmi ){
00082 if( !try_only )
00083 m_locked = m_pmi->Lock();
00084 else
00085 m_locked = m_pmi->TryLock();
00086 }
00087 }
00088 ~MutexLocker() { if( m_locked && m_pmi ) m_pmi->Unlock(); }
00089 protected:
00090 MutexI *m_pmi;
00091 bool m_locked;
00092 };
00093
00094
00098
00099 class MallocI {
00100 public:
00104 virtual void* docall Malloc( unsigned int size ) = 0;
00105 virtual bool docall Free( void *pv ) = 0;
00106 virtual void* docall ReAlloc( void* pv, unsigned int size ) = 0;
00107 virtual int docall GetNumberAlloc( ) = 0;
00108 virtual bool docall GetSumAlloced( unsigned int& i64_hi, unsigned int& i64_lo ) = 0;
00110 };
00111
00112 class DynStr;
00113
00117
00118 class TextIoI {
00119 public:
00123 virtual int docall puts(const char *ps) = 0;
00124 virtual int docall puterr(const char *pe) = 0;
00125 virtual int docall gets(DynStr& ds, bool sync_wait=true) = 0;
00127 };
00128
00129
00130
00134
00135 class NotifDataWithPtr : public NotifDataI {
00136 public:
00137 NotifDataWithPtr( DynI *pdi=NULL, void *pv=NULL ) : m_pdi(pdi), m_pv(pv) { }
00138 DynI *m_pdi;
00139 void *m_pv;
00140 };
00141
00142
00151
00152 class NamedRefI : public DynI {
00153 public:
00157 virtual DynObjType* docall doGetType( const DynI **pself=0 ) const;
00158
00163 virtual DynI* docall GetByName( const char *name, int type_id ){ return NULL; }
00164
00170 virtual DynI* docall GetByIndex( int index, DynStr &ds, int type_id=0 ){ return NULL; }
00171
00178 virtual bool docall Set( const char *name, DynI *pdi ){ return false; }
00179
00183 virtual bool docall Remove( DynI *pdi ){ return false; }
00185
00188
00189 DynI* Get( const char *name ){ return GetByName(name,0); }
00190
00191
00192 };
00193
00194
00198
00199 class DynObjLibI : public DynI {
00200 public:
00201
00203 enum {
00204 NotLoaded = 0,
00205 LoadFailed,
00206 Loaded,
00207 Incompatible,
00208 InitFailed,
00209 CannotInit,
00210 Initialized,
00211 InitializedWithoutDoInit
00212 };
00213
00217 virtual DynObjType* docall doGetType( const DynI **pself=0 ) const;
00218
00220 virtual int docall GetState() = 0;
00221
00223
00224
00226 virtual const char* docall GetLibName() = 0;
00227
00229 virtual const char* docall GetLibFileName() = 0;
00230
00238 virtual DynObj* docall Create( const char* type, int type_id, const DynI* pdi_init=NULL ) = 0;
00239
00245 virtual bool docall Destroy( VObj *pvo ) = 0;
00246
00249 virtual void docall OnObjectCreate( DynObj *pdo, DynObjType *pdt ) = 0;
00250
00251 virtual void docall OnObjectDestroy( DynObj *pdo, DynObjType *pdt ) = 0;
00252
00253 virtual bool docall MayUnload( ) = 0;
00255
00259
00264 template<class T>
00265 T* Create( const DynI* pdi_init=NULL ){
00266 DynObj *pdo = Create( doTypeInfo<T>::Name(), doTypeInfo<T>::Id(), pdi_init );
00267 return pdo ? (T*)pdo->doQueryI( doTypeInfo<T>::Name() ) : NULL;
00268 }
00270 };
00271
00272
00276
00277 class DoRunTimeI : public DynI {
00278 public:
00279
00280 virtual DynObjType* docall doGetType( const DynI **pself=0 ) const;
00281
00285
00286 virtual MallocI* docall GetMalloc() const{ return NULL; }
00287 virtual TextIoI* docall GetTextIo() const{ return NULL; }
00288
00289
00290 virtual DynObj* docall Create( const char *lib, const char *type, int type_id, const DynI* pdi_init=NULL ){ return NULL; }
00291
00292
00293
00294
00295
00296 virtual int docall GetLastErrCode( int thid=-1 ){ return -1; }
00297 virtual bool docall GetLastErrStr( DynStr& ds, int thid=-1 ){ return false; }
00298 virtual bool docall SetLastError( int err_code, const char *err_msg ){ return false; }
00299 virtual int docall GetThreadId( ){ return -1; }
00300
00301
00302 virtual DynObjType* docall GetTypeOf( void **vtbl ){ return NULL; }
00303 virtual DynObjType* docall FindTypeFromName( const char *type, const char *header=NULL ){ return NULL; }
00304 virtual DynObjType* docall FindTypeFromId( int type_id ){ return NULL; }
00305
00306
00308 virtual DynI* docall LoadLib( const char *lib_name, DynI* pdi_init ){ return NULL; }
00310 virtual bool docall UnloadLib( const char *lib_name, bool force_unload ){ return false; }
00312 virtual DynObjLibI* docall GetLib( const char *lib ){ return NULL; }
00313 virtual const char* docall GetLoadError( ) { return "<no impl>"; }
00314
00315
00316 virtual int docall GenerateTypeId(){ return 0; }
00317
00318
00319 virtual bool docall SetObjectError( const void *pvo, const char *err_str, int err_code ){ return false; }
00320 virtual bool docall ClearObjectError( const void *pvo ){ return false; }
00321 virtual const char* docall GetObjectError( const void *pvo, int *perr_code ){ return NULL; }
00323
00327
00332 template<class T>
00333 T* Create( const DynI* pdi_init=NULL ){
00334 const char *type = doTypeInfo<T>::Name();
00335 DynObj *pdo = Create( NULL, type, doTypeInfo<T>::Id(), pdi_init );
00336 return pdo ? (T*)pdo->doQueryI( type ) : NULL;
00337 }
00338
00339
00340
00341 bool ObjectPublish( DynI *pdi, const char *name ){
00342 NamedRefI *pni = (NamedRefI*)doQueryI("NamedRefI");
00343 if( !pni ) return false;
00344 return pni->Set(name,pdi);
00345 }
00346 DynI* ObjectLookup( const char *name, int type_id=0 ){
00347 NamedRefI *pni = (NamedRefI*)doQueryI("NamedRefI");
00348 if( !pni ) return NULL;
00349 return pni->GetByName(name,type_id);
00350 }
00352 };
00353
00354
00355
00356
00357 #ifdef DO_IMPLEMENT_DOBASEI
00358
00359
00360
00361 DynObjType g_do_vtype_NotifDataI("NotifDataI","VObj",NOTIFDATAI_TYPE_ID,0,sizeof(NotifDataI),NULL,NULL,__FILE__);
00362 DynObjType g_do_vtype_NotifierI("NotifierI","VObj",NOTIFIERI_TYPE_ID,0,sizeof(NotifierI),NULL,NULL,__FILE__);
00363 DynObjType g_do_vtype_NotifiableI("NotifiableI","VObj",NOTIFIABLEI_TYPE_ID,0,sizeof(NotifiableI),NULL,NULL,__FILE__);
00364
00365 DynObjType g_do_vtype_MutexI("MutexI","VObj",MUTEXI_TYPE_ID,0,sizeof(MutexI),NULL,NULL,__FILE__);
00366 DynObjType g_do_vtype_MallocI("MallocI","VObj",MALLOCI_TYPE_ID,0,sizeof(MallocI),NULL,NULL,__FILE__);
00367 DynObjType g_do_vtype_TextIoI("TextIoI","VObj",TEXTIOI_TYPE_ID,0,sizeof(TextIoI),NULL,NULL,__FILE__);
00368
00369 DynObjTypeI2R<NotifDataWithPtr,NotifDataI,false> g_do_vtype_NotifDataWithPtr("NotifDataWithPtr:NotifDataI",NOTIFDATAWITHPTR_TYPE_ID,0,__FILE__);
00370
00371 DynObjTypeI2R<NamedRefI,DynI,false> g_do_vtype_NamedRefI("NamedRefI:DynI",NAMEDREFI_TYPE_ID,0,__FILE__);
00372 DynObjType* NamedRefI::doGetType( const DynI **pself ) const {
00373 if(pself) *pself=(const DynI*)(const void*)this;
00374 return &g_do_vtype_NamedRefI;
00375 }
00376 DynObjType g_do_vtype_DynObjLibI("DynObjLibI","DynI",DYNOBJLIBI_TYPE_ID,0,sizeof(DynObjLibI),NULL,NULL,__FILE__);
00377 DynObjType* DynObjLibI::doGetType( const DynI **pself ) const {
00378 if(pself) *pself=(const DynI*)(const void*)this;
00379 return &g_do_vtype_DynObjLibI;
00380 }
00381
00382 DynObjType g_do_vtype_DoRunTimeI("DoRunTimeI","DynI",DORUNTIMEI_TYPE_ID,DOT_STATIC_TYPE,sizeof(DoRunTimeI),NULL,NULL,__FILE__);
00383 DynObjType* DoRunTimeI::doGetType( const DynI **pself ) const {
00384 if(pself) *pself=(const DynI*)(const void*)this;
00385 return &g_do_vtype_DoRunTimeI;
00386 }
00387
00388 #endif //DO_IMPLEMENT_...
00389
00390
00391 #endif //DOBASE_I
00392
00393