Update of /cvsroot/pclasses/pclasses2/src/System
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv10123/src/System
Added Files:
Plugin.cpp
Log Message:
- Added new PluginManager based on the new Factory code
--- NEW FILE: Plugin.cpp ---
/***************************************************************************
* Copyright (C) 2005 by Christian Prochnow *
* cp...@se... *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU Library General Public License as *
* published by the Free Software Foundation; either version 2 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 Library General Public *
* License along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "pclasses/pclasses-config.h"
#include "pclasses/Factory.h"
#include "pclasses/Trace.h"
#include "pclasses/System/Plugin.h"
#include "pclasses/System/Directory.h"
namespace P {
namespace System {
PluginManager::PluginManager()
{
P_TRACE(PluginManager) << "PluginManager()";
}
PluginManager::~PluginManager()
{
}
PathFinder& PluginManager::pathFinder(const std::string& ifaceType)
{
PathFinderMap::iterator i = _pathFinders.find(ifaceType);
if(i == _pathFinders.end())
{
P_TRACE(PluginManager) << "add PathFinder for ifaceType=" << ifaceType;
PathFinder& pathFinder = _pathFinders[ifaceType];
pathFinder.addExtension(std::string(".")+SharedLib::extension());
return pathFinder;
}
return i->second;
}
void PluginManager::addPluginDir(const std::string& ifaceType,
const Unicode::String& dir) throw(SystemError)
{
P_TRACE(PluginManager) << "addPluginDir(" << ifaceType << ", " << dir << ")";
const Directory d(dir); // throws on error
d.begin(); // avoid 'unused var' warning.
// ^^^ need to find a more elegant solution for that.
pathFinder(ifaceType).addPath( dir );
}
SharedLib* PluginManager::addPlugin(const std::string& ifaceType,
const Unicode::String& so_name) throw(SystemError)
{
// look for cached entry:
PluginMap::const_iterator it = _pluginMap.find( so_name );
if( _pluginMap.end() != it )
{
return (*it).second;
}
// find a DLL file:
Unicode::String fn = pathFinder(ifaceType).find( so_name );
if( fn.empty() )
return 0;
// check for entry cached under DLL's file name:
it = _pluginMap.find( fn );
if( _pluginMap.end() != it )
{
return (*it).second;
}
// open DLL and, if workie, cache it:
SharedLib * shl = new SharedLib(fn);
_pluginMap.insert(std::make_pair(fn,shl));
return shl;
}
PluginManager& PluginManager::instance()
{
static PluginManager inst;
return inst;
}
class PluginTypeLoader {
public:
PluginTypeLoader()
{
FactoryBase::registerLoader(&loader);
P_TRACE(PluginTypeLoader) << "PluginTypeLoader registered with FactoryBase";
}
static void loader(const std::string& ifaceType,
const std::string& contextType, const std::string& key)
{
P_TRACE_GLOBAL() << "PluginTypeLoader::loader() ifaceType="
<< ifaceType << ", contextType=" << contextType << ", key=" << key;
PluginManager& pluginMgr = PluginManager::instance();
pluginMgr.addPlugin(ifaceType, key);
}
};
PluginTypeLoader pluginLoader;
} // !namespace System
} // !namespace P
|