Menge
Modular Pedestrian Simulation Framework for Research and Development
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Resource.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 
44 #ifndef __RESOURCE_H__
45 #define __RESOURCE_H__
46 
47 #include "CoreConfig.h"
48 #include "ResourceManager.h"
49 #include "SimpleLock.h"
50 #include "MengeException.h"
51 #include <string>
52 
53 namespace Menge {
54 
58  class MENGE_API ResourceException : public virtual MengeException {
59  public:
64 
70  ResourceException( const std::string & s ): MengeException(s) {}
71  };
72 
77  public:
82 
89  };
90 
98  class MENGE_API Resource {
99  public:
107  Resource( const std::string & fileName ):_fileName(fileName), _refCount(0) {}
108 
123  void destroy();
124 
125  protected:
129  virtual ~Resource() {}
130 
131  public:
137  const std::string & getName() const { return _fileName; }
138 
147  int incRef() { _lock.lock(); int val = ++_refCount; _lock.release(); return val; }
148 
158  int decRef() { _lock.lock(); int val = --_refCount; _lock.release(); return val; }
159 
170  bool isUnreferenced() const { _lock.lock(); bool val = _refCount <= 0; _lock.release(); return val; }
171 
177  virtual const std::string & getLabel() const = 0;
178 
179  friend class ResourceManager;
180  protected:
184  const std::string _fileName;
185 
190 
195  };
196 
200  template < class Rsrc >
201  class ResourcePtr {
202  public:
208  ResourcePtr( Rsrc * rsrc=0x0 ):_data(rsrc) {
209  if ( _data ) _data->incRef();
210  }
211 
217  ResourcePtr( const ResourcePtr< Rsrc > & rPtr ):_data(rPtr._data) {
218  if ( _data ) _data->incRef();
219  }
220 
225  if ( _data ) {
226  _data->decRef();
227  if ( _data->isUnreferenced() ) {
229  }
230  }
231  }
232 
240  if ( this != &ptr ) {
241  if ( _data ) {
242  _data->decRef();
243  if ( _data->isUnreferenced() ) ResourceManager::removeResource( _data );
244  }
245  _data = ptr._data;
246  if ( _data ) _data->incRef();
247  }
248  return *this;
249  }
250 
256  Rsrc * operator->() const { return _data; }
257 
265  bool operator==( const ResourcePtr< Rsrc > & ptr ) const {
266  return _data == ptr._data;
267  }
268 
269  protected:
273  Rsrc * _data;
274  };
275 } //
276 
277 #endif // __RESOURCE_H__
Resource(const std::string &fileName)
Constructor.
Definition: Resource.h:107
The definition of a simple thrading lock.
int decRef()
Decrement references to the managed data.
Definition: Resource.h:158
The core namespace. All elements of Menge are contained in this namespace.
Definition: AgentGenerator.cpp:43
The basic class for all on-disk resources.
Sets up the proper compiler directives for platform and dll export/import.
bool operator==(const ResourcePtr< Rsrc > &ptr) const
Reports if to Resource pointers (of the same type) refer to the same data.
Definition: Resource.h:265
bool isUnreferenced() const
Reports if the data is referenced.
Definition: Resource.h:170
~ResourcePtr()
Destructor.
Definition: Resource.h:224
The definition of a simple mutex-style lock.
Definition: SimpleLock.h:62
The fatal resource exception.
Definition: Resource.h:76
virtual ~Resource()
Virtual destructor.
Definition: Resource.h:129
ResourcePtr(Rsrc *rsrc=0x0)
Constructor.
Definition: Resource.h:208
Basic class for managing on-disk resources.
Definition: Resource.h:98
The base definition for exceptions in Menge.
static bool removeResource(Resource *rsrc)
Remove the given resource.
Definition: ResourceManager.cpp:91
A base exception for resources to throw.
Definition: Resource.h:58
int incRef()
Increment references to the managed data.
Definition: Resource.h:147
Rsrc * operator->() const
The indirection operator.
Definition: Resource.h:256
SimpleLock _lock
Simple lock to handle reference counts safely.
Definition: Resource.h:194
const std::string _fileName
The file which contains the resource's data.
Definition: Resource.h:184
int _refCount
The number of data wrappers using this managed data.
Definition: Resource.h:189
const std::string & getName() const
Return the file name for this resource.
Definition: Resource.h:137
Class to handle management of on-disk resources.
Definition: ResourceManager.h:64
Base exception class for menge operations.
Definition: MengeException.h:58
Rsrc * _data
The underlying resource data.
Definition: Resource.h:273
ResourcePtr< Rsrc > & operator=(const ResourcePtr< Rsrc > &ptr)
Assignment operator.
Definition: Resource.h:239
ResourceException(const std::string &s)
Constructor with message.
Definition: Resource.h:70
ResourceException()
Default constructor.
Definition: Resource.h:63
Base class for fatal exceptions.
Definition: MengeException.h:99
ResourceFatalException()
Default constructor.
Definition: Resource.h:81
Base Class providing a pointer interface to Resources.
Definition: VelCompRoadMap.h:63
ResourcePtr(const ResourcePtr< Rsrc > &rPtr)
Copy constructor.
Definition: Resource.h:217
ResourceFatalException(const std::string &s)
Constructor with message.
Definition: Resource.h:88