#include <class_loader.h>
Public Types | |
typedef BaseType | value_type |
value_type is the base-most class type to be used for loading. | |
typedef KeyType | key_type |
The key type used for class lookups. | |
typedef class_loader< value_type, key_type, use_shared_instances > | ThisType |
Convenience typedef. | |
typedef value_type *(* | factory_type )() |
factory_type is a function pointer which takes no arguments and returns a value_type pointer. | |
typedef std::map< key_type, value_type * > | shared_inst_map |
shared_inst_map is only used when use_shared_instances is true. | |
Public Member Functions | |
value_type * | operator() (const key_type &key) |
Functionally identical to load(key). | |
virtual value_type * | load (const key_type &key) const |
Tries to instantiate an object using a factory which has been mapped to the given key. | |
Static Public Member Functions | |
ThisType & | shared () |
Returns a reference to a shared instance of this classloader type. | |
value_type * | load_class (const key_type &key) |
Convenience static version of load( key ), which uses the class_loader returned by shared(). | |
bool | is_registered (const key_type &key) |
Returns true if the given key is registered. | |
void | register_factory (const key_type &key, factory_type fp=0) |
register_factory() associates key with a factory function. | |
template<typename SubT> void | register_subtype (const key_type &key, factory_type fp=0) |
Convenience function which registers a factory creating SubT pointers but returning value_type pointers. | |
void | alias (const key_type &alias, const key_type &classname) |
Aliases alias as equivalent to classname. | |
shared_inst_map & | shared_objects () |
If use_shared_instances is true this stores the classloaded shared objects, otherwise it holds nothing. | |
Static Public Attributes | |
const bool | use_shared_instances = UseSharedInstances |
If this is true then calls to load(key) will always return the same instance for the same key. | |
Protected Types | |
typedef std::map< key_type, key_type > | alias_map |
Used to store alias-to-classname mappings. | |
Static Protected Member Functions | |
alias_map & | aliases () |
Returns the map of classname aliases, keyed on aliases, with their expansions as their values. |
Templatized on:
BaseType - this is the type to which all loaded classes should be cast. This is analogous to libfun's fun::LoadableClass.
KeyType - the key type to be used to search for classes. Historically this is a string, but any comparable type should work. It is important that KeyType support less-than ordering.
UseSharedInstances - determines the ownership policy for pointers returned from load(). See load() and the library manual for full details. This parameter should be left as-is for most use-cases. Note that all factories are shared amongst class_loader<BaseType>'s, regardless of their sharing policy, so it is a bad idea to mix classloaders of the same BaseType with different sharing policies, as pointer ownership then becomes confusing.
To make any given class classloadable by this class, call this macro one time (preferably from the class' header file):
s11n_CLASSLOADER_REGISTER(BaseType,SubType); s11n_CLASSLOADER_REGISTER(SubType,SubType); // optional, and sometimes useful
BaseType need not be ClassLoadable, but SubType must derive from (or be) BaseType. The exact meanings of the parameters are discussed at length in the documentation for that macro, in the class_loader.h header file and in the libs11n manual.
As a side-effect of it's template-ness, usage of this class is compile-time type-safe without the use of any casts.
See cllite.h for a simplified client-side interface for this class, and lite.cpp for demonstration code.
Definition at line 135 of file class_loader.h.
|
factory_type is a function pointer which takes no arguments and returns a value_type pointer. todo: add proper functor support. Definition at line 174 of file class_loader.h. Referenced by s11n::class_loader< BaseType, KeyType, UseSharedInstances >::register_factory(). |
|
The key type used for class lookups. The default is std::string. Definition at line 163 of file class_loader.h. Referenced by s11n::class_loader< BaseType, KeyType, UseSharedInstances >::load(). |
|
shared_inst_map is only used when It holds the shared classloaded objects. Definition at line 375 of file class_loader.h. Referenced by s11n::class_loader< BaseType, KeyType, UseSharedInstances >::load(), and s11n::class_loader< BaseType, KeyType, UseSharedInstances >::shared_objects(). |
|
Convenience typedef. It's only public for documentation reasons. Definition at line 167 of file class_loader.h. |
|
value_type is the base-most class type to be used for loading. Contrary to usage in some std::-namespace s11nasses, value_type is not a pointer type, even though this class deals only with pointers to value_type objects. This is intended to ease the use of value_type in many common contexts. Definition at line 157 of file class_loader.h. Referenced by s11n::class_loader< BaseType, KeyType, UseSharedInstances >::load(), s11n::class_loader< BaseType, KeyType, UseSharedInstances >::load_class(), and s11n::class_loader< BaseType, KeyType, UseSharedInstances >::operator()(). |
|
Aliases alias as equivalent to classname. Calls to load(alias) will work exactly as if load(classname) was called. Definition at line 362 of file class_loader.h. References s11n::class_loader< BaseType, KeyType, UseSharedInstances >::alias(). Referenced by s11n::class_loader< BaseType, KeyType, UseSharedInstances >::alias(). |
|
Returns the map of classname aliases, keyed on aliases, with their expansions as their values. The returned map is guaranteed to be post-main() safe in the sense that it will never return a dead reference. It is not post-main() safe in the sense that the aliases map will be depopulated at some arbitrary point post-main() and the returned map is not guaranteed to be populated after that point. Definition at line 411 of file class_loader.h. References s11n::class_loader< BaseType, KeyType, UseSharedInstances >::alias_map. Referenced by s11n::class_loader< BaseType, KeyType, UseSharedInstances >::load(). |
|
Returns true if the given key is registered. This is sometimes useful for checking whether a factory needs to be re-registered, which is sometimes necessary post-main(), when the internal map gets hosed before clients are done using it. Definition at line 296 of file class_loader.h. References s11n::class_loader< BaseType, KeyType, UseSharedInstances >::is_registered(). Referenced by s11n::class_loader< BaseType, KeyType, UseSharedInstances >::is_registered(). |
|
Tries to instantiate an object using a factory which has been mapped to the given key. Returns a non-NULL pointer on success and NULL on error. The caller takes ownership of the returned pointer... UNLESS: If ThisType::use_shared_instances is true then this function will always return the same object for any given key, and the client DOES NOT take ownership of the returned pointer! The objects will be deleted when the last class_loader of this type destructs, almost certainly post-main(). Subclasses should override this function to customize classloading behaviour. The default implementation loads classes which have been registered via:
(Remember, the CLASSLOADER_REGISTER macro calls those for you.) Definition at line 246 of file class_loader.h. References s11n::class_loader< BaseType, KeyType, UseSharedInstances >::aliases(), s11n::class_loader< BaseType, KeyType, UseSharedInstances >::key_type, s11n::class_loader< BaseType, KeyType, UseSharedInstances >::load(), s11n::class_loader< BaseType, KeyType, UseSharedInstances >::shared_inst_map, s11n::class_loader< BaseType, KeyType, UseSharedInstances >::shared_objects(), and s11n::class_loader< BaseType, KeyType, UseSharedInstances >::value_type. Referenced by cllite::classload(), cllite::classload_shared(), s11n::class_loader< BaseType, KeyType, UseSharedInstances >::load(), s11n::class_loader< BaseType, KeyType, UseSharedInstances >::load_class(), and s11n::class_loader< BaseType, KeyType, UseSharedInstances >::operator()(). |
|
Convenience static version of load( key ), which uses the class_loader returned by shared(). Avoid using this, it will directly use class_loader<>, bypassing any subclass-implemented loading improvements (like DLL loading). Definition at line 284 of file class_loader.h. References s11n::class_loader< BaseType, KeyType, UseSharedInstances >::load(), s11n::class_loader< BaseType, KeyType, UseSharedInstances >::load_class(), s11n::class_loader< BaseType, KeyType, UseSharedInstances >::shared(), and s11n::class_loader< BaseType, KeyType, UseSharedInstances >::value_type. Referenced by s11n::class_loader< BaseType, KeyType, UseSharedInstances >::load_class(). |
|
Functionally identical to load(key). This is for use i making this class usable as a functor, to clients can easily replace classloaders in contexts which use functors to load classes. Definition at line 204 of file class_loader.h. References s11n::class_loader< BaseType, KeyType, UseSharedInstances >::load(), s11n::class_loader< BaseType, KeyType, UseSharedInstances >::operator()(), and s11n::class_loader< BaseType, KeyType, UseSharedInstances >::value_type. Referenced by s11n::class_loader< BaseType, KeyType, UseSharedInstances >::operator()(). |
|
register_factory() associates key with a factory function. If fp == 0 then a default factory is used (normally this behaviour is just fine). register_factory() probably should not be static, but i'm not fully convinced that a non-static implementation is useful, and a static implementation eases use of this class. Since classloaders must(?) all use the same definitions for all like-types, anyway, it seems like a moot point. In any case, static appears to ease client usage, so static it is. Definition at line 315 of file class_loader.h. References s11n::class_loader< BaseType, KeyType, UseSharedInstances >::factory_type, and s11n::class_loader< BaseType, KeyType, UseSharedInstances >::register_factory(). Referenced by s11n::class_loader< BaseType, KeyType, UseSharedInstances >::register_factory(). |
|
Convenience function which registers a factory creating SubT pointers but returning value_type pointers. A factory of 0 means to use a default factory, generated by this library. ACHTUNG: gcc 3.3.1 often gives parse errors when compiling calls to this function, for reasons i don't understand: e.g.: classloader<BaseType,KeyType>().register_subtype<SubType>( classname, object_factory<BaseType,SubType>::new_instance ); Gives me: s11n::classloader_register(const std::string&)': ../../include/s11n/s11n_core.h:112: error: parse error before `>' token
IMO it's a compiler bug, but i don't know for sure. :/ Definition at line 349 of file class_loader.h. References s11n::class_loader< BaseType, KeyType, UseSharedInstances >::register_subtype(). Referenced by s11n::class_loader< BaseType, KeyType, UseSharedInstances >::register_subtype(). |
|
If When called post-main() this function may return an empty map. This list is provided in the public interface primarily so clients can do things like serialize the list at application shutdown, provided value_type supports such a feature. Definition at line 390 of file class_loader.h. References s11n::class_loader< BaseType, KeyType, UseSharedInstances >::shared_inst_map. Referenced by s11n::class_loader< BaseType, KeyType, UseSharedInstances >::load(). |
|
If this is true then calls to load(key) will always return the same instance for the same key. This might* not apply to calls made post-main(), i.e., it might return different objects after main() exits, but such calls are dubious, anyway. Definition at line 145 of file class_loader.h. |