osdir.com
mailing list archive

Subject: Question about s11n's classloader - msg#00006

List: lib.s11n.devel

Date: Prev Next Index Thread: Prev Next Index
Dear Stephan et al,

I've got a question right similar to that one from Ben Hymers on 2006-12-16
(http://sourceforge.net/mailarchive/message.php?msg_name=458406BA.40708%40ntlworld.com)

I read the paper about 21st-century-classloader and studied the
appropriate codesections in your s11n's library. I am impressed by
your way of getting typesafety and I want to use this approach in my
own project, too. But unfortunately, I don't get it working yet.

I attached my code at the end of the mail. The problem is, that I
get different copies of the "registration-map" in the binary and in
the library. Thus, the in the library registrated classes can not be
found by the binary. In fact, this is the same problem as Ben had,
but I tested it on a Debian-Linux and a Windows/Cygwin platform
and the result is always the same: there are different copies of
the map (this is also that, what I would expect - but I'm not a
C++-Guru...)

Do you see an error in my code? Is there something that I have
forgotten?

I hope I didn't disturb you. Thank you and best greetings,
Maik


The Codes:

############################# cl.hpp:
#ifndef CL_HPP_
#define CL_HPP_ 1

template <typename BaseT, typename SubT = BaseT> struct object_factory {

typedef BaseT result_type;
typedef SubT actual_type;
static result_type* new_instance() { return new actual_type; }
static result_type* no_instance() { return 0; }

};

#include <iostream>
#include <map>

template <typename BaseType> class instantiator {

public:

typedef BaseType value_type;
typedef value_type* (*factory_type)();
typedef std::map<std::string, factory_type> object_factory_map;

static value_type* instantiate(const std::string &key) {
typename object_factory_map::const_iterator it =
factory_map().find(key);
if(it != factory_map().end()) { return (it->second)(); }
else { return 0; }
}

static void register_factory(const std::string &key, factory_type fp) {
factory_map().insert(make_pair(key, fp));
}

template <typename SubOfBaseType> static void register_subtype(const
std::string &key, factory_type fp = 0) {
if(!fp) { fp =
object_factory<value_type,SubOfBaseType>::new_instance; }
register_factory(key, fp);
}

static object_factory_map& factory_map() {
static object_factory_map meyers;
std::cout << &meyers << std::endl;
return meyers;
}

static bool is_registered(const std::string & key) {
return (factory_map().end() != factory_map().find(key));
}

};

namespace {

template <class BaseT, class SubT = BaseT> struct
classloader_reg_placeholder {
static bool reg;
};

}

#endif


################################## cl_reg.hpp:
#ifndef CL_TYPE
#error "You must define CL_TYPE before including this file."
#endif
#ifndef CL_TYPE_NAME
#error "You must define CL_TYPE_NAME before including this file."
#endif
#ifndef CL_BASE_TYPE
#define CL_BASE_TYPE CL_TYPE
#endif
namespace {
# ifndef CL_REG_CONTEXT_DEFINED
# define CL_REG_CONTEXT_DEFINED 1
template <typename Context> struct cl_reg_context {
static bool placeholder;
};
template <typename Context> bool cl_reg_context<Context>::placeholder = false;
# endif
template<> bool cl_reg_context<CL_TYPE>::placeholder= (
#ifdef CL_ABSTRACT_BASE
instantiator<CL_BASE_TYPE>::register_factory(CL_TYPE_NAME,
object_factory<CL_TYPE>::no_instance),
#else

instantiator<CL_BASE_TYPE>::register_subtype<CL_TYPE>(CL_TYPE_NAME),
#endif
true);
}
#undef CL_TYPE
#undef CL_BASE_TYPE
#undef CL_TYPE_NAME
#ifdef CL_ABSTRACT_BASE
#undef CL_ABSTRACT_BASE
#endif


######################## abPar.hpp
#ifndef ABPARH
#define ABPARH

#include "cl.hpp"
#include <iostream>

class AbPar {

public:

AbPar() { std::cout << "Constructor in " << __FILE__ << " " <<
__LINE__ << " called." << std::endl; };
virtual ~AbPar(){};

virtual void Who() = 0;
virtual void Where() = 0;

};

#define CL_TYPE AbPar
#define CL_TYPE_NAME "AbPar"
#define CL_ABSTRACT_BASE 1
#include "cl_reg.hpp"

#endif


############################### child.hpp
#ifndef CHILDH
#define CHILDH

#include <iostream>
#include "abPar.hpp"

class Child : public AbPar {

public:

Child();
~Child();

void Who();
void Where();

};

#include "cl.hpp"

#define CL_TYPE Child
#define CL_BASE_TYPE AbPar
#define CL_TYPE_NAME "Child"
#include "cl_reg.hpp"

#endif


############################# child.cpp
#include "child.hpp"
#include <iostream>

Child::Child() {
std::cout << "Constructor in " << __FILE__ << "called" << std::endl;
}

Child::~Child() {
std::cout << "Destructor in " << __FILE__ << "called" << std::endl;
}

void Child::Who() {
std::cout << "Calling the Who() Method of Child" << std::endl;
}

void Child::Where() {
std::cout << "Calling the Where() Method of Child in " << __FILE__ << " at "
<< __LINE__ << std::endl;
}

##################################### main.cpp:
#include <iostream>
#include "abPar.hpp"
#include "cl.hpp"
#include <dlfcn.h>
int main(){

std::cout << "In the main program" << std::endl;
void* so_handle = dlopen("./Child.so", RTLD_LAZY);
if (!so_handle) {
std::cerr << "Cannot load library: " << dlerror() << std::endl;
return 1;
}

std::cout << "Instantiating..." << std::endl;
AbPar *m = instantiator<AbPar>::instantiate("Child");
if(m) {
(*m).Who();
(*m).Where();
}

dlclose(so_handle);

return 0;
}


##################### makefile
all:
g++ -shared -o Child.so child.cpp
g++ -ldl main.cpp -o test

clean:
rm -f *.so test test.exe


-------------------------------------------------------------------------
Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW!
Studies have shown that voting for your favorite open source project,
along with a healthy diet, reduces your potential for chronic lameness
and boredom. Vote Now at http://www.sourceforge.net/community/cca08


Was this page helpful?
Yes No
Thread at a glance:

Previous Message by Date: click to view message preview

Actors required Sign up now

Gun ban threatens to destroy obama's campaign http://bannery.alpha.pl/r.html ------------------------------------------------------------------------- Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! Studies have shown that voting for your favorite open source project, along with a healthy diet, reduces your potential for chronic lameness and boredom. Vote Now at http://www.sourceforge.net/community/cca08

Next Message by Date: click to view message preview

Madonna admits to extra marital affair

This week top travel destination http://www.cochesdeimportacion.formulacoches.com/r.html ------------------------------------------------------------------------- Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! Studies have shown that voting for your favorite open source project, along with a healthy diet, reduces your potential for chronic lameness and boredom. Vote Now at http://www.sourceforge.net/community/cca08

Previous Message by Thread: click to view message preview

Actors required Sign up now

Gun ban threatens to destroy obama's campaign http://bannery.alpha.pl/r.html ------------------------------------------------------------------------- Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! Studies have shown that voting for your favorite open source project, along with a healthy diet, reduces your potential for chronic lameness and boredom. Vote Now at http://www.sourceforge.net/community/cca08

Next Message by Thread: click to view message preview

Madonna admits to extra marital affair

This week top travel destination http://www.cochesdeimportacion.formulacoches.com/r.html ------------------------------------------------------------------------- Sponsored by: SourceForge.net Community Choice Awards: VOTE NOW! Studies have shown that voting for your favorite open source project, along with a healthy diet, reduces your potential for chronic lameness and boredom. Vote Now at http://www.sourceforge.net/community/cca08
Sign up for updates to this mailing list. email:
Loading Comments...
Home | News | Patents | Sitemap | FAQ | advertise

Advertising by