00001 /* 00002 * ExternalInterface.cc 00003 * 00004 * Smalltalk like class library for C++ 00005 * Interface to dynamicaly loaded libraries. 00006 * 00007 * Copyright (c) 2003-2005 Milan Cermak 00008 */ 00009 /* 00010 * This library is free software; you can redistribute it and/or 00011 * modify it under the terms of the GNU Lesser General Public 00012 * License as published by the Free Software Foundation; either 00013 * version 2.1 of the License, or (at your option) any later version. 00014 * 00015 * This library is distributed in the hope that it will be useful, 00016 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00017 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00018 * Lesser General Public License for more details. 00019 * 00020 * You should have received a copy of the GNU Lesser General Public 00021 * License along with this library; if not, write to the Free Software 00022 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 00023 */ 00024 #include <dlfcn.h> 00025 00026 #include <stlib/external/ExternalInterface.h> 00027 #include <stlib/external/EObjNotFoundError.h> 00028 #include <stlib/external/LibNotFoundError.h> 00029 00030 #include <stlib/String.h> 00031 00032 #if !defined(NOGC) 00033 /* It seems this declaration is missing in the GC headers */ 00034 extern "C" void *GC_dlopen(const char *libName, int flag); 00035 #endif 00036 00037 namespace External { 00038 00039 ExternalInterface::ExternalInterface(String *library) 00040 { 00041 libName = library; 00042 libHandler = dlopen(libName->asCString(), RTLD_NOW); 00043 if (libHandler == NULL) { 00044 (new LibraryNotFoundError(libName, new String(dlerror())))->raise(); 00045 } 00046 } 00047 00048 ExternalInterface::~ExternalInterface(void) 00049 { 00050 dlclose(libHandler); 00051 } 00052 00053 /* Private protocol */ 00054 void *ExternalInterface::symbol(String *selector) 00055 { 00056 void *funcHandler; 00057 const char *errMsg; 00058 00059 funcHandler = dlsym(libHandler, selector->asCString()); 00060 if ((errMsg = dlerror()) != NULL) { 00061 (new ExternalObjectNotFoundError(selector, new String(errMsg)))->raiseFrom(this); 00062 } 00063 return funcHandler; 00064 } 00065 00066 };