VR-Forces 4.0.4 Class Documentation
include/vrfVrlSharedSource/DtStateCacheManager.h
Go to the documentation of this file.
00001 /*******************************************************************************
00002 ** Copyright (c) 2009 MAK Technologies, Inc.
00003 ** All rights reserved.
00004 *******************************************************************************/
00005 /*******************************************************************************
00006 ** $RCSfile: DtStateCacheManager.h,v $ $Revision: 1.2 $ $State: Exp $
00007 *******************************************************************************/
00008 
00011 
00014 
00015 #pragma once
00016 
00017 #include <vrfGuiCore/DtAggregateState.h>
00018 #include <vrfGuiCore/DtEnvironmentState.h>
00019 #include <vrfGuiCore/DtIffStateData.h>
00020 #include <vrfGuiCore/DtExtendedStateData.h>
00021 
00022 #include <vrvCore/DtUniqueID.h>
00023 #include <vrvCore/DtEntityState.h>
00024 
00025 #include <vrvCore/DtSimDataProcessorInterface.h>
00026 
00027 #include <map>
00028 
00029 #include <boost/function.hpp>
00030 #include <boost/bind.hpp>
00031 
00032 #include <vlpi/appearance.h>
00033 
00034 namespace makVrf
00035 {
00036    namespace VRF_PROTOCOL_NAMESPACE
00037    {
00038       template <typename _StateType>
00039       class DtStateCacheManager
00040       {
00041       public:
00042          DtStateCacheManager(makVrv::DtSimDataProcessorInterface* iface)
00043          : myProcessor(iface)
00044          {
00045             myProcessor->signal_stateAdded.connect(boost::bind(&DtStateCacheManager::slot_stateAdded, this, _1, _2));
00046             myProcessor->signal_stateUpdated.connect(boost::bind(&DtStateCacheManager::slot_forceUpdateIfModified, this, _1, _2));
00047             myProcessor->signal_stateRemoved.connect(boost::bind(&DtStateCacheManager::slot_stateRemoved, this, _1));
00048          }
00049 
00050          virtual ~DtStateCacheManager()
00051          {
00052             myProcessor->signal_stateAdded.disconnect(boost::bind(&DtStateCacheManager::slot_stateAdded, this, _1, _2));
00053             myProcessor->signal_stateUpdated.disconnect(boost::bind(&DtStateCacheManager::slot_forceUpdateIfModified, this, _1, _2));
00054             myProcessor->signal_stateRemoved.disconnect(boost::bind(&DtStateCacheManager::slot_stateRemoved, this, _1));
00055          }
00056 
00057          void addIsModifiedCallbackCheck(const boost::function<bool (const _StateType& newState,
00058             const _StateType& originalState)>& function)
00059          {
00060             myIsModifiedFunctions.push_back(function);
00061          }
00062 
00063          void slot_stateRemoved(makVrv::DtUniqueID id)
00064          {
00065             typename std::map<makVrv::DtUniqueID, _StateType>::iterator iter;
00066             iter = myMap.find(id);
00067 
00068             if (iter != myMap.end())
00069             {
00070                myMap.erase(iter);
00071             }
00072          }
00073 
00074          void slot_stateAdded(makVrv::DtUniqueID id,
00075             const makVrv::DtVrlinkSimulatedBaseState& incoming)
00076          {
00077             const _StateType& state = (const _StateType&)incoming;
00078             myMap[id] = state;
00079          }
00080 
00081          void slot_forceUpdateIfModified(makVrv::DtUniqueID id,
00082             const makVrv::DtVrlinkSimulatedBaseState& incoming)
00083          {
00084             const _StateType& state = (const _StateType&)incoming;
00085 
00086             typename DtIsModifiedCallList::iterator iter =
00087                myIsModifiedFunctions.begin();
00088             typename std::map<makVrv::DtUniqueID, _StateType>::const_iterator
00089                cacheIter = myMap.find(id);
00090 
00091             while (iter != myIsModifiedFunctions.end())
00092             {
00093                if ((*iter)(state, cacheIter->second))
00094                {
00095                   myMap[id] = state;
00096                   myProcessor->forceObjectUpdate(id);
00097                   return;
00098                }
00099 
00100                ++iter;
00101             }
00102          }
00103 
00104       protected:
00105          std::map<makVrv::DtUniqueID, _StateType> myMap;
00106       
00107          typedef std::vector<boost::function<bool (const _StateType& newState,
00108             const _StateType& originalState)> > DtIsModifiedCallList;
00109 
00110          DtIsModifiedCallList                  myIsModifiedFunctions;
00111          makVrv::DtSimDataProcessorInterface*  myProcessor;
00112       };
00113 
00114       class DtEntityStateCacheManager : public DtStateCacheManager<makVrv::DtVrlinkSimulatedEntityState>
00115       {
00116       public:
00117          DtEntityStateCacheManager(makVrv::DtSimDataProcessorInterface* i)
00118          : DtStateCacheManager<makVrv::DtVrlinkSimulatedEntityState>(i)
00119          {
00120             addIsModifiedCallbackCheck(boost::bind(&DtEntityStateCacheManager::checkParentId, this, _1, _2));
00121             addIsModifiedCallbackCheck(boost::bind(&DtEntityStateCacheManager::checkGlobalId, this, _1, _2));
00122             addIsModifiedCallbackCheck(boost::bind(&DtEntityStateCacheManager::checkMarkingText, this, _1, _2));
00123             addIsModifiedCallbackCheck(boost::bind(&DtEntityStateCacheManager::checkDestroyed, this, _1, _2));
00124             addIsModifiedCallbackCheck(boost::bind(&DtEntityStateCacheManager::checkForceId, this, _1, _2));
00125          }
00126 
00127       protected:
00128          bool checkParentId(const makVrv::DtVrlinkSimulatedEntityState& newState,
00129             const makVrv::DtVrlinkSimulatedEntityState& cached) const
00130          {
00131             return (newState.myAttachedToId != cached.myAttachedToId);
00132          }
00133 
00134          bool checkForceId(const makVrv::DtVrlinkSimulatedEntityState& newState,
00135             const makVrv::DtVrlinkSimulatedEntityState& cached) const
00136          {
00137             return (newState.myForce != cached.myForce);
00138          }
00139 
00140          bool checkMarkingText(const makVrv::DtVrlinkSimulatedEntityState& newState,
00141             const makVrv::DtVrlinkSimulatedEntityState& cached) const
00142          {
00143             return (newState.myMarkingText != cached.myMarkingText);
00144          }
00145 
00146          bool checkGlobalId(const makVrv::DtVrlinkSimulatedEntityState& newState,
00147             const makVrv::DtVrlinkSimulatedEntityState& cached) const
00148          {
00149             return (newState.myGlobalId != cached.myGlobalId);
00150          }
00151 
00152          bool checkDestroyed(const makVrv::DtVrlinkSimulatedEntityState& newState,
00153             const makVrv::DtVrlinkSimulatedEntityState& state)
00154          {
00155             DtAppearance app1(DtEntityType(newState.myEntityType.c_str()));
00156 
00157             app1.setAppearance(newState.myAppearance);
00158 
00159             DtAppearance app2(DtEntityType(state.myEntityType.c_str()));
00160 
00161             app2.setAppearance(state.myAppearance);
00162 
00163             bool dest1 = (app1.damageState() == DtDamageDestroyed);
00164             bool dest2 = (app2.damageState() == DtDamageDestroyed);
00165 
00166             return (dest1 != dest2);
00167          }
00168       };
00169 
00170       class DtEnvironmentProcessCacheManager : public DtStateCacheManager<DtVrlinkSimulatedEnvironmentProcess>
00171       {
00172       public:
00173          DtEnvironmentProcessCacheManager(makVrv::DtSimDataProcessorInterface* i)
00174          : DtStateCacheManager<DtVrlinkSimulatedEnvironmentProcess>(i)
00175          {
00176             addIsModifiedCallbackCheck(boost::bind(&DtEnvironmentProcessCacheManager::checkAll, this, _1, _2));
00177          }
00178 
00179       protected:
00180          bool checkAll(const DtVrlinkSimulatedEnvironmentProcess& newState,
00181             const DtVrlinkSimulatedEnvironmentProcess& cached) const
00182          {
00183             return (newState != cached);
00184          }
00185       };
00186 
00187       class DtExtendedStateDataCacheManager : public DtStateCacheManager<DtExtendedStateData>
00188       {
00189       public:
00190          DtExtendedStateDataCacheManager(makVrv::DtSimDataProcessorInterface* i)
00191          : DtStateCacheManager<DtExtendedStateData>(i)
00192          {
00193             addIsModifiedCallbackCheck(boost::bind(&DtExtendedStateDataCacheManager::checkAll, this, _1, _2));
00194          }
00195 
00196       protected:
00197          bool checkAll(const DtExtendedStateData& newState,
00198             const DtExtendedStateData& cached) const
00199          {
00200             return (newState != cached);
00201          }
00202       };
00203 
00204       class DtIffStateCacheManager : public DtStateCacheManager<DtIffStateData>
00205       {
00206       public:
00207          DtIffStateCacheManager(makVrv::DtSimDataProcessorInterface* i)
00208          : DtStateCacheManager<DtIffStateData>(i)
00209          {
00210             addIsModifiedCallbackCheck(boost::bind(&DtIffStateCacheManager::checkAll, this, _1, _2));
00211          }
00212 
00213       protected:
00214          bool checkAll(const DtIffStateData& newState,
00215             const DtIffStateData& cached) const
00216          {
00217             return (newState.myStateData != cached.myStateData);
00218          }
00219       };
00220 
00221       class DtAggregateStateCacheManager : public DtStateCacheManager<DtVrlinkSimulatedAggregateState>
00222       {
00223       public:
00224          DtAggregateStateCacheManager(makVrv::DtSimDataProcessorInterface* i)
00225          : DtStateCacheManager<DtVrlinkSimulatedAggregateState>(i)
00226          {
00227             addIsModifiedCallbackCheck(boost::bind(&DtAggregateStateCacheManager::checkParentId, this, _1, _2));
00228             addIsModifiedCallbackCheck(boost::bind(&DtAggregateStateCacheManager::checkGlobalId, this, _1, _2));
00229             addIsModifiedCallbackCheck(boost::bind(&DtAggregateStateCacheManager::checkAggregateState, this, _1, _2));
00230             addIsModifiedCallbackCheck(boost::bind(&DtAggregateStateCacheManager::checkMarkingText, this, _1, _2));
00231             addIsModifiedCallbackCheck(boost::bind(&DtAggregateStateCacheManager::checkSubAggregates, this, _1, _2));
00232             addIsModifiedCallbackCheck(boost::bind(&DtAggregateStateCacheManager::checkSubEntities, this, _1, _2));
00233             addIsModifiedCallbackCheck(boost::bind(&DtAggregateStateCacheManager::checkDestroyed, this, _1, _2));
00234             addIsModifiedCallbackCheck(boost::bind(&DtAggregateStateCacheManager::checkForceId, this, _1, _2));
00235          }
00236 
00237       protected:
00238          bool checkDestroyed(const DtVrlinkSimulatedAggregateState& newState,
00239             const DtVrlinkSimulatedAggregateState& state)
00240          {
00241             bool dest1 = (newState.myAppearance == DtDamageDestroyed);
00242             bool dest2 = (state.myAppearance == DtDamageDestroyed);
00243 
00244             return (dest1 != dest2);
00245          }
00246 
00247          bool checkSubAggregates(const DtVrlinkSimulatedAggregateState& newState,
00248             const DtVrlinkSimulatedAggregateState& state)
00249          {
00250             int numberOfAggs = newState.mySubAggregates.size();
00251             int i;
00252             DtGlobalObjectDesignator id;
00253             std::string sId;
00254             int curAggLimit = state.mySubAggregates.size();
00255 
00256             if (numberOfAggs != curAggLimit)
00257             {
00258                return true;
00259             }
00260 
00261             for (i = 0; i < numberOfAggs; i++)
00262             {
00263                if (state.mySubAggregates[i] != newState.mySubAggregates[i])
00264                {
00265                   return true;
00266                }
00267             }
00268 
00269             return false;
00270          }
00271 
00272          bool checkSubEntities(const DtVrlinkSimulatedAggregateState& newState,
00273             const DtVrlinkSimulatedAggregateState& state)
00274          {
00275             int numberOfAggs = newState.mySubEntities.size();
00276             int i;
00277             DtGlobalObjectDesignator id;
00278             std::string sId;
00279             int curAggLimit = state.mySubEntities.size();
00280 
00281             if (numberOfAggs != curAggLimit)
00282             {
00283                return true;
00284             }
00285 
00286             for (i = 0; i < numberOfAggs; i++)
00287             {
00288                if (state.mySubEntities[i] != newState.mySubEntities[i])
00289                {
00290                   return true;
00291                }
00292             }
00293 
00294             return false;
00295          }
00296 
00297          bool checkParentId(const DtVrlinkSimulatedAggregateState& newState,
00298             const DtVrlinkSimulatedAggregateState& cached) const
00299          {
00300             return (newState.myAttachedToId != cached.myAttachedToId);
00301          }
00302 
00303          bool checkForceId(const DtVrlinkSimulatedAggregateState& newState,
00304             const DtVrlinkSimulatedAggregateState& cached) const
00305          {
00306             return (newState.myForce != cached.myForce);
00307          }
00308 
00309          bool checkGlobalId(const DtVrlinkSimulatedAggregateState& newState,
00310             const DtVrlinkSimulatedAggregateState& cached) const
00311          {
00312             return (newState.myGlobalId != cached.myGlobalId);
00313          }
00314 
00315          bool checkMarkingText(const DtVrlinkSimulatedAggregateState& newState,
00316             const DtVrlinkSimulatedAggregateState& cached) const
00317          {
00318             return (newState.myMarkingText != cached.myMarkingText);
00319          }
00320 
00321          bool checkAggregateState(const DtVrlinkSimulatedAggregateState& newState,
00322             const DtVrlinkSimulatedAggregateState& cached) const
00323          {
00324             return (newState.myAggregateState != cached.myAggregateState);
00325          }
00326       };
00327    }
00328 }

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)