CORGI
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
corgi::Component< T > Class Template Reference

A Component is an object that encapsulates all data and logic for Entities of a particular type. More...

#include <component.h>

Inherits corgi::ComponentInterface.

Detailed Description

template<typename T>
class corgi::Component< T >

A Component is an object that encapsulates all data and logic for Entities of a particular type.

Note
On some of the API, EntityRef& parameters are non-const so that the referenced Entity can be changed. The EntityRef itself is never modified.
Template Parameters
TThe structure of data that needs to be associated with each Entity.

Classes

struct  ComponentData
 A structure of data that is associated with each Entity. More...
 

Public Types

typedef VectorPool
< ComponentData >::Iterator 
EntityIterator
 An iterator to iterate through all of the Entities in the Component.
 
typedef T value_type
 The templated data type stored by this Component.
 
- Public Types inherited from corgi::ComponentInterface
typedef std::unique_ptr
< uint8_t, std::function< void(uint8_t *)> > 
RawDataUniquePtr
 A pointer type for exported raw data.
 

Public Member Functions

 Component ()
 Construct a Component without an EntityManager.
 
virtual ~Component ()
 Destructor for a Component.
 
virtual void AddEntityGenerically (EntityRef &entity)
 Provides an alternate way to add Entities if you do not care about the returned data structure, and if you do not feel like casting the BaseComponent into something more specific. More...
 
T * AddEntity (EntityRef &entity, AllocationLocation alloc_location)
 Adds an Entity to the list that this Component is tracking. More...
 
T * AddEntity (EntityRef &entity)
 Adds an Entity to the list that this Component is tracking. More...
 
virtual void RemoveEntity (EntityRef &entity)
 Removes an Entity from the list of Entities. More...
 
virtual EntityIterator RemoveEntity (EntityIterator iter)
 Removes an Entity from the list of Entities. More...
 
virtual EntityIterator begin ()
 Gets an iterator that will iterate over every Entity associated with the Component, starting from the beginning. More...
 
virtual EntityIterator end ()
 Gets an iterator that points to the end of the list of all entites in the Component. More...
 
virtual void UpdateAllEntities (WorldTime)
 Updates all Entities. This is normally called, once per frame, by the EntityManager.
 
virtual bool HasDataForEntity (const EntityRef &entity)
 Checks if this component contains any data associated with the supplied entity.
 
virtual void * GetComponentDataAsVoid (const EntityRef &entity)
 Gets the data for a given Entity as a void pointer. More...
 
virtual const void * GetComponentDataAsVoid (const EntityRef &entity) const
 Gets the data for a given Entity as a const void pointer. More...
 
T * GetComponentData (size_t data_index)
 Gets the Component data stored at a given index. More...
 
T * GetComponentData (const EntityRef &entity)
 Gets the data for a given Entity. More...
 
const T * GetComponentData (size_t data_index) const
 Gets the Component data stored at a given index. More...
 
const T * GetComponentData (const EntityRef &entity) const
 Gets the data for a given Entity. More...
 
virtual void ClearComponentData ()
 Clears all tracked Component data.
 
template<typename ComponentDataType >
ComponentDataType * Data (const EntityRef &entity)
 A utility function for retrieving the Component data for an Entity from a specific Component. More...
 
template<typename ComponentDataType >
bool IsRegisteredWithComponent (const EntityRef &entity)
 A utility function for checking if an entity is registered with a particular component. More...
 
template<typename ComponentDataType >
ComponentDataType * Data (const EntityRef &entity) const
 A utility function for retrieving the Component data for an Entity from a specific Component. More...
 
template<typename ComponentDataType >
ComponentDataType * GetComponent ()
 A utility function for retrieving a reference to a specific Component object, by type. More...
 
virtual void Init ()
 Override this function with code that should be executed when the Component is added to the EntityManager. (This typically happens once, at the beginning of the game before any Entities are added.)
 
virtual void InitEntity (EntityRef &)
 Override this function with code that should be executed when an Entity is added to the Component.
 
virtual RawDataUniquePtr ExportRawData (const EntityRef &) const
 Override this function to return raw data that can be read back later. More...
 
virtual void Cleanup ()
 Override this function with any code that executes when this Component is removed from the EntityManager. (i.e. Usually when the game/state is over and everything is shutting down.)
 
virtual void CleanupEntity (EntityRef &)
 Override this function with any code that needs to be executed when an Entity is removed from this Component.
 
virtual void SetEntityManager (EntityManager *entity_manager)
 Set the EntityManager for this Component. More...
 
virtual void SetComponentIdOnDataType (ComponentId id)
 Sets the Component ID on the data type. More...
 
- Public Member Functions inherited from corgi::ComponentInterface
virtual ~ComponentInterface ()
 A destructor for the Component interface.
 
virtual void AddFromRawData (EntityRef &entity, const void *data)=0
 Creates and populates an Entity from raw data. Components that want to be able to be constructed via the EntityFactory need to implement this. More...
 

Static Public Member Functions

static ComponentId GetComponentId ()
 Get the ID for this Component. More...
 

Protected Member Functions

