mapnik/src/plugin.cpp

107 lines
2.6 KiB
C++
Raw Normal View History

2006-03-31 12:32:02 +02:00
/*****************************************************************************
2012-02-02 02:53:35 +01:00
*
2006-03-31 12:32:02 +02:00
* This file is part of Mapnik (c++ mapping toolkit)
2005-06-14 17:06:59 +02:00
*
* Copyright (C) 2011 Artem Pavlenko
2005-06-14 17:06:59 +02:00
*
2006-03-31 12:32:02 +02:00
* 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.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
2005-06-14 17:06:59 +02:00
* but WITHOUT ANY WARRANTY; without even the implied warranty of
2006-03-31 12:32:02 +02:00
* 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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
2005-06-14 17:06:59 +02:00
*
2006-03-31 12:32:02 +02:00
*****************************************************************************/
2005-06-14 17:06:59 +02:00
#include <mapnik/plugin.hpp>
#include <stdexcept>
#ifdef _WINDOWS
#include <windows.h>
#define handle HMODULE
#define dlsym GetProcAddress
#define dlclose FreeLibrary
#define dlerror GetLastError
#else
#include <dlfcn.h>
#define handle void *
#endif
// TODO - handle/report dlerror
2005-06-14 17:06:59 +02:00
namespace mapnik
{
struct _mapnik_lib_t {
handle dl;
};
PluginInfo::PluginInfo(std::string const& filename,
std::string const& library_name)
: filename_(filename),
name_(),
module_(new mapnik_lib_t)
{
#ifdef _WINDOWS
if (module_) module_->dl = LoadLibraryA(filename.c_str());
#else
if (module_) module_->dl = dlopen(filename.c_str(),RTLD_LAZY);
#endif
if (module_ && module_->dl)
{
name_func* name = reinterpret_cast<name_func*>(dlsym(module_->dl, library_name.c_str()));
if (name) name_ = name();
}
}
2005-06-14 17:06:59 +02:00
2010-06-02 13:03:30 +02:00
PluginInfo::~PluginInfo()
{
if (module_)
2005-06-14 17:06:59 +02:00
{
if (module_->dl) dlclose(module_->dl),module_->dl=0;
delete module_;
2005-06-14 17:06:59 +02:00
}
2010-06-02 13:03:30 +02:00
}
2005-06-14 17:06:59 +02:00
void * PluginInfo::get_symbol(std::string const& sym_name) const
{
return dlsym(module_->dl, sym_name.c_str());
}
2012-09-03 19:27:48 +02:00
std::string const& PluginInfo::name() const
2010-06-02 13:03:30 +02:00
{
return name_;
}
2005-06-14 17:06:59 +02:00
bool PluginInfo::valid() const
2010-06-02 13:03:30 +02:00
{
if (module_ && module_->dl && !name_.empty()) return true;
return false;
2010-06-02 13:03:30 +02:00
}
2005-06-14 17:06:59 +02:00
std::string PluginInfo::get_error() const
{
return std::string("could not open: '") + name_ + "'";
}
void PluginInfo::init()
{
// do any initialization needed
}
void PluginInfo::exit()
{
// do any shutdown needed
}
2005-06-14 17:06:59 +02:00
}