-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Create
References<T>::adoptRef
and `References<T>::adoptEngin…
…eRef` (#10) Creates two helper functions in `References.h`: - `References<T>::adoptRef(...)`: Adopt a raw reference to `T` and take over it's memory ownership. When the resulting `shared_ptr<T>` is deleted, `cleanup` will be called with the reference to `T`, and the caller is expected to properly clean up `T`. - `References<T>::adoptEngineRef(...)`: Same as `adoptRef(...)`, but with an additional argument: `Engine`. This is used to clean up children of `Engine`, e.g. via `Engine::destroy(Renderer)`. Usage: ```cpp // 1. Adopt any reference to a shared_ptr with a custom deleter function (in this case, Engine*) std::shared_ptr<Engine> _engine = References<Engine>::adoptRef(Engine::create(backend), [](Engine* engine) { engine->destroy(&engine); }); // 2. Adopt a filament::Engine-specific object to a shared_ptr with a custom deleter function that needs the `Engine` to perform cleanup: std::shared_ptr<Renderer> renderer = References<Renderer>::adoptEngineRef(_engine, _engine->createRenderer(), [](const std::shared_ptr<Engine>& engine, Renderer* renderer) { engine->destroy(renderer); }); ```
- Loading branch information
Showing
3 changed files
with
56 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
// | ||
// Created by Marc Rousavy on 23.02.24. | ||
// | ||
|
||
#pragma once | ||
|
||
#include <filament/Engine.h> | ||
#include <functional> | ||
#include <memory> | ||
|
||
namespace margelo { | ||
|
||
template <typename T> struct References { | ||
public: | ||
using CleanupRefFunction = std::function<void(T* ref)>; | ||
using CleanupEngineRefFunction = std::function<void(std::shared_ptr<filament::Engine> engine, T* ref)>; | ||
|
||
struct Deleter { | ||
public: | ||
explicit Deleter(const CleanupRefFunction& cleanup) : _cleanup(std::move(cleanup)) {} | ||
void operator()(T* ref) { | ||
_cleanup(ref); | ||
} | ||
|
||
private: | ||
CleanupRefFunction _cleanup; | ||
}; | ||
|
||
/** | ||
* Adopt a raw reference to T and take over it's memory ownership. | ||
* When the resulting `shared_ptr` is deleted, `cleanup` will be called with the reference to T, and the caller is expected to properly | ||
* clean up T. | ||
* @param value The raw reference to T that will be adopted. | ||
* @param cleanup The function to run when the shared_ptr's ref count reaches zero. The caller is expected to delete the reference to T | ||
* here. | ||
* @return A shared_ptr safely managing the reference to T. | ||
*/ | ||
static std::shared_ptr<T> adoptRef(T* value, const CleanupRefFunction& cleanup) { | ||
return std::shared_ptr<T>(value, Deleter(std::move(cleanup))); | ||
} | ||
|
||
/** | ||
* Same as `adoptRef(T*, CleanupRefFunction)`, but with an additional argument: `Engine`. | ||
* This is used to clean up children of `Engine`, e.g. via `Engine::destroy(Renderer)`. | ||
*/ | ||
static std::shared_ptr<T> adoptEngineRef(const std::shared_ptr<filament::Engine>& engine, T* value, CleanupEngineRefFunction cleanup) { | ||
return adoptRef(value, [engine = std::move(engine), cleanup = std::move(cleanup)](T* ref) { cleanup(engine, ref); }); | ||
} | ||
}; | ||
|
||
} // namespace margelo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters