CORGI
An open source project by FPL.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Groups Pages
corgi::component_library::EntityFactory Class Referenceabstract

An EntityFactory builds Entities based on prototypes, using FlatBuffers to specify the raw data for Entities. More...

#include <entity_factory.h>

Inherits corgi::EntityFactoryInterface.

Inherited by corgi::component_library::DefaultEntityFactory.

Detailed Description

An EntityFactory builds Entities based on prototypes, using FlatBuffers to specify the raw data for Entities.

Public Member Functions

 EntityFactory ()
 The default constructor for an empty EntityFactory.
 
bool AddEntityLibrary (const char *entity_library_file)
 Add a file from which you can load Entity prototype definitions. More...
 
bool WillBeKeptInMemory (const void *pointer)
 Check if the given pointer points to something that will be kept in memory for the lifetime of the EntityFactory. More...
 
virtual int LoadEntitiesFromFile (const char *filename, corgi::EntityManager *entity_manager)
 Load Entities from a given Entity list file. More...
 
int LoadEntityListFromMemory (const void *raw_entity_list, corgi::EntityManager *entity_manager, std::vector< corgi::EntityRef > *entities_loaded)
 Loads a list of Entities from an in-memory buffer. More...
 
void OverrideCachedFile (const char *filename, std::unique_ptr< std::string > new_data)
 Override a cached file with data from memory that will persist until exit. More...
 
corgi::EntityRef CreateEntityFromData (const void *data, corgi::EntityManager *entity_manager)
 Initialize an Entity from an Entity definition. More...
 
virtual corgi::EntityRef CreateEntityFromPrototype (const char *prototype_name, corgi::EntityManager *entity_manager)
 Initialize an entity from a given prototype. More...
 
void SetComponentType (corgi::ComponentId component_id, unsigned int data_type, const char *table_name)
 When you register each Component with the Entity system, it will get a component ID. This factory needs to know the Component ID assigned for each Component data type (the data_type() in the flatbuffer union). More...
 
corgi::ComponentId DataTypeToComponentId (unsigned int data_type)
 Get the Component Id for a given data type specifier. More...
 
unsigned int ComponentIdToDataType (corgi::ComponentId component_id)
 Get the data type specifier for a Component, given a Component ID. More...
 
const char * ComponentIdToTableName (corgi::ComponentId component_id)
 Get the table name for a Component, given a Component ID. More...
 
virtual bool SerializeEntity (corgi::EntityRef &entity, corgi::EntityManager *entity_manager, std::vector< uint8_t > *entity_serialized_output)
 Serialize an Entity into whatever binary type you are using for them. More...
 
virtual bool SerializeEntityList (const std::vector< std::vector< uint8_t >> &entity_definitions, std::vector< uint8_t > *entity_list_serialized)
 After you call SerializeEntity on a few Entities, you may call this method to put them into a proper list. More...
 
corgi::ComponentId max_component_id ()
 Get the maximum component ID. More...
 
bool debug_entity_creation () const
 Check if debug logging is enabled. More...
 
void set_debug_entity_creation (bool b)
 Enable debug logging during Entity creation. More...
 
virtual bool ReadEntityList (const void *entity_list, std::vector< const void * > *entity_defs)=0
 Handles reading an Entity list and extracting the individual Entity data definitions. More...
 
virtual bool ReadEntityDefinition (const void *entity_definition, std::vector< const void * > *component_defs)=0
 Handles reading an Entity definition and extracting the individual Component data definitions. More...
 
virtual bool CreatePrototypeRequest (const char *prototype_name, std::vector< uint8_t > *request)=0
 Creates an Entity list that contains a single Entity definition, which contains a single Component definition (a MetaDef with prototype set to the requested prototype name). More...
 
virtual bool CreateEntityDefinition (const std::vector< const void * > &component_data, std::vector< uint8_t > *entity_definition)=0
 Handles building a single Entity definition flatbuffer from a list of an Entity's Component definitions. More...
 
virtual bool CreateEntityList (const std::vector< const void * > &entity_defs, std::vector< uint8_t > *entity_list)=0
 Handles building an Entity list flatbuffer from a collection of individual Entity flatbuffers. More...
 
