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