libbio
Loading...
Searching...
No Matches
LazyPointer.h
Go to the documentation of this file.
1/*
2 * This file is a part of the Biology project by eons LLC.
3 * Biology (aka Develop Biology) is a framework for approaching software
4 * development from a natural sciences perspective.
5 *
6 * Copyright (C) 2026 Séon O'Shannon & eons LLC
7 *
8 * This program is free software: you can redistribute it and/or modify
9 * it under the terms of the GNU Affero General Public License as
10 * published by the Free Software Foundation, either version 3 of the
11 * License, or (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU Affero General Public License for more details.
17 *
18 * You should have received a copy of the GNU Affero General Public License
19 * along with this program. If not, see <https://www.gnu.org/licenses/>.
20 */
21
22#pragma once
23
24#include <cstddef>
25
26namespace bio {
27
72template < typename T >
74{
75public:
76 typedef T* (*Factory)();
77
79 :
80 mPtr(NULL),
81 mFactory(NULL)
82 {
83 }
84
86 :
87 mPtr(NULL),
88 mFactory(factory)
89 {
90 }
91
93 :
94 mPtr(materialized),
95 mFactory(NULL)
96 {
97 }
98
100 :
101 mPtr(NULL),
102 mFactory(other.mFactory)
103 {
104 // Clone gets a fresh empty cache + the source's factory; first
105 // access re-materialises. Matches Wave copy-ctor semantics.
106 }
107
109 {
110 if (this != &other)
111 {
112 if (mPtr)
113 {
114 delete mPtr;
115 mPtr = NULL;
116 }
117 mFactory = other.mFactory;
118 }
119 return *this;
120 }
121
123 {
124 if (mPtr)
125 {
126 delete mPtr;
127 }
128 }
129
135 T* operator->() const
136 {
137 if (!mPtr && mFactory)
138 {
139 mPtr = mFactory();
140 }
141 return mPtr;
142 }
143
144 T& operator*() const
145 {
146 return *operator->();
147 }
148
153 T* Get() const
154 {
155 return operator->();
156 }
157
164 {
165 return mPtr;
166 }
167
168 bool IsMaterialized() const
169 {
170 return mPtr != NULL;
171 }
172
174 {
175 mFactory = factory;
176 }
177
179 {
180 return mFactory;
181 }
182
189 {
190 if (mPtr == materialized)
191 {
192 return;
193 }
194 if (mPtr)
195 {
196 delete mPtr;
197 }
198 mPtr = materialized;
199 }
200
207 {
208 T* p = mPtr;
209 mPtr = NULL;
210 return p;
211 }
212
213private:
214 mutable T* mPtr;
215 Factory mFactory;
216};
217
218} // namespace bio
Definition LazyPointer.h:74
T * Get() const
Definition LazyPointer.h:153
~LazyPointer()
Definition LazyPointer.h:122
LazyPointer & operator=(const LazyPointer &other)
Definition LazyPointer.h:108
T & operator*() const
Definition LazyPointer.h:144
LazyPointer(T *materialized)
Definition LazyPointer.h:92
bool IsMaterialized() const
Definition LazyPointer.h:168
void ResetWith(T *materialized)
Definition LazyPointer.h:188
void SetFactory(Factory factory)
Definition LazyPointer.h:173
T * operator->() const
Definition LazyPointer.h:135
Factory GetFactory() const
Definition LazyPointer.h:178
LazyPointer(const LazyPointer &other)
Definition LazyPointer.h:99
T *(* Factory)()
Definition LazyPointer.h:76
LazyPointer()
Definition LazyPointer.h:78
T * GetIfMaterialized() const
Definition LazyPointer.h:163
LazyPointer(Factory factory)
Definition LazyPointer.h:85
T * Release()
Definition LazyPointer.h:206
Definition Pointer.h:115
Definition FinalCell.h:29