Cogs.Foundation
Loading...
Searching...
No Matches
Public Member Functions | Static Public Member Functions | Private Member Functions | Static Private Member Functions | List of all members
Cogs::ComponentModel::Component Class Reference

Base class for Component instances. More...

#include <Component.h>

Public Member Functions

 Component ()=default
 
class EntitygetContainer () const
 Get the container currently owning this component instance.
 
void setContainer (class Entity *container)
 Set the container owning this component instance.
 
template<typename ComponentType >
ComponentType * getComponent () const
 
COGSFOUNDATION_API ComponentgetComponent (const Reflection::Name &name) const
 
COGSFOUNDATION_API ComponentgetComponent (const Reflection::TypeId &id) const
 
template<typename ComponentType >
ComponentHandle getComponentHandle () const
 
COGSFOUNDATION_API ComponentHandle getComponentHandle (const Reflection::Name &name) const
 
COGSFOUNDATION_API ComponentHandle getComponentHandle (const Reflection::TypeId &id) const
 
void setActive ()
 Sets the component to the ComponentFlags::Active state.
 
bool isActive () const
 Gets if the component is currently set to the ComponentFlags::Active state.
 
void setChanged ()
 Sets the component to the ComponentFlags::Changed state with carry.
 
void setChangedTransient ()
 Sets the component to the ComponentFlags::Changed state without carry.
 
void setFieldChanged (const Reflection::FieldId fieldId)
 Sets the component to the ComponentFlags::Changed state without carry.
 
template<typename ClassType , typename FieldType >
void setFieldChanged (FieldType ClassType::*field)
 Sets a flag indicating that the given field has changed.
 
void setFieldChangedTransient (const Reflection::FieldId fieldId)
 Sets the component to the ComponentFlags::Changed state without carry.
 
bool hasChanged () const
 
bool hasAnyfieldChanged () const
 Gets if any fields have been changed.
 
bool hasFieldChanged (const Reflection::FieldId fieldId) const
 Gets if the field with the given id on this component instance has changed.
 
template<typename ClassType , typename FieldType >
bool hasFieldChanged (FieldType ClassType::*field) const
 Gets if the given field has changed.
 
void resetCarryChanged ()
 Reset the CarryChanged flag. Called at start of redraw. See ComponentFlags::CarryChanged.
 
void resetChanged ()
 Resets the changed state of the component, respecting any carry state set.
 
template<typename ClassType , typename FieldType >
Reflection::FieldId getFieldId (FieldType ClassType::*field) const
 Gets field ID of the given field.
 
void resetFieldsChanged ()
 Resets the state of all changed field flags.
 
void resetFieldChanged (const Reflection::FieldId fieldId)
 Resets the changed state of the given fieldId.
 
template<typename ClassType , typename FieldType >
void resetFieldChanged (FieldType ClassType::*field)
 Resets the changed state of the given field.
 
constexpr void setFlags (const uint32_t flags)
 Override all flags of the Component, setting the given flags.
 
constexpr void setFlag (const uint32_t flag)
 Set the given flags. Does not override the currently set flags.
 
constexpr void unsetFlag (const uint32_t flag)
 Unset the given flag. Does not remove the status of other than the given flags.
 
constexpr bool isSet (const uint32_t flag) const
 Checks if the given flag is set. Requires exact bit match if test of several bits.
 
constexpr void setTypeId (const Reflection::TypeId typeId)
 Set the Reflection::TypeId of the component.
 
constexpr Reflection::TypeId getTypeId () const
 Get the Reflection::TypeId of the component.
 
COGSFOUNDATION_API const Reflection::TypegetType () const
 Get the full Reflection::Type of the component.
 
constexpr void setIndex (const ComponentIndex index)
 Set the components pool index. For internal use only.
 
constexpr ComponentIndex getIndex () const
 Get the components pool index. For internal use only.
 
constexpr void setGeneration (uint16_t generation)
 Sets the component generation.
 
constexpr uint16_t getGeneration () const
 Gets the component generation.
 
size_t hash (size_t hashValue=Cogs::hash()) const
 Calculates a hash of this component's members.
 

Static Public Member Functions

static COGSFOUNDATION_API void registerType ()
 Register the Component type in the global type database.
 
static COGSFOUNDATION_API Reflection::TypeId getComponentTypeId (const StringView &name)
 Get the type id of the component type with the given name.
 

Private Member Functions

COGSFOUNDATION_API Reflection::FieldId getFieldIdFromOffset (size_t offset) const
 Returns Field ID for the field with the given offset. Returns NoField if not a registered field.
 

