Vulkan Schnee 0.0.1
High-performance rendering engine
Loading...
Searching...
No Matches
Pointer.h
Go to the documentation of this file.
1#pragma once
2#include <memory>
3
4namespace EngineCore {
5
6 // Template function to cast std::weak_ptr<Base> to std::weak_ptr<Derived>
7 template <typename Derived, typename Base>
8 std::weak_ptr<Derived> castWeakPtr(const std::weak_ptr<Base>& baseWeakPtr) {
9 // Step 1: Lock the weak_ptr to obtain a shared_ptr<Base>
10 std::shared_ptr<Base> baseSharedPtr = baseWeakPtr.lock();
11
12 if (!baseSharedPtr) {
13 // The original object has been destroyed or the weak_ptr is expired
14 return std::weak_ptr<Derived>();
15 }
16
17 // Step 2: Attempt to dynamically cast the shared_ptr<Base> to shared_ptr<Derived>
18 std::shared_ptr<Derived> derivedSharedPtr = std::dynamic_pointer_cast<Derived>(baseSharedPtr);
19
20 if (!derivedSharedPtr) {
21 // The cast failed; the object is not of type Derived
22 return std::weak_ptr<Derived>();
23 }
24
25 // Step 3: Convert shared_ptr<Derived> back to weak_ptr<Derived>
26 return std::weak_ptr<Derived>(derivedSharedPtr);
27 }
28}
Log category system implementation.
std::weak_ptr< Derived > castWeakPtr(const std::weak_ptr< Base > &baseWeakPtr)
Definition Pointer.h:8