3#include "../Reflection/Name.h"
22 namespace ComponentModel
79 : pool(pool), index(index), typeId(typeId), generation(generation)
89 template<
typename ComponentType>
92 return static_cast<ComponentType *
>(
resolve());
116 explicit operator bool()
const {
return resolve() !=
nullptr; }
128 uint16_t generation = 0;
135 return a.pool == b.pool && a.index == b.index && a.typeId == b.typeId;
158 template<
typename ComponentType>
176 template<
typename ComponentType>
236 template<
typename ClassType,
typename FieldType>
289 template<
typename ClassType,
typename FieldType>
327 template<
typename ClassType,
typename FieldType>
358 template<
typename ClassType,
typename FieldType>
365 constexpr void setFlags(
const uint32_t flags) { this->flags = flags; }
368 constexpr void setFlag(
const uint32_t flag) { this->flags |= flag; }
371 constexpr void unsetFlag(
const uint32_t flag) { this->flags &= ~flag; }
374 [[nodiscard]]
constexpr bool isSet(
const uint32_t flag)
const {
return (flags & flag) == flag; }
396 this->generation = generation;
406 hashValue =
Cogs::hash(
reinterpret_cast<intptr_t
>(container), hashValue);
410 hashValue =
Cogs::hash(generation, hashValue);
417 [[nodiscard]]
static constexpr uint32_t leftShift(
const uint32_t value,
const uint32_t fieldId)
419 return fieldId >= 32 ? 0u : (value << fieldId);
426 class Entity * container =
nullptr;
430 uint16_t generation = 0;
436template<>
inline Cogs::StringView getName<Cogs::ComponentModel::Component>() {
return "Component"; }
Untyped Component pool base.
Base class for Component instances.
static COGSFOUNDATION_API void registerType()
Register the Component type in the global type database.
void setChanged()
Sets the component to the ComponentFlags::Changed state with carry.
constexpr uint16_t getGeneration() const
Gets the component generation.
Reflection::FieldId getFieldId(FieldType ClassType::*field) const
Gets field ID of the given field.
constexpr void setIndex(const ComponentIndex index)
Set the components pool index. For internal use only.
void setActive()
Sets the component to the ComponentFlags::Active state.
constexpr void unsetFlag(const uint32_t flag)
Unset the given flag. Does not remove the status of other than the given flags.
void setFieldChanged(const Reflection::FieldId fieldId)
Sets the component to the ComponentFlags::Changed state without carry.
void setFieldChangedTransient(const Reflection::FieldId fieldId)
Sets the component to the ComponentFlags::Changed state without carry.
constexpr ComponentIndex getIndex() const
Get the components pool index. For internal use only.
ComponentType * getComponent() const
bool isActive() const
Gets if the component is currently set to the ComponentFlags::Active state.
static COGSFOUNDATION_API Reflection::TypeId getComponentTypeId(const StringView &name)
Get the type id of the component type with the given name.
bool hasAnyfieldChanged() const
Gets if any fields have been changed.
constexpr void setFlags(const uint32_t flags)
Override all flags of the Component, setting the given flags.
void setChangedTransient()
Sets the component to the ComponentFlags::Changed state without carry.
constexpr bool isSet(const uint32_t flag) const
Checks if the given flag is set. Requires exact bit match if test of several bits.
void resetFieldsChanged()
Resets the state of all changed field flags.
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.
class Entity * getContainer() const
Get the container currently owning this component instance.
constexpr void setTypeId(const Reflection::TypeId typeId)
Set the Reflection::TypeId of the component.
void resetFieldChanged(const Reflection::FieldId fieldId)
Resets the changed state of the given fieldId.
void resetFieldChanged(FieldType ClassType::*field)
Resets the changed state of the given field.
ComponentHandle getComponentHandle() const
constexpr Reflection::TypeId getTypeId() const
Get the Reflection::TypeId of the component.
constexpr void setGeneration(uint16_t generation)
Sets the component generation.
constexpr void setFlag(const uint32_t flag)
Set the given flags. Does not override the currently set flags.
void setContainer(class Entity *container)
Set the container owning this component instance.
bool hasFieldChanged(FieldType ClassType::*field) const
Gets if the given field has changed.
void setFieldChanged(FieldType ClassType::*field)
Sets a flag indicating that the given field has changed.
size_t hash(size_t hashValue=Cogs::hash()) const
Calculates a hash of this component's members.
bool hasFieldChanged(const Reflection::FieldId fieldId) const
Gets if the field with the given id on this component instance has changed.
void resetCarryChanged()
Reset the CarryChanged flag. Called at start of redraw. See ComponentFlags::CarryChanged.
COGSFOUNDATION_API const Reflection::Type & getType() const
Get the full Reflection::Type of the component.
void resetChanged()
Resets the changed state of the component, respecting any carry state set.
Container for components, providing composition of dynamic entities.
Represents a discrete type definition, describing a native type class.
Provides a weakly referenced view over the contents of a string.
constexpr bool operator==(const ComponentHandle &a, const ComponentHandle &b)
Overloaded equality operator.
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
uint16_t FieldId
Type used to index fields.
constexpr TypeId NoType
Definition of no type.
Contains all Cogs related functionality.
constexpr size_t hash() noexcept
Simple getter function that returns the initial value for fnv1a hashing.
uint32_t ComponentIndex
Type used to track component indexes in pools.
ComponentIndex SizeType
Type used to track the size of pools.
size_t memberOffset(FieldType ClassType::*ptr)
Find the offset of a pointer to member in a class or struct.
Contains component flags.
EComponentFlags
Component flags describing the state of a component instance.
@ Changed
The components data has changed.
@ AllFieldChanged
Mark all fields changed. Sets FieldChange + all bits above + OverflowFieldChange.
@ None
The component has not been initialized.
@ DisableTracking
No change tracking should be performed on the component.
@ FieldChange
Flags above reserved for field change storage. Field0=FieldChange, Field 1 = FieldChange<<1.
@ Active
The component has been initialized and is currently allocated.
@ OverflowFieldChange
Field change bit for fields outside range of individual change tracking bit. Field 24++.
Handle to a Component instance.
COGSFOUNDATION_API class Component * resolve() const
Resolve the handle, returning a pointer to the held Component instance.
ComponentHandle(const ComponentHandle &other)=default
Defaulted copy constructor.
ComponentHandle & operator=(const ComponentHandle &rhs)=default
Defaulted assign operator.
ComponentHandle()=default
Defaulted constructor.
static ComponentHandle Empty()
Returns an empty, invalid handle. Will evaluate to false if tested against using operator bool().
constexpr ComponentHandle(class ComponentPoolBase *pool, ComponentIndex index, Reflection::TypeId typeId, uint16_t generation)
Explicit full initialization.
ComponentType * resolveComponent() const
Represents an unique name.