summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2014-10-08 09:36:50 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2014-10-08 09:36:50 +0200
commitd6d7bbc9c32d5c060e48a3cfae006871f756cfaf (patch)
tree6bc2e9b3f95163edd3948f7e234becd4ee9d4f1b
parent48fece09164ce52753579a7225bc471d1fa44dea (diff)
downloadcrawler-d6d7bbc9c32d5c060e48a3cfae006871f756cfaf.tar.gz
crawler-d6d7bbc9c32d5c060e48a3cfae006871f756cfaf.tar.bz2
added flags to module loader, CLOSE_DEFERRED is deferring dlclose with a atexit
function, so that the tolua test runs now
-rwxr-xr-xinclude/module/ModuleLoader.hpp70
-rwxr-xr-xtests/modules/test4.cpp2
-rwxr-xr-xtests/tolua/test1.cpp4
3 files changed, 55 insertions, 21 deletions
diff --git a/include/module/ModuleLoader.hpp b/include/module/ModuleLoader.hpp
index 2ae8421..9340166 100755
--- a/include/module/ModuleLoader.hpp
+++ b/include/module/ModuleLoader.hpp
@@ -7,6 +7,7 @@
#include <stdexcept>
#include <typeinfo>
#include <sstream>
+#include <cstdlib>
#ifndef _WIN32
#include <dlfcn.h>
@@ -21,13 +22,15 @@
#include "TypeList.hpp"
#include "TypeInfo.hpp"
-template< typename Interface, typename CtorParams = NullType >
-struct Module {
#ifndef _WIN32
- void *handle;
+typedef void * ModuleHandle;
#else
- HMODULE handle;
+typedef HMODULE ModuleHandle;
#endif
+
+template< typename Interface, typename CtorParams = NullType >
+struct Module {
+ ModuleHandle handle;
void *user_data;
ModuleRegistry< Interface, CtorParams > *registry;
@@ -43,6 +46,26 @@ struct Module {
}
};
+static size_t nofDeferredModules = 0;
+static size_t sizeofDeferredModules = 0;
+static ModuleHandle* deferredModules = 0;
+
+const int CLOSE_ON_DTOR = 0;
+const int CLOSE_DEFERRED = 1;
+
+const int DEFAULT_FLAGS = CLOSE_ON_DTOR;
+
+static void deferredClose( void )
+{
+ for( size_t i = 0; i <= nofDeferredModules - 1; i++ ) {
+#ifndef _WIN32
+ dlclose( deferredModules[i] );
+#else
+ (void)FreeLibrary( deferredModules[i] );
+#endif
+ }
+}
+
template< typename Interface, typename CtorParams = NullType >
class BaseModuleLoader {
@@ -53,10 +76,12 @@ class BaseModuleLoader {
protected:
mapType m_modules;
+ int m_flags;
public:
- BaseModuleLoader( const std::vector<std::string> files, void *user_data = 0 )
+ BaseModuleLoader( const std::vector<std::string> files, int flags = DEFAULT_FLAGS, void *user_data = 0 )
+ : m_flags( flags )
{
for( std::vector<std::string>::const_iterator it = files.begin( ); it != files.end( ); it++ ) {
@@ -114,11 +139,22 @@ class BaseModuleLoader {
}
}
if( (*it).second.handle ) {
+ if( m_flags & CLOSE_DEFERRED ) {
+ if( nofDeferredModules == 0 ) {
+ atexit( &deferredClose );
+ }
+ if( nofDeferredModules + 1 >= sizeofDeferredModules ) {
+ sizeofDeferredModules = ( sizeofDeferredModules + 1 ) * 2;
+ deferredModules = (ModuleHandle *)realloc( deferredModules, sizeofDeferredModules * sizeof( ModuleHandle * ) );
+ deferredModules[nofDeferredModules++] = (*it).second.handle;
+ }
+ } else {
#ifndef _WIN32
- dlclose( (*it).second.handle );
+ dlclose( (*it).second.handle );
#else
- (void)FreeLibrary( (*it).second.handle );
+ (void)FreeLibrary( (*it).second.handle );
#endif
+ }
}
}
@@ -145,8 +181,8 @@ class ModuleLoader< Interface, NullType > : public BaseModuleLoader< Interface,
{
public:
- ModuleLoader< Interface >( const std::vector<std::string> files, void *user_data = 0 )
- : BaseModuleLoader< Interface >( files, user_data ) { }
+ ModuleLoader< Interface >( const std::vector<std::string> files, int flags = DEFAULT_FLAGS, void *user_data = 0 )
+ : BaseModuleLoader< Interface >( files, flags, user_data ) { }
Interface *create( std::string subclass )
{
@@ -174,8 +210,8 @@ class ModuleLoader< Interface, TYPELIST_1( T1 ) > : public BaseModuleLoader< Int
{
public:
- ModuleLoader< Interface, TYPELIST_1( T1 ) >( const std::vector<std::string> files, void *user_data = 0 )
- : BaseModuleLoader< Interface, TYPELIST_1( T1 ) >( files, user_data ) { }
+ ModuleLoader< Interface, TYPELIST_1( T1 ) >( const std::vector<std::string> files, int flags = DEFAULT_FLAGS, void *user_data = 0 )
+ : BaseModuleLoader< Interface, TYPELIST_1( T1 ) >( files, flags, user_data ) { }
Interface *create( std::string subclass, T1 t1 )
{
@@ -203,8 +239,8 @@ class ModuleLoader< Interface, TYPELIST_2( T1, T2 ) > : public BaseModuleLoader<
{
public:
- ModuleLoader< Interface, TYPELIST_2( T1, T2 ) >( const std::vector<std::string> files, void *user_data = 0 )
- : BaseModuleLoader< Interface, TYPELIST_2( T1, T2 ) >( files, user_data ) { }
+ ModuleLoader< Interface, TYPELIST_2( T1, T2 ) >( const std::vector<std::string> files, int flags = DEFAULT_FLAGS, void *user_data = 0 )
+ : BaseModuleLoader< Interface, TYPELIST_2( T1, T2 ) >( files, flags, user_data ) { }
Interface *create( std::string subclass, T1 t1, T2 t2 )
{
@@ -232,8 +268,8 @@ class ModuleLoader< Interface, TYPELIST_3( T1, T2, T3 ) > : public BaseModuleLoa
{
public:
- ModuleLoader< Interface, TYPELIST_3( T1, T2, T3 ) >( const std::vector<std::string> files, void *user_data = 0 )
- : BaseModuleLoader< Interface, TYPELIST_3( T1, T2, T3 ) >( files, user_data ) { }
+ ModuleLoader< Interface, TYPELIST_3( T1, T2, T3 ) >( const std::vector<std::string> files, int flags = DEFAULT_FLAGS, void *user_data = 0 )
+ : BaseModuleLoader< Interface, TYPELIST_3( T1, T2, T3 ) >( files, flags, user_data ) { }
Interface *create( std::string subclass, T1 t1, T2 t2, T3 t3 )
{
@@ -261,8 +297,8 @@ class ModuleLoader< Interface, TYPELIST_4( T1, T2, T3, T4 ) > : public BaseModul
{
public:
- ModuleLoader< Interface, TYPELIST_4( T1, T2, T3, T4 ) >( const std::vector<std::string> files, void *user_data = 0 )
- : BaseModuleLoader< Interface, TYPELIST_4( T1, T2, T3, T4 ) >( files, user_data ) { }
+ ModuleLoader< Interface, TYPELIST_4( T1, T2, T3, T4 ) >( const std::vector<std::string> files, int flags = DEFAULT_FLAGS, void *user_data = 0 )
+ : BaseModuleLoader< Interface, TYPELIST_4( T1, T2, T3, T4 ) >( files, flags, user_data ) { }
Interface *create( std::string subclass, T1 t1, T2 t2, T3 t3, T4 t4 )
{
diff --git a/tests/modules/test4.cpp b/tests/modules/test4.cpp
index 4e71295..5630966 100755
--- a/tests/modules/test4.cpp
+++ b/tests/modules/test4.cpp
@@ -24,7 +24,7 @@ int main( void )
UserData user_data;
user_data.version = 47;
user_data.text.assign( "hello there" );
- ModuleLoader<Base> loader( modules, (void *)&user_data );
+ ModuleLoader<Base> loader( modules, DEFAULT_FLAGS, (void *)&user_data );
Base *obj = loader.create( "testmod4" );
c.print( "hello from main" );
diff --git a/tests/tolua/test1.cpp b/tests/tolua/test1.cpp
index 271544e..a3810e2 100755
--- a/tests/tolua/test1.cpp
+++ b/tests/tolua/test1.cpp
@@ -18,13 +18,11 @@ int main( int /* argc */, char *argv[] )
vector<string> modules;
modules.push_back( "./libtest1/mod_test.so" );
- ModuleLoader<Base> loader( modules, (void *)&luaVm );
+ ModuleLoader<Base> loader( modules, CLOSE_DEFERRED, (void *)&luaVm );
luaVm.loadSource( argv[1] );
luaVm.executeMain( );
- return 0;
-
} catch( exception &e ) {
LOG( logFATAL ) << "ERROR: " << e.what( );
return 1;