EGSnrc C++ class library  Report PIRS-898 (2021)
Iwan Kawrakow, Ernesto Mainegra-Hing, Frederic Tessier, Reid Townson and Blake Walters
egs_object_factory.h
Go to the documentation of this file.
1 /*
2 ###############################################################################
3 #
4 # EGSnrc egs++ object factory headers
5 # Copyright (C) 2015 National Research Council Canada
6 #
7 # This file is part of EGSnrc.
8 #
9 # EGSnrc is free software: you can redistribute it and/or modify it under
10 # the terms of the GNU Affero General Public License as published by the
11 # Free Software Foundation, either version 3 of the License, or (at your
12 # option) any later version.
13 #
14 # EGSnrc is distributed in the hope that it will be useful, but WITHOUT ANY
15 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 # FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for
17 # more details.
18 #
19 # You should have received a copy of the GNU Affero General Public License
20 # along with EGSnrc. If not, see <http://www.gnu.org/licenses/>.
21 #
22 ###############################################################################
23 #
24 # Author: Iwan Kawrakow, 2005
25 #
26 # Contributors: Reid Townson
27 #
28 ###############################################################################
29 */
30 
31 
37 #ifndef EGS_OBJECT_FACTORY_
38 #define EGS_OBJECT_FACTORY_
39 
40 #include "egs_libconfig.h"
41 #include "egs_functions.h"
42 
43 #include <string>
44 #include <vector>
45 #include <typeinfo>
46 using namespace std;
47 
48 class EGS_Input;
49 class EGS_ObjectFactory;
50 
82 
83 public:
84 
92  EGS_Object(const string &Name = "", EGS_ObjectFactory *f = 0);
93 
100  EGS_Object(EGS_Input *inp, EGS_ObjectFactory *f = 0);
101  virtual ~EGS_Object();
102 
104  const string &getObjectName() const {
105  return name;
106  };
107 
109  void setObjectName(const string &Name) {
110  name = Name;
111  };
112 
114  const string &getObjectType() const {
115  return otype;
116  };
117 
127  return 0;
128  };
129 
138  static string getUniqueName(const EGS_Object *o = 0);
139 
146  void setName(EGS_Input *inp);
147 
149  inline int ref() {
150  return ++nref;
151  };
153  inline int deref() {
154  return --nref;
155  };
161  void setFactory(EGS_ObjectFactory *f);
162 
168  static void deleteObject(EGS_Object *o) {
169  if (!o) {
170  return;
171  }
172  if (!o->deref()) {
173  delete o;
174  }
175  };
176 
177 protected:
178 
179  string name;
180  string otype;
181  int nref;
183 
184 };
185 
186 class EGS_Library;
187 
207 
208 public:
209 
221  EGS_ObjectFactory(const string &dsoPath, int where=0);
222 
231  virtual ~EGS_ObjectFactory();
232 
239  virtual void addKnownObject(EGS_Object *o) {
240  if (o) {
241  o->ref();
242  known_objects.push_back(o);
243  }
244  };
245 
268  virtual EGS_Object *createSingleObject(EGS_Input *inp,
269  const char *funcname = 0, bool unique = true);
270 
298  EGS_Object *createObjects(EGS_Input *inp, const string &section_delimeter,
299  const string &object_delimeter, const string &select_key,
300  const char *funcname = 0, bool unique = true);
301 
308  bool haveObject(const EGS_Object *o) const;
309 
318  EGS_Object *getObject(const string &Name);
319 
331  EGS_Object *takeObject(const string &Name);
332 
334  void removeObject(EGS_Object *o);
335 
343  virtual bool addObject(EGS_Object *o, bool unique = true);
344 
348  void addKnownTypeId(const char *typeid_name);
349 
351  int nObjects() const {
352  return objects.size();
353  };
354 
357  return (j>=0 && j<objects.size()) ? objects[j] : 0;
358  };
359 
360 protected:
361 
362  vector<EGS_Library *> libs;
363  vector<EGS_Object *> known_objects;
364  vector<EGS_Object *> objects;
365  vector<string> known_typeids;
366  string dso_path;
367 
368 };
369 
380 template <class T>
382 
383 public:
384 
385  EGS_TypedObjectFactory(const string &dsoPath, const string &type,
386  int where=0) :
387  EGS_ObjectFactory(dsoPath,where), otype(type) {};
389 
390  bool isKnownTypeId(EGS_Object *o) const {
391  for (int j=0; j<known_typeids.size(); j++) {
392  if (known_typeids[j] == typeid(*o).name()) {
393  return true;
394  }
395  }
396  return false;
397  };
398 
399  bool isMyObjectType(EGS_Object *o, const char *func) {
400  if (!o) {
401  return false;
402  }
403  T *t = dynamic_cast<T *>(o);
404  bool res;
405  if (t) {
406  res = true;
407  }
408  else {
409  res = isKnownTypeId(o);
410  }
411  if (!res && func) egsWarning("EGS_TypedObjectFactory::%s:\n"
412  " dynamic_cast to %s fails for object of type %s\n"
413  " This object's typeid is also not in the list of know typeids\n",
414  func,otype.c_str(),o->getObjectType().c_str());
415  return res;
416  };
417 
418  void addKnownObject(EGS_Object *o) {
419  if (isMyObjectType(o,"addKnownObject()")) {
421  }
423  };
424 
425  EGS_Object *createSingleObject(EGS_Input *i,
426  const char *fname = 0, bool u = true) {
428  if (o) {
429  if (!isMyObjectType(o,"createSingleObject()")) {
430  delete o;
431  o = 0;
432  }
433  }
434  return o;
435  };
436 
437  bool addObject(EGS_Object *o, bool unique = true) {
438  if (!isMyObjectType(o,"addObject()")) {
439  return false;
440  }
441  return EGS_ObjectFactory::addObject(o,unique);
442  };
443 
444 private:
445 
446  string otype;
447 
448 };
449 
450 #endif
A class for storing information in a tree-like structure of key-value pairs. This class is used throu...
Definition: egs_input.h:182
A class for dynamically loading shared libraries.
Definition: egs_library.h:52
An object factory.
vector< EGS_Object * > objects
Created objects.
virtual bool addObject(EGS_Object *o, bool unique=true)
Add the object o to the factory's list of objects.
vector< string > known_typeids
Known typeid's.
string dso_path
The path to look for DSOs.
virtual void addKnownObject(EGS_Object *o)
vector< EGS_Object * > known_objects
known Objects
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.
EGS_Object * getObject(int j)
Get the j'th object.
vector< EGS_Library * > libs
DSOs loaded so far.
int nObjects() const
Get the number of objects this factory has created so far.
Base egspp object.
virtual EGS_Object * createObject(EGS_Input *inp)
Create an object from the infromation pointed to by inp.
EGS_ObjectFactory * factory
The factory this object belongs to.
int nref
Number of references to the object.
int ref()
Increase the reference count to this object.
void setObjectName(const string &Name)
Set the object name to Name.
static void deleteObject(EGS_Object *o)
Delete an object.
string otype
The object type.
const string & getObjectType() const
Get the object type.
const string & getObjectName() const
Get the object name.
string name
The object name.
int deref()
Decrease the reference count to this object.
A typed object factory.
Global egspp functions header file.
Defines the EGS_EXPORT and EGS_LOCAL macros.
#define EGS_EXPORT
Export symbols from the egspp library.
Definition: egs_libconfig.h:90
EGS_InfoFunction EGS_EXPORT egsWarning
Always use this function for reporting warnings.