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  (void)inp;
128  return 0;
129  };
130 
139  static string getUniqueName(const EGS_Object *o = 0);
140 
147  void setName(EGS_Input *inp);
148 
150  inline int ref() {
151  return ++nref;
152  };
154  inline int deref() {
155  return --nref;
156  };
162  void setFactory(EGS_ObjectFactory *f);
163 
169  static void deleteObject(EGS_Object *o) {
170  if (!o) {
171  return;
172  }
173  if (!o->deref()) {
174  delete o;
175  }
176  };
177 
178 protected:
179 
180  string name;
181  string otype;
182  int nref;
184 
185 };
186 
187 class EGS_Library;
188 
208 
209 public:
210 
222  EGS_ObjectFactory(const string &dsoPath, int where=0);
223 
232  virtual ~EGS_ObjectFactory();
233 
240  virtual void addKnownObject(EGS_Object *o) {
241  if (o) {
242  o->ref();
243  known_objects.push_back(o);
244  }
245  };
246 
269  virtual EGS_Object *createSingleObject(EGS_Input *inp,
270  const char *funcname = 0, bool unique = true);
271 
299  EGS_Object *createObjects(EGS_Input *inp, const string &section_delimeter,
300  const string &object_delimeter, const string &select_key,
301  const char *funcname = 0, bool unique = true);
302 
309  bool haveObject(const EGS_Object *o) const;
310 
319  EGS_Object *getObject(const string &Name);
320 
332  EGS_Object *takeObject(const string &Name);
333 
335  void removeObject(EGS_Object *o);
336 
344  virtual bool addObject(EGS_Object *o, bool unique = true);
345 
349  void addKnownTypeId(const char *typeid_name);
350 
352  int nObjects() const {
353  return objects.size();
354  };
355 
357  EGS_Object *getObject(size_t j) {
358  return j<objects.size() ? objects[j] : 0;
359  };
360 
361 protected:
362 
363  vector<EGS_Library *> libs;
364  vector<EGS_Object *> known_objects;
365  vector<EGS_Object *> objects;
366  vector<string> known_typeids;
367  string dso_path;
368 
369 };
370 
381 template <class T>
383 
384 public:
385 
386  EGS_TypedObjectFactory(const string &dsoPath, const string &type,
387  int where=0) :
388  EGS_ObjectFactory(dsoPath,where), otype(type) {};
390 
391  bool isKnownTypeId(EGS_Object *o) const {
392  for (int j=0; j<known_typeids.size(); j++) {
393  if (known_typeids[j] == typeid(*o).name()) {
394  return true;
395  }
396  }
397  return false;
398  };
399 
400  bool isMyObjectType(EGS_Object *o, const char *func) {
401  if (!o) {
402  return false;
403  }
404  T *t = dynamic_cast<T *>(o);
405  bool res;
406  if (t) {
407  res = true;
408  }
409  else {
410  res = isKnownTypeId(o);
411  }
412  if (!res && func) egsWarning("EGS_TypedObjectFactory::%s:\n"
413  " dynamic_cast to %s fails for object of type %s\n"
414  " This object's typeid is also not in the list of know typeids\n",
415  func,otype.c_str(),o->getObjectType().c_str());
416  return res;
417  };
418 
419  void addKnownObject(EGS_Object *o) {
420  if (isMyObjectType(o,"addKnownObject()")) {
422  }
424  };
425 
426  EGS_Object *createSingleObject(EGS_Input *i,
427  const char *fname = 0, bool u = true) {
429  if (o) {
430  if (!isMyObjectType(o,"createSingleObject()")) {
431  delete o;
432  o = 0;
433  }
434  }
435  return o;
436  };
437 
438  bool addObject(EGS_Object *o, bool unique = true) {
439  if (!isMyObjectType(o,"addObject()")) {
440  return false;
441  }
442  return EGS_ObjectFactory::addObject(o,unique);
443  };
444 
445 private:
446 
447  string otype;
448 
449 };
450 
451 #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.
vector< EGS_Library * > libs
DSOs loaded so far.
int nObjects() const
Get the number of objects this factory has created so far.
EGS_Object * getObject(size_t j)
Get the j'th object.
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.