libshevek
|
00001 /* dl.hh - load dynamic libraries 00002 * Copyright 2005 Bas Wijnen <wijnen@debian.org> 00003 * 00004 * This program is free software: you can redistribute it and/or modify 00005 * it under the terms of the GNU General Public License as published by 00006 * the Free Software Foundation, either version 3 of the License, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details. 00013 * 00014 * You should have received a copy of the GNU General Public License 00015 * along with this program. If not, see <http://www.gnu.org/licenses/>. 00016 */ 00017 00018 #ifndef SHEVEK_DL_HH 00019 #define SHEVEK_DL_HH 00020 00021 #include <string> 00022 #include "dlfcn.h" 00023 #include "refbase.hh" 00024 #include "error.hh" 00025 00026 namespace shevek 00027 { 00029 00031 class dl : public refbase 00032 { 00033 void *m_handle; 00034 public: 00036 static Glib::RefPtr <dl> create () { return Glib::RefPtr <dl> (new dl ()); } 00038 ~dl (); 00040 void open (std::string const &file = std::string ()); 00042 00044 void close (); 00046 00048 template <typename T> T &get (std::string const &name); 00049 protected: 00050 dl (); 00051 }; 00052 00053 template <typename T> T &dl::get (std::string const &name) 00054 { 00055 if (!m_handle) 00056 shevek_error ("unable to get symbol: no valid handle"); 00057 union 00058 { 00059 void *input; 00060 T *output; 00061 } hack; 00062 hack.input = dlsym (m_handle, name.c_str ()); 00063 if (!hack.input) 00064 shevek_error (ostring ("unable to get symbol: %s", dlerror ())); 00065 return *hack.output; 00066 } 00067 } 00068 00069 #endif