RedCore
Loading...
Searching...
No Matches
ClassExtension.h
Go to the documentation of this file.
1#pragma once
2
3#include <container/seadTList.h>
4#include <prim/seadRuntimeTypeInfo.h>
5#include <basis/seadNew.h>
6#include <math/seadMathCalcCommon.h>
7#include <type_traits>
8
9namespace red {
10
11 class ClassExtension : public sead::TListNode<ClassExtension*> { // <-- this just builds a memory layout for the extension
12 public:
13 ClassExtension(const sead::RuntimeTypeInfo::Interface* typeInfo)
15 , mSize(0)
18 { }
19
20 template <typename T>
21 [[nodiscard]]
22 u32 add() {
23 static_assert(std::is_trivially_constructible_v<T>, "T must not have a constructor");
24 static_assert(std::is_trivially_destructible_v<T>, "T must not have a destructor");
25
26 u32 alignment = alignof(T);
27
28 mAlignment = sead::Mathu::max(mAlignment, alignment);
29
30 // round up to alignment
31 mSize = (mSize + alignment - 1) & ~(alignment - 1); // sead::Mathu::roundUp()
32
33 u32 start = mSize;
34 mSize += sizeof(T);
35 return start;
36 }
37
38 void finish() {
39 // round up to alignment
40 mSize = (mSize + mAlignment - 1) & ~(mAlignment - 1);
41 }
42
43 [[nodiscard]]
44 u32 getSize() const {
45 return mSize;
46 }
47
48 [[nodiscard]]
49 u32 getAlignment() const {
50 return mAlignment;
51 }
52
53 [[nodiscard]]
55 return mTypeInfo;
56 }
57
58 private:
59 u32 mSize;
60 u32 mAlignment;
61 const sead::RuntimeTypeInfo::Interface* mTypeInfo;
62 };
63
65 public:
66 ClassExtensionInstance(u32 size, u8* data, const sead::RuntimeTypeInfo::Interface* typeInfo)
68 , mSize(size)
69 , mData(data)
71 { }
72
73 template <typename T>
74 [[nodiscard]]
75 T* get(u32 offset) {
76 if (offset + sizeof(T) > mSize) [[unlikely]] {
77 // tk::fatal("bad");
78 return nullptr;
79 }
80
81 return reinterpret_cast<T*>(mData + offset);
82 }
83
84 [[nodiscard]]
86 return mTypeInfo;
87 }
88
89 private:
90 u32 mSize;
91 u8* mData;
92 const sead::RuntimeTypeInfo::Interface* mTypeInfo;
93 };
94}
Definition ClassExtension.h:64
ClassExtensionInstance(u32 size, u8 *data, const sead::RuntimeTypeInfo::Interface *typeInfo)
Definition ClassExtension.h:66
T * get(u32 offset)
Definition ClassExtension.h:75
const sead::RuntimeTypeInfo::Interface * getTypeInfo() const
Definition ClassExtension.h:85
Definition ClassExtension.h:11
const sead::RuntimeTypeInfo::Interface * getTypeInfo() const
Definition ClassExtension.h:54
void finish()
Definition ClassExtension.h:38
u32 getSize() const
Definition ClassExtension.h:44
u32 getAlignment() const
Definition ClassExtension.h:49
u32 add()
Definition ClassExtension.h:22
ClassExtension(const sead::RuntimeTypeInfo::Interface *typeInfo)
Definition ClassExtension.h:13
Definition ActorDonutBlock.h:5