00001 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- 00002 * 00003 * XPLC - Cross-Platform Lightweight Components 00004 * Copyright (C) 2002-2004, Net Integration Technologies, Inc. 00005 * Copyright (C) 2002-2004, Pierre Phaneuf 00006 * Copyright (C) 2002-2004, Stéphane Lajoie 00007 * 00008 * This library is free software; you can redistribute it and/or 00009 * modify it under the terms of the GNU Lesser General Public License 00010 * as published by the Free Software Foundation; either version 2.1 of 00011 * the License, or (at your option) any later version. 00012 * 00013 * This library is distributed in the hope that it will be useful, but 00014 * WITHOUT ANY WARRANTY; without even the implied warranty of 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00016 * Lesser General Public License for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License along with this library; if not, write to the Free Software 00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00021 * USA 00022 */ 00023 00024 #include <assert.h> 00025 #include <xplc/ICategoryManager.h> 00026 #include <xplc/uuidops.h> 00027 #include "moduleloader.h" 00028 #include "loader.h" 00029 00030 UUID_MAP_BEGIN(ModuleLoader) 00031 UUID_MAP_ENTRY(IObject) 00032 UUID_MAP_ENTRY(IModuleLoader) 00033 UUID_MAP_END 00034 00035 UUID_MAP_BEGIN(Module) 00036 UUID_MAP_ENTRY(IObject) 00037 UUID_MAP_ENTRY(IServiceHandler) 00038 UUID_MAP_ENTRY(IModule) 00039 UUID_MAP_END 00040 00041 IModule* ModuleLoader::loadModule(const char* modulename) { 00042 return Module::loadModule(modulename); 00043 } 00044 00045 Module* Module::loadModule(const char* modulename) { 00046 XPLC_ModuleInfo* moduleinfo = 0; 00047 void* dlh; 00048 const char* err; 00049 00050 err = loaderOpen(modulename, &dlh); 00051 if(err) 00052 return NULL; 00053 00054 err = loaderSymbol(dlh, "XPLC_Module", 00055 reinterpret_cast<void**>(&moduleinfo)); 00056 if(err 00057 || !moduleinfo 00058 || moduleinfo->magic != XPLC_MODULE_MAGIC) { 00059 loaderClose(dlh); 00060 return NULL; 00061 } 00062 00063 switch(moduleinfo->version_major) { 00064 #ifdef UNSTABLE 00065 case -1: 00066 /* nothing to do */ 00067 break; 00068 #endif 00069 default: 00070 loaderClose(dlh); 00071 return NULL; 00072 }; 00073 00074 return new Module(dlh, moduleinfo); 00075 } 00076 00077 Module::Module(void* aHandle, const XPLC_ModuleInfo* aModuleInfo): 00078 handle(aHandle), 00079 moduleinfo(aModuleInfo) 00080 { 00081 assert(moduleinfo); 00082 00083 if(moduleinfo->categories) { 00084 IServiceManager* servmgr; 00085 IObject* obj; 00086 ICategoryManager* catmgr; 00087 const XPLC_CategoryEntry* entry; 00088 00089 servmgr = XPLC_getServiceManager(); 00090 assert(servmgr); 00091 00092 obj = servmgr->getObject(XPLC_categoryManager); 00093 assert(obj); 00094 00095 servmgr->release(); 00096 00097 catmgr = mutate<ICategoryManager>(obj); 00098 assert(catmgr); 00099 00100 entry = moduleinfo->categories; 00101 while(entry->category != UUID_null && entry->uuid != UUID_null) { 00102 catmgr->registerComponent(entry->category, entry->uuid, entry->string); 00103 00104 ++entry; 00105 } 00106 00107 catmgr->release(); 00108 } 00109 } 00110 00111 IObject* Module::getObject(const UUID& cid) { 00112 const XPLC_ComponentEntry* entry = moduleinfo->components; 00113 IObject* obj = 0; 00114 00115 if(!entry) 00116 return NULL; 00117 00118 while(!obj && entry->uuid != UUID_null) { 00119 if(entry->uuid == cid) 00120 obj = entry->getObject(); 00121 00122 ++entry; 00123 } 00124 00125 return obj; 00126 } 00127 00128 Module::~Module() { 00129 /* 00130 * FIXME: Adding the conditional here is for future expansion, where 00131 * this class could be used for a non-dynamically loaded module. But 00132 * for some reason, the size of this file increases in an odd way 00133 * when it is added. This should be investigated. 00134 */ 00135 if(handle) 00136 loaderClose(handle); 00137 } 00138