Thrive
Classes | Public Types | Public Member Functions | Friends | List of all members
thrive::ComponentCollection Class Reference

Utility class for the EntityManager. More...

#include <component_collection.h>

Classes

struct  Implementation
 

Public Types

using ChangeCallback = std::function< void(EntityId, Component &)>
 Callback for when a component has been added or removed.
 

Public Member Functions

 ~ComponentCollection ()
 Destructor.
 
Component * operator[] (EntityId entityId) const
 
void clear ()
 Empties the component collection.
 
const std::unordered_map< EntityId, std::unique_ptr< Component > > & components () const
 Returns a reference to the internal component map.
 
bool empty () const
 Checks whether this collection is empty. More...
 
Component * get (EntityId entityId) const
 Retrieves a component from the collection. More...
 
unsigned int registerChangeCallbacks (ChangeCallback onComponentAdded, ChangeCallback onComponentRemoved)
 Registers callbacks for when components are added or removed. More...
 
ComponentTypeId type () const
 The type id of the collection's components.
 
void unregisterChangeCallbacks (unsigned int id)
 Unregisters change callbacks. More...
 

Friends

class EntityManager
 Only the EntityManager should be able to add / remove components.
 

Detailed Description

Utility class for the EntityManager.

A component collection handles components of one specific type. It offers functions to retrieve the component (if any) of a specific entity and callbacks for when a component has been added or removed (mainly used by the EntityFilter).

Component collections are pretty much read-only for anything but the EntityManager. Use the manager to actually add or remove components.

Member Function Documentation

bool ComponentCollection::empty ( ) const

Checks whether this collection is empty.

Returns
true if the collection is empty, false otherwise
Component * ComponentCollection::get ( EntityId  entityId) const

Retrieves a component from the collection.

Parameters
entityIdThe entity the component belongs to
Returns
A non-owning pointer to the component or nullptr if no such component exists
Component * ComponentCollection::operator[] ( EntityId  entityId) const
unsigned int ComponentCollection::registerChangeCallbacks ( ChangeCallback  onComponentAdded,
ChangeCallback  onComponentRemoved 
)

Registers callbacks for when components are added or removed.

Parameters
onComponentAddedCalled when a component has been added
onComponentRemovedCalled when a component has been removed
Returns
An identifier with which you can remove the callbacks.
See also
unregisterChangeCallbacks
void ComponentCollection::unregisterChangeCallbacks ( unsigned int  id)

Unregisters change callbacks.

If the id could not be found, does nothing.

Parameters
idThe id returned by registerChangeCallbacks

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