Static Private Member Functions

static constexpr uint32_t leftShift (const uint32_t value, const uint32_t fieldId)
 

Detailed Description

Base class for Component instances.

Contains housekeeping data and a pointer to the owner of the component instance.

Constructor & Destructor Documentation

◆ Component()

Cogs::ComponentModel::Component::Component ( )
default

Member Function Documentation

◆ getComponent() [1/3]

template<typename ComponentType >
ComponentType * Cogs::ComponentModel::Component::getComponent ( ) const
inline

Get a component with the given ComponentType type from the owner of this component. Returns null if no matching instance is found.

References getComponent(), and getComponentTypeId().

Referenced by getComponent().

◆ getComponent() [2/3]

Cogs::ComponentModel::Component * Cogs::ComponentModel::Component::getComponent ( const Reflection::Name name) const

Get a component with the given type name from the owner of this component. Returns null if no matching instance is found.

◆ getComponent() [3/3]

Cogs::ComponentModel::Component * Cogs::ComponentModel::Component::getComponent ( const Reflection::TypeId id) const

Get a component with the given type id from the owner of this component. Returns null if no matching instance is found.

◆ getComponentHandle() [1/3]

template<typename ComponentType >
ComponentHandle Cogs::ComponentModel::Component::getComponentHandle ( ) const
inline

Get a component handle with the given ComponentType type from the owner of this component. Returns null if no matching instance is found.

References getComponentHandle(), and getComponentTypeId().

Referenced by getComponentHandle().

◆ getComponentHandle() [2/3]

Cogs::ComponentModel::ComponentHandle Cogs::ComponentModel::Component::getComponentHandle ( const Reflection::Name name) const

Get a component handle with the given type name from the owner of this component. Returns ComponentHandle::Empty if no matching instance is found.

References Cogs::ComponentModel::ComponentHandle::Empty().

◆ getComponentHandle() [3/3]

Cogs::ComponentModel::ComponentHandle Cogs::ComponentModel::Component::getComponentHandle ( const Reflection::TypeId id) const

Get a component handle with the given type id from the owner of this component. Returns ComponentHandle::Empty if no matching instance is found.

References Cogs::ComponentModel::ComponentHandle::Empty().

◆ getComponentTypeId()

Cogs::Reflection::TypeId Cogs::ComponentModel::Component::getComponentTypeId ( const StringView name)
static

Get the type id of the component type with the given name.

References Cogs::Reflection::TypeDatabase::getType(), and Cogs::Reflection::Type::getTypeId().

Referenced by getComponent(), and getComponentHandle().

◆ getContainer()

class Entity * Cogs::ComponentModel::Component::getContainer ( ) const
inline

Get the container currently owning this component instance.

◆ getFieldId()

template<typename ClassType , typename FieldType >
Reflection::FieldId Cogs::ComponentModel::Component::getFieldId ( FieldType ClassType::*  field) const
inline

Gets field ID of the given field.

Allows caching the ID for later hasFieldChanged checking as hasFieldChanged using hasFieldChanged(&Component<T>::fieldMemberName) requires type DB lookups.

Template Parameters
ClassTypeType of the component class.
FieldTypeType of the field.
Parameters
fieldPointer to member to the field.
Returns
Type Database field ID (NoField if not a registered field).

References getFieldIdFromOffset(), and Cogs::memberOffset().

Referenced by hasFieldChanged(), resetFieldChanged(), and setFieldChanged().

◆ getFieldIdFromOffset()

Cogs::Reflection::FieldId Cogs::ComponentModel::Component::getFieldIdFromOffset ( size_t  offset) const
private

Returns Field ID for the field with the given offset. Returns NoField if not a registered field.

Referenced by getFieldId().

◆ getGeneration()

constexpr uint16_t Cogs::ComponentModel::Component::getGeneration ( ) const
inlineconstexpr

Gets the component generation.

◆ getIndex()

constexpr ComponentIndex Cogs::ComponentModel::Component::getIndex ( ) const
inlineconstexpr

Get the components pool index. For internal use only.

Referenced by Cogs::ComponentModel::ComponentPoolBase::getComponentIndex().

◆ getType()

const Cogs::Reflection::Type & Cogs::ComponentModel::Component::getType ( ) const

Get the full Reflection::Type of the component.

References Cogs::Reflection::TypeDatabase::getType().

◆ getTypeId()

constexpr Reflection::TypeId Cogs::ComponentModel::Component::getTypeId ( ) const
inlineconstexpr

Get the Reflection::TypeId of the component.

