NamedObjectCont.h
Go to the documentation of this file.00001
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019 #ifndef NamedObjectCont_h
00020 #define NamedObjectCont_h
00021
00022
00023
00024
00025
00026 #ifdef _MSC_VER
00027 #include <windows_config.h>
00028 #else
00029 #include <config.h>
00030 #endif
00031
00032 #include <map>
00033 #include <string>
00034 #include <vector>
00035 #include <algorithm>
00036
00037
00038
00039
00040
00050 template<class T>
00051 class NamedObjectCont {
00052 public:
00054 NamedObjectCont() throw() : myHaveChanged(false) { }
00055
00056
00058 virtual ~NamedObjectCont() throw() {
00059 for (typename IDMap::iterator i=myMap.begin(); i!=myMap.end(); i++) {
00060 delete(*i).second;
00061 }
00062 }
00063
00064
00074 virtual bool add(const std::string &id, T item) throw() {
00075 if (myMap.find(id)!=myMap.end()) {
00076 return false;
00077 }
00078 myMap.insert(std::make_pair(id, item));
00079 myHaveChanged = true;
00080 return true;
00081 }
00082
00083
00088 virtual bool remove(const std::string &id) throw() {
00089 if (myMap.find(id)==myMap.end()) {
00090 return false;
00091 }
00092 typename std::map<std::string, T>::iterator i = myMap.find(id);
00093 delete i->second;
00094 myMap.erase(i);
00095 myHaveChanged = true;
00096 return true;
00097 }
00098
00099
00107 T get(const std::string &id) const throw() {
00108 typename std::map<std::string, T>::const_iterator i = myMap.find(id);
00109 if (i==myMap.end()) {
00110 return 0;
00111 }
00112 return (*i).second;
00113 }
00114
00115
00117 void clear() throw() {
00118 for (typename IDMap::iterator i=myMap.begin(); i!=myMap.end(); i++) {
00119 delete(*i).second;
00120 }
00121 myMap.clear();
00122 myVector.clear();
00123 myHaveChanged = true;
00124 }
00125
00126
00131 unsigned int size() const throw() {
00132 return (unsigned int) myMap.size();
00133 }
00134
00135
00145 bool erase(const std::string &id) throw() {
00146 typename IDMap::iterator i=myMap.find(id);
00147 if (i==myMap.end()) {
00148 return false;
00149 }
00150 T o = (*i).second;
00151 myMap.erase(i);
00152
00153 typename ObjectVector::iterator i2 =
00154 find(myVector.begin(), myVector.end(), o);
00155 myHaveChanged = true;
00156 if (i2!=myVector.end()) {
00157 myVector.erase(i2);
00158 }
00159 delete o;
00160 return true;
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 const std::vector<T> &buildAndGetStaticVector() const throw() {
00174 if (myHaveChanged) {
00175 myVector.clear();
00176 typename IDMap::const_iterator i;
00177 for (i=myMap.begin(); i!=myMap.end(); ++i) {
00178 myVector.push_back((*i).second);
00179 }
00180 myHaveChanged = false;
00181 }
00182 return myVector;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 std::vector<T> getTempVector() const throw() {
00194 std::vector<T> ret;
00195 typename IDMap::const_iterator i;
00196 for (i=myMap.begin(); i!=myMap.end(); ++i) {
00197 ret.push_back((*i).second);
00198 }
00199 return ret;
00200 }
00201
00202
00203
00204
00205
00206 void insertIDs(std::vector<std::string> &into) const throw() {
00207 typename IDMap::const_iterator i;
00208 for (i=myMap.begin(); i!=myMap.end(); ++i) {
00209 into.push_back((*i).first);
00210 }
00211 }
00212
00213
00214
00215
00216
00217
00218 const std::map<std::string, T> &getMyMap() const throw() {
00219 return myMap;
00220 }
00221
00222
00223 private:
00225 typedef std::map< std::string, T > IDMap;
00226
00228 typedef typename IDMap::iterator myContIt;
00229
00231 IDMap myMap;
00232
00234 typedef std::vector<T> ObjectVector;
00235
00237 mutable ObjectVector myVector;
00238
00240 mutable bool myHaveChanged;
00241
00242 };
00243
00244
00245 #endif
00246
00247
00248