![]() |
VR-Forces 4.0.4 Class Documentation
|
00001 /****************************************************************************** 00002 ** Copyright (c) 2009 MAK Technologies, Inc. 00003 ** All rights reserved. 00004 ******************************************************************************/ 00005 /****************************************************************************** 00006 ** $RCSfile: DtStateViewCollection.h,v $ $Revision: 1.13 $ $State: Exp $ 00007 ******************************************************************************/ 00008 00012 00013 #ifndef DtStateViewCollection_H_ 00014 #define DtStateViewCollection_H_ 00015 00016 #include <vrvCore/vrvCore.h> 00017 #include <vrvCore/DtStateView.h> 00018 #include <vrvCore/DtDe.h> 00019 #include <vrvCore/DtUniqueID.h> 00020 #include <vrvCore/DtSimObjectEntry.h> 00021 #include <vrvCore/DtDataBank.h> 00022 00023 #include <boost/bind.hpp> 00024 00025 namespace makVrv 00026 { 00029 template<class T> 00030 class DtStateViewCollection 00031 { 00032 00033 public: 00034 00036 typedef std::map<DtUniqueID, DtStateView<T>* > StateViewMap; 00037 00042 DtStateViewCollection(DtDe& de, DtSimEntryUpdater::UpdateRate rate); 00043 00045 virtual ~DtStateViewCollection(); 00046 00048 void setUpdateRate(DtSimEntryUpdater::UpdateRate rate); 00049 00051 size_t size() const; 00052 00054 const StateViewMap& stateViewMap() const; 00055 00057 bool hasStateView(DtUniqueID id) const; 00058 00060 const DtStateView<T>* findStateView(DtUniqueID id) const; 00061 00063 boost::signal<void (const DtStateView<T>&)> signal_stateViewAdded; 00064 00066 boost::signal<void (const DtStateView<T>&)> signal_stateViewAboutToBeRemoved; 00067 00068 protected: 00069 00071 bool isCompatible(DtSimObjectEntry& entry); 00072 00074 void onSimObjectEntryAdded(DtSimObjectEntry& entry); 00075 00077 void onSimObjectEntryAboutToBeRemoved(DtSimObjectEntry& entry); 00078 00080 void populateFromExistingEntries(); 00081 00082 protected: 00083 00084 DtDe& myDe; 00085 StateViewMap myStateViewMap; 00086 DtSimEntryUpdater::UpdateRate myRate; 00087 00088 }; 00089 00090 00091 template<class T> 00092 DtStateViewCollection<T>::DtStateViewCollection(DtDe& de, 00093 DtSimEntryUpdater::UpdateRate rate) 00094 : myDe(de) 00095 , myStateViewMap() 00096 , myRate(rate) 00097 { 00099 myDe.dataBank().simData().signal_simObjectEntryAdded.connect(boost::bind( 00100 &DtStateViewCollection<T>::onSimObjectEntryAdded, this, _1)); 00101 00102 myDe.dataBank().simData().signal_simObjectEntryToBeRemoved.connect(boost::bind( 00103 &DtStateViewCollection<T>::onSimObjectEntryAboutToBeRemoved, this, _1)); 00104 00106 myDe.dataBank().simData().signal_simStateAdded.connect(boost::bind( 00107 &DtStateViewCollection<T>::onSimObjectEntryAdded, this, _1)); 00108 00109 myDe.dataBank().simData().signal_simStateToBeRemoved.connect(boost::bind( 00110 &DtStateViewCollection<T>::onSimObjectEntryAboutToBeRemoved, this, _1)); 00111 00112 populateFromExistingEntries(); 00113 } 00114 00115 template<class T> 00116 DtStateViewCollection<T>::~DtStateViewCollection() 00117 { 00119 myDe.dataBank().simData().signal_simObjectEntryAdded.disconnect(boost::bind( 00120 &DtStateViewCollection<T>::onSimObjectEntryAdded, this, _1)); 00121 00122 myDe.dataBank().simData().signal_simObjectEntryToBeRemoved.disconnect(boost::bind( 00123 &DtStateViewCollection<T>::onSimObjectEntryAboutToBeRemoved, this, _1)); 00124 00126 myDe.dataBank().simData().signal_simStateAdded.disconnect(boost::bind( 00127 &DtStateViewCollection<T>::onSimObjectEntryAdded, this, _1)); 00128 00129 myDe.dataBank().simData().signal_simStateToBeRemoved.disconnect(boost::bind( 00130 &DtStateViewCollection<T>::onSimObjectEntryAboutToBeRemoved, this, _1)); 00131 } 00132 00133 template<class T> 00134 void DtStateViewCollection<T>::onSimObjectEntryAdded(DtSimObjectEntry& entry) 00135 { 00136 if(isCompatible(entry)) 00137 { 00139 onSimObjectEntryAboutToBeRemoved(entry); 00140 00142 DtStateView<T>* sview = new DtStateView<T>(entry); 00143 sview->setUpdateRate(myRate); 00144 myStateViewMap[entry.id()] = sview; 00145 signal_stateViewAdded(*sview); 00146 } 00147 } 00148 00149 template<class T> 00150 void DtStateViewCollection<T>::onSimObjectEntryAboutToBeRemoved( 00151 DtSimObjectEntry& entry) 00152 { 00153 if(isCompatible(entry)) 00154 { 00155 typename StateViewMap::iterator iter = myStateViewMap.find(entry.id()); 00156 if(iter != myStateViewMap.end()) 00157 { 00158 signal_stateViewAboutToBeRemoved(*iter->second ); 00159 delete iter->second; 00160 myStateViewMap.erase(iter); 00161 } 00162 } 00163 } 00164 00165 template<class T> 00166 bool DtStateViewCollection<T>::isCompatible(DtSimObjectEntry& entry) 00167 { 00168 return DtStateView<T>::isCompatible(entry); 00169 } 00170 00171 template<class T> 00172 size_t DtStateViewCollection<T>::size() const 00173 { 00174 return myStateViewMap.size(); 00175 } 00176 00177 template<class T> 00178 void DtStateViewCollection<T>::populateFromExistingEntries() 00179 { 00180 DtSimData::SimEntryMap::const_iterator pos; 00181 for(pos=myDe.dataBank().simData().simEntryMap().begin(); 00182 pos != myDe.dataBank().simData().simEntryMap().end(); ++pos ) 00183 { 00184 DtSimObjectEntry* pEntry = 0; 00185 if(pEntry = dynamic_cast<DtSimObjectEntry*>(pos->second)) 00186 { 00187 onSimObjectEntryAdded(*pEntry); 00188 } 00189 } 00190 } 00191 00192 template<class T> 00193 void DtStateViewCollection<T>::setUpdateRate(DtSimEntryUpdater::UpdateRate rate) 00194 { 00195 myRate = rate; 00196 00197 typename StateViewMap::const_iterator pos; 00198 for(pos = myStateViewMap.begin(); pos != myStateViewMap.end(); ++pos ) 00199 { 00200 pos->second->setUpdateRate(myRate); 00201 } 00202 } 00203 00204 template<class T> 00205 const typename DtStateViewCollection<T>::StateViewMap& DtStateViewCollection<T>::stateViewMap() const 00206 { 00207 return myStateViewMap; 00208 } 00209 00210 template<class T> 00211 bool DtStateViewCollection<T>::hasStateView(DtUniqueID id) const 00212 { 00213 typename StateViewMap::const_iterator iter = myStateViewMap.find(id); 00214 return (iter != myStateViewMap.end()); 00215 } 00216 00217 template<class T> 00218 const DtStateView<T>* DtStateViewCollection<T>::findStateView(DtUniqueID id) const 00219 { 00220 typename StateViewMap::const_iterator iter = myStateViewMap.find(id); 00221 if(iter != myStateViewMap.end()) 00222 { 00223 return iter->second; 00224 } 00225 00226 return 0; 00227 } 00228 } 00229 00230 #endif 00231