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