2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2019-12-26 21:12:45 +00:00
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <AK/Noncopyable.h>
|
2020-09-22 11:42:30 +00:00
|
|
|
#include <AK/Types.h>
|
2019-12-26 21:12:45 +00:00
|
|
|
|
|
|
|
namespace AK {
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class NeverDestroyed {
|
2020-08-26 19:52:24 +00:00
|
|
|
AK_MAKE_NONCOPYABLE(NeverDestroyed);
|
|
|
|
AK_MAKE_NONMOVABLE(NeverDestroyed);
|
|
|
|
|
2019-12-26 21:12:45 +00:00
|
|
|
public:
|
|
|
|
template<typename... Args>
|
2020-11-22 00:23:17 +00:00
|
|
|
NeverDestroyed(Args&&... args)
|
2019-12-26 21:12:45 +00:00
|
|
|
{
|
|
|
|
new (storage) T(forward<Args>(args)...);
|
|
|
|
}
|
|
|
|
|
|
|
|
~NeverDestroyed() = default;
|
|
|
|
|
|
|
|
T* operator->() { return &get(); }
|
|
|
|
const T* operator->() const { return &get(); }
|
|
|
|
|
|
|
|
T& operator*() { return get(); }
|
2020-02-16 08:32:18 +00:00
|
|
|
const T& operator*() const { return get(); }
|
2019-12-26 21:12:45 +00:00
|
|
|
|
|
|
|
T& get() { return reinterpret_cast<T&>(storage); }
|
|
|
|
const T& get() const { return reinterpret_cast<T&>(storage); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
alignas(T) u8 storage[sizeof(T)];
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2022-11-26 11:18:30 +00:00
|
|
|
#if USING_AK_GLOBALLY
|
2019-12-26 21:12:45 +00:00
|
|
|
using AK::NeverDestroyed;
|
2022-11-26 11:18:30 +00:00
|
|
|
#endif
|