Cogs.Core
Component.cpp
1#include "Component.h"
2#include "ComponentPool.h"
3#include "Entity.h"
4
5#include "../Reflection/TypeDatabase.h"
6
7#include "../Logging/Logger.h"
8
9namespace {
10 const Cogs::Logging::Log logger = Cogs::Logging::getLogger("ComponentModel.Component");
11 size_t generationMismatchCount = 0;
12}
13
14// Need these values see implementation of resetChanged().
16
18{
19 Reflection::TypeDatabase::createAbstractType<Component>();
20}
21
23{
24 return getType().getFieldId(offset);
25}
26
28{
30}
31
33{
35}
36
38{
39 if (!container) return nullptr;
40
41 return container->getComponent(name);
42}
43
45{
46 if (!container) return nullptr;
47
48 return container->getComponent(id);
49}
50
52{
53 if (!container) return ComponentHandle::Empty();
54
55 return container->getComponentHandle(name);
56}
57
59{
60 if (!container) return ComponentHandle::Empty();
61
62 return container->getComponentHandle(id);
63}
64
66{
67 if (pool) {
68 auto * comp = pool->resolve(*this);
69 if (comp->getGeneration() == generation) {
70 return comp;
71 }
72 else {
73 // Observed when using dangling component handles (Pools not freeing memory).
74 if (++generationMismatchCount <= 10) {
75 LOG_WARNING(logger, "Component generation mismatch: %u, Expected: %u", comp->getGeneration(), generation);
76 }
77 return nullptr;
78 }
79 }
80
81 return nullptr;
82}
Base class for Component instances.
Definition: Component.h:143
static COGSFOUNDATION_API void registerType()
Register the Component type in the global type database.
Definition: Component.cpp:17
ComponentType * getComponent() const
Definition: Component.h:159
static COGSFOUNDATION_API Reflection::TypeId getComponentTypeId(const StringView &name)
Get the type id of the component type with the given name.
Definition: Component.cpp:32
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.
Definition: Component.cpp:22
ComponentHandle getComponentHandle() const
Definition: Component.h:177
COGSFOUNDATION_API const Reflection::Type & getType() const
Get the full Reflection::Type of the component.
Definition: Component.cpp:27
Log implementation class.
Definition: LogManager.h:139
static const Type & getType()
Get the Type of the given template argument.
Definition: TypeDatabase.h:168
Represents a discrete type definition, describing a native type class.
Definition: Type.h:89
constexpr TypeId getTypeId() const
Get the unique Reflection::TypeId of this instance.
Definition: Type.h:325
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
constexpr Log getLogger(const char(&name)[LEN]) noexcept
Definition: LogManager.h:180
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
Definition: Name.h:48
uint16_t FieldId
Type used to index fields.
Definition: Name.h:54
@ Changed
The components data has changed.
Definition: Component.h:38
Handle to a Component instance.
Definition: Component.h:67
COGSFOUNDATION_API class Component * resolve() const
Resolve the handle, returning a pointer to the held Component instance.
Definition: Component.cpp:65
static ComponentHandle Empty()
Returns an empty, invalid handle. Will evaluate to false if tested against using operator bool().
Definition: Component.h:119
Represents an unique name.
Definition: Name.h:70