VR-Forces 4.0.4 Class Documentation
include/vrvCore/DtSimObjectEntryCollection.h
Go to the documentation of this file.
00001 /******************************************************************************
00002 ** Copyright (c) 2009 MAK Technologies, Inc.
00003 ** All rights reserved.
00004 ******************************************************************************/
00005 /******************************************************************************
00006 ** $RCSfile: DtSimObjectEntryCollection.h,v $ $Revision: 1.10 $ $State: Exp $
00007 ******************************************************************************/
00008 
00012 
00013 #ifndef DtSimObjectEntryCollection_H_
00014 #define DtSimObjectEntryCollection_H_
00015 
00016 #include <vrvCore/vrvCore.h>
00017 #include <vrvCore/DtDe.h>
00018 #include <vrvCore/DtUniqueID.h>
00019 #include <vrvCore/DtSimObjectEntry.h>
00020 #include <vrvCore/DtDataBank.h>
00021 #include <vrvCore/DtStateView.h>
00022 #include <boost/bind.hpp>
00023 
00024 namespace makVrv
00025 {
00028    template<class T>
00029    class DtSimObjectEntryCollection
00030    {
00031 
00032    public:
00033 
00035       typedef std::map<DtUniqueID, DtSimObjectEntry*> SimObjectEntryMap;
00036 
00040       DtSimObjectEntryCollection(DtDe& de);
00041 
00043       virtual ~DtSimObjectEntryCollection();
00044 
00046       size_t size() const;
00047 
00049       const SimObjectEntryMap& simObjectEntryMap() const;
00050 
00052       const DtSimObjectEntry* findSimObjectEntry(DtUniqueID id) const;
00053 
00055       boost::signal<void (DtSimObjectEntry&)> signal_simObjectEntryAdded;
00056 
00058       boost::signal<void (DtSimObjectEntry&)> signal_simObjectEntryAboutToBeRemoved;
00059 
00060    protected:
00061 
00063       void onSimObjectEntryAdded(DtSimObjectEntry& entry);
00064 
00066       void onSimObjectEntryAboutToBeRemoved(DtSimObjectEntry& entry);
00067 
00069       bool isCompatible(DtSimObjectEntry& entry);
00070 
00072       void populateFromExistingEntries();
00073 
00074    private:
00076       DtSimObjectEntryCollection();
00077       DtSimObjectEntryCollection(const DtSimObjectEntryCollection&);
00078       DtSimObjectEntryCollection& operator=(const DtSimObjectEntryCollection&);
00079 
00080    protected:
00081 
00082       DtDe& myDe;
00083       SimObjectEntryMap mySimObjectEntryMap;
00084 
00085    };
00086 
00087    template<class T>
00088    DtSimObjectEntryCollection<T>::DtSimObjectEntryCollection(DtDe& de)
00089    : myDe(de)
00090    , mySimObjectEntryMap()
00091    {
00093       myDe.dataBank().simData().signal_simObjectEntryAdded.connect(boost::bind(
00094          &DtSimObjectEntryCollection<T>::onSimObjectEntryAdded, this, _1));
00095 
00096       myDe.dataBank().simData().signal_simObjectEntryToBeRemoved.connect(boost::bind(
00097          &DtSimObjectEntryCollection<T>::onSimObjectEntryAboutToBeRemoved, this, _1));
00098 
00100       myDe.dataBank().simData().signal_simStateAdded.connect(boost::bind(
00101          &DtSimObjectEntryCollection<T>::onSimObjectEntryAdded, this, _1));
00102 
00103       myDe.dataBank().simData().signal_simStateToBeRemoved.connect(boost::bind(
00104          &DtSimObjectEntryCollection<T>::onSimObjectEntryAboutToBeRemoved, this, _1));
00105 
00106       populateFromExistingEntries();
00107    }
00108 
00109    template<class T>
00110    DtSimObjectEntryCollection<T>::~DtSimObjectEntryCollection()
00111    {
00113       myDe.dataBank().simData().signal_simObjectEntryAdded.disconnect(boost::bind(
00114          &DtSimObjectEntryCollection<T>::onSimObjectEntryAdded, this, _1));
00115 
00116       myDe.dataBank().simData().signal_simObjectEntryToBeRemoved.disconnect(boost::bind(
00117          &DtSimObjectEntryCollection<T>::onSimObjectEntryAboutToBeRemoved, this, _1));
00118 
00120       myDe.dataBank().simData().signal_simStateAdded.disconnect(boost::bind(
00121          &DtSimObjectEntryCollection<T>::onSimObjectEntryAdded, this, _1));
00122 
00123       myDe.dataBank().simData().signal_simStateToBeRemoved.disconnect(boost::bind(
00124          &DtSimObjectEntryCollection<T>::onSimObjectEntryAboutToBeRemoved, this, _1));
00125    }
00126 
00127    template<class T>
00128    void DtSimObjectEntryCollection<T>::onSimObjectEntryAdded(DtSimObjectEntry& entry)
00129    {
00130       if(isCompatible(entry))
00131       {
00133          onSimObjectEntryAboutToBeRemoved(entry);
00134 
00136          mySimObjectEntryMap[ entry.id() ] = &entry;
00137          signal_simObjectEntryAdded(entry);
00138       }
00139    }
00140 
00141    template<class T>
00142    void DtSimObjectEntryCollection<T>::onSimObjectEntryAboutToBeRemoved(DtSimObjectEntry& entry)
00143    {
00144       if(isCompatible(entry))
00145       {
00146          SimObjectEntryMap::iterator iter = mySimObjectEntryMap.find(entry.id());
00147          if(iter != mySimObjectEntryMap.end())
00148          {
00149             signal_simObjectEntryAboutToBeRemoved(entry);
00150             mySimObjectEntryMap.erase(iter);
00151          }
00152       }
00153    } 
00154 
00155    template<class T>
00156    bool DtSimObjectEntryCollection<T>::isCompatible(DtSimObjectEntry& entry)
00157    {
00158       return DtStateView<T>::isCompatible(entry);
00159    }
00160 
00161    template<class T>
00162    size_t DtSimObjectEntryCollection<T>::size() const
00163    {
00164       return mySimObjectEntryMap.size();
00165    }
00166 
00167    template<class T>
00168    void DtSimObjectEntryCollection<T>::populateFromExistingEntries()
00169    {
00170       DtSimData::SimEntryMap::const_iterator pos;
00171       for(pos=myDe.dataBank().simData().simEntryMap().begin(); pos != 
00172          myDe.dataBank().simData().simEntryMap().end(); ++pos )
00173       {
00174          DtSimObjectEntry* pEntry = 0;
00175          if(pEntry = dynamic_cast<DtSimObjectEntry*>(pos->second))
00176          {
00177             onSimObjectEntryAdded(*pEntry);
00178          }         
00179       }
00180    }
00181 
00182    template<class T>
00183    const typename DtSimObjectEntryCollection<T>::SimObjectEntryMap& 
00184       DtSimObjectEntryCollection<T>::simObjectEntryMap() const
00185    {
00186       return mySimObjectEntryMap;
00187    }
00188 
00189    template<class T>
00190    const DtSimObjectEntry* DtSimObjectEntryCollection<T>::findSimObjectEntry(
00191       DtUniqueID id) const
00192    {
00193       DtSimObjectEntry* entry = 0;
00194       SimObjectEntryMap::const_iterator iter = mySimObjectEntryMap.find(id);
00195       if(iter != mySimObjectEntryMap.end())
00196       {
00197          entry = iter->second;
00198       }
00199       return entry;
00200    }
00201 }
00202 
00203 #endif
00204 

Document ID: Generated on Fri Jun 29 16:33:32 EDT 2012 from SVN revision 116588
Copyright © 2005-2012 VT MÄK Inc. All Rights Reserved (www.mak.com)