2020-01-18 08:38:21 +00:00
|
|
|
/*
|
2024-10-04 11:19:50 +00:00
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <andreas@ladybird.org>
|
2020-01-18 08:38:21 +00:00
|
|
|
*
|
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>
|
2024-11-16 21:23:41 +00:00
|
|
|
#include <AK/StdLibExtras.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(); }
|
2022-10-16 22:06:11 +00:00
|
|
|
T const* operator->() const { return &get(); }
|
2019-12-26 21:12:45 +00:00
|
|
|
|
|
|
|
T& operator*() { return get(); }
|
2022-10-16 22:06:11 +00:00
|
|
|
T const& operator*() const { return get(); }
|
2019-12-26 21:12:45 +00:00
|
|
|
|
|
|
|
T& get() { return reinterpret_cast<T&>(storage); }
|
2022-10-16 22:06:11 +00:00
|
|
|
T const& get() const { return reinterpret_cast<T&>(storage); }
|
2019-12-26 21:12:45 +00:00
|
|
|
|
|
|
|
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
|