From: Christian P. <cp...@us...> - 2004-12-23 05:27:58
|
Update of /cvsroot/pclasses/pclasses2/src/System In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24631/src/System Modified Files: SharedLib.dl.cpp SharedLib.dyld.cpp SharedLib.shl.cpp SharedLib.win32.cpp Log Message: Added std::string support. Append platform specific file extenstion to shared lib name. Converted SharedLib::_handle to unsigned long. Index: SharedLib.dyld.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.dyld.cpp,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- SharedLib.dyld.cpp 22 Dec 2004 17:54:37 -0000 1.1.1.1 +++ SharedLib.dyld.cpp 23 Dec 2004 05:27:49 -0000 1.2 @@ -1,55 +1,89 @@ -/* - * P::Classes - Portable C++ Application Framework - * Copyright (C) 2000-2003 Christian Prochnow <cp...@se...> - * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2 of the License, or (at your option) any later version. - * - * This library is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA - */ +/*************************************************************************** + * Copyright (C) 2004 by Christian Prochnow * + * cp...@se... * + * * + * This program is free software; you can redistribute it and/or modify * + * it under the terms of the GNU Library General Public License as * + * published by the Free Software Foundation; either version 2 of the * + * License, or (at your option) any later version. * + * * + * This program is distributed in the hope that it will be useful, * + * but WITHOUT ANY WARRANTY; without even the implied warranty of * + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * + * GNU General Public License for more details. * + * * + * You should have received a copy of the GNU Library General Public * + * License along with this program; if not, write to the * + * Free Software Foundation, Inc., * + * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * + ***************************************************************************/ #include "pclasses/System/Sharedlib.h" + #include <mach-o/dyld.h> +#ifdef PCLASSES_WITH_STL +# include <string> +# include <sstream> +#endif + namespace P { namespace System { -struct SharedLib::Handle {}; - -SharedLib::SharedLib(const char* name, BindMode mode) throw(SystemError) +int BindMode2Flags(SharedLib::BindMode mode) { int flags = 0; switch(mode) { - case BindLazy: + case SharedLib::BindLazy: break; - case BindNow: + + case SharedLib::BindNow: flags = NSLINKMODULE_OPTION_BINDNOW; break; } - flags |= NSLINKMODULE_OPTION_RETURN_ON_ERROR; + return flags; +} + +SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) +{ + Unicode::String realName = name; + realName.append(".dyld"); + + int flags = BindMode2Flags(mode) | NSLINKMODULE_OPTION_RETURN_ON_ERROR; NSObjectFileImage file; NSObjectFileImageReturnCode ret; - ret = NSCreateObjectFileImageFromFile(name, &file); + ret = NSCreateObjectFileImageFromFile(realName.utf8(), &file); if(ret != NSObjectFileImageSuccess) throw SystemError(0, "Could not load shared library", P_SOURCEINFO); - NSModule out = NSLinkModule(file, name, flags); - _handle = (Handle*)out; + NSModule out = NSLinkModule(file, realName.utf8(), flags); + _handle = (unsigned long)out; } +#ifndef PCLASSES_WITH_STL +SharedLib::SharedLib(const std::string& name, BindMode mode) throw(SystemError) +{ + std::ostringstream realName; + realName << name; + realName << ".dyld"; + + int flags = BindMode2Flags(mode) | NSLINKMODULE_OPTION_RETURN_ON_ERROR; + + NSObjectFileImage file; + NSObjectFileImageReturnCode ret; + ret = NSCreateObjectFileImageFromFile(realName.c_str(), &file); + if(ret != NSObjectFileImageSuccess) + throw SystemError(0, "Could not load shared library", P_SOURCEINFO); + + NSModule out = NSLinkModule(file, realName.c_str(), flags); + _handle = (unsigned long)out; +} +#endif + SharedLib::~SharedLib() throw() { NSUnLinkModule((NSModule)_handle, NSUNLINKMODULE_OPTION_NONE); @@ -64,6 +98,13 @@ return (void*)addr; } +#ifdef PCLASSES_WITH_STL +void* SharedLib::operator[](const std::string& symbol) throw(RuntimeError) +{ + return operator[](symbol.c_str()); +} +#endif + } // !namespace System } // !namespace P Index: SharedLib.dl.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.dl.cpp,v retrieving revision 1.2 retrieving revision 1.3 diff -u -d -r1.2 -r1.3 --- SharedLib.dl.cpp 23 Dec 2004 04:32:18 -0000 1.2 +++ SharedLib.dl.cpp 23 Dec 2004 05:27:49 -0000 1.3 @@ -22,36 +22,56 @@ #include <dlfcn.h> #include <errno.h> -#include <string.h> + +#ifdef PCLASSES_WITH_STL +# include <string> +# include <sstream> +#endif namespace P { namespace System { -struct SharedLib::Handle {}; - -SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) +int BindMode2Flags(SharedLib::BindMode mode) { int flags = 0; switch(mode) { - case BindLazy: - flags = RTLD_LAZY; - break; - case BindNow: - flags = RTLD_NOW; - break; + case SharedLib::BindLazy: + flags = RTLD_LAZY; + break; + + case SharedLib::BindNow: + flags = RTLD_NOW; + break; } - //flags |= RTLD_GLOBAL; this causes a SIGSEGV when loading multiple plugins with same exported vars - Unicode::String so_name = name; - so_name.append(".so"); + return flags; +} - //_handle = (Handle*)dlopen(name.utf8(), flags); +SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) +{ + Unicode::String realName = name; + realName.append(".so"); + + //@fixme _handle = (unsigned long)dlopen(realName.utf8(), BindMode2Flags(mode)); if(!_handle) throw SystemError(errno, dlerror(), P_SOURCEINFO); } +#ifndef PCLASSES_WITH_STL +SharedLib::SharedLib(const std::string& name, BindMode mode) throw(SystemError) +{ + std::ostringstream realName; + realName << name; + realName << ".so"; + + _handle = (unsigned long)dlopen(realName.str().c_str(), BindMode2Flags(mode)); + if(!_handle) + throw SystemError(errno, dlerror(), P_SOURCEINFO); +} +#endif + SharedLib::~SharedLib() throw() { dlclose((void*)_handle); @@ -66,6 +86,13 @@ return addr; } +#ifdef PCLASSES_WITH_STL +void* SharedLib::operator[](const std::string& symbol) throw(RuntimeError) +{ + return operator[](symbol.c_str()); +} +#endif + } // !namespace System } // !namespace P Index: SharedLib.win32.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.win32.cpp,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- SharedLib.win32.cpp 22 Dec 2004 17:54:36 -0000 1.1.1.1 +++ SharedLib.win32.cpp 23 Dec 2004 05:27:49 -0000 1.2 @@ -21,22 +21,42 @@ #include "pclasses/System/SharedLib.h" #include <windows.h> +#ifdef PCLASSES_WITH_STL +# include <string> +# include <sstream> +#endif + namespace P { namespace System { -struct SharedLib::Handle {}; +SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) +{ + Unicode::String realName = name; + realName.append(".dll"); -SharedLib::SharedLib(const char* name, BindMode mode) throw(SystemError) + //@fixme _handle = (unsigned long)LoadLibrary(name); + if(!_handle) + throw SystemError(GetLastError(), "Could not load shared library", P_SOURCEINFO); +} + +#ifndef PCLASSES_WITH_STL +SharedLib::SharedLib(const std::string& name, BindMode mode) throw(SystemError) { - _handle = (Handle*)LoadLibrary(name); - if(_handle == 0) - throw SystemError(GetLastError(), "Could not load shared library", P_SOURCEINFO); + std::ostringstream realName; + realName << name; + realName << ".dll"; + + _handle = (unsigned long)LoadLibrary(realName.c_str()); + + if(!_handle) + throw SystemError(GetLastError(), "Could not load shared library", P_SOURCEINFO); } +#endif SharedLib::~SharedLib() throw() { - FreeLibrary((HMODULE)_handle); + FreeLibrary((HMODULE)_handle); } void* SharedLib::operator[](const char* symbol) throw(RuntimeError) @@ -48,6 +68,13 @@ return addr; } +#ifdef PCLASSES_WITH_STL +void* SharedLib::operator[](const std::string& symbol) throw(RuntimeError) +{ + return operator[](symbol.c_str()); +} +#endif + } // !namespace System } // !namespace P Index: SharedLib.shl.cpp =================================================================== RCS file: /cvsroot/pclasses/pclasses2/src/System/SharedLib.shl.cpp,v retrieving revision 1.1.1.1 retrieving revision 1.2 diff -u -d -r1.1.1.1 -r1.2 --- SharedLib.shl.cpp 22 Dec 2004 17:54:35 -0000 1.1.1.1 +++ SharedLib.shl.cpp 23 Dec 2004 05:27:49 -0000 1.2 @@ -19,32 +19,63 @@ ***************************************************************************/ #include "pclasses/System/SharedLib.h" + #include <dl.h> #include <errno.h> +#ifdef PCLASSES_WITH_STL +# include <string> +# include <sstream> +#endif + namespace P { namespace System { -struct SharedLib::Handle {}; - -SharedLib::SharedLib(const char* name, BindMode mode) throw(SystemError) +int BindMode2Flags(SharedLib::BindMode mode) { int flags = 0; switch(mode) { - case BindLazy: + case SharedLib::BindLazy: flags = BIND_DEFERRED; break; - case BindNow: + + case SharedLib::BindNow: flags = BIND_IMMEDIATE; break; } - - _handle = (Handle*)shl_load(name, flags); + + return flags; +} + +SharedLib::SharedLib(const Unicode::String& name, BindMode mode) throw(SystemError) +{ + Unicode::String realName = name; + realName.append(".sl"); + + _handle = (unsigned long)shl_load(realName.utf8(), BindMode2Flags(mode)); + + //@fixme dlerror() on hpux ?? + if(!_handle) + throw SystemError(errno, dlerror(), P_SOURCEINFO); +} + +#ifndef PCLASSES_WITH_STL +SharedLib::SharedLib(const std::string& name, BindMode mode) throw(SystemError) +{ + std::ostringstream realName; + realName << name; + realName << ".sl"; + + _handle = (unsigned long)shl_load(realName.str().c_str(), + BindMode2Flags(mode)); + + //@fixme dlerror() on hpux ?? if(!_handle) throw SystemError(errno, dlerror(), P_SOURCEINFO); } +#endif SharedLib::~SharedLib() throw() { @@ -60,6 +91,13 @@ return address; } +#ifdef PCLASSES_WITH_STL +void* SharedLib::operator[](const std::string& symbol) throw(RuntimeError) +{ + return operator[](symbol.c_str()); +} +#endif + } // !namespace System } // !namespace P |