AnimatLab  2
Test
ActivatedItemMgr.cpp
Go to the documentation of this file.
1 
7 #include "StdAfx.h"
8 #include "IMovableItemCallback.h"
9 #include "ISimGUICallback.h"
10 #include "AnimatBase.h"
11 
12 #include "Node.h"
13 #include "IPhysicsMovableItem.h"
14 #include "IPhysicsBody.h"
15 #include "BoundingBox.h"
16 #include "MovableItem.h"
17 #include "BodyPart.h"
18 #include "Adapter.h"
19 #include "Joint.h"
20 #include "ReceptiveField.h"
21 #include "ContactSensor.h"
22 #include "RigidBody.h"
23 #include "Structure.h"
24 #include "Organism.h"
25 #include "ActivatedItem.h"
26 #include "ActivatedItemMgr.h"
27 #include "DataChartMgr.h"
28 #include "ExternalStimuliMgr.h"
29 #include "KeyFrame.h"
30 #include "SimulationRecorder.h"
31 #include "OdorType.h"
32 #include "Odor.h"
33 #include "Light.h"
34 #include "LightManager.h"
35 #include "Simulator.h"
36 
37 namespace AnimatSim
38 {
39 
49 {
50 }
51 
61 {
62 
63 try
64 {
65  Reset();
66 }
67 catch(...)
68 {Std_TraceMsg(0, "Caught Error in desctructor of ActivatedItemMgr\r\n", "", -1, false, true);}
69 }
70 
82 {
83 try
84 {
85  m_aryItems.RemoveAll();
86  m_aryItemsMap.RemoveAll();
87 }
88 catch(...)
89 {Std_TraceMsg(0, "Caught Error in desctructor of DataChart\r\n", "", -1, false, true);}
90 }
91 
103 {
104  if(!lpItem)
105  THROW_ERROR(Al_Err_lActivatedItemNull, Al_Err_strActivatedItemNull);
106 
107  //Lets make sure the ID of the item is in upper case.
108  lpItem->ID(Std_CheckString(lpItem->ID()));
109 
110  //lets make sure this is a unique item key.
111  try
112  {
113  m_aryItemsMap.Add(lpItem->ID(), lpItem);
114  }
115  catch(CStdErrorInfo oError)
116  {
117  oError.m_strError += " Duplicate activate item Key: " + lpItem->ID();
118  THROW_ERROR(oError.m_lError, oError.m_strError);
119  }
120 
121  m_aryItems.Add(lpItem);
122 
123  stable_sort(m_aryItems.begin(), m_aryItems.end(), LessThanActivatedItemCompare);
124 }
125 
138 void ActivatedItemMgr::Remove(std::string strID, bool bThrowError)
139 {
140  int iIndex=0;
141  ActivatedItem *lpItem = Find(strID, iIndex, bThrowError);
142 
143  if(lpItem)
144  {
145  if(lpItem->IsActivated())
146  lpItem->Deactivate();
147 
148  m_aryItems.RemoveAt(iIndex);
149 
150  //Must remove it from here last because this stores the actual pointer.
151  m_aryItemsMap.Remove(Std_CheckString(strID));
152  }
153 
154  stable_sort(m_aryItems.begin(), m_aryItems.end(), LessThanActivatedItemCompare);
155 }
156 
174 ActivatedItem *ActivatedItemMgr::Find(std::string strID, int &iIndex, bool bThrowError)
175 {
176  int iCount = m_aryItems.GetSize();
177  ActivatedItem *lpItem = NULL;
178  strID = Std_CheckString(strID);
179  for(iIndex=0; iIndex<iCount; iIndex++)
180  {
181  lpItem = m_aryItems[iIndex];
182 
183  if(lpItem->ID() == strID)
184  return lpItem;
185  }
186 
187  if(bThrowError)
188  THROW_TEXT_ERROR(Al_Err_lActivatedItemIDNotFound, Al_Err_strActivatedItemIDNotFound, " ActivatedItemID: " + strID);
189 
190  return NULL;
191 }
192 
209 ActivatedItem *ActivatedItemMgr::Find(std::string strID, bool bThrowError)
210 {
211  ActivatedItem *lpItem = NULL;
212  CStdMap<std::string, ActivatedItem *>::iterator oPos;
213  oPos = m_aryItemsMap.find(Std_CheckString(strID));
214 
215  if(oPos != m_aryItemsMap.end())
216  lpItem = oPos->second;
217  else if(bThrowError)
218  THROW_TEXT_ERROR(Al_Err_lActivatedItemIDNotFound, Al_Err_strActivatedItemIDNotFound, " ActivatedItemID: " + strID);
219 
220  return lpItem;
221 }
222 
236 int ActivatedItemMgr::FindListPos(std::string strID, bool bThrowError)
237 {
238  std::string sID = Std_ToUpper(Std_Trim(strID));
239 
240  int iCount = m_aryItems.GetSize();
241  for(int iIndex=0; iIndex<iCount; iIndex++)
242  if(m_aryItems[iIndex]->ID() == sID)
243  return iIndex;
244 
245  if(bThrowError)
246  THROW_PARAM_ERROR(Al_Err_lActivatedItemIDNotFound, Al_Err_strActivatedItemIDNotFound, "ID", m_strID);
247 
248  return -1;
249 }
250 
251 
253 {
255 
256  int iCount = m_aryItems.GetSize();
257  for(int iChart=0; iChart<iCount; iChart++)
258  m_aryItems[iChart]->Initialize();
259 
260  stable_sort(m_aryItems.begin(), m_aryItems.end(), LessThanActivatedItemCompare);
261 }
262 
264 {
265  int iCount = m_aryItems.GetSize();
266  for(int iChart=0; iChart<iCount; iChart++)
267  m_aryItems[iChart]->ReInitialize();
268 
269  stable_sort(m_aryItems.begin(), m_aryItems.end(), LessThanActivatedItemCompare);
270 }
271 
273 {
274  int iCount = m_aryItems.GetSize();
275  for(int iIndex=0; iIndex<iCount; iIndex++)
276  m_aryItems[iIndex]->ResetSimulation();
277 }
278 
280 {
281  ActivatedItem *lpItem;
282  int iCount = m_aryItems.GetSize();
283  for(int iIndex=0; iIndex<iCount; iIndex++)
284  {
285  lpItem = m_aryItems[iIndex];
286 
287  if(lpItem->Enabled())
288  {
289  if(lpItem->NeedToActivate())
290  lpItem->Activate();
291  else if(lpItem->NeedToDeactivate())
292  lpItem->Deactivate();
293 
294  if(lpItem->IsActivated() && lpItem->NeedToStep())
295  lpItem->StepSimulation();
296  }
297  }
298 }
299 
300 } //AnimatSim
virtual void ReInitialize()
Re-initialize this object.
Base class file for all Animat simulation objects.
virtual void Activate()
Activates this item.
Declares the simulation recorder class.
bool LessThanActivatedItemCompare(ActivatedItem *lpItem1, ActivatedItem *lpItem2)
Compares the start times of two activated items to see which is sooner.
Root namespace for the base simulation library for AnimatLab.
virtual void ResetSimulation()
Resets the simulation back to time 0.
Declares the body part class.
std::string m_strError
The error message.
Definition: StdErrorInfo.h:26
virtual bool Enabled()
Gets whether the item is enabled or not.
Information about the standard error.
Definition: StdErrorInfo.h:19
virtual std::string ID()
Gets the unique GUID ID of this object.
Definition: AnimatBase.cpp:167
virtual ~ActivatedItemMgr()
Destructor.
long m_lError
The error number.
Definition: StdErrorInfo.h:23
virtual void Initialize()
Initializes this object.
Definition: AnimatBase.cpp:573
Declares the key frame class.
CStdPtrMap< std::string, ActivatedItem > m_aryItemsMap
Base class for all activated items.
Definition: ActivatedItem.h:27
bool IsActivated()
Query if this object is activated.
Declares the joint class.
Declares the organism class.
std::string m_strID
The unique Id for this object.
Definition: AnimatBase.h:55
std::string Std_Trim(std::string strVal)
Trims a string.
Declares a light object.
Declares the activated item class.
Declares a light manager object.
Declares the bounding box class.
virtual void Initialize()
Initializes this object.
virtual void StepSimulation()
Step the simulation for this object.
Definition: AnimatBase.cpp:643
virtual void StepSimulation()
Step the simulation for this object.
virtual void Reset()
Resets this manager.
bool NeedToActivate()
Determines if this item needs to be activated.
Declares the node class.
ActivatedItemMgr()
Default constructor.
CStdArray< ActivatedItem * > m_aryItems
void Std_TraceMsg(const int iLevel, std::string strMessage, std::string strSourceFile, int iSourceLine, bool bLogToFile, bool bPrintHeader)
Traces a message to the debugger window.
virtual ActivatedItem * Find(std::string strID, int &iIndex, bool bThrowError)
Searches for an item with the specified ID and sets its index in the array.
virtual void Deactivate()
Deactivates this item.
Declares the data chart manager class.
Declares the rigid body class.
std::string Std_CheckString(std::string strVal)
Converts a string to upper case and trims it.
virtual void Remove(std::string strID, bool bThrowError=true)
Removes the item with the specified ID.
Declares the structure class.
Declares the odor type class.
virtual bool NeedToStep()
Tells if this item needs to call StepSimulation or not.
Declares the odor class.
Declares the simulator class.
virtual int FindListPos(std::string strID, bool bThrowError=true)
Searches for the item with the specified ID and returns its position in the m_aryItems array...
std::string Std_ToUpper(std::string strVal)
Converts a string to upper case.
Declares the activated item manager class.
Declares the contact sensor class.
Declares the external stimuli manager class.
bool NeedToDeactivate()
Determines if this item needs to be deactivated.
Declares the receptive field class.
virtual void Add(ActivatedItem *lpItem)
Adds a new ActivatedItem to be managed.