Public Types | Public Member Functions | List of all members
ime::ObjectContainer< T > Class Template Reference

A container for Object instances. More...

#include <ObjectContainer.h>

Inheritance diagram for ime::ObjectContainer< T >:
ime::DrawableContainer< T >

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...
 

Detailed Description

template<typename T>
class ime::ObjectContainer< T >

A container for Object instances.

Definition at line 41 of file ObjectContainer.h.

Member Typedef Documentation

◆ constObjectPtr

template<typename T >
using ime::ObjectContainer< T >::constObjectPtr = std::shared_ptr<const T>

Const shared object pointer.

Definition at line 44 of file ObjectContainer.h.

◆ ObjectPtr

template<typename T >
using ime::ObjectContainer< T >::ObjectPtr = std::shared_ptr<T>

Shared object pointer.

Definition at line 43 of file ObjectContainer.h.

Constructor & Destructor Documentation

◆ ObjectContainer()

template<typename T >
ime::ObjectContainer< T >::ObjectContainer ( )

Default constructor.

◆ ~ObjectContainer()

template<typename T >
virtual ime::ObjectContainer< T >::~ObjectContainer ( )
virtualdefault

Destructor.

Member Function Documentation

◆ addObject()

template<typename T >
void ime::ObjectContainer< T >::addObject ( ObjectPtr  object,
const std::string &  group = "none" 
)

Add an object to the container.

Parameters
objectobject to be added
groupThe 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

See also
createGroup

◆ createGroup()

template<typename T >
ObjectContainer<T>& ime::ObjectContainer< T >::createGroup ( const std::string &  name)

Create a group to add objects to.

Parameters
nameThe 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

auto weapons = objectContainer.createGroup("weapons");
weapons.add(knife);
weapons.add(machete, 5);
weapons.add(pistol, 0, "pistols");
Note
The name of the groups must be unique
See also
add

◆ findById() [1/3]

template<typename T >
ObjectPtr ime::ObjectContainer< T >::findById ( unsigned int  id)

Get an object with the given id.

Parameters
idThe id of the object to be retrieved
Returns
The object with the given id or a nullptr if the object could not be found in the container

◆ findById() [2/3]

template<typename T >
template<typename U >
std::shared_ptr<U> ime::ObjectContainer< T >::findById ( unsigned int  id)

Get an object with the given id.

Parameters
idThe id of the object to be retrieved
Returns
The object with the given id or a nullptr if the object could not be found in the container or the the object is found but it is not of type U

You can use this function to get the derived class type U if T is a base class

// The type of rectangle is std::shared_ptr<ime::Shape>
auto rectangle = shapeContainer.findById(4);
// The type of rectangle2 is std::shared_ptr<ime::RectangleShape>
auto rectangle2 = shapeContainer.findById<ime::RectangleShape>(4);

◆ findById() [3/3]

template<typename T >
template<typename U >
std::shared_ptr<const U> ime::ObjectContainer< T >::findById ( unsigned int  id) const

Get an object with the given id.

Parameters
idThe id of the object to be retrieved
Returns
The object with the given id or a nullptr if the object could not be found in the container or the the object is found but it is not of type U

You can use this function to get the derived class type U if T is a base class

// The type of rectangle is std::shared_ptr<ime::Shape>
auto rectangle = shapeContainer.findById(4);
// The type of rectangle2 is std::shared_ptr<ime::RectangleShape>
auto rectangle2 = shapeContainer.findById<ime::RectangleShape>(4);

◆ findByTag() [1/3]

template<typename T >
ObjectPtr ime::ObjectContainer< T >::findByTag ( const std::string &  tag)

Get an object with a given tag.

Parameters
tagThe tag of the object to be searched
Returns
The object with the given tag or a nullptr if the object could not be found in the container

Note that this function will return the first object it finds with the the given tag

◆ findByTag() [2/3]

template<typename T >
template<typename U >
std::shared_ptr<U> ime::ObjectContainer< T >::findByTag ( const std::string &  tag)

Get an object with a given tag.

Parameters
tagThe tag of the object to be searched
Returns
The object with the given tag or a nullptr if the object could not be found in the container or the the object is found but it is not of type U

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:

// The type of rectangle is std::shared_ptr<ime::Shape>
auto rectangle = shapeContainer.findByTag("myRect");
// The type of rectangle2 is std::shared_ptr<ime::RectangleShape>
auto rectangle2 = shapeContainer.findByTag<ime::RectangleShape>("myRect");

