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