MayaFlux 0.4.0
Digital-First Multimedia Processing Framework
Loading...
Searching...
No Matches
Persist.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "LiveArena.hpp"
4
5namespace MayaFlux {
6
7namespace internal {
8 inline std::vector<std::shared_ptr<void>>& persistent_store()
9 {
10 static std::vector<std::shared_ptr<void>> s_store;
11 return s_store;
12 }
13
15 {
16 persistent_store().clear();
17 }
18
19} // namespace MayaFlux::internal
20
21/**
22 * @brief Transfer ownership of an existing object to the persistent store for process lifetime.
23 * @tparam T Any type managed by shared_ptr.
24 * @param obj Object to retain.
25 * @return The same shared_ptr, allowing inline chaining.
26 */
27template <typename T>
28std::shared_ptr<T> store(std::shared_ptr<T> obj)
29{
30 internal::persistent_store().push_back(std::static_pointer_cast<void>(obj));
31 return obj;
32}
33
34/**
35 * @brief Transfer ownership of an existing object to the persistent store and
36 * expose it to the live arena under @p key.
37 *
38 * The persistent store retains the object for process lifetime. The live arena
39 * entry allows JIT'd code to reach the object via live_cast<T>(key).
40 * No-op on the arena side when MAYAFLUX_LIVE is not defined.
41 *
42 * @tparam T Any type managed by shared_ptr.
43 * @param key Arena key used to locate the object from JIT'd code.
44 * @param obj Object to retain and expose.
45 * @return The same shared_ptr, allowing inline chaining.
46 */
47template <typename T>
48std::shared_ptr<T> store(std::string_view key, std::shared_ptr<T> obj)
49{
50 MF_LIVE_EXPOSE(key.data(), obj);
51 internal::persistent_store().push_back(std::static_pointer_cast<void>(obj));
52 return obj;
53}
54
55/**
56 * @brief Transfer ownership of a value to the persistent store for process lifetime.
57 * @tparam T Any movable type.
58 * @param obj Value to store. Moved into a shared_ptr internally.
59 */
60template <typename T>
61void store(T obj)
62{
64 std::make_shared<T>(std::move(obj)));
65}
66
67/**
68 * @brief Construct a T in place, retain it for process lifetime, and return a shared handle.
69 * @tparam T Type to construct.
70 * @tparam Args Constructor argument types.
71 * @param args Forwarded to std::make_shared<T>.
72 * @return Shared pointer to the newly constructed object.
73 */
74template <typename T, typename... Args>
75std::shared_ptr<T> make_persistent_shared(Args&&... args)
76{
77 auto obj = store(std::make_shared<T>(std::forward<Args>(args)...));
79 return obj;
80}
81
82/**
83 * @brief Construct a T in place, retain it for process lifetime, and return a direct reference.
84 * @tparam T Type to construct.
85 * @tparam Args Constructor argument types.
86 * @param args Forwarded to std::make_shared<T>.
87 * @return Reference to the newly constructed object. Lifetime is the process lifetime.
88 */
89template <typename T, typename... Args>
90T& make_persistent(Args&&... args)
91{
92 return *make_persistent_shared<T>(std::forward<Args>(args)...);
93}
94
95/**
96 * @brief Store a value in the persistent store and return a reference to it.
97 * @tparam T Deduced from the argument.
98 * @param val Value to store. Moved into a shared_ptr internally.
99 * @return Reference to the stored object. Lifetime is the process lifetime.
100 */
101template <typename T>
102std::decay_t<T>& make_persistent(T&& val)
103{
104 auto ptr = std::make_shared<std::decay_t<T>>(std::forward<T>(val));
105 auto& ref = *ptr;
106 internal::persistent_store().push_back(std::static_pointer_cast<void>(std::move(ptr)));
107 return ref;
108}
109
110} // namespace MayaFlux
#define MF_LIVE_EXPOSE(key, ptr)
Exposes ptr to the live arena under a user-supplied key when MAYAFLUX_LIVE is defined.
#define MF_LIVE_EXPOSE_AUTO(ptr)
Auto-expose variant that deduces the key prefix from the shared_ptr element type.
std::vector< std::shared_ptr< void > > & persistent_store()
Definition Persist.hpp:8
void cleanup_persistent_store()
Definition Persist.hpp:14
std::shared_ptr< T > store(std::shared_ptr< T > obj)
Transfer ownership of an existing object to the persistent store for process lifetime.
Definition Persist.hpp:28
std::shared_ptr< T > make_persistent_shared(Args &&... args)
Construct a T in place, retain it for process lifetime, and return a shared handle.
Definition Persist.hpp:75
T & make_persistent(Args &&... args)
Construct a T in place, retain it for process lifetime, and return a direct reference.
Definition Persist.hpp:90
Main namespace for the Maya Flux audio engine.
Definition Runtime.cpp:12