HyperspaceExplorer 0.7.1
PluginCreator.h
00001 /*
00002 Hyperspace Explorer - visualizing higher-dimensional geometry
00003 Copyright (C) 2010  Lene Preuss <lene.preuss@gmail.com>
00004 
00005 This program is free software; you can redistribute it and/or modify
00006 it under the terms of the GNU General Public License as published by
00007 the Free Software Foundation; either version 2 of the License, or
00008 (at your option) any later version.
00009 
00010 This program is distributed in the hope that it will be useful,
00011 but WITHOUT ANY WARRANTY; without even the implied warranty of
00012 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00013 GNU General Public License for more details.
00014 
00015 You should have received a copy of the GNU General Public License along
00016 with this program; if not, write to the Free Software Foundation, Inc.,
00017 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
00018 
00019 */
00020 
00021 #ifndef PLUGIN_CREATOR_H
00022 #define PLUGIN_CREATOR_H
00023 
00024 namespace UI {
00025     namespace Dialogs {
00027 
00039         class PluginCreator {
00041 
00044             template <class F> class LoadFunctionHelper {
00045                 public:
00046                     static bool functionPresent(const QString &, QDialog *);
00047             };
00048 
00049             public:
00050 
00051                 virtual ~PluginCreator() { }    
00052 
00054 
00055                 QString libraryName () { return LibraryName; }
00056 
00057             protected:
00058                 bool loadFunction(const QString &, QDialog *);
00059                 bool checkValidity(const QString &, const QString &, QDialog *);
00060 
00061                 bool compile (QString);
00062                 bool link (QString);
00063 
00064             private:
00066                 virtual bool loadFunction() = 0;
00068                 virtual bool functionPresent(const QString &) = 0;
00070                 virtual void writeSource() = 0;
00071 
00072                 QString LibraryName;    
00073         };
00074     }
00075 }
00076 
00077 #include <dlfcn.h>
00078 #include <iostream>
00079 #include <QFile>
00080 #include <QMessageBox>
00081 
00082 namespace UI {
00083     namespace Dialogs {
00085 
00093         template <class F>
00094                 bool PluginCreator::LoadFunctionHelper<F>::functionPresent(
00095                     const QString &libName, QDialog *parent) {
00096             void *handle;
00097             F *f;
00098             const char *error;
00099 
00100             if (!QFile::exists (libName)) {
00101                 QMessageBox::warning (parent, "Error opening library",
00102                                     "Library does not exist: "+libName);
00103                 std::cerr << "Library does not exist: "+libName.toStdString()
00104                         << std::endl;
00105                 return false;
00106             }
00107 
00108             handle = dlopen (libName.toStdString().c_str(), RTLD_LAZY);
00109             if (!handle) {
00110                 QMessageBox::warning (parent, "Error opening library", dlerror());
00111                 return false;
00112             }
00113 
00114             f = (F *)dlsym(handle, "f");
00115 
00116             if (f == NULL) {
00117                 if ((error = dlerror()) != NULL)  {
00118                     QMessageBox::warning (parent, "Error finding function", error);
00119                     abort ();
00120                     return false;
00121                 }
00122             }
00123 
00124             dlclose(handle);
00125 
00126             return true;
00127         }
00128     }
00129 }
00130 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Friends

Generated on Mon Apr 9 2012 20:25:16 for HyperspaceExplorer 0.7.1 by doxygen 1.7.4  -  Hosted bySourceForge.net Logo