Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
ManagedData.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 
49 #ifndef __MANAGED_DATA_H__
50 #define __MANAGED_DATA_H__
51 
52 #include "CoreConfig.h"
53 #include <map>
54 #include <string>
55 
56 namespace Menge {
57 
61  class MENGE_API ManagedData {
62  public:
66  ManagedData():_refCount(0){}
67 
76  virtual ~ManagedData(){}
77 
86  int incRef() { return ++_refCount; }
87 
97  int decRef() { return --_refCount; }
98 
109  bool isUnreferenced() const { return _refCount == 0; }
110 
111  protected:
116  };
117 
126  template < class TData >
128  public:
137  ManagedDataWrapper( TData * data ) {
138  data->incRef();
139  _data = data;
140  }
141 
150  void freeData() {
151  if ( _data->decRef() == 0 ) {
152  delete _data;
153  }
154  }
155 
156  protected:
160  TData * _data;
161  };
162 
163  // These templated functions serve as the basis for loading new instances of a resource (or
164  // grabbing them from the cached database.)
165  //
166  // To use them the derived class needs to satisfy the following requirements:
167  // 1. The class, Data, must have a PUBLIC static member: std::map< std::string, TData * >
168  // This class is a map that maps filenames to pointers to the corresponding loaded
169  // data. If the filename is not a key in this map, then the data has never been loaded
170  // 2. You must provide a function: Data * readData( const std::string )
171  // This function constructs a new Data, reads the file and populates the data file,
172  // ADDS THE DATA FILE TO THE MAP, and returns the pointer to the data.
173  // 3. The wrapper class, T, must instantiate using TData * as the only argument with the
174  // constructor.
175  //
176  // If I do this, then any final user can simply call "T * myT = loadT( fileName );" And get
177  // a pointer to the data contained in the fileName which prevents duplicate loading and
178  // automatically tracks references so when all the T's that reference a particular TData
179  // are destroyed.
180 
188  template< typename TData >
189  TData * getResource( const std::string & name, std::map< std::string, TData * > & map ) {
190  if ( map.find( name ) == map.end() ) {
191  return 0x0;
192  } else {
193  return map[ name ];
194  }
195  }
196 
198 
211  template< typename T, typename TData>
212  T* loadManagedData( const std::string & fileName, TData * (*reader)(const std::string & ) ) {
213  TData * data = getResource<TData>( fileName, TData::RESOURCES );
214  if ( data == 0x0 ) {
215  data = (*reader)( fileName );
216  if ( data != 0x0 ) {
217  TData::RESOURCES[ fileName ] = data;
218  }
219  }
220  if ( data == 0x0 ) {
221  return 0x0;
222  }
223  return new T( data );
224  }
225 
227 
237  template < typename TData >
238  void removeResource( TData * data, std::map< std::string, TData * > & map ) {
239  // remove self from the resources set
240  typename std::map< std::string, TData *>::iterator itr = map.begin();
241  for ( ; itr != map.end(); ++itr ) {
242  if ( itr->second == data ) {
243  map.erase( itr );
244  break;
245  }
246  }
247  }
248 } // namespace Menge
249 
250 #endif
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
int _refCount
The number of data wrappers using this managed data.
Definition: ManagedData.h:115
Sets up the proper compiler directives for platform and dll export/import.
T * loadManagedData(const std::string &fileName, TData *(*reader)(const std::string &))
Templated function for loading a managed data resource.
Definition: ManagedData.h:212
bool isUnreferenced() const
Reports if the data is referenced.
Definition: ManagedData.h:109
A wrapper for managed data - automatically handles referencing and deletion of managed data...
Definition: ManagedData.h:127
The interface for managed data (essentially smart poitners).
Definition: ManagedData.h:61
int incRef()
Increment references to the managed data.
Definition: ManagedData.h:86
void freeData()
Technically, decrements the data's reference count and garbage collects it when necessary.
Definition: ManagedData.h:150
virtual ~ManagedData()
Virtual destructor.
Definition: ManagedData.h:76
ManagedDataWrapper(TData *data)
Constructor.
Definition: ManagedData.h:137
ManagedData()
Constructor.
Definition: ManagedData.h:66
void removeResource(TData *data, std::map< std::string, TData * > &map)
Tempalted function for removing a resource from the managed databse.
Definition: ManagedData.h:238
int decRef()
Decrement references to the managed data.
Definition: ManagedData.h:97
TData * getResource(const std::string &name, std::map< std::string, TData * > &map)
Templated function for acquiring a managed data resource from its name.
Definition: ManagedData.h:189
TData * _data
The managed data for this wrapper.
Definition: ManagedData.h:160