Cogs.Core
Attributes.h
1#pragma once
2
3#include "Name.h"
4
5#include <vector>
6
7namespace Cogs
8{
9 namespace Reflection
10 {
11 class Field;
12 class Type;
13
24 struct Attribute
25 {
26 public:
28 TypeId getType() const { return type; }
29
32 TypeId getGenericType() const { return genericType; }
33
35 bool isRuntime() const { return runtime; }
36
37 protected:
43 Attribute(TypeId type) : Attribute(type, NoType) {}
44
52 Attribute(TypeId type, TypeId genericType, bool runtime = true)
53 : type(type), genericType(genericType), runtime(runtime)
54 {
55 std::memset(storage, 0, sizeof(storage));
56 }
57
70 template<typename T>
71 T & get(size_t index = 0)
72 {
73 return reinterpret_cast<T *>(storage)[index];
74 }
75
79 template<typename T>
80 const T & get(size_t index = 0) const
81 {
82 return reinterpret_cast<const T *>(storage)[index];
83 }
84
86 TypeId type = NoType;
87 TypeId genericType = NoType;
88 private:
89 bool runtime;
90 uint8_t storage[256 - (sizeof(TypeId) * 2) - sizeof(bool)];
92 };
93
102 template<typename T>
104 {
106 RegularAttribute(bool runtime = true);
107 };
108
117 template<template<typename> class T, typename U>
119 {
121 GenericAttribute(bool runtime = true);
122 };
123
129 struct COGSFOUNDATION_API Attributes
130 {
138 template<typename T>
139 void add(T & attribute)
140 {
141 attributes.push_back(attribute);
142 }
143
154 template<typename T>
155 Field & add(Field & field, T & attribute)
156 {
157 attributes.push_back(attribute);
158
159 return field;
160 }
161
171 template<typename T>
172 const T * get() const
173 {
174 return static_cast<const T *>(getAttribute(static_cast<T *>(nullptr)));
175 }
176
177 private:
179 template<typename T>
180 const Attribute * getAttribute(RegularAttribute<T> * a) const;
181
182 template<template<typename> class T, typename U>
183 const Attribute * getAttribute(GenericAttribute<T, U> * a) const;
184
185 const Attribute * getAttributeByTypeId(TypeId typeId, TypeId genericTypeId = NoType) const;
186
187 std::vector<Attribute> attributes;
189 };
190 }
191}
Field definition describing a single data member of a data structure.
Definition: Field.h:68
uint16_t TypeId
Built in type used to uniquely identify a single type instance.
Definition: Name.h:48
constexpr TypeId NoType
Definition of no type.
Definition: Name.h:51
Contains all Cogs related functionality.
Definition: FieldSetter.h:23
An attribute containing metadata or describing behavior of a structure.
Definition: Attributes.h:25
Attribute(TypeId type)
Constructs an attribute instance with the given type.
Definition: Attributes.h:43
const T & get(size_t index=0) const
See non-const get() implementation.
Definition: Attributes.h:80
TypeId getGenericType() const
Definition: Attributes.h:32
bool isRuntime() const
Gets value of the runtime attribute flag.
Definition: Attributes.h:35
T & get(size_t index=0)
Get the stored value of the given type, with an optional index.
Definition: Attributes.h:71
TypeId getType() const
Get the type of the derived attribute class.
Definition: Attributes.h:28
Attribute(TypeId type, TypeId genericType, bool runtime=true)
Constructs a generic attribute instance with the given type and generic type parameter.
Definition: Attributes.h:52
Attribute storage for structures.
Definition: Attributes.h:130
Field & add(Field &field, T &attribute)
Add the given attribute to the attribute storage, with the given field as continuation parameter.
Definition: Attributes.h:155
const T * get() const
Retrieve an attribute of the given type from storage, if present.
Definition: Attributes.h:172
void add(T &attribute)
Add the given attribute to the attribute storage.
Definition: Attributes.h:139
Base attribute type for generic attributes.
Definition: Attributes.h:119
GenericAttribute(bool runtime=true)
Construct a generic attribute. True if runtime attribute.
Base attribute type for regular attributes.
Definition: Attributes.h:104
RegularAttribute(bool runtime=true)
Construct a regular attribute. True if runtime attribute.