summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndreas Baumann <mail@andreasbaumann.cc>2014-10-04 20:43:17 +0200
committerAndreas Baumann <mail@andreasbaumann.cc>2014-10-04 20:43:17 +0200
commit578f8d8b0bec9ba3e5a1f8b4bf26b66f58dd6752 (patch)
treea51aa18fa56847586e1ce87e4c4ee2a6997244ab
parentccf23e71918d2af7ad9d261a2b012836cd168d60 (diff)
downloadcrawler-578f8d8b0bec9ba3e5a1f8b4bf26b66f58dd6752.tar.gz
crawler-578f8d8b0bec9ba3e5a1f8b4bf26b66f58dd6752.tar.bz2
added init and destroy functions to modules
-rwxr-xr-xinclude/module/ModuleLoader.hpp8
-rwxr-xr-xinclude/module/ModuleRegistry.hpp94
-rwxr-xr-xtests/modules/GNUmakefile6
-rwxr-xr-xtests/modules/Makefile.W327
-rw-r--r--tests/modules/test4.MUST5
-rwxr-xr-xtests/modules/test4.cpp36
-rwxr-xr-xtests/modules/testmod/TestMod.cpp2
-rwxr-xr-xtests/modules/testmod2/TestMod2.cpp2
-rwxr-xr-xtests/modules/testmod3/TestMod3.cpp2
-rwxr-xr-xtests/modules/testmod4/GNUmakefile43
-rwxr-xr-xtests/modules/testmod4/Makefile.W3251
-rwxr-xr-xtests/modules/testmod4/TestMod4.cpp34
-rwxr-xr-xtests/modules/testmod4/TestMod4.hpp25
13 files changed, 283 insertions, 32 deletions
diff --git a/include/module/ModuleLoader.hpp b/include/module/ModuleLoader.hpp
index d3b7d00..02b368e 100755
--- a/include/module/ModuleLoader.hpp
+++ b/include/module/ModuleLoader.hpp
@@ -79,6 +79,10 @@ class BaseModuleLoader {
#endif
throw std::runtime_error( "missing module registry" );
}
+
+ if( m.registry->initModule != 0 ) {
+ m.registry->initModule( );
+ }
m_modules.insert( std::make_pair( m.registry->name, m ) );
}
@@ -88,6 +92,10 @@ class BaseModuleLoader {
{
for( typename mapType::iterator it = m_modules.begin( ); it != m_modules.end( ); it++ ) {
if( (*it).second.handle ) {
+ if( (*it).second.registry->destroyModule != 0 ) {
+ (*it).second.registry->destroyModule( );
+ }
+
#ifndef _WIN32
dlclose( (*it).second.handle );
#else
diff --git a/include/module/ModuleRegistry.hpp b/include/module/ModuleRegistry.hpp
index e217092..d8ee90c 100755
--- a/include/module/ModuleRegistry.hpp
+++ b/include/module/ModuleRegistry.hpp
@@ -11,12 +11,19 @@ struct ModuleRegistry;
template< typename Interface>
struct ModuleRegistry< Interface > {
std::string name;
+ void (*initModule)( );
+ void (*destroyModule)( );
Interface *(*create)( );
void (*destroy)( Interface *obj );
- ModuleRegistry( std::string _name, Interface *(*_create)( ),
+ ModuleRegistry( std::string _name,
+ void (*_initModule)( ),
+ void (*_destroyModule)( ),
+ Interface *(*_create)( ),
void (*_destroy)( Interface *obj ) )
- : name( _name ), create( _create ), destroy( _destroy )
+ : name( _name ),
+ initModule( _initModule ), destroyModule( _destroyModule ),
+ create( _create ), destroy( _destroy )
{
}
};
@@ -24,12 +31,19 @@ struct ModuleRegistry< Interface > {
template< typename Interface, typename P1 >
struct ModuleRegistry< Interface, TYPELIST_1( P1 ) > {
std::string name;
+ void (*initModule)( );
+ void (*destroyModule)( );
Interface *(*create)( P1 );
void (*destroy)( Interface *obj );
- ModuleRegistry( std::string _name, Interface *(*_create)( P1 ),
+ ModuleRegistry( std::string _name,
+ void (*_initModule)( ),
+ void (*_destroyModule)( ),
+ Interface *(*_create)( P1 ),
void (*_destroy)( Interface *obj ) )
- : name( _name ), create( _create ), destroy( _destroy )
+ : name( _name ),
+ initModule( _initModule ), destroyModule( _destroyModule ),
+ create( _create ), destroy( _destroy )
{
}
};
@@ -37,12 +51,19 @@ struct ModuleRegistry< Interface, TYPELIST_1( P1 ) > {
template< typename Interface, typename P1, typename P2 >
struct ModuleRegistry< Interface, TYPELIST_2( P1, P2 ) > {
std::string name;
+ void (*initModule)( );
+ void (*destroyModule)( );
Interface *(*create)( P1, P2 );
void (*destroy)( Interface *obj );
- ModuleRegistry( std::string _name, Interface *(*_create)( P1, P2 ),
+ ModuleRegistry( std::string _name,
+ void (*_initModule)( ),
+ void (*_destroyModule)( ),
+ Interface *(*_create)( P1, P2 ),
void (*_destroy)( Interface *obj ) )
- : name( _name ), create( _create ), destroy( _destroy )
+ : name( _name ),
+ initModule( _initModule ), destroyModule( _destroyModule ),
+ create( _create ), destroy( _destroy )
{
}
};
@@ -50,12 +71,19 @@ struct ModuleRegistry< Interface, TYPELIST_2( P1, P2 ) > {
template< typename Interface, typename P1, typename P2, typename P3 >
struct ModuleRegistry< Interface, TYPELIST_3( P1, P2, P3 ) > {
std::string name;
+ void (*initModule)( );
+ void (*destroyModule)( );
Interface *(*create)( P1, P2, P3 );
void (*destroy)( Interface *obj );
- ModuleRegistry( std::string _name, Interface *(*_create)( P1, P2, P3 ),
+ ModuleRegistry( std::string _name,
+ void (*_initModule)( ),
+ void (*_destroyModule)( ),
+ Interface *(*_create)( P1, P2, P3 ),
void (*_destroy)( Interface *obj ) )
- : name( _name ), create( _create ), destroy( _destroy )
+ : name( _name ),
+ initModule( _initModule ), destroyModule( _destroyModule ),
+ create( _create ), destroy( _destroy )
{
}
};
@@ -63,12 +91,19 @@ struct ModuleRegistry< Interface, TYPELIST_3( P1, P2, P3 ) > {
template< typename Interface, typename P1, typename P2, typename P3, typename P4 >
struct ModuleRegistry< Interface, TYPELIST_4( P1, P2, P3, P4 ) > {
std::string name;
+ void (*initModule)( );
+ void (*destroyModule)( );
Interface *(*create)( P1, P2, P3, P4 );
void (*destroy)( Interface *obj );
- ModuleRegistry( std::string _name, Interface *(*_create)( P1, P2, P3, P4 ),
+ ModuleRegistry( std::string _name,
+ void (*_initModule)( ),
+ void (*_destroyModule)( ),
+ Interface *(*_create)( P1, P2, P3, P4 ),
void (*_destroy)( Interface *obj ) )
- : name( _name ), create( _create ), destroy( _destroy )
+ : name( _name ),
+ initModule( _initModule ), destroyModule( _destroyModule ),
+ create( _create ), destroy( _destroy )
{
}
};
@@ -86,7 +121,7 @@ struct ModuleRegistry< Interface, TYPELIST_4( P1, P2, P3, P4 ) > {
#define DECLARE_MODULE( baseClass ) \
extern "C" DLLEXPORT ModuleRegistry<baseClass> registry ## _ ## baseClass;
-#define REGISTER_MODULE( name, baseClass, subClass ) \
+#define REGISTER_MODULE( name, initModule, destroyModule, baseClass, subClass ) \
static baseClass *create( ) \
{ \
return new subClass( ); \
@@ -97,14 +132,14 @@ static void destroy( baseClass *obj ) \
delete obj; \
} \
\
-ModuleRegistry<baseClass> registry ## _ ## baseClass( name, &create, &destroy );
+ModuleRegistry<baseClass> registry ## _ ## baseClass( name, initModule, destroyModule, &create, &destroy );
// 1 param macro
#define DECLARE_MODULE_1( baseClass, T1 ) \
extern "C" DLLEXPORT ModuleRegistry<baseClass, TYPELIST_1( T1 ) > registry ## _ ## baseClass;
-#define REGISTER_MODULE_1( name, baseClass, subClass, T1 ) \
+#define REGISTER_MODULE_1( name, initModule, destroyModule, baseClass, subClass, T1 ) \
static baseClass *create( T1 t1 ) \
{ \
return new subClass( t1 ); \
@@ -115,14 +150,14 @@ static void destroy( baseClass *obj ) \
delete obj; \
} \
\
-ModuleRegistry<baseClass, TYPELIST_1( T1 )> registry ## _ ## baseClass( name, &create, &destroy );
+ModuleRegistry<baseClass, TYPELIST_1( T1 )> registry ## _ ## baseClass( name, initModule, destroyModule, &create, &destroy );
// 2 param macro
#define DECLARE_MODULE_2( baseClass, T1, T2 ) \
extern "C" DLLEXPORT ModuleRegistry<baseClass, TYPELIST_2( T1, T2 ) > registry ## _ ## baseClass;
-#define REGISTER_MODULE_2( name, baseClass, subClass, T1, T2 ) \
+#define REGISTER_MODULE_2( name, initModule, destroyModule, baseClass, subClass, T1, T2 ) \
static baseClass *create( T1 t1, T2 t2 ) \
{ \
return new subClass( t1, t2 ); \
@@ -133,14 +168,14 @@ static void destroy( baseClass *obj ) \
delete obj; \
} \
\
-ModuleRegistry<baseClass, TYPELIST_2( T1, T2 )> registry ## _ ## baseClass( name, &create, &destroy );
+ModuleRegistry<baseClass, TYPELIST_2( T1, T2 )> registry ## _ ## baseClass( name, initModule, destroyModule, &create, &destroy );
// 3 param macro
#define DECLARE_MODULE_3( baseClass, T1, T2, T3 ) \
extern "C" DLLEXPORT ModuleRegistry<baseClass, TYPELIST_3( T1, T2, T3 ) > registry ## _ ## baseClass;
-#define REGISTER_MODULE_3( name, baseClass, subClass, T1, T2, T3 ) \
+#define REGISTER_MODULE_3( name, initModule, destroyModule, baseClass, subClass, T1, T2, T3 ) \
static baseClass *create( T1 t1, T2 t2, T3 t3 ) \
{ \
return new subClass( t1, t2, t3 ); \
@@ -151,14 +186,14 @@ static void destroy( baseClass *obj ) \
delete obj; \
} \
\
-ModuleRegistry<baseClass, TYPELIST_3( T1, T2, T3 )> registry ## _ ## baseClass( name, &create, &destroy );
+ModuleRegistry<baseClass, TYPELIST_3( T1, T2, T3 )> registry ## _ ## baseClass( name, initModule, destroyModule, &create, &destroy );
// 4 param macro
#define DECLARE_MODULE_4( baseClass, T1, T2, T3, T4 ) \
extern "C" DLLEXPORT ModuleRegistry<baseClass, TYPELIST_4( T1, T2, T3, T4 ) > registry ## _ ## baseClass;
-#define REGISTER_MODULE_4( name, baseClass, subClass, T1, T2, T3, T4 ) \
+#define REGISTER_MODULE_4( name, initModule, destroyModule, baseClass, subClass, T1, T2, T3, T4 ) \
static baseClass *create( T1 t1, T2 t2, T3 t3, T4 t4 ) \
{ \
return new subClass( t1, t2, t3, t4 ); \
@@ -169,7 +204,7 @@ static void destroy( baseClass *obj ) \
delete obj; \
} \
\
-ModuleRegistry<baseClass, TYPELIST_4( T1, T2, T3, T4 )> registry ## _ ## baseClass( name, &create, &destroy );
+ModuleRegistry<baseClass, TYPELIST_4( T1, T2, T3, T4 )> registry ## _ ## baseClass( name, initModule, destroyModule, &create, &destroy );
#else // SHARED
@@ -179,11 +214,20 @@ ModuleRegistry<baseClass, TYPELIST_4( T1, T2, T3, T4 )> registry ## _ ## baseCla
#define DECLARE_MODULE_3( baseClass, T1, T2, T3 )
#define DECLARE_MODULE_4( baseClass, T1, T2, T3, T4 )
-#define REGISTER_MODULE( name, baseClass, subClass )
-#define REGISTER_MODULE_1( name, baseClass, subClass, T1 )
-#define REGISTER_MODULE_2( name, baseClass, subClass, T1, T2 )
-#define REGISTER_MODULE_3( name, baseClass, subClass, T1, T2, T3 )
-#define REGISTER_MODULE_4( name, baseClass, subClass, T1, T2, T3, T4 )
+#define REGISTER_MODULE( name, initModule, destroyModule, baseClass, subClass ) \
+ModuleRegistry<baseClass> registry ## _ ## baseClass( name, initModule, destroyModule, 0, 0 );
+
+#define REGISTER_MODULE_1( name, initModule, destroyModule, baseClass, subClass, T1 ) \
+ModuleRegistry<baseClass, TYPELIST_1( T1 )> registry ## _ ## baseClass( name, initModule, destroyModule, 0, 0 );
+
+#define REGISTER_MODULE_2( name, initModule, destroyModule, baseClass, subClass, T1, T2 ) \
+ModuleRegistry<baseClass, TYPELIST_2( T1, T2 )> registry ## _ ## baseClass( name, initModule, destroyModule, 0, 0 );
+
+#define REGISTER_MODULE_3( name, initModule, destroyModule, baseClass, subClass, T1, T2, T3 ) \
+ModuleRegistry<baseClass, TYPELIST_3( T1, T2, T3 )> registry ## _ ## baseClass( name, initModule, destroyModule, 0, 0 );
+
+#define REGISTER_MODULE_4( name, initModule, destroyModule, baseClass, subClass, T1, T2, T3, T4 ) \
+ModuleRegistry<baseClass, TYPELIST_4( T1, T2, T3, T4 )> registry ## _ ## baseClass( name, initModule, destroyModule, 0, 0 );
#endif // SHARED
diff --git a/tests/modules/GNUmakefile b/tests/modules/GNUmakefile
index f61374d..2c4aac9 100755
--- a/tests/modules/GNUmakefile
+++ b/tests/modules/GNUmakefile
@@ -1,6 +1,6 @@
TOPDIR = ../..
-SUBDIRS = libcommon testmod testmod2 testmod3
+SUBDIRS = libcommon testmod testmod2 testmod3 testmod4
INCLUDE_DIRS = \
-I. -I$(TOPDIR)/src \
@@ -19,7 +19,8 @@ INCLUDE_LIBS = \
TEST_CPP_BINS = \
test1$(EXE) \
test2$(EXE) \
- test3$(EXE)
+ test3$(EXE) \
+ test4$(EXE)
OBJS =
@@ -36,3 +37,4 @@ local_test:
@./exec_test test1 "Module loader"
@./exec_test test2 "Module loader with singleton"
@./exec_test test3 "Load module in module"
+ @./exec_test test4 "Module initialization"
diff --git a/tests/modules/Makefile.W32 b/tests/modules/Makefile.W32
index ea86b36..5705031 100755
--- a/tests/modules/Makefile.W32
+++ b/tests/modules/Makefile.W32
@@ -1,6 +1,6 @@
TOPDIR = ..\..
-SUBDIRS = testmod testmod2 testmod3
+SUBDIRS = testmod testmod2 testmod3 testmod4
!INCLUDE $(TOPDIR)\makefiles\nmake\platform.mk
@@ -25,7 +25,8 @@ INCLUDE_LIBS = \
TEST_CPP_BINS = \
test1.exe \
test2.exe \
- test3.exe
+ test3.exe \
+ test4.exe
OBJS =
@@ -34,6 +35,7 @@ OBJS =
test1.exe: test1.obj
test2.exe: test2.obj
test3.exe: test3.obj
+test4.exe: test4.obj
# must build test2.lib first becase it contains DDL exports for the
# Common singleton
@@ -48,3 +50,4 @@ local_test:
@-exec_test test1 "Module loader"
@-exec_test test2 "Module loader with singleton"
@-exec_test test3 "Load module in module"
+ @-exec_test test4 "Module initialization"
diff --git a/tests/modules/test4.MUST b/tests/modules/test4.MUST
new file mode 100644
index 0000000..2a45d20
--- /dev/null
+++ b/tests/modules/test4.MUST
@@ -0,0 +1,5 @@
+Created common object
+test4: Module 4 initModule called
+test4: hello from main
+test4: hello world from module
+test4: Module 4 destroyModule called
diff --git a/tests/modules/test4.cpp b/tests/modules/test4.cpp
new file mode 100755
index 0000000..8e1bfbe
--- /dev/null
+++ b/tests/modules/test4.cpp
@@ -0,0 +1,36 @@
+#include "ModuleLoader.hpp"
+#include "Base.hpp"
+#include "Common.hpp"
+
+#include <vector>
+#include <string>
+#include <iostream>
+
+using namespace std;
+
+int main( void )
+{
+ try {
+ Common &c = Common::instance( );
+ c.setName( "test4" );
+
+ vector<string> modules;
+#ifndef _WIN32
+ modules.push_back( "./testmod4/mod_test4.so" );
+#else
+ modules.push_back( ".\\testmod4\\mod_test4.dll" );
+#endif
+ ModuleLoader<Base> loader( modules );
+
+ Base *obj = loader.create( "testmod4" );
+ c.print( "hello from main" );
+ obj->hello( );
+ loader.destroy( obj );
+
+ } catch( exception &e ) {
+ cerr << "Module loader error: " << e.what( );
+ return 1;
+ }
+
+ return 0;
+}
diff --git a/tests/modules/testmod/TestMod.cpp b/tests/modules/testmod/TestMod.cpp
index 0728b87..e019e30 100755
--- a/tests/modules/testmod/TestMod.cpp
+++ b/tests/modules/testmod/TestMod.cpp
@@ -9,4 +9,4 @@ void Derived::hello( )
cout << "hello" << endl;
}
-REGISTER_MODULE( "testmod", Base, Derived )
+REGISTER_MODULE( "testmod", 0, 0, Base, Derived )
diff --git a/tests/modules/testmod2/TestMod2.cpp b/tests/modules/testmod2/TestMod2.cpp
index 972ccb0..454bf8f 100755
--- a/tests/modules/testmod2/TestMod2.cpp
+++ b/tests/modules/testmod2/TestMod2.cpp
@@ -10,4 +10,4 @@ void Derived::hello( )
Common::instance( ).print( "hello from module" );
}
-REGISTER_MODULE( "testmod2", Base, Derived )
+REGISTER_MODULE( "testmod2", 0, 0, Base, Derived )
diff --git a/tests/modules/testmod3/TestMod3.cpp b/tests/modules/testmod3/TestMod3.cpp
index 4962442..c37fe72 100755
--- a/tests/modules/testmod3/TestMod3.cpp
+++ b/tests/modules/testmod3/TestMod3.cpp
@@ -32,4 +32,4 @@ void Derived::hello( )
Common::instance( ).print( "hello world from module" );
}
-REGISTER_MODULE( "testmod3", Base, Derived )
+REGISTER_MODULE( "testmod3", 0, 0, Base, Derived )
diff --git a/tests/modules/testmod4/GNUmakefile b/tests/modules/testmod4/GNUmakefile
new file mode 100755
index 0000000..b5bdf06
--- /dev/null
+++ b/tests/modules/testmod4/GNUmakefile
@@ -0,0 +1,43 @@
+TOPDIR = ../../..
+
+SUBDIRS =
+
+-include $(TOPDIR)/makefiles/gmake/platform.mk
+
+INCLUDE_DIRS = \
+ -I. -I$(TOPDIR)/src -I.. \
+ -I$(TOPDIR)/include/module \
+ -I$(TOPDIR)/include/util \
+ -I$(TOPDIR)/tests/modules/libcommon
+
+INCLUDE_CXXFLAGS = \
+
+INCLUDE_LDFLAGS = \
+ -L$(TOPDIR)/src/libcrawler
+
+INCLUDE_LIBS = \
+ -lcrawler
+
+DYNAMIC_MODULE = \
+ mod_test4.so
+
+STATIC_LIB = \
+ libtest4.a
+
+CPP_OBJS = \
+ TestMod4.o
+
+-include $(TOPDIR)/makefiles/gmake/sub.mk
+
+local_all:
+
+local_clean:
+
+local_distclean:
+
+local_install:
+
+local_uninstall:
+
+local_test:
+
diff --git a/tests/modules/testmod4/Makefile.W32 b/tests/modules/testmod4/Makefile.W32
new file mode 100755
index 0000000..6bf7af1
--- /dev/null
+++ b/tests/modules/testmod4/Makefile.W32
@@ -0,0 +1,51 @@
+TOPDIR = ..\..\..
+
+SUBDIRS =
+
+!INCLUDE $(TOPDIR)\makefiles\nmake\platform.mk
+
+INCLUDE_CXXFLAGS = \
+ /D_WIN32_WINNT=0x504
+
+INCLUDE_DIRS = \
+ /I. \
+ /I$(TOPDIR)\src \
+ /I$(TOPDIR)\include\module \
+ /I$(TOPDIR)\include\util \
+ /I$(TOPDIR)\include\crawler \
+ /I.. \
+ /I..\libcommon
+
+INCLUDE_LDFLAGS = \
+
+INCLUDE_LIBS = \
+ $(TOPDIR)\src\libutil\util.lib \
+ ..\libcommon\common.lib
+
+DYNAMIC_MODULE = \
+ mod_test4.dll
+
+STATIC_LIB = \
+ test4.lib
+
+CPP_OBJS = \
+ TestMod4.obj
+
+SHARED_CPP_OBJS = \
+ TestMod4.dllobj
+
+!INCLUDE $(TOPDIR)\makefiles\nmake\sub.mk
+
+$(STATIC_LIB): $(CPP_OBJS)
+ $(LINK) /lib /nologo /out:$@ $(STATIC_LDFLAGS) $?
+
+$(DYNAMIC_MODULE): $(SHARED_CPP_OBJS)
+ $(LINK) /dll /nologo /out:$@ $(LDFLAGS) $(LIBS) $?
+
+local_all: $(STATIC_LIB) $(DYNAMIC_MODULE)
+
+local_clean:
+
+local_distclean:
+
+local_test:
diff --git a/tests/modules/testmod4/TestMod4.cpp b/tests/modules/testmod4/TestMod4.cpp
new file mode 100755
index 0000000..cd0578e
--- /dev/null
+++ b/tests/modules/testmod4/TestMod4.cpp
@@ -0,0 +1,34 @@
+#include "TestMod4.hpp"
+#include "Common.hpp"
+
+#include <iostream>
+
+#include <vector>
+#include <string>
+
+using namespace std;
+
+Derived::Derived( )
+{
+}
+
+Derived::~Derived( )
+{
+}
+
+void Derived::hello( )
+{
+ Common::instance( ).print( "hello world from module" );
+}
+
+static void initModule( )
+{
+ Common::instance( ).print( "Module 4 initModule called" );
+}
+
+static void destroyModule( )
+{
+ Common::instance( ).print( "Module 4 destroyModule called" );
+}
+
+REGISTER_MODULE( "testmod4", &initModule, &destroyModule, Base, Derived )
diff --git a/tests/modules/testmod4/TestMod4.hpp b/tests/modules/testmod4/TestMod4.hpp
new file mode 100755
index 0000000..d4d4d99
--- /dev/null
+++ b/tests/modules/testmod4/TestMod4.hpp
@@ -0,0 +1,25 @@
+#ifndef __TESTMOD_H
+#define __TESTMOD_H
+
+#include "Base.hpp"
+
+#include "ModuleRegistry.hpp"
+
+#include "ModuleLoader.hpp"
+
+class Derived : public Base
+{
+ public:
+ Derived( );
+
+ virtual ~Derived( );
+
+ virtual void hello( );
+
+ private:
+ ModuleLoader<Base> *m_loader;
+};
+
+DECLARE_MODULE( Base )
+
+#endif