A container for Object instances. More...
#include <ObjectContainer.h>
Public Types | |
using | ObjectPtr = std::shared_ptr< T > |
Shared object pointer. More... | |
using | constObjectPtr = std::shared_ptr< const T > |
Const shared object pointer. More... | |
using | constIterator = typename std::vector< ObjectPtr >::const_iterator |
template<typename... Args> | |
using | Callback = std::function< void(Args...)> |
using | Predicate = std::function< bool(const constObjectPtr)> |
Public Member Functions | |
ObjectContainer () | |
Default constructor. More... | |
void | addObject (ObjectPtr object, const std::string &group="none") |
Add an object to the container. More... | |
ObjectPtr | findByTag (const std::string &tag) |
Get an object with a given tag. More... | |
ObjectPtr | findByTag (const std::string &tag) const |
template<typename U > | |
std::shared_ptr< U > | findByTag (const std::string &tag) |
Get an object with a given tag. More... | |
template<typename U > | |
std::shared_ptr< const U > | findByTag (const std::string &tag) const |
Get an object with a given tag. More... | |
ObjectPtr | findById (unsigned int id) |
Get an object with the given id. More... | |
ObjectPtr | findById (unsigned int id) const |
template<typename U > | |
std::shared_ptr< U > | findById (unsigned int id) |
Get an object with the given id. More... | |
template<typename U > | |
std::shared_ptr< const U > | findById (unsigned int id) const |
Get an object with the given id. More... | |
ObjectPtr | findIf (Predicate predicate) |
Conditionally find an object in the container. More... | |
const ObjectPtr | findIf (Predicate predicate) const |
void | removeByTag (const std::string &tag) |
Remove all objects with the given tag. More... | |
void | removeById (unsigned int id) |
Remove a game object with the given id. More... | |
bool | remove (ObjectPtr object) |
Remove an object from the container. More... | |
void | removeIf (Predicate predicate) |
Conditionally remove objects from the container. More... | |
void | removeAll () |
Remove all objects from the container. More... | |
std::size_t | getCount () const |
Get the number of objects in the container. More... | |
ObjectContainer< T > & | createGroup (const std::string &name) |
Create a group to add objects to. More... | |
ObjectContainer< T > & | getGroup (const std::string &name) const |
Get objects in a group. More... | |
bool | hasGroup (const std::string &name) const |
Check whether or not the container has a given group. More... | |
bool | removeGroup (const std::string &name) |
Remove a group from the container. More... | |
void | removeAllGroups () |
Remove all groups from the container. More... | |
void | forEach (Callback< ObjectPtr > callback) |
Execute a callback function for each object in the container. More... | |
void | forEachInGroup (const std::string &name, Callback< ObjectPtr > callback) |
Execute a callback for each object in a group. More... | |
virtual | ~ObjectContainer ()=default |
Destructor. More... | |
A container for Object instances.
Definition at line 41 of file ObjectContainer.h.
using ime::ObjectContainer< T >::constObjectPtr = std::shared_ptr<const T> |
Const shared object pointer.
Definition at line 44 of file ObjectContainer.h.
using ime::ObjectContainer< T >::ObjectPtr = std::shared_ptr<T> |
Shared object pointer.
Definition at line 43 of file ObjectContainer.h.
ime::ObjectContainer< T >::ObjectContainer | ( | ) |
Default constructor.
|
virtualdefault |
Destructor.
void ime::ObjectContainer< T >::addObject | ( | ObjectPtr | object, |
const std::string & | group = "none" |
||
) |
Add an object to the container.
object | object to be added |
group | The name of the group to add the object to |
If the group is not found, it will be created and the object will be the first member of it. Groups are useful if you want to refer to objects that are the same or similar as a whole
By default the object does not belong to any group
ObjectContainer<T>& ime::ObjectContainer< T >::createGroup | ( | const std::string & | name | ) |
Create a group to add objects to.
name | The name of the group |
This function is useful if you want to relate some objects and refer to them as a whole using a common group name instead of using a common tag name and looping though the container to find which objects have a given tag
ObjectPtr ime::ObjectContainer< T >::findById | ( | unsigned int | id | ) |
Get an object with the given id.
id | The id of the object to be retrieved |
std::shared_ptr<U> ime::ObjectContainer< T >::findById | ( | unsigned int | id | ) |
Get an object with the given id.
id | The id of the object to be retrieved |
You can use this function to get the derived class type U if T is a base class
std::shared_ptr<const U> ime::ObjectContainer< T >::findById | ( | unsigned int | id | ) | const |
Get an object with the given id.
id | The id of the object to be retrieved |
You can use this function to get the derived class type U if T is a base class
ObjectPtr ime::ObjectContainer< T >::findByTag | ( | const std::string & | tag | ) |
Get an object with a given tag.
tag | The tag of the object to be searched |
Note that this function will return the first object it finds with the the given tag
std::shared_ptr<U> ime::ObjectContainer< T >::findByTag | ( | const std::string & | tag | ) |
Get an object with a given tag.
tag | The tag of the object to be searched |
Note that this function will return the first object it finds with the the given tag. You can use this function to get the derived class type U if T is a base class:
std::shared_ptr<const U> ime::ObjectContainer< T >::findByTag | ( | const std::string & | tag | ) | const |
Get an object with a given tag.
tag | The tag of the object to be searched |
Note that this function will return the first object it finds with the the given tag. You can use this function to get the derived class type U if T is a base class:
ObjectPtr ime::ObjectContainer< T >::findIf | ( | Predicate | predicate | ) |
Conditionally find an object in the container.
predicate | A function which returns true if the object should be returned or false if the search should continue |
void ime::ObjectContainer< T >::forEach | ( | Callback< ObjectPtr > | callback | ) |
Execute a callback function for each object in the container.
callback | The function to be executed |
void ime::ObjectContainer< T >::forEachInGroup | ( | const std::string & | name, |
Callback< ObjectPtr > | callback | ||
) |
Execute a callback for each object in a group.
name | The name of the group to execute callback on |
callback | The function to be executed for each object in the group |
This function is a shortcut for:
std::size_t ime::ObjectContainer< T >::getCount | ( | ) | const |
Get the number of objects in the container.
ObjectContainer<T>& ime::ObjectContainer< T >::getGroup | ( | const std::string & | name | ) | const |
Get objects in a group.
name | The name of the group |
bool ime::ObjectContainer< T >::hasGroup | ( | const std::string & | name | ) | const |
Check whether or not the container has a given group.
name | The name of the group to be checked |
bool ime::ObjectContainer< T >::remove | ( | ObjectPtr | object | ) |
Remove an object from the container.
object | The object to be removed |
void ime::ObjectContainer< T >::removeAll | ( | ) |
Remove all objects from the container.
void ime::ObjectContainer< T >::removeAllGroups | ( | ) |
Remove all groups from the container.
This function will remove all objects that belong to a group from the container, leaving only objects that do not belong to a group if any
void ime::ObjectContainer< T >::removeById | ( | unsigned int | id | ) |
Remove a game object with the given id.
id | The id of the object to be removed |
void ime::ObjectContainer< T >::removeByTag | ( | const std::string & | tag | ) |
Remove all objects with the given tag.
tag | Tag of the objects to be removed |
bool ime::ObjectContainer< T >::removeGroup | ( | const std::string & | name | ) |
Remove a group from the container.
name | The name of the group to be removed |
This function will remove all objects in the given group from the container
void ime::ObjectContainer< T >::removeIf | ( | Predicate | predicate | ) |
Conditionally remove objects from the container.
predicate | A function which returns true if the object should be removed or false if it should not be removed from the container |
Note that this function will remove all objects for which the predicate return true