ResidualVM logo ResidualVM website - Forums - Contact us BuildBot - Doxygen - Wiki curved edge

posix-provider.cpp

Go to the documentation of this file.
00001 /* ScummVM - Graphic Adventure Engine
00002  *
00003  * ScummVM is the legal property of its developers, whose names
00004  * are too numerous to list here. Please refer to the COPYRIGHT
00005  * file distributed with this source distribution.
00006  *
00007  * This program is free software; you can redistribute it and/or
00008  * modify it under the terms of the GNU General Public License
00009  * as published by the Free Software Foundation; either version 2
00010  * of the License, or (at your option) any later version.
00011  *
00012  * This program is distributed in the hope that it will be useful,
00013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00015  * GNU General Public License for more details.
00016  *
00017  * You should have received a copy of the GNU General Public License
00018  * along with this program; if not, write to the Free Software
00019  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
00020  *
00021  */
00022 
00023 #include "common/scummsys.h"
00024 
00025 #if defined(DYNAMIC_MODULES) && defined(POSIX)
00026 
00027 #include "backends/plugins/posix/posix-provider.h"
00028 #include "backends/plugins/dynamic-plugin.h"
00029 #include "common/fs.h"
00030 
00031 #include <dlfcn.h>
00032 
00033 
00034 class POSIXPlugin : public DynamicPlugin {
00035 protected:
00036     void *_dlHandle;
00037 
00038     virtual VoidFunc findSymbol(const char *symbol) {
00039         void *func = dlsym(_dlHandle, symbol);
00040         if (!func)
00041             warning("Failed loading symbol '%s' from plugin '%s' (%s)", symbol, _filename.c_str(), dlerror());
00042 
00043         // FIXME HACK: This is a HACK to circumvent a clash between the ISO C++
00044         // standard and POSIX: ISO C++ disallows casting between function pointers
00045         // and data pointers, but dlsym always returns a void pointer. For details,
00046         // see e.g. <http://www.trilithium.com/johan/2004/12/problem-with-dlsym/>.
00047         assert(sizeof(VoidFunc) == sizeof(func));
00048         VoidFunc tmp;
00049         memcpy(&tmp, &func, sizeof(VoidFunc));
00050         return tmp;
00051     }
00052 
00053 public:
00054     POSIXPlugin(const Common::String &filename)
00055         : DynamicPlugin(filename), _dlHandle(0) {}
00056 
00057     bool loadPlugin() {
00058         assert(!_dlHandle);
00059         _dlHandle = dlopen(_filename.c_str(), RTLD_LAZY);
00060 
00061         if (!_dlHandle) {
00062             warning("Failed loading plugin '%s' (%s)", _filename.c_str(), dlerror());
00063             return false;
00064         }
00065 
00066         return DynamicPlugin::loadPlugin();
00067     }
00068 
00069     void unloadPlugin() {
00070         DynamicPlugin::unloadPlugin();
00071         if (_dlHandle) {
00072             if (dlclose(_dlHandle) != 0)
00073                 warning("Failed unloading plugin '%s' (%s)", _filename.c_str(), dlerror());
00074             _dlHandle = 0;
00075         }
00076     }
00077 };
00078 
00079 
00080 Plugin *POSIXPluginProvider::createPlugin(const Common::FSNode &node) const {
00081     return new POSIXPlugin(node.getPath());
00082 }
00083 
00084 
00085 #endif // defined(DYNAMIC_MODULES) && defined(POSIX)


Generated on Sat Mar 23 2019 05:01:57 for ResidualVM by doxygen 1.7.1
curved edge   curved edge