Skip to content

Commit de28767

Browse files
ideas
1 parent ea3bc89 commit de28767

File tree

2 files changed

+382
-57
lines changed

2 files changed

+382
-57
lines changed

include/nbl/core/StorageTrivializer.h

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
#ifndef __NBL_CORE_STORAGE_TRIVIALIZER_H_INCLUDED__
2+
#define __NBL_CORE_STORAGE_TRIVIALIZER_H_INCLUDED__
3+
4+
namespace nbl::core
5+
{
6+
7+
// This construct makes it so that we don't trigger T's constructors and destructors.
8+
template<typename T>
9+
struct StorageTrivializer;
10+
11+
template<>
12+
struct NBL_FORCE_EBO StorageTrivializer<void>
13+
{
14+
void* getStorage() {return nullptr;}
15+
const void* getStorage() const {return nullptr;}
16+
17+
void construct() {}
18+
void destruct() {}
19+
};
20+
21+
template<typename T>
22+
struct alignas(T) StorageTrivializer
23+
{
24+
T* getStorage() {return reinterpret_cast<T*>(storage); }
25+
const T* getStorage() const {return reinterpret_cast<const T*>(storage);}
26+
27+
void construct(Args&&... args)
28+
{
29+
new (getStorage()) T(std::forward<Args>(args)...);
30+
}
31+
void destruct()
32+
{
33+
getStorage->~T();
34+
}
35+
36+
uint8_t* storage[sizeof(T)];
37+
};
38+
39+
}
40+
41+
#endif

0 commit comments

Comments
 (0)