added: plugin mechanism
files: core/plugin.h core/plugin.cpp core/pluginmsg.h added: directory 'main' with main.cpp (moved from 'core' directory) changed: the way of building nearly everything is in cmslu.so only main() is in cmslu and is dynamically linked with cmslu.so git-svn-id: svn://ttmath.org/publicrep/cmslu/trunk@516 e52654a7-88a9-db11-a3e9-0013d4bc506e
This commit is contained in:
127
core/plugin.cpp
Executable file
127
core/plugin.cpp
Executable file
@@ -0,0 +1,127 @@
|
||||
/*
|
||||
* This file is a part of CMSLU -- Content Management System like Unix
|
||||
* and is not publicly distributed
|
||||
*
|
||||
* Copyright (c) 2008-2009, Tomasz Sowa
|
||||
* All rights reserved.
|
||||
*
|
||||
*/
|
||||
|
||||
#include <dlfcn.h>
|
||||
#include "plugin.h"
|
||||
#include "pluginmsg.h"
|
||||
|
||||
|
||||
|
||||
|
||||
void Plugin::UnloadPlugins()
|
||||
{
|
||||
size_t i;
|
||||
|
||||
slots.clear();
|
||||
|
||||
for(i=0 ; i<plugins.size() ; ++i)
|
||||
dlclose(plugins[i]);
|
||||
|
||||
plugins.clear();
|
||||
}
|
||||
|
||||
|
||||
Plugin::Plugin()
|
||||
{
|
||||
}
|
||||
|
||||
|
||||
Plugin::~Plugin()
|
||||
{
|
||||
UnloadPlugins();
|
||||
}
|
||||
|
||||
|
||||
void Plugin::LoadPlugins(const std::vector<std::string> & plugins)
|
||||
{
|
||||
size_t i;
|
||||
|
||||
for(i=0 ; i<plugins.size() ; ++i)
|
||||
{
|
||||
LoadPlugin(plugins[i]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
void Plugin::LoadPlugin(const std::string & filename)
|
||||
{
|
||||
LoadPlugin(filename.c_str());
|
||||
}
|
||||
|
||||
|
||||
void Plugin::LoadPlugin(const char * filename)
|
||||
{
|
||||
void * p = dlopen(filename, RTLD_NOW | RTLD_LOCAL);
|
||||
|
||||
if( !p )
|
||||
{
|
||||
log << log1 << "Pl: cannot load a plugin: " << filename << logend;
|
||||
|
||||
/*
|
||||
const char * t = dlerror();
|
||||
if( t )
|
||||
log << log1 << t << logend;
|
||||
*/
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
Fun fun = (Fun)dlfunc(p, "Init");
|
||||
|
||||
if( !fun )
|
||||
{
|
||||
log << log1 << "Pl: cannot load a plugin: " << filename << " (there is no Init() function)" << logend;
|
||||
dlclose(p);
|
||||
return;
|
||||
}
|
||||
|
||||
arg.Clear();
|
||||
|
||||
if( fun(&arg) )
|
||||
{
|
||||
log << log1 << "Pl: plugin loaded: " << filename << logend;
|
||||
plugins.push_back(p);
|
||||
}
|
||||
else
|
||||
{
|
||||
log << log1 << "Pl: plugin Init() returned false (" << filename << ") " << logend;
|
||||
dlclose(p);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
Arg * Plugin::Call(int message, void * a, void * a2, void * a3)
|
||||
{
|
||||
Slots::iterator i = slots.lower_bound(message);
|
||||
arg.Clear();
|
||||
arg.app = a;
|
||||
arg.app2 = a2;
|
||||
arg.app3 = a3;
|
||||
|
||||
for( ; i!=slots.end() && i->first==message ; ++i )
|
||||
{
|
||||
if( i->second(&arg) )
|
||||
arg.ret_true++;
|
||||
else
|
||||
arg.ret_false++;
|
||||
}
|
||||
|
||||
return &arg;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
void Plugin::Assign(int message, Fun fun)
|
||||
{
|
||||
slots.insert( std::make_pair(message, fun) );
|
||||
}
|
Reference in New Issue
Block a user