Shamrock 2025.10.0
Astrophysical Code
Loading...
Searching...
No Matches
LifetimeTracker.hpp
Go to the documentation of this file.
1// -------------------------------------------------------//
2//
3// SHAMROCK code for hydrodynamics
4// Copyright (c) 2021-2026 Timothée David--Cléris <tim.shamrock@proton.me>
5// SPDX-License-Identifier: CeCILL Free Software License Agreement v2.1
6// Shamrock is licensed under the CeCILL 2.1 License, see LICENSE for more information
7//
8// -------------------------------------------------------//
9
10#pragma once
11
18
19#include "shambase/WithUUID.hpp"
21#include <string_view>
22#include <utility>
23
24namespace shamrock::solvergraph {
25
41 template<typename T>
42 class LifetimeTracker : public shambase::WithUUID<LifetimeTracker<T>, u64> {
43 public:
45 inline static void (*on_create)(u64 uuid) = nullptr;
47 inline static void (*on_destroy)(u64 uuid) = nullptr;
48
50 inline static void (*on_state_update)(T &object) = nullptr;
52 inline static void (*on_event)(u64 uuid, std::string_view s) = nullptr;
53
56 if (on_create != nullptr) {
57 on_create(this->get_uuid());
58 }
59 };
60
61 LifetimeTracker(const LifetimeTracker &) = delete;
63
65 LifetimeTracker(LifetimeTracker &&) noexcept = default;
66
69 inline LifetimeTracker &operator=(LifetimeTracker &&other) noexcept {
70 if (this != &other) {
73 }
74 return *this;
75 }
76
78 inline void trace_create() {
79 if (on_create) {
80 on_create(this->uuid);
81 }
82 }
83
85 inline void trace_destroy() {
86 if (this->is_alive()) {
87 if (on_destroy) {
88 on_destroy(this->uuid);
89 }
90 this->invalidate();
91 }
92 }
93
94 // notify and update of the owning object.
95 inline void trace_state_update(T &object) {
96 if (this->is_alive() && on_state_update) {
97 on_state_update(object);
98 }
99 }
100
103 template<class F>
104 inline void trace_event(F &&event_info_builder) {
105 if (this->is_alive() && on_event) {
106 on_event(this->uuid, event_info_builder());
107 }
108 }
109
112 };
113
114} // namespace shamrock::solvergraph
std::uint64_t u64
64 bit unsigned integer
A class that provides unique identifiers (UUID) to instances.
Definition WithUUID.hpp:41
WithUUID & operator=(const WithUUID &)=delete
would duplicate the uuid
static void(* on_state_update)(T &object)
Called when the state of a tracked object changes (e.g. edges are rebound).
~LifetimeTracker()
Destructor, notifies destruction (unless already notified, or moved from).
static void(* on_create)(u64 uuid)
Called when a tracked object is created.
LifetimeTracker(LifetimeTracker &&) noexcept=default
Move constructor: transfers the uuid to this and invalidates other.
void trace_create()
Notifies creation of the tracked object.
void trace_event(F &&event_info_builder)
LifetimeTracker()
Constructor, notifies the creation of the tracked object.
static void(* on_destroy)(u64 uuid)
Called when a tracked object is destroyed.
LifetimeTracker(const LifetimeTracker &)=delete
would duplicate the UUID
LifetimeTracker & operator=(const LifetimeTracker &)=delete
would duplicate the UUID
static void(* on_event)(u64 uuid, std::string_view s)
Called when an operation is performed on a tracked object (e.g. evaluation).
void trace_destroy()
Fires the destroy notification, if not already fired or moved from. Idempotent.
namespace for basic c++ utilities