Nebula
Loading...
Searching...
No Matches
materialvariant.h
Go to the documentation of this file.
1#pragma once
2//------------------------------------------------------------------------------
9//------------------------------------------------------------------------------
10#include "coregraphics/config.h"
11namespace Materials
12{
13
15{
16 struct TextureHandleTuple;
17
20 : mem(nullptr)
21 , needsDeref(false)
22 , size(0)
23 {};
24
27 : mem(rhs.mem)
29 , size(rhs.size)
30 {
31 }
32
34 void operator=(const MaterialVariant& rhs)
35 {
36 this->mem = rhs.mem;
37 this->needsDeref = rhs.needsDeref;
38 this->size = rhs.size;
39 }
40
43 : mem(rhs.mem)
45 , size(rhs.size)
46 {
47 rhs.mem = nullptr;
48 rhs.needsDeref = false;
49 rhs.size = 0;
50 }
51
54 {
55 this->mem = rhs.mem;
56 this->needsDeref = rhs.needsDeref;
57 this->size = rhs.size;
58
59 rhs.mem = nullptr;
60 rhs.needsDeref = false;
61 rhs.size = 0;
62 }
63
65 MaterialVariant(std::nullptr_t)
66 : mem(nullptr)
67 , needsDeref(false)
68 , size(0)
69 {}
70
72 void* mem;
74
76 const void* Get() const
77 {
78 // If the data is stored in a pointer, and not the pointer value it self, we need to deref the pointer
79 return this->needsDeref ? this->mem : reinterpret_cast<const void*>(&this->mem);
80 }
81
83 template <typename T> const T& ConstGet() const
84 {
85 return this->needsDeref ? *reinterpret_cast<T*>(this->mem) : reinterpret_cast<const T&>(this->mem);
86 }
87
89 template <typename T> T& Get()
90 {
91 return this->needsDeref ? *reinterpret_cast<T*>(this->mem) : reinterpret_cast<T&>(this->mem);
92 }
93
95 template <typename T> void Set(const T& data, void* mem)
96 {
97 this->size = sizeof(T);
98 this->mem = mem;
99 this->needsDeref = this->size > sizeof(void*);
100 memcpy(this->needsDeref ? this->mem : reinterpret_cast<void*>(&this->mem), &data, this->size);
101 }
102
104 template <typename T> void Set(void* mem)
105 {
106 this->size = sizeof(T);
107 this->mem = mem;
108 this->needsDeref = true;
109 }
110};
111
112} // namespace Materials
Material special version of variant.
Definition material.cc:11
Compile time configuration options for the CoreGraphics subsystem.
Definition materialvariant.h:15
SizeT size
Definition materialvariant.h:73
void Set(const T &data, void *mem)
Set.
Definition materialvariant.h:95
const T & ConstGet() const
Const get.
Definition materialvariant.h:83
MaterialVariant()
Nullptr constructor.
Definition materialvariant.h:19
MaterialVariant(const MaterialVariant &rhs)
Copy constructor.
Definition materialvariant.h:26
void * mem
Definition materialvariant.h:72
void operator=(MaterialVariant &&rhs)
Move operator.
Definition materialvariant.h:53
MaterialVariant(MaterialVariant &&rhs)
Move constructor.
Definition materialvariant.h:42
bool needsDeref
Definition materialvariant.h:71
T & Get()
Mutable get.
Definition materialvariant.h:89
void operator=(const MaterialVariant &rhs)
Assign operator.
Definition materialvariant.h:34
const void * Get() const
Get pointer.
Definition materialvariant.h:76
void Set(void *mem)
Set from raw memory.
Definition materialvariant.h:104
MaterialVariant(std::nullptr_t)
Nullptr constructor.
Definition materialvariant.h:65
int SizeT
Definition types.h:49