BVB Source Codes

CRYENGINE Show ObjectContainer.h Source code

Return Download CRYENGINE: download ObjectContainer.h Source code - Download CRYENGINE Source code - Type:.h
  1. // Copyright 2001-2016 Crytek GmbH / Crytek Group. All rights reserved.
  2.  
  3. // -------------------------------------------------------------------------
  4. //  File name:   ObjectContainer.h
  5. //  Created:     11/02/2008 by Matthew
  6. //  Description: Manages the stubs and pointers to all AI objects
  7. // -------------------------------------------------------------------------
  8. //  History:
  9. //
  10. ////////////////////////////////////////////////////////////////////////////
  11.  
  12. #ifndef __AIOBJECTCONTAINER
  13. #define __AIOBJECTCONTAINER
  14.  
  15. #pragma once
  16.  
  17. #include <vector>
  18. #include <CryCore/Containers/IDMap.h>
  19.  
  20. #include "Reference.h"
  21. #include <CryAISystem/IAIObjectManager.h>
  22.  
  23. struct IAIObject;
  24. class CAIObject;
  25. class CAbstractUntypedRef;
  26. template<class T> class CAbstractRef;
  27. template<class T> class CCountedRef;
  28. template<class T> class CStrongRef;
  29. template<class T> class CWeakRef;
  30.  
  31. #define MAX_AI_OBJECTS (1 << 13)
  32.  
  33. /**
  34.  * Container for AI Objects.
  35.  * All AI objects should be registered through the the ObjectContainer, which is the only way strong references
  36.  * can be assigned. The container manages the objects themselves, their stubs, their validity status
  37.  * (though the use of salts) and the destruction of the objects at the end of an AI frame.
  38.  */
  39. class CObjectContainer
  40. {
  41. public:
  42.         typedef std::vector<IAIObject*> TAIObjectVec;
  43.         CObjectContainer(void);
  44.  
  45.         void       Reset();
  46.  
  47.         ILINE bool IsValid(const CAbstractUntypedRef& ref) const
  48.         {
  49.                 return (GetAIObject(ref) != NULL);
  50.         }
  51.  
  52.         ILINE const CAIObject* GetAIObject(const CAbstractUntypedRef& ref) const
  53.         {
  54.                 tAIObjectID objectID = ref.GetObjectID();
  55.  
  56.                 return m_objects.validate(objectID) ? m_objects[objectID] : 0;
  57.         }
  58.  
  59.         ILINE CAIObject* GetAIObject(const CAbstractUntypedRef& ref)
  60.         {
  61.                 tAIObjectID objectID = ref.GetObjectID();
  62.  
  63.                 return m_objects.validate(objectID) ? m_objects[objectID] : 0;
  64.         }
  65.  
  66.         ILINE CAIObject* GetAIObject(tAIObjectID objectID)
  67.         {
  68.                 return m_objects.validate(objectID) ? m_objects[objectID] : 0;
  69.         }
  70.  
  71.         // Add a new AI Object to the manager, which will then own it
  72.         // In theory, this might fail due to lack of buffer space
  73.         // Might need to pass in an existing stub...
  74.         // Also, the Object should keep a pointer to its stub... hmm...
  75.         // Allows registration of objects with a specified ID (mostly for use after serialization.
  76.         //  If specified, the ID to be used should already be in the reserved list)
  77.         bool RegisterObject(CAIObject* pObject, CStrongRef<CAIObject>& ref, tAIObjectID inId = INVALID_AIOBJECTID);
  78.  
  79.         // Remove an object from the manager via its handle
  80.         // Returns false if no object was associated with that handle
  81.         template<class T> bool DeregisterObject(CStrongRef<T>& ref)
  82.         {
  83.                 // The templating is really just a wrapper, so call an untyped private function to do the real work
  84.                 return DeregisterObjectUntyped(&ref);
  85.         }
  86.  
  87.         int                           ReleaseDeregisteredObjects(bool checkForLeaks);
  88.  
  89.         template<class T> CWeakRef<T> GetWeakRef(T* pObject)
  90.         {
  91.                 return pObject ? GetWeakRef(pObject->GetAIObjectID()) : CWeakRef<T>();
  92.         }
  93.  
  94.         // Get a weak reference corresponding to a handle
  95.         CWeakRef<CAIObject> GetWeakRef(tAIObjectID nID);
  96.  
  97.         int                 GetNumRegistered() { return m_snObjectsRegistered - m_snObjectsDeregistered; }
  98.  
  99.         void                DumpRegistered();
  100.  
  101.         // Serialization: the Object Container and all the objects it contains are handled in
  102.         //      the normal Serialize function. SerializeObjectIDs is used to read and write the
  103.         //      full list of required object IDs, to prevent ID collisions. PostSerialize
  104.         //      just allows objects to fix up things relating to other entities / ai objects
  105.         void SerializeObjectIDs(TSerialize ser);
  106.         void Serialize(TSerialize ser);
  107.         void PostSerialize();
  108.  
  109.         void RebuildObjectMaps(std::multimap<short, CCountedRef<CAIObject>>& objectMap, std::multimap<short, CWeakRef<CAIObject>>& dummyMap);
  110.  
  111.         void ReserveID(tAIObjectID id);
  112.         void UnreserveID(tAIObjectID id);
  113.  
  114. protected:
  115.         // Perform much of the work of deregistering an object ignoring the type info
  116.         bool DeregisterObjectUntyped(CAIObject* pObject);
  117.         bool DeregisterObjectUntyped(CAbstractUntypedRef* pRef);
  118.  
  119.         id_map<tAIObjectID, CAIObject*> m_objects;
  120.  
  121.         typedef std::vector<tAIObjectID> TVecAIObjects;
  122.         TVecAIObjects m_DeregisteredBuffer;                 // When deregistered, CAIObjects are added here
  123.         TVecAIObjects m_DeregisteredWorkingBuffer;          // Working buffer during deletion (consider re-entrant deletes)
  124.         static int    m_snObjectsRegistered, m_snObjectsDeregistered;
  125.  
  126.         // certain objects (eg those for pooled entities) may be removed temporarily during gameplay,
  127.         // yet need to be recreated later using the same object ID. In those cases we keep the ID reserved
  128.         // by adding a NULL entry to m_objects, so nothing else can take an ID corresponding
  129.         //      to the same index in the buffer.
  130.         TVecAIObjects m_reservedIDs;
  131. };
  132.  
  133. #include "Reference.inl"
  134.  
  135. #endif // __AIOBJECTCONTAINER
  136.  
downloadObjectContainer.h Source code - Download CRYENGINE Source code
Related Source Codes/Software:
postal - 2017-06-11
reactide - Reactide is the first dedicated IDE for React web ... 2017-06-11
rkt - rkt is a pod-native container engine for Linux. It... 2017-06-11
uWebSockets - Tiny WebSockets https://for... 2017-06-11
realworld - TodoMVC for the RealWorld - Exemplary fullstack Me... 2017-06-11
CRYENGINE - CRYENGINE is a powerful real-time game development... 2017-06-11
goreplay - GoReplay is an open-source tool for capturing and ... 2017-06-10
pyenv - Simple Python version management 2017-06-10
redux-saga - An alternative side effect model for Redux apps ... 2017-06-10
angular-starter - 2017-06-10

 Back to top