SUMO - Simulation of Urban MObility
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
InstancePool.h
Go to the documentation of this file.
1 /****************************************************************************/
7 // A pool of resuable instances
8 /****************************************************************************/
9 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/
10 // Copyright (C) 2001-2012 DLR (http://www.dlr.de/) and contributors
11 /****************************************************************************/
12 //
13 // This file is part of SUMO.
14 // SUMO is free software: you can redistribute it and/or modify
15 // it under the terms of the GNU General Public License as published by
16 // the Free Software Foundation, either version 3 of the License, or
17 // (at your option) any later version.
18 //
19 /****************************************************************************/
20 #ifndef InstancePool_h
21 #define InstancePool_h
22 
23 
24 // ===========================================================================
25 // included modules
26 // ===========================================================================
27 #ifdef _MSC_VER
28 #include <windows_config.h>
29 #else
30 #include <config.h>
31 #endif
32 
33 #include <vector>
34 #include <algorithm>
35 #include <cassert>
36 
37 
38 // ===========================================================================
39 // class definitions
40 // ===========================================================================
45 template<typename T>
46 class InstancePool {
47 public:
52  InstancePool(bool deleteOnQuit) : myDeleteOnQuit(deleteOnQuit) { }
53 
54 
57  typedef typename std::vector<T*>::iterator It;
58  if (myDeleteOnQuit) {
59  for (It i = myFreeInstances.begin(); i != myFreeInstances.end(); i++) {
60  delete *i;
61  }
62  }
63  }
64 
65 
74  if (myFreeInstances.size() == 0) {
75  return 0;
76  } else {
77  T* instance = myFreeInstances.back();
78  myFreeInstances.pop_back();
79  return instance;
80  }
81  }
82 
83 
88  void addFreeInstance(T* instance) {
89  myFreeInstances.push_back(instance);
90  }
91 
92 
97  void addFreeInstances(const std::vector<T*> instances) {
98  std::copy(instances.begin(), instances.end(),
99  std::back_inserter(myFreeInstances));
100  }
101 
102 
103 private:
105  std::vector<T*> myFreeInstances;
106 
109 
110 
111 };
112 
113 
114 #endif
115 
116 /****************************************************************************/
117