Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ElementDatabase.h
Go to the documentation of this file.
1 /*
2 
3 License
4 
5 Menge
6 Copyright © and trademark ™ 2012-14 University of North Carolina at Chapel Hill.
7 All rights reserved.
8 
9 Permission to use, copy, modify, and distribute this software and its documentation
10 for educational, research, and non-profit purposes, without fee, and without a
11 written agreement is hereby granted, provided that the above copyright notice,
12 this paragraph, and the following four paragraphs appear in all copies.
13 
14 This software program and documentation are copyrighted by the University of North
15 Carolina at Chapel Hill. The software program and documentation are supplied "as is,"
16 without any accompanying services from the University of North Carolina at Chapel
17 Hill or the authors. The University of North Carolina at Chapel Hill and the
18 authors do not warrant that the operation of the program will be uninterrupted
19 or error-free. The end-user understands that the program was developed for research
20 purposes and is advised not to rely exclusively on the program for any reason.
21 
22 IN NO EVENT SHALL THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE AUTHORS
23 BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT, SPECIAL, INCIDENTAL, OR CONSEQUENTIAL
24 DAMAGES, INCLUDING LOST PROFITS, ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS
25 DOCUMENTATION, EVEN IF THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL OR THE
26 AUTHORS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 
28 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS SPECIFICALLY
29 DISCLAIM ANY WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
30 OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE AND ANY STATUTORY WARRANTY
31 OF NON-INFRINGEMENT. THE SOFTWARE PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND
32 THE UNIVERSITY OF NORTH CAROLINA AT CHAPEL HILL AND THE AUTHORS HAVE NO OBLIGATIONS
33 TO PROVIDE MAINTENANCE, SUPPORT, UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
34 
35 Any questions or comments should be sent to the authors {menge,geom}@cs.unc.edu
36 
37 */
38 
45 #ifndef __ELEMENT_DATABASE_H__
46 #define __ELEMENT_DATABASE_H__
47 
48 #include <string>
49 #include <list>
50 #include "tinyxml.h"
51 #include "Logger.h"
52 
53 namespace Menge {
54 
64  template < class Factory, class Element >
65  class ElementDB {
66  public:
72  static inline size_t count() { return _factories.size(); }
73 
87  static Element * getInstance( TiXmlElement * node, const std::string & behaveFldr ) {
88  // extract type name
89  const char * typeCStr = node->Attribute( "type" );
90  if ( ! typeCStr ) {
91  logger << Logger::ERR_MSG << "A " << getElementName() << " tag has been provided with no \"type\" attribute on line " << node->Row() << "\n";
92  return 0x0;
93  }
94  std::string typeName( typeCStr );
95  typename std::list< Factory * >::iterator itr = _factories.begin();
96  for ( ; itr != _factories.end(); ++itr ) {
97  const Factory * factory = *itr;
98  if ( factory->thisFactory( typeName ) ) {
99  // try to parse it
100  Element * element = factory->createInstance( node, behaveFldr );
101  if ( ! element ) {
102  logger << Logger::ERR_MSG << "The " << getElementName() << " of type \"" << typeName << "\" defined on line " << node->Row() << " could not be instantiated.\n";
103  return 0x0;
104  }
105  return element;
106  }
107  }
108 
109  logger << Logger::ERR_MSG << "Found an undefined " << getElementName() << " type (" << typeName << ") on line " << node->Row() << "\n";
110  return 0x0;
111  }
112 
116  static void initialize() {
117  if ( ! _initialized ) {
118  _initialized = true;
119  addBuiltins();
120  }
121  }
122 
129  static void addBuiltins();
130 
142  static bool addFactory( Factory * factory ) {
143  std::string testName( factory->name() );
144  typename std::list< Factory * >::iterator itr = _factories.begin();
145  for (; itr != _factories.end(); ++itr ) {
146  std::string prevName( (*itr)->name() );
147  if ( testName == prevName ) {
148  logger << Logger::ERR_MSG << "Trying to add a " << getElementName() << " factory which conflicts with a previous condition factories.\n";
149  logger << "\tBoth " << getElementName() << "s use the name: " << testName << ".\n";
150  logger << "\tDescription of the first " << getElementName() << " type: " << (*itr)->description() << ".\n";
151  logger << "\tDescription of the new " << getElementName() << " type: " << factory->description() << ".";
152  factory->destroy();
153  return false;
154  }
155  }
156  _factories.push_back( factory );
157  return true;
158  }
159 
165  static std::string getElementName();
166 
170  static void clear() {
171  typename std::list< Factory * >::iterator itr = _factories.begin();
172  for ( ; itr != _factories.end(); ++itr ) {
173  (*itr)->destroy();
174  }
175  _factories.clear();
176  }
177  protected:
182  static bool _initialized;
183 
187  static std::list< Factory * > _factories;
188 
189  };
190 
191  // The two functions, addBuiltins and getElementName are *not* defined in-line
192  // because of a quirk of visual studio's template system. By defining the
193  // default functionality for the template OUTSIDE the class definition,
194  // these can be properly, explicitly specialized in separate C++ files.
195 
196  template < class Factory, class Element >
198 
199  template < class Factory, class Element >
200  std::string ElementDB< Factory, Element >::getElementName(){ return "unnamed"; }
201 
202  template < class Factory, class Element >
204 
205  template < class Factory, class Element >
206  std::list< Factory * > ElementDB< Factory, Element >::_factories;
207 } // namespace Menge
208 
209 #endif // __ELEMENT_DATABASE_H__
static size_t count()
Reports the number of registered target factories.
Definition: ElementDatabase.h:72
static void clear()
Removes all registered factories from the database.
Definition: ElementDatabase.h:170
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
The base functionality of an element database.
Definition: ElementDatabase.h:65
static Element * getInstance(TiXmlElement *node, const std::string &behaveFldr)
Returns an instance of the TransitionTarget defined in the XML node.
Definition: ElementDatabase.h:87
static void addBuiltins()
Adds the built-in factories to the database.
Definition: ElementDatabase.h:197
static void initialize()
Initializes the database.
Definition: ElementDatabase.h:116
The basic interface of extendible Menge Elements.
Definition: Element.h:67
The specificaiton of a message logger for menge, such that all messages to the system get properly re...
static bool addFactory(Factory *factory)
Adds a new Target factory to the database.
Definition: ElementDatabase.h:142
static bool _initialized
Tracks whether the database has been initialized, so that multiple calls to initialize will not cause...
Definition: ElementDatabase.h:182
static std::list< Factory * > _factories
The registered factories.
Definition: ElementDatabase.h:187
Logger logger
Globally available Logger.
Definition: Logger.cpp:49
static std::string getElementName()
Returns the name of the element managed by this database.
Definition: ElementDatabase.h:200
Error encountered but not handled.
Definition: Logger.h:69