virtual void LoadEntityData (const void *def, corgi::EntityManager *entity_manager, corgi::EntityRef &entity, bool is_prototype)
 Creates an Entity from the Entity definition, recursively building up from the prototypes. More...
 
void SetFlatbufferSchema (const char *binary_schema_filename)
 This factory and its subclasses need to know how to parse the Entity FlatBuffers using reflection. More...
 
const std::string & flatbuffer_binary_schema_data () const
 Get the FlatBuffer binary schema that you loaded with SetFlatbufferSchema(). More...
 
const std::unordered_map
< std::string, const void * > & 
prototype_data () const
 Get the map with all the current prototypes. More...
 
- Public Member Functions inherited from corgi::EntityFactoryInterface
virtual ~EntityFactoryInterface ()
 A destructor of the entity factory interface.
 

Static Public Attributes

static const unsigned int kDataTypeNone = 0
 This is equivalent to the *_NONE value of the FlatBuffer union enum.
 
static const int kErrorLoadingEntities = -1
 The return value for LoadEntitiesFromFile and LoadEntityListFromMemory if the methods are unable to read the Entity list.
 

Member Function Documentation

bool corgi::component_library::EntityFactory::AddEntityLibrary ( const char *  entity_library_file)

Add a file from which you can load Entity prototype definitions.

Parameters
[in]entity_library_fileA UTF-8 C-string representing the filename of the Entity library file that contains the prototype definitions.
Returns
Returns true if the file was added successfully. Otherwise it returns false.
unsigned int corgi::component_library::EntityFactory::ComponentIdToDataType ( corgi::ComponentId  component_id)
inline

Get the data type specifier for a Component, given a Component ID.

Parameters
[in]component_idThe ComponentId of the Component whose data type specifier should be returned.
Returns
Returns an int corresponding to the data type specifier.
const char* corgi::component_library::EntityFactory::ComponentIdToTableName ( corgi::ComponentId  component_id)
inline

Get the table name for a Component, given a Component ID.

Parameters
[in]component_idThe ComponentId of the Component whose table name should be returned.
Returns
Returns a C-string of the FlatBuffer schema table name for the Component.
virtual bool corgi::component_library::EntityFactory::CreateEntityDefinition ( const std::vector< const void * > &  component_data,
std::vector< uint8_t > *  entity_definition 
)
pure virtual

Handles building a single Entity definition flatbuffer from a list of an Entity's Component definitions.

Note
You MUST override this function to create your own EntityFactory.
Parameters
[in]component_dataA const reference to a std::vector that contains the list of the Entity's Component definitions, which can be indexed by Component ID.
[out]entity_definitionA vector to capture the output of the Entity definition.
Returns
Returns true if the Entity definition was successfully created. Otherwise, it returns false.

Implemented in corgi::component_library::DefaultEntityFactory.

corgi::EntityRef corgi::component_library::EntityFactory::CreateEntityFromData ( const void *  data,
corgi::EntityManager entity_manager 
)
virtual

Initialize an Entity from an Entity definition.

Note
This helper method is called by LoadRawEntityList for each Entity definition.
Parameters
[in]dataA void pointer to the data to create an Entity from.
[in,out]entity_managerAn EntityManager pointer to the EntityManager that should load the Entity from the data.
Returns
Returns an EntityRef to the new Entity that was created.

Implements corgi::EntityFactoryInterface.

virtual corgi::EntityRef corgi::component_library::EntityFactory::CreateEntityFromPrototype ( const char *  prototype_name,
corgi::EntityManager entity_manager 
)
virtual

Initialize an entity from a given prototype.

Parameters
[in]prototype_nameA C-string containing the name of the prototype to initialize the Entity with.
[in,out]entity_managerAn EntityManager pointer to the EntityManager that should create the initialized Entity.
Returns
Returns an EntityRef to the new Entity that was initialized from the prototype.
virtual bool corgi::component_library::EntityFactory::CreateEntityList ( const std::vector< const void * > &  entity_defs,
std::vector< uint8_t > *  entity_list 
)
pure virtual

Handles building an Entity list flatbuffer from a collection of individual Entity flatbuffers.

