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.");
88 class COGSFOUNDATION_API
Type
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);
140 this->base = base ? base->typeId :
NoType;
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();
416#include "Attributes.inl"
Field definition describing a single data member of a data structure.
Simple method definition.
Manages all Type instances currently created in the system.
Represents a discrete type definition, describing a native type class.
Type & operator=(const Type &other)=delete
Disallow copy assignment.
size_t getNumFields() const
Get the number of fields in the type.
std::vector< Enumerator > enumerators
Collection of enumerators if the type is an enum type.
bool isEnum() const
Get if the type is an enumeration type.
constexpr const Name & getName() const
Get the unique name of the type.
bool isEnumFlags() const
Get if the type is a flag enumeration type.
std::vector< Method > methods
Collection of methods available on the type.
Name name
Unique name of the type.
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.
size_t getNumEnumerators() const
Get the number of enumerators in the type.
TypeId typeId
Unique type id for this type.
StringView description
Optional description of the type.
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.
const Method * getMethod(const MethodId id) const
Get a pointer to the method with the given id.
constexpr void setFlags(uint32_t flags)
Set type flags.
constexpr bool operator==(const Type &other) const
Check if this type is equal to other.
FieldId getFieldId(FieldType ClassType::*field) const
Get the id of the field given by pointer to member.
Type & setEnumerators(const EnumeratorDef(&enumerators)[count])
Set the enumerators of the type to the given array of enumerator instances.
constexpr Type & setEnumFlags()
Indicate the type is a flag enum.
Type & setMethods(Method(&functions)[count])
Set the methods of the type to the given array of method instances.
constexpr TypeId getTypeId() const
Get the unique Reflection::TypeId of this instance.
const Enumerator * getEnumerator(const size_t index) const
Get the enumerator with the given index.
Type & setFields(Field(&fields)[count])
Set the fields of the type to the given array of field instances.
Type & setBase(const Type *base)
Set the base type of this base. If base is nullptr, there will be no base type.
MethodId getMethodId(const Method *method) const
Get the Reflection::MethodId of the given method.
constexpr size_t getSize() const
Get the size of an instance of the reflected type, in bytes.
std::vector< Field > fields
Collection of fields available on the type.
Provides a weakly referenced view over the contents of a string.
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
void(*)(void *) DestroyInstance
Object destroy function type. Comparable to regular delete.
void *(*)(void *) ConstructInstance
Object construction function type. Comparable to placement new.
uint16_t FieldId
Type used to index fields.
constexpr TypeId NoType
Definition of no type.
void *(*)() CreateInstance
Object creation function type. Comparable to regular new.
void(*)(void *) DestructInstance
Object destruction function type. Comparable to calling the destructor.
uint16_t MethodId
Type used to index methods.
Contains all Cogs related functionality.
size_t memberOffset(FieldType ClassType::*ptr)
Find the offset of a pointer to member in a class or struct.
constexpr EnumeratorDef(Cogs::StringView name, T value)
Construct a new enumerator with the given name and value.
constexpr Cogs::StringView getName() const
Get the name of the enumerator.
constexpr int getValue() const
Get the value of the enumerator.
Single integral constant enumerator.
Enumerator(const Name &name, T value)
Construct a new enumerator with the given name and value.
const Name & getName() const
Get the name of the enumerator.
int getValue() const
Get the value of the enumerator.
Represents an unique name.
Type flags controlling type behavior.
ETypeFlags
Type flag enumerators.
@ EnumFlags
Enum type is bit flag type.
size_t operator()(const Cogs::Reflection::Type &type) const noexcept
Function call operator for hash object.