light/modules/lsd/ref.cppm

33 lines
888 B
Text
Raw Normal View History

2025-11-16 14:29:03 +03:30
export module lsd.ref_ptr;
2025-09-22 18:53:35 +03:30
import std;
2025-09-22 18:53:35 +03:30
2025-11-16 14:29:03 +03:30
export namespace lt::lsd {
2025-09-22 18:53:35 +03:30
/** Wrapper around std::shared_ptr.
*
* @note Currently just an alias, might turn into an implementation later.
* @ref https://en.cppreference.com/w/cpp/memory/shared_ptr.html
*/
2025-11-16 14:29:03 +03:30
template<typename T>
2025-11-18 19:06:44 +03:30
using ref = std::shared_ptr<T>;
2025-09-22 18:53:35 +03:30
/** Allocates memory for an `Underlying_T` and directly constructs it there.
*
* @return A Ref<Underlying_T> to the constructed object.
*/
2025-11-16 14:29:03 +03:30
template<typename Underlying_T, typename... Args>
2025-11-18 19:06:44 +03:30
constexpr auto create_ref(Args &&...args) -> ref<Underlying_T>
2025-09-22 18:53:35 +03:30
{
return std::make_shared<Underlying_T>(std::forward<Args>(args)...);
}
/** Converts c-style pointer of type `Underlying_T` to a `Ref<Underlying_T>`. */
2025-11-16 14:29:03 +03:30
template<typename Underlying_T>
2025-11-18 19:06:44 +03:30
constexpr auto make_ref(Underlying_T *raw_pointer) -> ref<Underlying_T>
2025-09-22 18:53:35 +03:30
{
2025-11-18 19:06:44 +03:30
return ref<Underlying_T>(raw_pointer);
2025-09-22 18:53:35 +03:30
}
2025-11-16 14:29:03 +03:30
} // namespace lt::lsd