Note
You MUST override this function to create your own EntityFactory.
Parameters
[in]entity_defsA const reference to a std::vector that contains all the Entity flatbuffers.
[out]entity_listA vector to capture the output of the Entity list.
Returns
Returns true if the Entity list was successfully created. Otherwise, it returns false.

Implemented in corgi::component_library::DefaultEntityFactory.

virtual bool corgi::component_library::EntityFactory::CreatePrototypeRequest ( const char *  prototype_name,
std::vector< uint8_t > *  request 
)
pure virtual

Creates an Entity list that contains a single Entity definition, which contains a single Component definition (a MetaDef with prototype set to the requested prototype name).

Note
You MUST override this function to create your own EntityFactory.
Parameters
[in]prototype_nameA C-string name of the prototype, which is used to set the prototype field in the MetaComponent.
[out]requestA vector to capture the output of the prototype request.
Returns
Returns true if the request was created successfully. Otherwise it returns false.

Implemented in corgi::component_library::DefaultEntityFactory.

corgi::ComponentId corgi::component_library::EntityFactory::DataTypeToComponentId ( unsigned int  data_type)
inline

Get the Component Id for a given data type specifier.

Parameters
[in]data_typeAn int corresponding to the data type specifier.
Returns
Returns a ComponentId for the Component with a data type of data_type.
bool corgi::component_library::EntityFactory::debug_entity_creation ( ) const
inline

Check if debug logging is enabled.

Returns
Returns true if debug logging is enabled. Otherwise, it returns false.
const std::string& corgi::component_library::EntityFactory::flatbuffer_binary_schema_data ( ) const
inline

Get the FlatBuffer binary schema that you loaded with SetFlatbufferSchema().

Returns
Returns a const reference to the std::string name of the FlatBuffer schema.
virtual int corgi::component_library::EntityFactory::LoadEntitiesFromFile ( const char *  filename,
corgi::EntityManager entity_manager 
)
virtual

Load Entities from a given Entity list file.

Parameters
[in]filenameA UTF-8 C-string representing the filename of the Entity list file to load the Entities from.
[in,out]entity_managerAn EntityManager pointer to the EntityManager that should load all the Entities from filename.
Returns
Returns the number of Entities that were loaded, or returns kErrorLoadingEntities if there was en error.
virtual void corgi::component_library::EntityFactory::LoadEntityData ( const void *  def,
corgi::EntityManager entity_manager,
corgi::EntityRef entity,
bool  is_prototype 
)
virtual

Creates an Entity from the Entity definition, recursively building up from the prototypes.

Note
: You MAY override this function if you wish to change the way that prototyping works.
Parameters
[in]defA const void pointer to the Entity definition that will be used to build the prototypes.
[in,out]entity_managerA pointer to the EntityManager that the Entity will be created with.
[out]entityA reference to the Entity that is being created.
[in]is_prototypeA bool determining if this is a prototype.
int corgi::component_library::EntityFactory::LoadEntityListFromMemory ( const void *  raw_entity_list,
corgi::EntityManager entity_manager,
std::vector< corgi::EntityRef > *  entities_loaded 
)

Loads a list of Entities from an in-memory buffer.

Note
This is a helper method used by LoadEntitiesFromFile to do a lot of the work actually loading the Entities.
Parameters
[in]raw_entity_listA void pointer to the Entity list.
[in,out]entity_managerAn EntityManager pointer to the EntityManager that should load all the Entities from memory.
[out]entities_loadedAn optional parameter to obtain the list of the loaded Entities. Pass in a pointer to a std::vector of EntityRefs to capture this output. Otherwise, pass in NULL to ignore this parameter.
Returns
Returns the number of Entities that were loaded, or returns kErrorLoadingEntities if there was an error.
corgi::ComponentId corgi::component_library::EntityFactory::max_component_id ( )
inline

Get the maximum component ID.

Returns
Returns the highest component ID.
void corgi::component_library::EntityFactory::OverrideCachedFile ( const char *  filename,
std::unique_ptr< std::string >  new_data 
)

Override a cached file with data from memory that will persist until exit.

Parameters
[in]filenameA UTF-8 C-string representing the filename of the cached file whose data should be overridden.
[in]new_dataA std::unique_ptr that points to a std::string of data to override the cached file with.
const std::unordered_map<std::string, const void*>& corgi::component_library::EntityFactory::prototype_data ( ) const
inline

