Flip Reference

Entity Class Reference

Declared in

flip/Entity.h

class Entity;

flip::Entity is a generic associative container of any types where the key is the key type itself and the value an instance of that type.

Entitys are used to store custom data in flip Objects and are typically used in observers.

They are a map where the key is a type and the value a type erasure.

Member Functions Synopsys

emplace

Constructs in-place a new element

erase

Removes an element

use

Returns a reference to the element

get

Returns a pointer to the element

Member Functions

emplace

template <class T, class... Args>   T &   emplace (Args &&... args);

Constructs in-place a new element of type T by calling T constructor with variadic template parameters args.

WARNING: Only one instance of type T can exist in the entity.

Returns a reference to the new in-place constructed element.

Example:

class A
{
public:
   A (double value);
};
Entity entity;
entity.emplace <A> (2.5);

erase

template <class T>   void  erase ();

Removes the element of type T.

WARNING: The element must exist in the entity.

class A
{
public:
   A (double value);
};
Entity entity;
entity.emplace <A> (2.5);
// 'entity' owns a 'A' object
entity.erase <A> ();
// 'entity' does not have anymore the 'A' object

use

template <class T>   T &   use ();

Returns a reference to the element of type T.

WARNING: The element must exist in the entity. If not the method will throw an exception

class A
{
public:
   A (double value);
};
Entity entity;
entity.emplace <A> (2.5);
auto & a = entity.use <A> ();
// 'a' is of type 'A &' and its value is '2.5'

get

template <class T>   T *   get ();

Returns a pointer to the element of type T if it exists, nullptr otherwise.

class A
{
public:
   A (double value);
};
Entity entity;
entity.emplace <A> (2.5);
auto ptr = entity.get <A> ();
// 'ptr' points to entity object 'A'
entity.erase <A> ();
// 'entity' does not have anymore the 'A' object
auto ptr2 = entity.get <A> ();
// 'ptr2' is 'nullptr'