◆ hasAnyfieldChanged()

bool Cogs::ComponentModel::Component::hasAnyfieldChanged ( ) const
inline

Gets if any fields have been changed.

References Cogs::ComponentModel::ComponentFlags::AllFieldChanged.

◆ hasChanged()

bool Cogs::ComponentModel::Component::hasChanged ( ) const
inline

Gets if the component is currently set to the ComponentFlags::Changed state. Use hasFieldChanged to check for individual fields.

References Cogs::ComponentModel::ComponentFlags::Changed, and isSet().

◆ hasFieldChanged() [1/2]

bool Cogs::ComponentModel::Component::hasFieldChanged ( const Reflection::FieldId  fieldId) const
inline

Gets if the field with the given id on this component instance has changed.

Note that fieldIds in Overflow range share change flag.

Parameters
fieldIdField id of the field to check for changes.
Returns
A boolean indicating if the given field changed.

References Cogs::ComponentModel::ComponentFlags::FieldChange, isSet(), leftShift(), and Cogs::ComponentModel::ComponentFlags::OverflowFieldChange.

Referenced by hasFieldChanged().

◆ hasFieldChanged() [2/2]

template<typename ClassType , typename FieldType >
bool Cogs::ComponentModel::Component::hasFieldChanged ( FieldType ClassType::*  field) const
inline

Gets if the given field has changed.

Expensive - cache fieldID and use this where possible. It is usually not necessary to manually specify template parameters for this method, as they will be deduced from the method parameter.

Template Parameters
ClassTypeType of the component class.
FieldTypeType of the field to check for changed status.
Parameters
fieldPointer to member to the field to check for changes.
Returns
A boolean indicating if the given field has changed.

References getFieldId(), and hasFieldChanged().

◆ hash()

size_t Cogs::ComponentModel::Component::hash ( size_t  hashValue = Cogs::hash()) const
inline

Calculates a hash of this component's members.

References Cogs::hash().

◆ isActive()

bool Cogs::ComponentModel::Component::isActive ( ) const
inline

Gets if the component is currently set to the ComponentFlags::Active state.

References Cogs::ComponentModel::ComponentFlags::Active, and isSet().

◆ isSet()

constexpr bool Cogs::ComponentModel::Component::isSet ( const uint32_t  flag) const
inlineconstexpr

Checks if the given flag is set. Requires exact bit match if test of several bits.

Referenced by hasChanged(), hasFieldChanged(), isActive(), and resetChanged().

◆ leftShift()

static constexpr uint32_t Cogs::ComponentModel::Component::leftShift ( const uint32_t  value,
const uint32_t  fieldId 
)
inlinestaticconstexprprivate

◆ registerType()

void Cogs::ComponentModel::Component::registerType ( )
static

Register the Component type in the global type database.

◆ resetCarryChanged()

void Cogs::ComponentModel::Component::resetCarryChanged ( )
inline

Reset the CarryChanged flag. Called at start of redraw. See ComponentFlags::CarryChanged.

References Cogs::ComponentModel::ComponentFlags::CarryChanged, and unsetFlag().

◆ resetChanged()

void Cogs::ComponentModel::Component::resetChanged ( )
inline

Resets the changed state of the component, respecting any carry state set.

If the CarryChanged flag is set, the Changed and FieldChange flags will still be present. The CarryChanged flag is not changed. Called at end of redraw loop.

Call resetCarryChanged() first to ensure a full change state reset.

References Cogs::ComponentModel::ComponentFlags::CarryChanged, Cogs::ComponentModel::ComponentFlags::Changed, isSet(), resetFieldsChanged(), and unsetFlag().

◆ resetFieldChanged() [1/2]

void Cogs::ComponentModel::Component::resetFieldChanged ( const Reflection::FieldId  fieldId)
inline

Resets the changed state of the given fieldId.

Note that fieldIds in Overflow range share change flag and cannot be reset.

Parameters
fieldIdField id of the field to reset.

References Cogs::ComponentModel::ComponentFlags::FieldChange, leftShift(), and unsetFlag().

Referenced by resetFieldChanged().

◆ resetFieldChanged() [2/2]

template<typename ClassType , typename FieldType >
void Cogs::ComponentModel::Component::resetFieldChanged ( FieldType ClassType::*  field)
inline

Resets the changed state of the given field.

Expensive - cache fieldID and use this where possible. Note that fieldIds in Overflow range share change flag and cannot be reset.

Template Parameters
ClassTypeType of the component class.
FieldTypeType of the changed field.
Parameters
fieldPointer to member to the field to reset.

