Sumit Nagpal wrote:
I certainly have to go for FACTORY method but in my case I need
something more...
I think 'dlsym' should work for me !
Warning ... try to steer away from dlsym. This reqyures you to know
what the mangling convention is - this is not portable.
Austria C++ (Shameless plug) has a solution for you and it works with
DLL's or DSO's.
Below is an example.
-------------- test code ------------------
#include "interface.h"
#include "at_factory.h"
using namespace at;
#include <iostream>
#include <dlfcn.h>
void Test( Interface * ptr )
{
if ( ! ptr )
{
std::cout << "There is no ptr\n";
return;
}
std::cout << "Thingy() = " << ptr->Thingy() << "\n";
std::cout << "Thingy1() = " << ptr->Thingy1() << "\n";
std::cout << "Thingy2() = " << ptr->Thingy2() << "\n";
bool is_del = false;
ptr->MarkPtr( & is_del );
delete ptr;
std::cout << ( is_del ? "Deleted" : "Not Deleted" ) << "\n";
}
void testfactory()
{
Interface * ptr =
FactoryRegister< Interface, DKy, Creator1P< Interface, DKy,
const char * > >
::Get().Create( "ImplementorKEY" )( "contructor param" );
Test( ptr );
ptr =
FactoryRegister< Interface, DKy, Creator2P< Interface, DKy,
const char *, const char * > >
::Get().Create( "ImplementorKEY" )( "contructor param1",
"contructor param2" );
Test( ptr );
}
void loaddso()
{
if ( ! dlopen( "xxx_dso.so", RTLD_LAZY ) )
{
std::cerr << "dlopen error is: " << dlerror();
}
}
int main()
{
/* output should be :
There is no ptr
There is no ptr
*/
testfactory();
// load the objects ...
loaddso();
/*output should now be :
Thingy() = contructor param
Thingy1() =
Thingy2() =
Deleted
Thingy() = contructor param1
Thingy1() = contructor param2
Thingy2() =
Deleted
*/
testfactory();
}
------------------- interface.h -------------------------
class Interface
{
public:
virtual const char * Thingy() = 0;
virtual const char * Thingy1() = 0;
virtual const char * Thingy2() = 0;
bool * mark_del;
Interface()
: mark_del( 0 )
{
}
virtual ~Interface()
{
if ( mark_del )
{
* mark_del = true;
}
}
void MarkPtr( bool * i_mark_del )
{
mark_del = i_mark_del;
}
virtual int ContructParamCount() = 0;
};
--------------- implementation code ---- xxx_dso.cpp ----------
#include "interface.h"
#include "at_factory.h"
using namespace at;
class Implementor
: public Interface
{
public:
virtual const char * Thingy()
{
return m_str;
}
virtual const char * Thingy1()
{
return m_str2;
}
virtual const char * Thingy2()
{
return m_str3;
}
Implementor( const char * str, const char * str2, const char *
str3 )
: m_str( str ),
m_str2( str2 ),
m_str3( str3 ),
contruction_parameter_count( 3 )
{
}
Implementor( const char * str, const char * str2 )
: m_str( str ),
m_str2( str2 ),
m_str3( "" ),
contruction_parameter_count( 2 )
{
}
Implementor( const char * str )
: m_str( str ),
m_str2( "" ),
m_str3( "" ),
contruction_parameter_count( 1 )
{
}
Implementor()
: m_str( "" ),
m_str2( "" ),
m_str3( "" ),
contruction_parameter_count( 0 )
{
}
virtual int ContructParamCount()
{
return contruction_parameter_count;
}
int contruction_parameter_count;
const char * m_str;
const char * m_str2;
const char * m_str3;
};
AT_MakeFactory0P( "ImplementorKEY", Implementor, Interface, DKy );
AT_MakeFactory1P( "ImplementorKEY", Implementor, Interface, DKy, const
char * );
AT_MakeFactory2P( "ImplementorKEY", Implementor, Interface, DKy, const
char *, const char * );
AT_MakeFactory3P( "ImplementorKEY", Implementor, Interface, DKy, const
char *, const char *, const char * );