00001 /****************************************************************************/ 00007 // A pool of resuable instances 00008 /****************************************************************************/ 00009 // SUMO, Simulation of Urban MObility; see http://sumo.sourceforge.net/ 00010 // Copyright 2001-2010 DLR (http://www.dlr.de/) and contributors 00011 /****************************************************************************/ 00012 // 00013 // This program is free software; you can redistribute it and/or modify 00014 // it under the terms of the GNU General Public License as published by 00015 // the Free Software Foundation; either version 2 of the License, or 00016 // (at your option) any later version. 00017 // 00018 /****************************************************************************/ 00019 #ifndef InstancePool_h 00020 #define InstancePool_h 00021 00022 00023 // =========================================================================== 00024 // included modules 00025 // =========================================================================== 00026 #ifdef _MSC_VER 00027 #include <windows_config.h> 00028 #else 00029 #include <config.h> 00030 #endif 00031 00032 #include <vector> 00033 #include <algorithm> 00034 #include <cassert> 00035 00036 00037 // =========================================================================== 00038 // class definitions 00039 // =========================================================================== 00044 template<typename T> 00045 class InstancePool { 00046 public: 00051 InstancePool(bool deleteOnQuit) : myDeleteOnQuit(deleteOnQuit) { } 00052 00053 00055 ~InstancePool() { 00056 typedef typename std::vector<T*>::iterator It; 00057 if (myDeleteOnQuit) { 00058 for (It i=myFreeInstances.begin(); i!=myFreeInstances.end(); i++) { 00059 delete *i; 00060 } 00061 } 00062 } 00063 00064 00072 T* getFreeInstance() { 00073 if (myFreeInstances.size()==0) { 00074 return 0; 00075 } else { 00076 T *instance = myFreeInstances.back(); 00077 myFreeInstances.pop_back(); 00078 return instance; 00079 } 00080 } 00081 00082 00087 void addFreeInstance(T *instance) { 00088 myFreeInstances.push_back(instance); 00089 } 00090 00091 00096 void addFreeInstances(const std::vector<T*> instances) { 00097 std::copy(instances.begin(), instances.end(), 00098 std::back_inserter(myFreeInstances)); 00099 } 00100 00101 00102 private: 00104 std::vector<T*> myFreeInstances; 00105 00107 bool myDeleteOnQuit; 00108 00109 00110 }; 00111 00112 00113 #endif 00114 00115 /****************************************************************************/ 00116
1.5.6