EGSnrc C++ class library
Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
|
An object factory. More...
#include <egs_object_factory.h>
Public Member Functions | |
EGS_ObjectFactory (const string &dsoPath, int where=0) | |
Create an object factory that will load shared libraries from the directory dsoPath. More... | |
virtual | ~EGS_ObjectFactory () |
Destructor. More... | |
virtual void | addKnownObject (EGS_Object *o) |
virtual EGS_Object * | createSingleObject (EGS_Input *inp, const char *funcname=0, bool unique=true) |
Create a single object from the information pointed to by inp. More... | |
EGS_Object * | createObjects (EGS_Input *inp, const string §ion_delimeter, const string &object_delimeter, const string &select_key, const char *funcname=0, bool unique=true) |
Create all objects specified by the information inp. More... | |
bool | haveObject (const EGS_Object *o) const |
Does the factory own the object pointed to by o? More... | |
EGS_Object * | getObject (const string &Name) |
Get the object named Name. More... | |
EGS_Object * | takeObject (const string &Name) |
Take the object named Name from the list of objects. More... | |
void | removeObject (EGS_Object *o) |
Remove o from the list of objects. | |
virtual bool | addObject (EGS_Object *o, bool unique=true) |
Add the object o to the factory's list of objects. More... | |
void | addKnownTypeId (const char *typeid_name) |
Add a known typeid to this factory. More... | |
int | nObjects () const |
Get the number of objects this factory has created so far. | |
EGS_Object * | getObject (int j) |
Get the j'th object. | |
Protected Attributes | |
vector< EGS_Library * > | libs |
DSOs loaded so far. | |
vector< EGS_Object * > | known_objects |
known Objects | |
vector< EGS_Object * > | objects |
Created objects. | |
vector< string > | known_typeids |
Known typeid's. | |
string | dso_path |
The path to look for DSOs. | |
An object factory.
An object factory can produce objects derived from EGS_Object from information stored in an instance of the EGS_Input class by either using the EGS_Object::createObject() function of "known" object types added to the factory with addKnownObject(), or by dynamically loading DSOs (a.k.a. DLLs) that provide an object creation function. This functionality is provided by the createSingleObject() and createObjects() functions. Object factories maintain lists of objects created so far and provide functionality for retrieving these objects by name. The main egspp library has static object factories for particle sources and shapes .
Definition at line 206 of file egs_object_factory.h.
EGS_ObjectFactory::EGS_ObjectFactory | ( | const string & | dsoPath, |
int | where = 0 |
||
) |
Create an object factory that will load shared libraries from the directory dsoPath.
If dsoPath is an absolute path name, the object factory will ignore where and look for DSOs (a.k.a. DLLs) in the directory dsoPath. If dsoPath is a relative path name, then the the path in which the factory will look for DSOs is constructed from $HEN_HOUSE
and dsoPath when where=0, else from $EGS_HOME
and dsoPath. In such cases, a fatal error occurs if the environment variable HEN_HOUSE
or EGS_HOME
is not set.
Definition at line 103 of file egs_object_factory.cpp.
References dso_path, egsFatal, egsIsAbsolutePath(), and egsJoinPath().
|
virtual |
Destructor.
The destructor calls EGS_Object::deleteObject() for all known objects added using addKnownObject() and added to the factory via addObject() or createSingleObject(). It also unloads all DSOs that may have been loaded in order to create objects by deleting their EGS_Library instances.
Definition at line 119 of file egs_object_factory.cpp.
References EGS_Object::deleteObject(), known_objects, libs, and objects.
|
virtual |
Add a "known" object to the factorty.
The factory maintains a list of "known" objects and this function adds o to the list. The list of known object is queried when constructing objects with createSingleObject().
Definition at line 239 of file egs_object_factory.h.
References EGS_Object::ref().
|
virtual |
Create a single object from the information pointed to by inp.
If inp has a key type
, the factory checks if any of the known objects added with addKnownObject() is of the same type and if yes, uses the EGS_Object::createObject() function of this object to create a new object of this type from the information provided by inp. If no type
key exists or if no known object is of the same type, this function looks for a library
key. If such a key is present, it attempts to load a DSO with name given by the library
value (the library
value must not contain platform specific prefixes and extensions) from the directory specified by the factory constructor. If this is successfull, the address of the object creation function of the DSO is resolved using funcname (or "createObject", if funcname is null
) and this function is called with inp as argument to create the new object. If one of the two methods succeed, the newly created object is added to the list of objects using addObject() with unique as argument. If this also suceeds, this function returns a pointer to the newly created object. In all other cases null
is returned.
Definition at line 197 of file egs_object_factory.cpp.
References addObject(), EGS_Input::compare(), EGS_Object::deleteObject(), dso_path, egsWarning, EGS_Input::getInput(), EGS_Library::isLoaded(), known_objects, EGS_Library::libraryName(), libs, EGS_Library::load(), EGS_Input::name(), and EGS_Library::resolve().
Referenced by createObjects().
EGS_Object * EGS_ObjectFactory::createObjects | ( | EGS_Input * | inp, |
const string & | section_delimeter, | ||
const string & | object_delimeter, | ||
const string & | select_key, | ||
const char * | funcname = 0 , |
||
bool | unique = true |
||
) |
Create all objects specified by the information inp.
This function creates all objects specified by the information pointed to by inp using createSingleObject(). The implementation works as follows:
null
is returned immediately.Note that in all of the above this function uses the takeInputItem() function of the input object and therefore all object_delimeter properties and the first section_delimeter property are removed from inp after a call to this function.
The meaning of the funcname and unique parameters is the same as in createSingleObject().
Definition at line 154 of file egs_object_factory.cpp.
References createSingleObject(), egsWarning, EGS_Input::getInput(), getObject(), EGS_Input::isA(), objects, and EGS_Input::takeInputItem().
bool EGS_ObjectFactory::haveObject | ( | const EGS_Object * | o | ) | const |
Does the factory own the object pointed to by o?
This function returns true
, if the factory owns the object pointed to by o (i.e. the object is in the factory's list of objects), null
otherwise.
Definition at line 291 of file egs_object_factory.cpp.
References objects.
EGS_Object * EGS_ObjectFactory::getObject | ( | const string & | Name | ) |
Get the object named Name.
This function returns a pointer to the object named Name, if such object exists, null
otherwise. Ownership remains with the factory.
Definition at line 300 of file egs_object_factory.cpp.
References objects.
Referenced by createObjects().
EGS_Object * EGS_ObjectFactory::takeObject | ( | const string & | Name | ) |
Take the object named Name from the list of objects.
If an object named Name exists in the list of objects, this function removes it from the list, decreases its reference count and returns a pointer to the object. This implies that ownership is transfered to the caller, who is reposnsible for deleting the object when not needed. The return value is null
, if no object with name Name exists in the list.
Definition at line 309 of file egs_object_factory.cpp.
References EGS_Object::deref(), and objects.
|
virtual |
Add the object o to the factory's list of objects.
This function returns true
, if the object was successfully added to the list. An object can be added to the list, if o is not null
and unique is false
or unique is true
and an object with the same name does not already exist in the list.
Definition at line 263 of file egs_object_factory.cpp.
References egsWarning, EGS_Object::getObjectName(), objects, and EGS_Object::setFactory().
Referenced by createSingleObject(), and EGS_Object::setFactory().
void EGS_ObjectFactory::addKnownTypeId | ( | const char * | typeid_name | ) |
Add a known typeid to this factory.
Definition at line 321 of file egs_object_factory.cpp.
References known_typeids.