Get the map with all the current prototypes.

Returns
Returns a const reference to the std::unordered_map with the prototype data
virtual bool corgi::component_library::EntityFactory::ReadEntityDefinition ( const void *  entity_definition,
std::vector< const void * > *  component_defs 
)
pure virtual

Handles reading an Entity definition and extracting the individual Component data definitions.

In your output, you should index component_defs by component ID, and any Components not specified by this Entity's definition should be set to nullptr.

Note
You MUST override this function to create your own EntityFactory.
Parameters
[in]entity_definitionA const void pointer to the Entity definition whose Component data definitions should be extracted.
[out]component_defsA vector that captures the output of the extracted Component data definitions.
Returns
Returns true if the entity_definition was parsed successfully. Otherwise it returns false.

Implemented in corgi::component_library::DefaultEntityFactory.

virtual bool corgi::component_library::EntityFactory::ReadEntityList ( const void *  entity_list,
std::vector< const void * > *  entity_defs 
)
pure virtual

Handles reading an Entity list and extracting the individual Entity data definitions.

Note
You MUST override this function to create your own EntityFactory.
Parameters
[in]entity_listA const void pointer to the start of the list of Entities.
[out]entity_defsA vector that captures the output of the extracted entity data definitions.
Returns
returns true if the list was parsed successfully. Otherwise it returns false.

Implemented in corgi::component_library::DefaultEntityFactory.

virtual bool corgi::component_library::EntityFactory::SerializeEntity ( corgi::EntityRef entity,
corgi::EntityManager entity_manager,
std::vector< uint8_t > *  entity_serialized_output 
)
virtual

Serialize an Entity into whatever binary type you are using for them.

Note
Calls CreateEntityDefinition(), which you implement, to do the work.
Parameters
[in]entityThe Entity that should be serialized.
[in]entity_managerThe EntityManager responsible for the Entity that should be serialized.
[out]entity_serialized_outputA vector to capture the output of the Entity definition.
Returns
Returns true if the Entity definition was successfully created. Otherwise, it returns false.
virtual bool corgi::component_library::EntityFactory::SerializeEntityList ( const std::vector< std::vector< uint8_t >> &  entity_definitions,
std::vector< uint8_t > *  entity_list_serialized 
)
virtual

After you call SerializeEntity on a few Entities, you may call this method to put them into a proper list.

Note
: Calls CreateEntityList(), which you implement.
Parameters
[in]entity_definitionsA reference to a vector list of Entity definitions (vector<uint8_t>) that should be put into a proper Entity list.
[out]entity_list_serializedA vector to capture the output of the Entity list.
Returns
Returns true if the Entity list was successfully created. Otherwise, it returns false.
void corgi::component_library::EntityFactory::set_debug_entity_creation ( bool  b)
inline

Enable debug logging during Entity creation.

Parameters
[in]bA bool determining if logging should be enabled.
void corgi::component_library::EntityFactory::SetComponentType ( corgi::ComponentId  component_id,
unsigned int  data_type,
const char *  table_name 
)

When you register each Component with the Entity system, it will get a component ID. This factory needs to know the Component ID assigned for each Component data type (the data_type() in the flatbuffer union).

Parameters
[in]component_idThe ComponentId of the Component in the Entity system.
[in]data_typeAn enum for the data type of the Component definition within the FlatBuffer union.
[in]table_nameA C-string of the table name of the FlatBuffer schema table for the Component definition.
void corgi::component_library::EntityFactory::SetFlatbufferSchema ( const char *  binary_schema_filename)

This factory and its subclasses need to know how to parse the Entity FlatBuffers using reflection.

You will need to specify the .bfbs file for your Component data type.

Parameters
[in]binary_schema_filenameA C-string of the filename for the FlatBuffer schema file.
bool corgi::component_library::EntityFactory::WillBeKeptInMemory ( const void *  pointer)

Check if the given pointer points to something that will be kept in memory for the lifetime of the EntityFactory.

Parameters
[in]pointerA void pointer that should be checked if its data will be kept in memory.
Returns
Returns true if the given pointer points to something that will be kept in memory for the of the EntityFactory. Otherwise it returns false.

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