References getFieldId(), and resetFieldChanged().

◆ resetFieldsChanged()

void Cogs::ComponentModel::Component::resetFieldsChanged ( )
inline

Resets the state of all changed field flags.

References Cogs::ComponentModel::ComponentFlags::AllFieldChanged, and unsetFlag().

Referenced by resetChanged().

◆ setActive()

void Cogs::ComponentModel::Component::setActive ( )
inline

Sets the component to the ComponentFlags::Active state.

References Cogs::ComponentModel::ComponentFlags::Active, and setFlag().

◆ setChanged()

void Cogs::ComponentModel::Component::setChanged ( )
inline

◆ setChangedTransient()

void Cogs::ComponentModel::Component::setChangedTransient ( )
inline

Sets the component to the ComponentFlags::Changed state without carry.

Also marks all fields changed. This call is typically set to trigger an update for another component but will only have effect if the component is not yet updated.

References Cogs::ComponentModel::ComponentFlags::AllFieldChanged, Cogs::ComponentModel::ComponentFlags::Changed, and setFlag().

◆ setContainer()

void Cogs::ComponentModel::Component::setContainer ( class Entity container)
inline

Set the container owning this component instance.

◆ setFieldChanged() [1/2]

void Cogs::ComponentModel::Component::setFieldChanged ( const Reflection::FieldId  fieldId)
inline

Sets the component to the ComponentFlags::Changed state without carry.

Also sets a flag indicating that the field with the given field id has changed. The first N (24) fields are tracked individually. The remaining tracked by same flag.

Parameters
fieldIdField id of the changed field.

References Cogs::ComponentModel::ComponentFlags::CarryChanged, Cogs::ComponentModel::ComponentFlags::Changed, Cogs::ComponentModel::ComponentFlags::FieldChange, leftShift(), Cogs::ComponentModel::ComponentFlags::OverflowFieldChange, and setFlag().

Referenced by setFieldChanged().

◆ setFieldChanged() [2/2]

template<typename ClassType , typename FieldType >
void Cogs::ComponentModel::Component::setFieldChanged ( FieldType ClassType::*  field)
inline

Sets a flag indicating that the given field has changed.

Expensive - cache fieldID and use this where possible. It is usually not necessary to manually specify template parameters for this method, as they will be deduced from the method parameter.

Template Parameters
ClassTypeType of the component class.
FieldTypeType of the changed field.
Parameters
fieldPointer to member to the field that changed.

References getFieldId(), and setFieldChanged().

◆ setFieldChangedTransient()

void Cogs::ComponentModel::Component::setFieldChangedTransient ( const Reflection::FieldId  fieldId)
inline

Sets the component to the ComponentFlags::Changed state without carry.

Also sets a flag indicating that the field with the given field id has changed. The first N (24) fields are tracked individually. The remaining tracked by same flag. This call is typically set to trigger an update for another component but will only have effect if the component is not yet updated.

Parameters
fieldIdField id of the changed field.

References Cogs::ComponentModel::ComponentFlags::Changed, Cogs::ComponentModel::ComponentFlags::FieldChange, leftShift(), Cogs::ComponentModel::ComponentFlags::OverflowFieldChange, and setFlag().

◆ setFlag()

constexpr void Cogs::ComponentModel::Component::setFlag ( const uint32_t  flag)
inlineconstexpr

Set the given flags. Does not override the currently set flags.

Referenced by setActive(), setChanged(), setChangedTransient(), setFieldChanged(), and setFieldChangedTransient().

◆ setFlags()

constexpr void Cogs::ComponentModel::Component::setFlags ( const uint32_t  flags)
inlineconstexpr

Override all flags of the Component, setting the given flags.

◆ setGeneration()

constexpr void Cogs::ComponentModel::Component::setGeneration ( uint16_t  generation)
inlineconstexpr

Sets the component generation.

◆ setIndex()

constexpr void Cogs::ComponentModel::Component::setIndex ( const ComponentIndex  index)
inlineconstexpr

Set the components pool index. For internal use only.

◆ setTypeId()

constexpr void Cogs::ComponentModel::Component::setTypeId ( const Reflection::TypeId  typeId)
inlineconstexpr

Set the Reflection::TypeId of the component.

◆ unsetFlag()

constexpr void Cogs::ComponentModel::Component::unsetFlag ( const uint32_t  flag)
inlineconstexpr

Unset the given flag. Does not remove the status of other than the given flags.

Referenced by resetCarryChanged(), resetChanged(), resetFieldChanged(), and resetFieldsChanged().


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