CComponentManager Class Reference

#include <ComponentManager.h>

List of all members.

Classes

struct  ComponentType

Public Types

typedef int InterfaceId
typedef int ComponentTypeId
typedef int MessageTypeId
typedef std::vector< std::pair
< entity_id_t, IComponent * > > 
InterfaceList
typedef boost::unordered_map
< entity_id_t, IComponent * > 
InterfaceListUnordered

Public Member Functions

 CComponentManager (CSimContext &, bool skipScriptFunctions=false)
 ~CComponentManager ()
void LoadComponentTypes ()
bool LoadScript (const VfsPath &filename, bool hotload=false)
 Load a script and execute it in a new function scope.
void RegisterMessageType (MessageTypeId mtid, const char *name)
void RegisterComponentType (InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, const char *, const std::string &schema)
void RegisterComponentTypeScriptWrapper (InterfaceId, ComponentTypeId, AllocFunc, DeallocFunc, const char *, const std::string &schema)
void SubscribeToMessageType (MessageTypeId mtid)
 Subscribe the current component type to the given message type.
void SubscribeGloballyToMessageType (MessageTypeId mtid)
 Subscribe the current component type to all messages of the given message type.
ComponentTypeId LookupCID (const std::string &cname) const
std::string LookupComponentTypeName (ComponentTypeId cid) const
entity_id_t AllocateNewEntity ()
 Returns a new entity ID that has never been used before.
entity_id_t AllocateNewLocalEntity ()
 Returns a new local entity ID that has never been used before.
entity_id_t AllocateNewEntity (entity_id_t preferredId)
 Returns a new entity ID that has never been used before.
bool AddComponent (entity_id_t ent, ComponentTypeId cid, const CParamNode &paramNode)
 Constructs a component of type 'cid', initialised with data 'paramNode', and attaches it to entity 'ent'.
void AddMockComponent (entity_id_t ent, InterfaceId iid, IComponent &component)
 Adds an externally-created component, so that it is returned by QueryInterface but does not get destroyed and does not receive messages from the component manager.
IComponentConstructComponent (entity_id_t ent, ComponentTypeId cid)
 Allocates a component object of type 'cid', and attaches it to entity 'ent'.
entity_id_t AddEntity (const std::wstring &templateName, entity_id_t ent)
 Constructs an entity based on the given template, and adds it the world with entity ID ent.
void DestroyComponentsSoon (entity_id_t ent)
 Destroys all the components belonging to the specified entity when FlushDestroyedComponents is called.
void FlushDestroyedComponents ()
 Does the actual destruction of components from DestroyComponentsSoon.
IComponentQueryInterface (entity_id_t ent, InterfaceId iid) const
InterfaceList GetEntitiesWithInterface (InterfaceId iid) const
const InterfaceListUnorderedGetEntitiesWithInterfaceUnordered (InterfaceId iid) const
void PostMessage (entity_id_t ent, const CMessage &msg) const
 Send a message, targeted at a particular entity.
void BroadcastMessage (const CMessage &msg) const
 Send a message, not targeted at any particular entity.
