dl.hh
/* dl.hh - load dynamic libraries
* Copyright 2005 Bas Wijnen <wijnen@debian.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 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 General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef SHEVEK_DL_HH
#define SHEVEK_DL_HH
#include <string>
#include "dlfcn.h"
#include "refbase.hh"
#include "error.hh"
namespace shevek
{
class dl : public refbase
{
void *m_handle;
public:
static Glib::RefPtr <dl> create () { return Glib::RefPtr <dl> (new dl ()); }
~dl ();
void open (std::string const &file = std::string ());
void close ();
template <typename T> T &get (std::string const &name);
protected:
dl ();
};
template <typename T> T &dl::get (std::string const &name)
{
if (!m_handle)
shevek_error ("unable to get symbol: no valid handle");
union
{
void *input;
T *output;
} hack;
hack.input = dlsym (m_handle, name.c_str ());
if (!hack.input)
shevek_error (ostring ("unable to get symbol: %s", dlerror ()));
return *hack.output;
}
}
#endif
shevek::ostring
shevek::ostring is a C++ version of printf.
Definition: iostring.hh:322
shevek::dl
Load symbols from dynamic libraries.
Definition: dl.hh:31
shevek::file
Use normal files with the fd class.
Definition: file.hh:26
shevek::refbase
Base class for classes which want reference counting through Glib::RefPtr.
Definition: refbase.hh:27
shevek::dl::open
void open(std::string const &file=std::string())
Open a shared library.
shevek::dl::close
void close()
Close the library, freeing the resources.
shevek
Definition: args.hh:52
shevek::dl::get
T & get(std::string const &name)
Get a symbol from the library.
Definition: dl.hh:53
shevek::dl::~dl
~dl()
Close object and free structures.
shevek::dl::create
static Glib::RefPtr< dl > create()
Create a new dl object.
Definition: dl.hh:36