comparison foosdk/sdk/pfc/ptrholder.h @ 1:20d02a178406 default tip

*: check in everything else yay
author Paper <paper@tflc.us>
date Mon, 05 Jan 2026 02:15:46 -0500
parents
children
comparison
equal deleted inserted replaced
0:e9bb126753e7 1:20d02a178406
1 #pragma once
2
3 // ptrholder_t<> : std::unique_ptr<> equivalent
4 // Obsolete, use std::unique_ptr<> instead
5 // Warning: release() deletes instead of letting go of ownership, contrary to std::unique_ptr<>
6
7 namespace pfc {
8 class releaser_delete {
9 public:
10 template<typename T> static void release(T* p_ptr) { delete p_ptr; }
11 };
12 class releaser_delete_array {
13 public:
14 template<typename T> static void release(T* p_ptr) { delete[] p_ptr; }
15 };
16 class releaser_free {
17 public:
18 static void release(void* p_ptr) { free(p_ptr); }
19 };
20
21 //! Assumes t_freefunc to never throw exceptions.
22 template<typename T, typename t_releaser = releaser_delete >
23 class ptrholder_t {
24 private:
25 typedef ptrholder_t<T, t_releaser> t_self;
26 public:
27 inline ptrholder_t(T* p_ptr) : m_ptr(p_ptr) {}
28 inline ptrholder_t() : m_ptr(NULL) {}
29 inline ~ptrholder_t() { t_releaser::release(m_ptr); }
30 inline bool is_valid() const { return m_ptr != NULL; }
31 inline bool is_empty() const { return m_ptr == NULL; }
32 inline T* operator->() const { return m_ptr; }
33 inline T* get_ptr() const { return m_ptr; }
34 inline void release() { t_releaser::release(replace_null_t(m_ptr));; }
35 inline void attach(T* p_ptr) { release(); m_ptr = p_ptr; }
36 inline const t_self& operator=(T* p_ptr) { set(p_ptr); return *this; }
37 inline T* detach() { return pfc::replace_null_t(m_ptr); }
38 inline T& operator*() const { return *m_ptr; }
39
40 inline t_self& operator<<(t_self& p_source) { attach(p_source.detach()); return *this; }
41 inline t_self& operator>>(t_self& p_dest) { p_dest.attach(detach()); return *this; }
42
43 //deprecated
44 inline void set(T* p_ptr) { attach(p_ptr); }
45
46 ptrholder_t(t_self&& other) {m_ptr = other.detach(); }
47 const t_self& operator=(t_self&& other) { attach(other.detach()); return *this; }
48 private:
49 ptrholder_t(const t_self&) = delete;
50 const t_self& operator=(const t_self&) = delete;
51
52 T* m_ptr;
53 };
54
55 //avoid "void&" breakage
56 template<typename t_releaser>
57 class ptrholder_t<void, t_releaser> {
58 private:
59 typedef void T;
60 typedef ptrholder_t<T, t_releaser> t_self;
61 public:
62 inline ptrholder_t(T* p_ptr) : m_ptr(p_ptr) {}
63 inline ptrholder_t() : m_ptr(NULL) {}
64 inline ~ptrholder_t() { t_releaser::release(m_ptr); }
65 inline bool is_valid() const { return m_ptr != NULL; }
66 inline bool is_empty() const { return m_ptr == NULL; }
67 inline T* operator->() const { return m_ptr; }
68 inline T* get_ptr() const { return m_ptr; }
69 inline void release() { t_releaser::release(replace_null_t(m_ptr));; }
70 inline void attach(T* p_ptr) { release(); m_ptr = p_ptr; }
71 inline const t_self& operator=(T* p_ptr) { set(p_ptr); return *this; }
72 inline T* detach() { return pfc::replace_null_t(m_ptr); }
73
74 inline t_self& operator<<(t_self& p_source) { attach(p_source.detach()); return *this; }
75 inline t_self& operator>>(t_self& p_dest) { p_dest.attach(detach()); return *this; }
76
77 //deprecated
78 inline void set(T* p_ptr) { attach(p_ptr); }
79 private:
80 ptrholder_t(const t_self&) = delete;
81 const t_self& operator=(const t_self&) = delete;
82
83 T* m_ptr;
84 };
85
86 }