void ResetState ()
 Resets the dynamic simulation state (deletes all entities, resets entity ID counters; doesn't unload/reload component scripts).
bool ComputeStateHash (std::string &outHash, bool quick)
bool DumpDebugState (std::ostream &stream, bool includeDebugInfo)
bool SerializeState (std::ostream &stream)
bool DeserializeState (std::istream &stream)
std::string GenerateSchema ()
ScriptInterfaceGetScriptInterface ()

Private Types

enum  EComponentTypeType { CT_Native, CT_ScriptWrapper, CT_Script }
typedef IComponent *(* AllocFunc )(ScriptInterface &scriptInterface, jsval ctor)
typedef void(* DeallocFunc )(IComponent *)

Private Member Functions

 NONCOPYABLE (CComponentManager)
CMessageConstructMessage (int mtid, CScriptVal data)
void SendGlobalMessage (entity_id_t ent, const CMessage &msg) const
ComponentTypeId GetScriptWrapper (InterfaceId iid)

Static Private Member Functions

static void Script_RegisterComponentType (void *cbdata, int iid, std::string cname, CScriptVal ctor)
static void Script_RegisterInterface (void *cbdata, std::string name)
static void Script_RegisterMessageType (void *cbdata, std::string name)
static void Script_RegisterGlobal (void *cbdata, std::string name, CScriptVal value)
static IComponentScript_QueryInterface (void *cbdata, int ent, int iid)
static std::vector< int > Script_GetEntitiesWithInterface (void *cbdata, int iid)
static std::vector< IComponent * > Script_GetComponentsWithInterface (void *cbdata, int iid)
static void Script_PostMessage (void *cbdata, int ent, int mtid, CScriptVal data)
static void Script_BroadcastMessage (void *cbdata, int mtid, CScriptVal data)
static int Script_AddEntity (void *cbdata, std::string templateName)
static int Script_AddLocalEntity (void *cbdata, std::string templateName)
static void Script_DestroyEntity (void *cbdata, int ent)
static CScriptVal Script_ReadJSONFile (void *cbdata, std::wstring fileName)
static CScriptVal Script_ReadCivJSONFile (void *cbdata, std::wstring fileName)
static std::vector< std::string > Script_FindJSONFiles (void *cbdata, std::wstring subPath)
static CScriptVal ReadJSONFile (void *cbdata, std::wstring filePath, std::wstring fileName)

Private Attributes

ScriptInterface m_ScriptInterface
const CSimContextm_SimContext
ComponentTypeId m_CurrentComponent
bool m_CurrentlyHotloading
std::map< ComponentTypeId,
ComponentType
m_ComponentTypesById
std::vector
< boost::unordered_map
< entity_id_t, IComponent * > > 
m_ComponentsByInterface
std::map< ComponentTypeId,
std::map< entity_id_t,
IComponent * > > 
m_ComponentsByTypeId
std::map< MessageTypeId,
std::vector< ComponentTypeId > > 
m_LocalMessageSubscriptions
std::map< MessageTypeId,
std::vector< ComponentTypeId > > 
m_GlobalMessageSubscriptions
std::map< std::string,
ComponentTypeId
m_ComponentTypeIdsByName
std::map< std::string,
MessageTypeId
m_MessageTypeIdsByName
std::map< MessageTypeId,
std::string > 
m_MessageTypeNamesById
std::map< std::string,
InterfaceId
m_InterfaceIdsByName
std::vector< entity_id_tm_DestructionQueue
ComponentTypeId m_NextScriptComponentTypeId
entity_id_t m_NextEntityId
entity_id_t m_NextLocalEntityId
boost::rand48 m_RNG

Friends

class TestComponentManager

Member Typedef Documentation

typedef IComponent*(* CComponentManager::AllocFunc)(ScriptInterface &scriptInterface, jsval ctor) [private]
typedef void(* CComponentManager::DeallocFunc)(IComponent *) [private]
typedef std::vector<std::pair<entity_id_t, IComponent*> > CComponentManager::InterfaceList

Member Enumeration Documentation

Enumerator:
CT_Native 
CT_ScriptWrapper 
CT_Script 

Constructor & Destructor Documentation

CComponentManager::CComponentManager ( CSimContext context,
bool  skipScriptFunctions = false 
)
CComponentManager::~CComponentManager (  ) 

Member Function Documentation

bool CComponentManager::AddComponent ( entity_id_t  ent,
ComponentTypeId  cid,
const CParamNode paramNode 
)

Constructs a component of type 'cid', initialised with data 'paramNode', and attaches it to entity 'ent'.

Returns:
true on success; false on failure, and logs an error message
entity_id_t CComponentManager::AddEntity ( const std::wstring &  templateName,
entity_id_t  ent 
)

Constructs an entity based on the given template, and adds it the world with entity ID ent.

There should not be any existing components with that entity ID.

Returns:
ent, or INVALID_ENTITY on error
void CComponentManager::AddMockComponent ( entity_id_t  ent,
InterfaceId  iid,
IComponent component 
)

Adds an externally-created component, so that it is returned by QueryInterface but does not get destroyed and does not receive messages from the component manager.

(This is intended for unit tests that need to add mock objects the tested components expect to exist.)

entity_id_t CComponentManager::AllocateNewEntity ( entity_id_t  preferredId  ) 

Returns a new entity ID that has never been used before.

If possible, returns preferredId, and ensures this ID won't be allocated again. This affects the simulation state so it must only be called in network-synchronised ways.

entity_id_t CComponentManager::AllocateNewEntity (  ) 

Returns a new entity ID that has never been used before.

This affects the simulation state so it must only be called in network-synchronised ways.

entity_id_t CComponentManager::AllocateNewLocalEntity (  ) 

Returns a new local entity ID that has never been used before.

This entity will not be synchronised over the network, stored in saved games, etc.

void CComponentManager::BroadcastMessage ( const CMessage msg  )  const

Send a message, not targeted at any particular entity.

The message will be received by any components that subscribed (either globally or not) to the message type.

bool CComponentManager::ComputeStateHash ( std::string &  outHash,
bool  quick 
)
IComponent * CComponentManager::ConstructComponent ( entity_id_t  ent,
ComponentTypeId  cid 
)

Allocates a component object of type 'cid', and attaches it to entity 'ent'.

(The component's Init is not called here - either Init or Deserialize must be called before using the returned object.)

CMessage * CComponentManager::ConstructMessage ( int  mtid,
CScriptVal  data 
) [private]
bool CComponentManager::DeserializeState ( std::istream &  stream  ) 
void CComponentManager::DestroyComponentsSoon ( entity_id_t  ent  ) 

Destroys all the components belonging to the specified entity when FlushDestroyedComponents is called.

Has no effect if the entity does not exist, or has already been added to the destruction queue.

bool CComponentManager::DumpDebugState ( std::ostream &  stream,
bool  includeDebugInfo 
)
void CComponentManager::FlushDestroyedComponents (  ) 

Does the actual destruction of components from DestroyComponentsSoon.

This must not be called if the component manager is on the call stack (since it will break internal iterators).

std::string CComponentManager::GenerateSchema (  ) 
CComponentManager::InterfaceList CComponentManager::GetEntitiesWithInterface ( InterfaceId  iid  )  const
const CComponentManager::InterfaceListUnordered & CComponentManager::GetEntitiesWithInterfaceUnordered ( InterfaceId  iid  )  const
ScriptInterface& CComponentManager::GetScriptInterface (  )  [inline]
CComponentManager::ComponentTypeId CComponentManager::GetScriptWrapper ( InterfaceId  iid  )  [private]
void CComponentManager::LoadComponentTypes (  ) 
bool CComponentManager::LoadScript ( const VfsPath filename,
bool  hotload = false 
)

Load a script and execute it in a new function scope.

Parameters:
filename VFS path to load
hotload set to true if this script has been loaded before, and redefinitions of existing components should not be considered errors
CComponentManager::ComponentTypeId CComponentManager::LookupCID ( const std::string &  cname  )  const
Parameters:
cname Requested component type name (not including any "CID" or "CCmp" prefix)
Returns:
The component type id, or CID__Invalid if not found
std::string CComponentManager::LookupComponentTypeName ( ComponentTypeId  cid  )  const
Returns:
The name of the given component type, or "" if not found
CComponentManager::NONCOPYABLE ( CComponentManager   )  [private]
void CComponentManager::PostMessage ( entity_id_t  ent,
const CMessage msg 
) const

Send a message, targeted at a particular entity.

The message will be received by any components of that entity which subscribed to the message type, and by any other components that subscribed globally to the message type.

IComponent * CComponentManager::QueryInterface ( entity_id_t  ent,
InterfaceId  iid 
) const
CScriptVal CComponentManager::ReadJSONFile ( void *  cbdata,
std::wstring  filePath,
std::wstring  fileName 
) [static, private]
void CComponentManager::RegisterComponentType ( InterfaceId  iid,
ComponentTypeId  cid,
AllocFunc  alloc,
DeallocFunc  dealloc,
const char *  name,
const std::string &  schema 
)
void CComponentManager::RegisterComponentTypeScriptWrapper ( InterfaceId  iid,
ComponentTypeId  cid,
AllocFunc  alloc,
DeallocFunc  dealloc,
const char *  name,
const std::string &  schema 
)
void CComponentManager::RegisterMessageType ( MessageTypeId  mtid,
const char *  name 
)
void CComponentManager::ResetState (  ) 

Resets the dynamic simulation state (deletes all entities, resets entity ID counters; doesn't unload/reload component scripts).

int CComponentManager::Script_AddEntity ( void *  cbdata,
std::string  templateName 
) [static, private]
int CComponentManager::Script_AddLocalEntity ( void *  cbdata,
std::string  templateName 
) [static, private]
void CComponentManager::Script_BroadcastMessage ( void *  cbdata,
int  mtid,
CScriptVal  data 
) [static, private]
void CComponentManager::Script_DestroyEntity ( void *  cbdata,
int  ent 
) [static, private]
std::vector< std::string > CComponentManager::Script_FindJSONFiles ( void *  cbdata,
std::wstring  subPath 
) [static, private]
std::vector< IComponent * > CComponentManager::Script_GetComponentsWithInterface ( void *  cbdata,
int  iid 
) [static, private]
std::vector< int > CComponentManager::Script_GetEntitiesWithInterface ( void *  cbdata,
int  iid 
) [static, private]
void CComponentManager::Script_PostMessage ( void *  cbdata,
int  ent,
int  mtid,
CScriptVal  data 
) [static, private]
IComponent * CComponentManager::Script_QueryInterface ( void *  cbdata,
int  ent,
int  iid 
) [static, private]
CScriptVal CComponentManager::Script_ReadCivJSONFile ( void *  cbdata,
std::wstring  fileName 
) [static, private]
CScriptVal CComponentManager::Script_ReadJSONFile ( void *  cbdata,
std::wstring  fileName 
) [static, private]
void CComponentManager::Script_RegisterComponentType ( void *  cbdata,
int  iid,
std::string  cname,
CScriptVal  ctor 
) [static, private]
void CComponentManager::Script_RegisterGlobal ( void *  cbdata,
std::string  name,
CScriptVal  value 
) [static, private]
void CComponentManager::Script_RegisterInterface ( void *  cbdata,
std::string  name 
) [static, private]
void CComponentManager::Script_RegisterMessageType ( void *  cbdata,
std::string  name 
) [static, private]
void CComponentManager::SendGlobalMessage ( entity_id_t  ent,
const CMessage msg 
) const [private]
bool CComponentManager::SerializeState ( std::ostream &  stream  ) 
void CComponentManager::SubscribeGloballyToMessageType ( MessageTypeId  mtid  ) 

Subscribe the current component type to all messages of the given message type.

Each component's HandleMessage will be called on any BroadcastMessage or PostMessage of this message type, regardless of the entity. Must only be called by a component type's ClassInit.

void CComponentManager::SubscribeToMessageType ( MessageTypeId  mtid  ) 

Subscribe the current component type to the given message type.

Each component's HandleMessage will be called on any BroadcastMessage of this message type, or on any PostMessage of this type targeted at the component's entity. Must only be called by a component type's ClassInit.


Friends And Related Function Documentation

friend class TestComponentManager [friend]

Member Data Documentation

std::vector<boost::unordered_map<entity_id_t, IComponent*> > CComponentManager::m_ComponentsByInterface [private]
std::map<std::string, InterfaceId> CComponentManager::m_InterfaceIdsByName [private]
std::map<std::string, MessageTypeId> CComponentManager::m_MessageTypeIdsByName [private]
std::map<MessageTypeId, std::string> CComponentManager::m_MessageTypeNamesById [private]
boost::rand48 CComponentManager::m_RNG [private]

The documentation for this class was generated from the following files:
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines
Generated on Wed May 16 03:15:25 2012 for Pyrogenesis by  doxygen 1.6.3