size_t GetComponentDataIndex (const EntityRef &entity) const
 Get the index of the Component data for a given Entity. More...
 

Protected Attributes

VectorPool< ComponentDatacomponent_data_
 Storage for all of the data for the Component.
 
EntityManagerentity_manager_
 A pointer to the EntityManager for this Component. This is the main point of contact for Components that need to talk to other things.
 
std::unordered_map
< EntityIdType, ComponentIndex
component_index_lookup_
 A map, for translating unique entity IDs into vectorpool indexes.
 

Member Function Documentation

template<typename T>
T* corgi::Component< T >::AddEntity ( EntityRef entity,
AllocationLocation  alloc_location 
)
inline

Adds an Entity to the list that this Component is tracking.

Parameters
[in,out]entityAn EntityRef reference used to add an Entity to the list of Entities that this Component keeps track of.
[in]alloc_locationAn Enum that specifies whether to allocate from the beginning or end of the memory pool.
Returns
Returns the data structure associated with the Component.
Note
If you have already registered for this Component, this will just return a reference to the existing data and will not change anything.
template<typename T>
T* corgi::Component< T >::AddEntity ( EntityRef entity)
inline

Adds an Entity to the list that this Component is tracking.

Note
Entities added through this function allocate from the back of the memory pool.
Parameters
[in,out]entityAn EntityRef reference used to add an Entity to the list of Entities that this Component keeps track of.
Returns
Returns the data structure associated with the Component.
template<typename T>
virtual void corgi::Component< T >::AddEntityGenerically ( EntityRef entity)
inlinevirtual

Provides an alternate way to add Entities if you do not care about the returned data structure, and if you do not feel like casting the BaseComponent into something more specific.

Note
AddEntity is a much better function, except we cannot have it in the interface class, since it needs to know about type T for the return value.
Parameters
[in,out]entityAn EntityRef reference used to add an Entity.

Implements corgi::ComponentInterface.

template<typename T>
virtual EntityIterator corgi::Component< T >::begin ( )
inlinevirtual

Gets an iterator that will iterate over every Entity associated with the Component, starting from the beginning.

Returns
Returns an iterator in the style of std that points to the first Entity in the list of all Entities in the Component.
template<typename T>
template<typename ComponentDataType >
ComponentDataType* corgi::Component< T >::Data ( const EntityRef entity)
inline

A utility function for retrieving the Component data for an Entity from a specific Component.

Template Parameters
ComponentDataTypeThe data type of the Component whose data is returned for the given Entity.
Parameters
[in]entityAn EntityRef reference to the Entity whose Component data should be returned.
Returns
Returns a pointer to the data for the Component of the given Entity or returns null if the Entity is not registered with the Component.
template<typename T>
template<typename ComponentDataType >
ComponentDataType* corgi::Component< T >::Data ( const EntityRef entity) const
inline

A utility function for retrieving the Component data for an Entity from a specific Component.

Template Parameters
ComponentDataTypeThe data type of the Component whose data is returned for the given Entity.
Parameters
[in]entityAn EntityRef reference to the Entity whose Component data should be returned.
Returns
Returns a pointer to the data for the Component of the given Entity or returns null if the Entity is not registered with the Component.
template<typename T>
virtual EntityIterator corgi::Component< T >::end ( )
inlinevirtual

Gets an iterator that points to the end of the list of all entites in the Component.

Returns
Returns an iterator in the style of std that points to the last Entity in the list of all Entities in the Component.
template<typename T>
virtual RawDataUniquePtr corgi::Component< T >::ExportRawData ( const EntityRef ) const
inlinevirtual

Override this function to return raw data that can be read back later.

Warning
By default, Components do not support this functionality. If you wish to support this, you will need to override this function.
Returns
By default, this returns a nullptr.

Implements corgi::ComponentInterface.

Reimplemented in PhysicsComponent, corgi::component_library::RenderMeshComponent, corgi::component_library::TransformComponent, corgi::component_library::GraphComponent, corgi::component_library::AnimationComponent, and corgi::component_library::MetaComponent.

template<typename T>
template<typename ComponentDataType >
ComponentDataType* corgi::Component< T >::GetComponent ( )
inline

A utility function for retrieving a reference to a specific Component object, by type.

Template Parameters
ComponentDataTypeThe data type of the Component.
Returns
Returns a pointer to the data for a specific Component.
template<typename T>
T* corgi::Component< T >::GetComponentData ( size_t  data_index)
inline

Gets the Component data stored at a given index.

Warning
This pointer is NOT stable in memory. Calls to AddEntity and AddEntityGenerically may force the storage class to resize, shuffling around the location of this data.
Parameters
[in]data_indexA size_t representing the index of the desired Component data.
Returns
Returns a pointer of the data structure associated with the Component data, or returns a nullptr if given an invalid data_index.
template<typename T>
T* corgi::Component< T >::GetComponentData ( const EntityRef entity)
inline

Gets the data for a given Entity.

