mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-04-21 20:15:17 +00:00
AK: Add NeverDestroyed<T>, for things that should never be destroyed
This template allows you to define static globals without having them destroyed on exit.
This commit is contained in:
parent
b830639912
commit
f607cab235
Notes:
sideshowbarker
2024-07-19 10:38:56 +09:00
Author: https://github.com/awesomekling Commit: https://github.com/SerenityOS/serenity/commit/f607cab2358
1 changed files with 36 additions and 0 deletions
36
AK/NeverDestroyed.h
Normal file
36
AK/NeverDestroyed.h
Normal file
|
@ -0,0 +1,36 @@
|
|||
#pragma once
|
||||
|
||||
#include <AK/Noncopyable.h>
|
||||
#include <AK/StdLibExtras.h>
|
||||
|
||||
namespace AK {
|
||||
|
||||
template<typename T>
|
||||
class NeverDestroyed {
|
||||
AK_MAKE_NONCOPYABLE(NeverDestroyed)
|
||||
AK_MAKE_NONMOVABLE(NeverDestroyed)
|
||||
public:
|
||||
template<typename... Args>
|
||||
NeverDestroyed(Args... args)
|
||||
{
|
||||
new (storage) T(forward<Args>(args)...);
|
||||
}
|
||||
|
||||
~NeverDestroyed() = default;
|
||||
|
||||
T* operator->() { return &get(); }
|
||||
const T* operator->() const { return &get(); }
|
||||
|
||||
T& operator*() { return get(); }
|
||||
const T* operator*() const { return get(); }
|
||||
|
||||
T& get() { return reinterpret_cast<T&>(storage); }
|
||||
const T& get() const { return reinterpret_cast<T&>(storage); }
|
||||
|
||||
private:
|
||||
alignas(T) u8 storage[sizeof(T)];
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
using AK::NeverDestroyed;
|
Loading…
Add table
Reference in a new issue