00001
00002
00003
00004 #ifndef DYNOBJLIB_H
00005 #define DYNOBJLIB_H
00006
00007 #include "DynObj.h"
00008 #include "DoBaseI.h"
00009 #include "pi/DynLib.h"
00010 #include "utils/CharBuf.h"
00011 #include "utils/HashTable.hpp"
00012 #include "dynobj/DynObjPtr.hpp"
00013
00014
00015
00016 #ifdef DO_IMPLEMENT_DYNOBJLIB
00017 #define DO_IMPLEMENTING // If app has not defined it already
00018 #endif
00019 #include "dynobj/DynObj.h"
00020
00021
00022 #define DYNOBJLIB_TYPE_ID 0x68D85000
00023
00024
00025 class DynObjLib;
00026
00027
00028 DO_DECL_TYPE_INFO(DynObjLib,DYNOBJLIB_TYPE_ID);
00029
00030
00031
00032 #if DO_ENABLE_OBJECT_TRACKING==1
00033 #include "utils/ExpArr.hpp"
00034 #endif
00035
00036 #include "DORT.h"
00037
00038 struct DortInternalI;
00039
00040
00041
00047
00048 class DynObjLib : public DynObjLibI {
00049 public:
00050
00051 virtual DynObjType* docall doGetType( const DynI **pself=0 ) const;
00052
00053
00054 enum { LibMayLoadModules=1, LibMayOverrideTypes=2, LibIsMain=4 };
00055
00061 DynObjLib( const char *lib,
00062 bool do_call_init,
00063 DynI* pdi_init = NULL,
00064 int mod_flags = 0,
00065 int so_load_flags = -1 );
00066
00067 #ifdef DO_MAIN
00068
00069
00070 DynObjLib( const char *name, CreateDynObjFn pcfn );
00071 #endif
00072 ~DynObjLib( );
00073
00074 virtual int docall GetState() { return m_state; }
00075
00076
00077 virtual const char* docall GetLibName();
00078 virtual const char* docall GetLibFileName();
00079
00080 virtual DynObj* docall Create( const char *type, int type_id, const DynI* pdi_init=NULL );
00081 virtual bool docall Destroy( VObj *pvo );
00082
00083 virtual void docall OnObjectCreate( DynObj *pdo, DynObjType *pdt );
00084 virtual void docall OnObjectDestroy( DynObj *pdo, DynObjType *pdt );
00085
00086 virtual bool docall MayUnload( );
00087
00093 bool ExitDynLib( bool is_query );
00094
00096 bool EnableObjectTracking( bool enable );
00097
00100 bool IsOk() { return m_state>=Initialized; }
00101
00104 const char *GetCallStr(){ return m_callstr; }
00105
00108 const int GetPlatform(){ return m_platform; }
00109
00113 const int GetDoTraits(){ return m_do_traits; }
00114
00118 void *FindSymbol( const char *symb_name );
00119
00121 void* GetBase( );
00122
00123
00129 int Implements( const char *type );
00130
00136 int Implements( int type_id );
00137
00143 const char *GetCallConv( );
00144
00148 static bool AddLibPath( const char* path );
00149
00150 #ifdef DO_MAIN
00151
00155 bool SetCreateDynObjFn( CreateDynObjFn cdofn );
00156 #endif
00157
00158 bool operator==(const char *name) { return m_name==name; }
00159
00160 DynI *InitDynLib( DynI* pdi_init );
00161 DynI *GetInitDynLibRetVal( );
00162
00163 protected:
00164
00165 void Ctor( const char *lib, bool do_call_init, DynI* pdi_init );
00166 bool InitFromModuleData( doModuleData* impl );
00167 static void TrimLibName( CharBuf& name );
00168 static DynLib *LoadDynLib( const char *lib, int so_load_flags=-1, DynObjLib *plib=NULL );
00169
00170 DortInternalI *m_pdorti;
00171
00172 DynLib *m_pdlib;
00173 CharBuf m_name;
00174 int m_state;
00175 int m_lib_flags;
00176
00177 CreateDynObjFn m_pcfn;
00178
00179 InitDynLibFn m_pidlfn;
00180 ExitDynLibFn m_pedlfn;
00181 DestroyDynObjFn m_pdofn;
00182
00183 doModuleData *m_pdol_impl;
00184 DynObjPtr<DynI> m_idl_ret_val;
00185 const char *m_instantiates;
00186 const char *m_callstr;
00187 int m_platform;
00188 int m_do_traits;
00189
00190 CharBuf m_last_err;
00191
00192 #if DO_ENABLE_OBJECT_TRACKING==1
00193 bool m_ot_has_unregistered;
00194 bool m_ot_enable;
00195 HashTable<DynObj*,void> m_objs;
00196 #endif
00197
00198 friend class DoRunTimeC;
00199 };
00200
00201
00202 #define DOH_LIB_FIRST ((const char*)0)
00203
00204
00205
00206 #if DO_USE_TEMPLATES==1
00207 #ifdef DO_USE_RUNTIME
00208 #ifdef DO_MAIN
00209
00210
00211 template <class T, class A>
00212 T *do_new( A arg );
00213
00214 template <class T, class A>
00215 T *do_new( const char* lib_name, A arg );
00216
00217 template <class T>
00218 T *do_new( );
00219
00220 #else // DO_MODULE
00221
00222 #include "DoBase.hpp"
00223 #include "DORT.h"
00224
00225
00226 template<class T,class A, bool dyni_arg>
00227 struct DoCreator;
00228
00229 template<class T,class A>
00230 struct DoCreator<T,A,false> {
00231 static T *Create( A arg ){
00232
00233 DynData dd(arg);
00234 return DORT.Create<T>( &dd );
00235 }
00236 static T *Create( const char* lib_name, A arg ){
00237 DynObjLibI *plib = DORT.GetLib( lib_name );
00238 if( !plib ) return NULL;
00239
00240 DynData dd(arg);
00241 return plib->Create<T>( &dd );
00242 }
00243 };
00244
00245 template<class T,class A>
00246 struct DoCreator<T,A*,true> {
00247 static T *Create( A* parg){
00248
00249 return DORT.Create<T>( parg );
00250 }
00251 static T *Create( const char* lib_name, A* parg ){
00252 DynObjLibI *plib = DORT.GetLib( lib_name );
00253 if( !plib ) return NULL;
00254 return plib->Create<T>( parg );
00255 }
00256 };
00257
00258 template<class T,class A>
00259 struct DoCreator<T,A&,true> {
00260 static T *Create( A& arg ){
00261
00262 return DORT.Create<T>( &arg );
00263 }
00264 static T *Create( const char* lib_name, A& arg ){
00265 DynObjLibI *plib = DORT.GetLib( lib_name );
00266 if( !plib ) return NULL;
00267 return plib->Create<T>( &arg );
00268 }
00269 };
00270
00271
00272 template <class T, class A>
00273 T *do_new( A arg ){
00274 return DoCreator<T,A,DoIsTypeA<A,DynI>::v>::Create( arg );
00275 }
00276
00277 template <class T, class A>
00278 T *do_new( const char* lib_name, A arg ){
00279 return DoCreator<T,A,DoIsTypeA<A,DynI>::v>::Create( lib_name, arg );
00280 }
00281
00282 template <class T>
00283 T *do_new( ){
00284 return DoCreator<T,DynI*,true>::Create( (DynI*)NULL );
00285 }
00286
00287 #endif // DO_MAIN + DO_MODULE
00288 #endif // DO_USE_RUNTIME
00289 #endif // DO_USE_TEMPLATES==1
00290
00291
00292
00293 #ifdef DO_IMPLEMENT_DYNOBJLIB
00294
00295
00296 DynObjType g_do_vtype_DynObjLib("DynObjLib:DynObjLibI",DYNOBJLIB_TYPE_ID,0,sizeof(DynObjLib),NULL,NULL,__FILE__);
00297 DynObjType* DynObjLib::doGetType( const DynI **pself ) const {
00298 if(pself) *pself=(const DynI*)(const void*)this;
00299 return &g_do_vtype_DynObjLib;
00300 }
00301 #endif //DO_IMPLEMENT_...
00302
00303
00304 #endif // DYNOBJLIB_H
00305
00306