33 static_assert(
sizeof(
T) <= 4,
"64bit enums not supported.");
40 constexpr int getValue()
const {
return value; }
61 Enumerator(
const Name& name, T value) : name(name), value(static_cast<int>(value))
64 static_assert(
sizeof(
T) <= 4,
"64bit enums not supported.");
109 template<
size_t count>
115 template<
size_t count>
121 Type & setMethods(
Method * functions,
size_t count);
126 template<
size_t count>
129 return setEnumerators(enumerators, count);
146 template<
typename BaseType>
150 [[nodiscard]]
const Type * getBase()
const;
159 template<
typename DestinationType>
160 [[nodiscard]]
bool canCastTo()
const;
171 return canCastTo(destination.
getTypeId());
184 [[nodiscard]]
bool canCastTo(
const TypeId& destinationId)
const;
194 return this->typeId == other.
typeId;
198 [[nodiscard]]
constexpr const Name &
getName()
const {
return name; }
201 [[nodiscard]]
constexpr size_t getSize()
const {
return size; }
208 [[nodiscard]]
const Field * getField(
const Name & name)
const;
215 [[nodiscard]]
FieldId getFieldId(
const Field * field)
const;
222 [[nodiscard]]
static FieldId getFieldId(
TypeId componentTypeId,
const Name& name);
239 [[nodiscard]]
size_t getNumHierarchyFields()
const;
246 [[nodiscard]]
const Field * getField(
const FieldId id)
const;
255 [[nodiscard]]
FieldId getFieldId(
size_t offset)
const;
264 template<
typename ClassType,
typename FieldType>
269 return getFieldId(offset);
277 [[nodiscard]]
const Method * getMethod(
const Name & name)
const;
286 return static_cast<MethodId>(method - methods.data());
300 [[nodiscard]]
const Enumerator * getEnumerator(
const Name & name)
const;
309 if (index >= enumerators.size())
return nullptr;
311 return &enumerators[index];
315 [[nodiscard]]
bool isEnum()
const {
return !enumerators.empty(); }
331 constexpr void setFlags(uint32_t flags) { this->flags = flags; }
334 [[nodiscard]]
bool isEnumFlags()
const {
return isEnum() && (flags & TypeFlags::EnumFlags) != 0; }
340 Type& setFields(
Field* fields,
size_t count);
350 uint32_t flags = TypeFlags::None;
387 template<
size_t count>
390 return setFields(fields, count);
393 template<
size_t count>
396 return setMethods(functions, count);
405 struct hash<
Cogs::Reflection::Type>
410 return type.getName().getId();
#define COGSFOUNDATION_API
Definition: FoundationBase.h:31
Field definition describing a single data member of a data structure.
Definition: Field.h:68
Simple method definition.
Definition: Method.h:72
Manages all Type instances currently created in the system.
Definition: TypeDatabase.h:105
Represents a discrete type definition, describing a native type class.
Definition: Type.h:89
Type & operator=(const Type &other)=delete
Disallow copy assignment.
size_t getNumFields() const
Get the number of fields in the type.
Definition: Type.h:230
std::vector< Enumerator > enumerators
Collection of enumerators if the type is an enum type.
Definition: Type.h:365
bool isEnum() const
Get if the type is an enumeration type.
Definition: Type.h:315
constexpr const Name & getName() const
Get the unique name of the type.
Definition: Type.h:198
bool isEnumFlags() const
Get if the type is a flag enumeration type.
Definition: Type.h:334
std::vector< Method > methods
Collection of methods available on the type.
Definition: Type.h:362
Name name
Unique name of the type.
Definition: Type.h:344
Type & setBase()
Set the base of this type to the given type.
Type(const Type &other)=delete
Copying type instances is disallowed.
bool canCastTo(const Type &destination) const
Check if this type can be cast to the destination type.
Definition: Type.h:169
size_t getNumEnumerators() const
Get the number of enumerators in the type.
Definition: Type.h:322
TypeId typeId
Unique type id for this type.
Definition: Type.h:353
StringView description
Optional description of the type.
Definition: Type.h:381
Type()=default
Default constructor.
Type & operator=(Type &&other) noexcept=delete
Default move assignment.
Type(Type &&other) noexcept=default
Default move constructor.
constexpr bool isValid() const
Gets if the type is considered a valid, registered type.
Definition: Type.h:328
const Method * getMethod(const MethodId id) const
Get a pointer to the method with the given id.
Definition: Type.h:290
constexpr void setFlags(uint32_t flags)
Set type flags.
Definition: Type.h:331
constexpr bool operator==(const Type &other) const
Check if this type is equal to other.
Definition: Type.h:192
FieldId getFieldId(FieldType ClassType::*field) const
Get the id of the field given by pointer to member.
Definition: Type.h:265
Type & setEnumerators(const EnumeratorDef(&enumerators)[count])
Set the enumerators of the type to the given array of enumerator instances.
Definition: Type.h:127
constexpr Type & setEnumFlags()
Indicate the type is a flag enum.
Definition: Type.h:337
Type & setMethods(Method(&functions)[count])
Set the methods of the type to the given array of method instances.
Definition: Type.h:394
constexpr TypeId getTypeId() const
Get the unique Reflection::TypeId of this instance.
Definition: Type.h:325
const Enumerator * getEnumerator(const size_t index) const
Get the enumerator with the given index.
Definition: Type.h:307
Type & setFields(Field(&fields)[count])
Set the fields of the type to the given array of field instances.
Definition: Type.h:388
Type & setBase(const Type *base)
Set the base type of this base. If base is nullptr, there will be no base type.
Definition: Type.h:138
MethodId getMethodId(const Method *method) const
Get the Reflection::MethodId of the given method.
Definition: Type.h:284
constexpr size_t getSize() const
Get the size of an instance of the reflected type, in bytes.
Definition: Type.h:201
std::vector< Field > fields
Collection of fields available on the type.
Definition: Type.h:359
Provides a weakly referenced view over the contents of a string.
Definition: StringView.h:24
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
Definition: Name.h:48
void(*)(void *) DestroyInstance
Object destroy function type. Comparable to regular delete.
Definition: Name.h:42
void *(*)(void *) ConstructInstance
Object construction function type. Comparable to placement new.
Definition: Name.h:39
uint16_t FieldId
Type used to index fields.
Definition: Name.h:54
constexpr TypeId NoType
Definition of no type.
Definition: Name.h:51
void *(*)() CreateInstance
Object creation function type. Comparable to regular new.
Definition: Name.h:36
void(*)(void *) DestructInstance
Object destruction function type. Comparable to calling the destructor.
Definition: Name.h:45
uint16_t MethodId
Type used to index methods.
Definition: Name.h:57
Main Cogs namespace.
Definition: MortonCode.h:5
size_t memberOffset(FieldType ClassType::*ptr)
Find the offset of a pointer to member in a class or struct.
Definition: FoundationBase.h:67
constexpr EnumeratorDef(Cogs::StringView name, T value)
Construct a new enumerator with the given name and value.
Definition: Type.h:30
constexpr Cogs::StringView getName() const
Get the name of the enumerator.
Definition: Type.h:37
constexpr int getValue() const
Get the value of the enumerator.
Definition: Type.h:40
Single integral constant enumerator.
Definition: Type.h:58
Enumerator(const Name &name, T value)
Construct a new enumerator with the given name and value.
Definition: Type.h:61
const Name & getName() const
Get the name of the enumerator.
Definition: Type.h:68
int getValue() const
Get the value of the enumerator.
Definition: Type.h:71
Represents an unique name.
Definition: Name.h:70
Type flags controlling type behavior.
Definition: Type.h:13
ETypeFlags
Type flag enumerators.
Definition: Type.h:16
@ EnumFlags
Enum type is bit flag type.
Definition: Type.h:20
@ None
No flags.
Definition: Type.h:18
size_t operator()(const Cogs::Reflection::Type &type) const noexcept
Function call operator for hash object.
Definition: Type.h:408