Warning
This pointer is NOT stable in memory. Calls to AddEntity and AddEntityGenerically may force the storage class to resize, shuffling around the location of this data.
Parameters
[in]entityAn EntityRef reference to the Entity whose data should be returned.
Returns
Returns the Entity's data as a pointer of the data structure associated with the Component data, or returns a nullptr if the data does not exist.
template<typename T>
const T* corgi::Component< T >::GetComponentData ( size_t  data_index) const
inline

Gets the Component data stored at a given index.

Warning
This pointer is NOT stable in memory. Calls to AddEntity and AddEntityGenerically may force the storage class to resize, shuffling around the location of this data.
Parameters
[in]data_indexA size_t representing the index of the desired Component data.
Returns
Returns a const pointer of the data structure associated with the Component data, or returns a nullptr if given an invalid data_index.
template<typename T>
const T* corgi::Component< T >::GetComponentData ( const EntityRef entity) const
inline

Gets the data for a given Entity.

Warning
This pointer is NOT stable in memory. Calls to AddEntity and AddEntityGenerically may force the storage class to resize, shuffling around the location of this data.
Parameters
[in]entityAn EntityRef reference to the Entity whose data should be returned.
Returns
Returns the Entity's data as a const pointer of the data structure associated with the Component data, or returns a nullptr if the data does not exist.
template<typename T>
virtual void* corgi::Component< T >::GetComponentDataAsVoid ( const EntityRef entity)
inlinevirtual

Gets the data for a given Entity as a void pointer.

Note
When using GetComponentDataAsVoid, the calling function is expected to know how to handle the data (since it is returned as a void pointer).
Warning
This pointer is NOT stable in memory. Calls to AddEntity and AddEntityGenerically may force the storage class to resize, shuffling around the location of this data.
Parameters
[in]entityAn EntityRef reference to the Entity whose data should be returned.
Returns
Returns the Entity's data as a void pointer, or returns a nullptr if the data does not exist.

Implements corgi::ComponentInterface.

template<typename T>
virtual const void* corgi::Component< T >::GetComponentDataAsVoid ( const EntityRef entity) const
inlinevirtual

Gets the data for a given Entity as a const void pointer.

Note
When using GetComponentDataAsVoid, the calling function is expected to know how to handle the data (since it is returned as a const void pointer).
Warning
This pointer is NOT stable in memory. Calls to AddEntity and AddEntityGenerically may force the storage class to resize, shuffling around the location of this data.
Parameters
[in]entityAn EntityRef reference to the Entity whose data should be returned.
Returns
Returns the Entity's data as a const void pointer, or returns a nullptr if the data does not exist.

Implements corgi::ComponentInterface.

template<typename T>
size_t corgi::Component< T >::GetComponentDataIndex ( const EntityRef entity) const
inlineprotected

Get the index of the Component data for a given Entity.

Parameters
[in]entityAn EntityRef reference to the Entity whose data index will be returned.
Returns
Returns a size_t corresponding to the index of the Component data, or kInvalidComponentIndex if no data could be found.
template<typename T>
static ComponentId corgi::Component< T >::GetComponentId ( )
inlinestatic

Get the ID for this Component.

Returns
Returns the Component ID for this Component.
template<typename T>
template<typename ComponentDataType >
bool corgi::Component< T >::IsRegisteredWithComponent ( const EntityRef entity)
inline

A utility function for checking if an entity is registered with a particular component.

Template Parameters
ComponentDataTypeThe data type of the Component to be checked for registration.
Parameters
[in]entityAn EntityRef reference to the Entity whose Component data is checked.
Returns
Returns true if the entity has been registered with the Component, false otherwise.
template<typename T>
virtual void corgi::Component< T >::RemoveEntity ( EntityRef entity)
inlinevirtual

Removes an Entity from the list of Entities.

This is done by marking the Entity as no longer using this Component, calling the destructor on the data, and returning the memory to the memory pool.

Parameters
[in,out]entityAn EntityRef reference used to remove an Entity from the list of Entities that this Component keeps track of.

Implements corgi::ComponentInterface.

template<typename T>
virtual EntityIterator corgi::Component< T >::RemoveEntity ( EntityIterator  iter)
inlinevirtual

Removes an Entity from the list of Entities.

This is done by marking the Entity as no longer using this Component, calling the destructor on the data, and returning the memory to the memory pool.

Parameters
[in,out]iterAn EntityIterator that references an Entity that should be removed.
Returns
Returns an iterator to the next Entity after the one that was just removed.
template<typename T>
virtual void corgi::Component< T >::SetComponentIdOnDataType ( ComponentId  id)
inlinevirtual

Sets the Component ID on the data type.

Note
This is usually only called by the EntityManager.
Parameters
[in]idThe Component ID to set on the data type.

Implements corgi::ComponentInterface.

template<typename T>
virtual void corgi::Component< T >::SetEntityManager ( EntityManager entity_manager)
inlinevirtual

Set the EntityManager for this Component.

Note
The EntityManager is used as the main point of contact for Components that need to talk to other things.
Parameters
[in]entity_managerA pointer to an EntityManager to associate with this Component.

Implements corgi::ComponentInterface.


The documentation for this class was generated from the following file: