Compare commits

...

2 Commits

Author SHA1 Message Date
J. Nick Koston
6eeaca2020 bot 2026-01-19 22:36:28 -10:00
J. Nick Koston
7bc142ad02 [core] Simplify LazyCallbackManager memory management 2026-01-19 22:20:43 -10:00

View File

@@ -1343,16 +1343,30 @@ template<typename... X> class LazyCallbackManager;
*
* Memory overhead comparison (32-bit systems):
* - CallbackManager: 12 bytes (empty std::vector)
* - LazyCallbackManager: 4 bytes (nullptr unique_ptr)
* - LazyCallbackManager: 4 bytes (nullptr pointer)
*
* Uses plain pointer instead of unique_ptr to avoid template instantiation overhead.
* The class is explicitly non-copyable/non-movable for Rule of Five compliance.
*
* @tparam Ts The arguments for the callbacks, wrapped in void().
*/
template<typename... Ts> class LazyCallbackManager<void(Ts...)> {
public:
LazyCallbackManager() = default;
/// Destructor - clean up allocated CallbackManager if any.
/// In practice this never runs (entities live for device lifetime) but included for correctness.
~LazyCallbackManager() { delete this->callbacks_; }
// Non-copyable and non-movable (entities are never copied or moved)
LazyCallbackManager(const LazyCallbackManager &) = delete;
LazyCallbackManager &operator=(const LazyCallbackManager &) = delete;
LazyCallbackManager(LazyCallbackManager &&) = delete;
LazyCallbackManager &operator=(LazyCallbackManager &&) = delete;
/// Add a callback to the list. Allocates the underlying CallbackManager on first use.
void add(std::function<void(Ts...)> &&callback) {
if (!this->callbacks_) {
this->callbacks_ = make_unique<CallbackManager<void(Ts...)>>();
this->callbacks_ = new CallbackManager<void(Ts...)>();
}
this->callbacks_->add(std::move(callback));
}
@@ -1374,7 +1388,7 @@ template<typename... Ts> class LazyCallbackManager<void(Ts...)> {
void operator()(Ts... args) { this->call(args...); }
protected:
std::unique_ptr<CallbackManager<void(Ts...)>> callbacks_;
CallbackManager<void(Ts...)> *callbacks_{nullptr};
};
/// Helper class to deduplicate items in a series of values.