MSUpdateEachTimestepContainer.h

Go to the documentation of this file.
00001 /****************************************************************************/
00007 // Container of typed objects that shall be updated in each simulation step
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 MSUpdateEachTimestepContainer_h
00020 #define MSUpdateEachTimestepContainer_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 
00035 
00036 // ===========================================================================
00037 // class definitions
00038 // ===========================================================================
00043 template< class UpdateEachTimestep >
00044 class MSUpdateEachTimestepContainer {
00045 public:
00049     static MSUpdateEachTimestepContainer* getInstance() throw() {
00050         if (myInstance == 0) {
00051             myInstance = new MSUpdateEachTimestepContainer();
00052         }
00053         return myInstance;
00054     }
00055 
00056 
00060     void addItemToUpdate(UpdateEachTimestep* item) throw() {
00061         myContainer.push_back(item);
00062     }
00063 
00064 
00068     void removeItemToUpdate(UpdateEachTimestep* item) throw() {
00069         typename std::vector< UpdateEachTimestep* >::iterator i =
00070             std::find(myContainer.begin(), myContainer.end(), item);
00071         if (i!=myContainer.end()) {
00072             myContainer.erase(i);
00073         }
00074     }
00075 
00076 
00082     void updateAll() {
00083         std::for_each(myContainer.begin(), myContainer.end(),
00084                       std::mem_fun(&UpdateEachTimestep::updateEachTimestep));
00085     }
00086 
00087 
00089     ~MSUpdateEachTimestepContainer() throw() {
00090         myContainer.clear();
00091         myInstance = 0;
00092     }
00093 
00094 
00099     void clear() throw() {
00100         for (typename std::vector< UpdateEachTimestep* >::iterator i=myContainer.begin(); i!=myContainer.end(); ++i) {
00101             delete(*i);
00102         }
00103         myContainer.clear();
00104     }
00105 
00106 
00107 private:
00109     MSUpdateEachTimestepContainer()
00110             : myContainer() {}
00111 
00112 
00114     std::vector< UpdateEachTimestep* > myContainer;
00115 
00117     static MSUpdateEachTimestepContainer* myInstance;
00118 
00119 
00120 };
00121 
00122 // initialize static member
00123 template< class UpdateEachTimestep >
00124 MSUpdateEachTimestepContainer< UpdateEachTimestep >*
00125 MSUpdateEachTimestepContainer< UpdateEachTimestep >::myInstance = 0;
00126 
00127 
00128 #endif
00129 
00130 /****************************************************************************/
00131 

Generated on Wed May 5 00:06:32 2010 for Sumo - Simulation of Urban MObility by  doxygen 1.5.6