From: stephan b. <st...@wa...> - 2003-09-28 12:40:07
|
i just found a way to use dlsym() to add new class name-->factory maps to= the=20 classloader from within DLLs when opening them for the first time. When opening a DLL for the first time we look for a symbol (i'll call it=20 boostrap, for brevity). If that symbol exists, we run it. Here's an example implementation: extern "C" { void bootstrap( const char * ) { typedef s11n::Factory<s11n::Serializable,foo::FooClass> Fac= ; s11n::register_serializable_type<foo::FooClass>("WonderBra"= ); s11n::register_serializable_type<foo::FooClass>("foo:FooCla= ss"); } } The const char * parm is a future-compatibility thing, and can normally b= e=20 ignored. i can imagine that it might be useful in some contexts to know w= hat=20 name a DLL is being initialized under. This sample simply adds new name -> class factory mappings for the=20 classloader. This allows a single DLL to contain multiple loadable types.= As=20 a side-effect, those types no longer need an INSTANTIATOR macro (but then= =20 won't be classloadable until manually registered, e.g. via this bootstrap= =20 code). Let's say Foo.so contains Foo, Bar and Baz, and we load this tree: foo class=3DFoo { bar class=3DBar { baz class=3DBaz { } } } Foo's boostrap function can register factories for Bar and Baz (mapping=20 multiple alternate names, if it wants), so those classes needn't be finda= ble=20 by the default classloader. If classloader chaining was an option (it's currently not), then a DLL co= uld=20 even add another loader to the default chain. One conceivable use for this bootstrap is for a DLL to map a set of short= -form=20 names or aliases for the classloader (e.g., "default_document_class"), an= d=20 then it's inner serializable classes could use those as their implClass()= es.=20 This allows some degree degree of maintainable type-swapping, by having t= he=20 factory for default_document_class defined only in the bootstrap code. Note that the main() space also gets dlopen()ed when the classloader is=20 instantiated, so it can also have a bootstrap function, which is called w= hen=20 the classloader is instantiated the first time. todo: consider extending the bootstrap() mechanic to return false or thro= w an=20 exception if the classloader should ignore or perhaps close the dll. --=20 ----- st...@wa... http://qub.sourceforge.net http://libfunutil.sourceforge.net =20 http://toc.sourceforge.net http://countermoves.sourceforge.net http://stephan.rootonfire.org |