◆ findByTag() [3/3]

template<typename T >
template<typename U >
std::shared_ptr<const U> ime::ObjectContainer< T >::findByTag ( const std::string &  tag) const

Get an object with a given tag.

Parameters
tagThe tag of the object to be searched
Returns
The object with the given tag or a nullptr if the object could not be found in the container or the the object is found but it is not of type U

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:

// The type of rectangle is std::shared_ptr<ime::Shape>
auto rectangle = shapeContainer.findByTag("myRect");
// The type of rectangle2 is std::shared_ptr<ime::RectangleShape>
auto rectangle2 = shapeContainer.findByTag<ime::RectangleShape>("myRect");

◆ findIf()

template<typename T >
ObjectPtr ime::ObjectContainer< T >::findIf ( Predicate  predicate)

Conditionally find an object in the container.

Parameters
predicateA function which returns true if the object should be returned or false if the search should continue
Returns
The object that matches the found condition or a nullptr if an object that matches the success condition could not be found in the container

◆ forEach()

template<typename T >
void ime::ObjectContainer< T >::forEach ( Callback< ObjectPtr callback)

Execute a callback function for each object in the container.

Parameters
callbackThe function to be executed
Note
The given callback is applied to all objects that do NOT belong to a group
See also
forEachInGroup

◆ forEachInGroup()

template<typename T >
void ime::ObjectContainer< T >::forEachInGroup ( const std::string &  name,
Callback< ObjectPtr callback 
)

Execute a callback for each object in a group.

Parameters
nameThe name of the group to execute callback on
callbackThe function to be executed for each object in the group

This function is a shortcut for:

//It's undefined behaviour to call getGroup if the group does not exist
if (container.hasGroup(name)) {
container.getGroup(name)->forEach(...);
}
See also
forEach

◆ getCount()

template<typename T >
std::size_t ime::ObjectContainer< T >::getCount ( ) const

Get the number of objects in the container.

Returns
The number of objects in the container

◆ getGroup()

template<typename T >
ObjectContainer<T>& ime::ObjectContainer< T >::getGroup ( const std::string &  name) const

Get objects in a group.

Parameters
nameThe name of the group
Warning
The specified group must exist first before calling this function otherwise undefined behavior
See also
hasGroup

◆ hasGroup()

template<typename T >
bool ime::ObjectContainer< T >::hasGroup ( const std::string &  name) const

Check whether or not the container has a given group.

Parameters
nameThe name of the group to be checked
Returns
True if the container has the specified group, otherwise false

◆ remove()

template<typename T >
bool ime::ObjectContainer< T >::remove ( ObjectPtr  object)

Remove an object from the container.

Parameters
objectThe object to be removed
Returns
True if the object was removed or false if the object does not exist in the container

◆ removeAll()

template<typename T >
void ime::ObjectContainer< T >::removeAll ( )

Remove all objects from the container.

◆ removeAllGroups()

template<typename T >
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

See also
removeGroup

◆ removeById()

template<typename T >
void ime::ObjectContainer< T >::removeById ( unsigned int  id)

Remove a game object with the given id.

Parameters
idThe id of the object to be removed

◆ removeByTag()

template<typename T >
void ime::ObjectContainer< T >::removeByTag ( const std::string &  tag)

Remove all objects with the given tag.

Parameters
tagTag of the objects to be removed

◆ removeGroup()

template<typename T >
bool ime::ObjectContainer< T >::removeGroup ( const std::string &  name)

Remove a group from the container.

Parameters
nameThe name of the group to be removed
Returns
True if the group was removed or false if the specified group does not exist in the container

This function will remove all objects in the given group from the container

See also
add and removeAllGroups

◆ removeIf()

template<typename T >
void ime::ObjectContainer< T >::removeIf ( Predicate  predicate)

Conditionally remove objects from the container.

Parameters
predicateA function which returns true if the object should be removed or false if it should not be removed from the container
Returns
True if the objects were removed from the container or false if no objects matched the remove condition

Note that this function will remove all objects for which the predicate return true


The documentation for this class was generated from the following file:
ime::RectangleShape
A 2D shape having four sides and four corners (90 degree angles)
Definition: RectangleShape.h:36