annotate CSP2/CSP2_env/env-d9b9114564458d9d-741b3de822f2aaca6c6caa4325c4afce/include/kj/memory.h @ 69:33d812a61356

planemo upload commit 2e9511a184a1ca667c7be0c6321a36dc4e3d116d
author jpayne
date Tue, 18 Mar 2025 17:55:14 -0400
parents
children
rev   line source
jpayne@69 1 // Copyright (c) 2013-2014 Sandstorm Development Group, Inc. and contributors
jpayne@69 2 // Licensed under the MIT License:
jpayne@69 3 //
jpayne@69 4 // Permission is hereby granted, free of charge, to any person obtaining a copy
jpayne@69 5 // of this software and associated documentation files (the "Software"), to deal
jpayne@69 6 // in the Software without restriction, including without limitation the rights
jpayne@69 7 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
jpayne@69 8 // copies of the Software, and to permit persons to whom the Software is
jpayne@69 9 // furnished to do so, subject to the following conditions:
jpayne@69 10 //
jpayne@69 11 // The above copyright notice and this permission notice shall be included in
jpayne@69 12 // all copies or substantial portions of the Software.
jpayne@69 13 //
jpayne@69 14 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
jpayne@69 15 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
jpayne@69 16 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
jpayne@69 17 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
jpayne@69 18 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
jpayne@69 19 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
jpayne@69 20 // THE SOFTWARE.
jpayne@69 21
jpayne@69 22 #pragma once
jpayne@69 23
jpayne@69 24 #include "common.h"
jpayne@69 25
jpayne@69 26 KJ_BEGIN_HEADER
jpayne@69 27
jpayne@69 28 namespace kj {
jpayne@69 29
jpayne@69 30 template <typename T>
jpayne@69 31 inline constexpr bool _kj_internal_isPolymorphic(T*) {
jpayne@69 32 // If you get a compiler error here complaining that T is incomplete, it's because you are trying
jpayne@69 33 // to use kj::Own<T> with a type that has only been forward-declared. Since KJ doesn't know if
jpayne@69 34 // the type might be involved in inheritance (especially multiple inheritance), it doesn't know
jpayne@69 35 // how to correctly call the disposer to destroy the type, since the object's true memory address
jpayne@69 36 // may differ from the address used to point to a superclass.
jpayne@69 37 //
jpayne@69 38 // However, if you know for sure that T is NOT polymorphic (i.e. it doesn't have a vtable and
jpayne@69 39 // isn't involved in inheritance), then you can use KJ_DECLARE_NON_POLYMORPHIC(T) to declare this
jpayne@69 40 // to KJ without actually completing the type. Place this macro invocation either in the global
jpayne@69 41 // scope, or in the same namespace as T is defined.
jpayne@69 42 return __is_polymorphic(T);
jpayne@69 43 }
jpayne@69 44
jpayne@69 45 #define KJ_DECLARE_NON_POLYMORPHIC(...) \
jpayne@69 46 inline constexpr bool _kj_internal_isPolymorphic(__VA_ARGS__*) { \
jpayne@69 47 return false; \
jpayne@69 48 }
jpayne@69 49 // If you want to use kj::Own<T> for an incomplete type T that you know is not polymorphic, then
jpayne@69 50 // write `KJ_DECLARE_NON_POLYMORPHIC(T)` either at the global scope or in the same namespace as
jpayne@69 51 // T is declared.
jpayne@69 52 //
jpayne@69 53 // This also works for templates, e.g.:
jpayne@69 54 //
jpayne@69 55 // template <typename X, typename Y>
jpayne@69 56 // struct MyType;
jpayne@69 57 // template <typename X, typename Y>
jpayne@69 58 // KJ_DECLARE_NON_POLYMORPHIC(MyType<X, Y>)
jpayne@69 59
jpayne@69 60 namespace _ { // private
jpayne@69 61
jpayne@69 62 template <typename T> struct RefOrVoid_ { typedef T& Type; };
jpayne@69 63 template <> struct RefOrVoid_<void> { typedef void Type; };
jpayne@69 64 template <> struct RefOrVoid_<const void> { typedef void Type; };
jpayne@69 65
jpayne@69 66 template <typename T>
jpayne@69 67 using RefOrVoid = typename RefOrVoid_<T>::Type;
jpayne@69 68 // Evaluates to T&, unless T is `void`, in which case evaluates to `void`.
jpayne@69 69 //
jpayne@69 70 // This is a hack needed to avoid defining Own<void> as a totally separate class.
jpayne@69 71
jpayne@69 72 template <typename T, bool isPolymorphic = _kj_internal_isPolymorphic((T*)nullptr)>
jpayne@69 73 struct CastToVoid_;
jpayne@69 74
jpayne@69 75 template <typename T>
jpayne@69 76 struct CastToVoid_<T, false> {
jpayne@69 77 static void* apply(T* ptr) {
jpayne@69 78 return static_cast<void*>(ptr);
jpayne@69 79 }
jpayne@69 80 static const void* applyConst(T* ptr) {
jpayne@69 81 const T* cptr = ptr;
jpayne@69 82 return static_cast<const void*>(cptr);
jpayne@69 83 }
jpayne@69 84 };
jpayne@69 85
jpayne@69 86 template <typename T>
jpayne@69 87 struct CastToVoid_<T, true> {
jpayne@69 88 static void* apply(T* ptr) {
jpayne@69 89 return dynamic_cast<void*>(ptr);
jpayne@69 90 }
jpayne@69 91 static const void* applyConst(T* ptr) {
jpayne@69 92 const T* cptr = ptr;
jpayne@69 93 return dynamic_cast<const void*>(cptr);
jpayne@69 94 }
jpayne@69 95 };
jpayne@69 96
jpayne@69 97 template <typename T>
jpayne@69 98 void* castToVoid(T* ptr) {
jpayne@69 99 return CastToVoid_<T>::apply(ptr);
jpayne@69 100 }
jpayne@69 101
jpayne@69 102 template <typename T>
jpayne@69 103 const void* castToConstVoid(T* ptr) {
jpayne@69 104 return CastToVoid_<T>::applyConst(ptr);
jpayne@69 105 }
jpayne@69 106
jpayne@69 107 } // namespace _ (private)
jpayne@69 108
jpayne@69 109 // =======================================================================================
jpayne@69 110 // Disposer -- Implementation details.
jpayne@69 111
jpayne@69 112 class Disposer {
jpayne@69 113 // Abstract interface for a thing that "disposes" of objects, where "disposing" usually means
jpayne@69 114 // calling the destructor followed by freeing the underlying memory. `Own<T>` encapsulates an
jpayne@69 115 // object pointer with corresponding Disposer.
jpayne@69 116 //
jpayne@69 117 // Few developers will ever touch this interface. It is primarily useful for those implementing
jpayne@69 118 // custom memory allocators.
jpayne@69 119
jpayne@69 120 protected:
jpayne@69 121 // Do not declare a destructor, as doing so will force a global initializer for each HeapDisposer
jpayne@69 122 // instance. Eww!
jpayne@69 123
jpayne@69 124 virtual void disposeImpl(void* pointer) const = 0;
jpayne@69 125 // Disposes of the object, given a pointer to the beginning of the object. If the object is
jpayne@69 126 // polymorphic, this pointer is determined by dynamic_cast<void*>(). For non-polymorphic types,
jpayne@69 127 // Own<T> does not allow any casting, so the pointer exactly matches the original one given to
jpayne@69 128 // Own<T>.
jpayne@69 129
jpayne@69 130 public:
jpayne@69 131
jpayne@69 132 template <typename T>
jpayne@69 133 void dispose(T* object) const;
jpayne@69 134 // Helper wrapper around disposeImpl().
jpayne@69 135 //
jpayne@69 136 // If T is polymorphic, calls `disposeImpl(dynamic_cast<void*>(object))`, otherwise calls
jpayne@69 137 // `disposeImpl(implicitCast<void*>(object))`.
jpayne@69 138 //
jpayne@69 139 // Callers must not call dispose() on the same pointer twice, even if the first call throws
jpayne@69 140 // an exception.
jpayne@69 141
jpayne@69 142 private:
jpayne@69 143 template <typename T, bool polymorphic = _kj_internal_isPolymorphic((T*)nullptr)>
jpayne@69 144 struct Dispose_;
jpayne@69 145 };
jpayne@69 146
jpayne@69 147 template <typename T>
jpayne@69 148 class DestructorOnlyDisposer: public Disposer {
jpayne@69 149 // A disposer that merely calls the type's destructor and nothing else.
jpayne@69 150
jpayne@69 151 public:
jpayne@69 152 static const DestructorOnlyDisposer instance;
jpayne@69 153
jpayne@69 154 void disposeImpl(void* pointer) const override {
jpayne@69 155 reinterpret_cast<T*>(pointer)->~T();
jpayne@69 156 }
jpayne@69 157 };
jpayne@69 158
jpayne@69 159 template <typename T>
jpayne@69 160 const DestructorOnlyDisposer<T> DestructorOnlyDisposer<T>::instance = DestructorOnlyDisposer<T>();
jpayne@69 161
jpayne@69 162 class NullDisposer: public Disposer {
jpayne@69 163 // A disposer that does nothing.
jpayne@69 164
jpayne@69 165 public:
jpayne@69 166 static const NullDisposer instance;
jpayne@69 167
jpayne@69 168 void disposeImpl(void* pointer) const override {}
jpayne@69 169 };
jpayne@69 170
jpayne@69 171 // =======================================================================================
jpayne@69 172 // Own<T> -- An owned pointer.
jpayne@69 173
jpayne@69 174 template <typename T, typename StaticDisposer = decltype(nullptr)>
jpayne@69 175 class Own;
jpayne@69 176
jpayne@69 177 template <typename T>
jpayne@69 178 class Own<T, decltype(nullptr)> {
jpayne@69 179 // A transferrable title to a T. When an Own<T> goes out of scope, the object's Disposer is
jpayne@69 180 // called to dispose of it. An Own<T> can be efficiently passed by move, without relocating the
jpayne@69 181 // underlying object; this transfers ownership.
jpayne@69 182 //
jpayne@69 183 // This is much like std::unique_ptr, except:
jpayne@69 184 // - You cannot release(). An owned object is not necessarily allocated with new (see next
jpayne@69 185 // point), so it would be hard to use release() correctly.
jpayne@69 186 // - The deleter is made polymorphic by virtual call rather than by template. This is much
jpayne@69 187 // more powerful -- it allows the use of custom allocators, freelists, etc. This could
jpayne@69 188 // _almost_ be accomplished with unique_ptr by forcing everyone to use something like
jpayne@69 189 // std::unique_ptr<T, kj::Deleter>, except that things get hairy in the presence of multiple
jpayne@69 190 // inheritance and upcasting, and anyway if you force everyone to use a custom deleter
jpayne@69 191 // then you've lost any benefit to interoperating with the "standard" unique_ptr.
jpayne@69 192
jpayne@69 193 public:
jpayne@69 194 KJ_DISALLOW_COPY(Own);
jpayne@69 195 inline Own(): disposer(nullptr), ptr(nullptr) {}
jpayne@69 196 inline Own(Own&& other) noexcept
jpayne@69 197 : disposer(other.disposer), ptr(other.ptr) { other.ptr = nullptr; }
jpayne@69 198 inline Own(Own<RemoveConstOrDisable<T>>&& other) noexcept
jpayne@69 199 : disposer(other.disposer), ptr(other.ptr) { other.ptr = nullptr; }
jpayne@69 200 template <typename U, typename = EnableIf<canConvert<U*, T*>()>>
jpayne@69 201 inline Own(Own<U>&& other) noexcept
jpayne@69 202 : disposer(other.disposer), ptr(cast(other.ptr)) {
jpayne@69 203 other.ptr = nullptr;
jpayne@69 204 }
jpayne@69 205 template <typename U, typename StaticDisposer, typename = EnableIf<canConvert<U*, T*>()>>
jpayne@69 206 inline Own(Own<U, StaticDisposer>&& other) noexcept;
jpayne@69 207 // Convert statically-disposed Own to dynamically-disposed Own.
jpayne@69 208 inline Own(T* ptr, const Disposer& disposer) noexcept: disposer(&disposer), ptr(ptr) {}
jpayne@69 209
jpayne@69 210 ~Own() noexcept(false) { dispose(); }
jpayne@69 211
jpayne@69 212 inline Own& operator=(Own&& other) {
jpayne@69 213 // Move-assignnment operator.
jpayne@69 214
jpayne@69 215 // Careful, this might own `other`. Therefore we have to transfer the pointers first, then
jpayne@69 216 // dispose.
jpayne@69 217 const Disposer* disposerCopy = disposer;
jpayne@69 218 T* ptrCopy = ptr;
jpayne@69 219 disposer = other.disposer;
jpayne@69 220 ptr = other.ptr;
jpayne@69 221 other.ptr = nullptr;
jpayne@69 222 if (ptrCopy != nullptr) {
jpayne@69 223 disposerCopy->dispose(const_cast<RemoveConst<T>*>(ptrCopy));
jpayne@69 224 }
jpayne@69 225 return *this;
jpayne@69 226 }
jpayne@69 227
jpayne@69 228 inline Own& operator=(decltype(nullptr)) {
jpayne@69 229 dispose();
jpayne@69 230 return *this;
jpayne@69 231 }
jpayne@69 232
jpayne@69 233 template <typename... Attachments>
jpayne@69 234 Own<T> attach(Attachments&&... attachments) KJ_WARN_UNUSED_RESULT;
jpayne@69 235 // Returns an Own<T> which points to the same object but which also ensures that all values
jpayne@69 236 // passed to `attachments` remain alive until after this object is destroyed. Normally
jpayne@69 237 // `attachments` are other Own<?>s pointing to objects that this one depends on.
jpayne@69 238 //
jpayne@69 239 // Note that attachments will eventually be destroyed in the order they are listed. Hence,
jpayne@69 240 // foo.attach(bar, baz) is equivalent to (but more efficient than) foo.attach(bar).attach(baz).
jpayne@69 241
jpayne@69 242 template <typename U>
jpayne@69 243 Own<U> downcast() {
jpayne@69 244 // Downcast the pointer to Own<U>, destroying the original pointer. If this pointer does not
jpayne@69 245 // actually point at an instance of U, the results are undefined (throws an exception in debug
jpayne@69 246 // mode if RTTI is enabled, otherwise you're on your own).
jpayne@69 247
jpayne@69 248 Own<U> result;
jpayne@69 249 if (ptr != nullptr) {
jpayne@69 250 result.ptr = &kj::downcast<U>(*ptr);
jpayne@69 251 result.disposer = disposer;
jpayne@69 252 ptr = nullptr;
jpayne@69 253 }
jpayne@69 254 return result;
jpayne@69 255 }
jpayne@69 256
jpayne@69 257 #define NULLCHECK KJ_IREQUIRE(ptr != nullptr, "null Own<> dereference")
jpayne@69 258 inline T* operator->() { NULLCHECK; return ptr; }
jpayne@69 259 inline const T* operator->() const { NULLCHECK; return ptr; }
jpayne@69 260 inline _::RefOrVoid<T> operator*() { NULLCHECK; return *ptr; }
jpayne@69 261 inline _::RefOrVoid<const T> operator*() const { NULLCHECK; return *ptr; }
jpayne@69 262 #undef NULLCHECK
jpayne@69 263 inline T* get() { return ptr; }
jpayne@69 264 inline const T* get() const { return ptr; }
jpayne@69 265 inline operator T*() { return ptr; }
jpayne@69 266 inline operator const T*() const { return ptr; }
jpayne@69 267
jpayne@69 268 private:
jpayne@69 269 const Disposer* disposer; // Only valid if ptr != nullptr.
jpayne@69 270 T* ptr;
jpayne@69 271
jpayne@69 272 inline explicit Own(decltype(nullptr)): disposer(nullptr), ptr(nullptr) {}
jpayne@69 273
jpayne@69 274 inline bool operator==(decltype(nullptr)) { return ptr == nullptr; }
jpayne@69 275 inline bool operator!=(decltype(nullptr)) { return ptr != nullptr; }
jpayne@69 276 // Only called by Maybe<Own<T>>.
jpayne@69 277
jpayne@69 278 inline void dispose() {
jpayne@69 279 // Make sure that if an exception is thrown, we are left with a null ptr, so we won't possibly
jpayne@69 280 // dispose again.
jpayne@69 281 T* ptrCopy = ptr;
jpayne@69 282 if (ptrCopy != nullptr) {
jpayne@69 283 ptr = nullptr;
jpayne@69 284 disposer->dispose(const_cast<RemoveConst<T>*>(ptrCopy));
jpayne@69 285 }
jpayne@69 286 }
jpayne@69 287
jpayne@69 288 template <typename U>
jpayne@69 289 static inline T* cast(U* ptr) {
jpayne@69 290 static_assert(_kj_internal_isPolymorphic((T*)nullptr),
jpayne@69 291 "Casting owned pointers requires that the target type is polymorphic.");
jpayne@69 292 return ptr;
jpayne@69 293 }
jpayne@69 294
jpayne@69 295 template <typename, typename>
jpayne@69 296 friend class Own;
jpayne@69 297 friend class Maybe<Own<T>>;
jpayne@69 298 };
jpayne@69 299
jpayne@69 300 template <>
jpayne@69 301 template <typename U>
jpayne@69 302 inline void* Own<void>::cast(U* ptr) {
jpayne@69 303 return _::castToVoid(ptr);
jpayne@69 304 }
jpayne@69 305
jpayne@69 306 template <>
jpayne@69 307 template <typename U>
jpayne@69 308 inline const void* Own<const void>::cast(U* ptr) {
jpayne@69 309 return _::castToConstVoid(ptr);
jpayne@69 310 }
jpayne@69 311
jpayne@69 312 template <typename T, typename StaticDisposer>
jpayne@69 313 class Own {
jpayne@69 314 // If a `StaticDisposer` is specified (which is not the norm), then the object will be deleted
jpayne@69 315 // by calling StaticDisposer::dispose(pointer). The pointer passed to `dispose()` could be a
jpayne@69 316 // superclass of `T`, if the pointer has been upcast.
jpayne@69 317 //
jpayne@69 318 // This type can be useful for micro-optimization, if you've found that you are doing excessive
jpayne@69 319 // heap allocations to the point where the virtual call on destruction is costing non-negligible
jpayne@69 320 // resources. You should avoid this unless you have a specific need, because it precludes a lot
jpayne@69 321 // of power.
jpayne@69 322
jpayne@69 323 public:
jpayne@69 324 KJ_DISALLOW_COPY(Own);
jpayne@69 325 inline Own(): ptr(nullptr) {}
jpayne@69 326 inline Own(Own&& other) noexcept
jpayne@69 327 : ptr(other.ptr) { other.ptr = nullptr; }
jpayne@69 328 inline Own(Own<RemoveConstOrDisable<T>, StaticDisposer>&& other) noexcept
jpayne@69 329 : ptr(other.ptr) { other.ptr = nullptr; }
jpayne@69 330 template <typename U, typename = EnableIf<canConvert<U*, T*>()>>
jpayne@69 331 inline Own(Own<U, StaticDisposer>&& other) noexcept
jpayne@69 332 : ptr(cast(other.ptr)) {
jpayne@69 333 other.ptr = nullptr;
jpayne@69 334 }
jpayne@69 335 inline explicit Own(T* ptr) noexcept: ptr(ptr) {}
jpayne@69 336
jpayne@69 337 ~Own() noexcept(false) { dispose(); }
jpayne@69 338
jpayne@69 339 inline Own& operator=(Own&& other) {
jpayne@69 340 // Move-assignnment operator.
jpayne@69 341
jpayne@69 342 // Careful, this might own `other`. Therefore we have to transfer the pointers first, then
jpayne@69 343 // dispose.
jpayne@69 344 T* ptrCopy = ptr;
jpayne@69 345 ptr = other.ptr;
jpayne@69 346 other.ptr = nullptr;
jpayne@69 347 if (ptrCopy != nullptr) {
jpayne@69 348 StaticDisposer::dispose(ptrCopy);
jpayne@69 349 }
jpayne@69 350 return *this;
jpayne@69 351 }
jpayne@69 352
jpayne@69 353 inline Own& operator=(decltype(nullptr)) {
jpayne@69 354 dispose();
jpayne@69 355 return *this;
jpayne@69 356 }
jpayne@69 357
jpayne@69 358 template <typename U>
jpayne@69 359 Own<U, StaticDisposer> downcast() {
jpayne@69 360 // Downcast the pointer to Own<U>, destroying the original pointer. If this pointer does not
jpayne@69 361 // actually point at an instance of U, the results are undefined (throws an exception in debug
jpayne@69 362 // mode if RTTI is enabled, otherwise you're on your own).
jpayne@69 363
jpayne@69 364 Own<U, StaticDisposer> result;
jpayne@69 365 if (ptr != nullptr) {
jpayne@69 366 result.ptr = &kj::downcast<U>(*ptr);
jpayne@69 367 ptr = nullptr;
jpayne@69 368 }
jpayne@69 369 return result;
jpayne@69 370 }
jpayne@69 371
jpayne@69 372 #define NULLCHECK KJ_IREQUIRE(ptr != nullptr, "null Own<> dereference")
jpayne@69 373 inline T* operator->() { NULLCHECK; return ptr; }
jpayne@69 374 inline const T* operator->() const { NULLCHECK; return ptr; }
jpayne@69 375 inline _::RefOrVoid<T> operator*() { NULLCHECK; return *ptr; }
jpayne@69 376 inline _::RefOrVoid<const T> operator*() const { NULLCHECK; return *ptr; }
jpayne@69 377 #undef NULLCHECK
jpayne@69 378 inline T* get() { return ptr; }
jpayne@69 379 inline const T* get() const { return ptr; }
jpayne@69 380 inline operator T*() { return ptr; }
jpayne@69 381 inline operator const T*() const { return ptr; }
jpayne@69 382
jpayne@69 383 private:
jpayne@69 384 T* ptr;
jpayne@69 385
jpayne@69 386 inline explicit Own(decltype(nullptr)): ptr(nullptr) {}
jpayne@69 387
jpayne@69 388 inline bool operator==(decltype(nullptr)) { return ptr == nullptr; }
jpayne@69 389 inline bool operator!=(decltype(nullptr)) { return ptr != nullptr; }
jpayne@69 390 // Only called by Maybe<Own<T>>.
jpayne@69 391
jpayne@69 392 inline void dispose() {
jpayne@69 393 // Make sure that if an exception is thrown, we are left with a null ptr, so we won't possibly
jpayne@69 394 // dispose again.
jpayne@69 395 T* ptrCopy = ptr;
jpayne@69 396 if (ptrCopy != nullptr) {
jpayne@69 397 ptr = nullptr;
jpayne@69 398 StaticDisposer::dispose(ptrCopy);
jpayne@69 399 }
jpayne@69 400 }
jpayne@69 401
jpayne@69 402 template <typename U>
jpayne@69 403 static inline T* cast(U* ptr) {
jpayne@69 404 return ptr;
jpayne@69 405 }
jpayne@69 406
jpayne@69 407 template <typename, typename>
jpayne@69 408 friend class Own;
jpayne@69 409 friend class Maybe<Own<T, StaticDisposer>>;
jpayne@69 410 };
jpayne@69 411
jpayne@69 412 namespace _ { // private
jpayne@69 413
jpayne@69 414 template <typename T, typename D>
jpayne@69 415 class OwnOwn {
jpayne@69 416 public:
jpayne@69 417 inline OwnOwn(Own<T, D>&& value) noexcept: value(kj::mv(value)) {}
jpayne@69 418
jpayne@69 419 inline Own<T, D>& operator*() & { return value; }
jpayne@69 420 inline const Own<T, D>& operator*() const & { return value; }
jpayne@69 421 inline Own<T, D>&& operator*() && { return kj::mv(value); }
jpayne@69 422 inline const Own<T, D>&& operator*() const && { return kj::mv(value); }
jpayne@69 423 inline Own<T, D>* operator->() { return &value; }
jpayne@69 424 inline const Own<T, D>* operator->() const { return &value; }
jpayne@69 425 inline operator Own<T, D>*() { return value ? &value : nullptr; }
jpayne@69 426 inline operator const Own<T, D>*() const { return value ? &value : nullptr; }
jpayne@69 427
jpayne@69 428 private:
jpayne@69 429 Own<T, D> value;
jpayne@69 430 };
jpayne@69 431
jpayne@69 432 template <typename T, typename D>
jpayne@69 433 OwnOwn<T, D> readMaybe(Maybe<Own<T, D>>&& maybe) { return OwnOwn<T, D>(kj::mv(maybe.ptr)); }
jpayne@69 434 template <typename T, typename D>
jpayne@69 435 Own<T, D>* readMaybe(Maybe<Own<T, D>>& maybe) { return maybe.ptr ? &maybe.ptr : nullptr; }
jpayne@69 436 template <typename T, typename D>
jpayne@69 437 const Own<T, D>* readMaybe(const Maybe<Own<T, D>>& maybe) {
jpayne@69 438 return maybe.ptr ? &maybe.ptr : nullptr;
jpayne@69 439 }
jpayne@69 440
jpayne@69 441 } // namespace _ (private)
jpayne@69 442
jpayne@69 443 template <typename T, typename D>
jpayne@69 444 class Maybe<Own<T, D>> {
jpayne@69 445 public:
jpayne@69 446 inline Maybe(): ptr(nullptr) {}
jpayne@69 447 inline Maybe(Own<T, D>&& t) noexcept: ptr(kj::mv(t)) {}
jpayne@69 448 inline Maybe(Maybe&& other) noexcept: ptr(kj::mv(other.ptr)) {}
jpayne@69 449
jpayne@69 450 template <typename U>
jpayne@69 451 inline Maybe(Maybe<Own<U, D>>&& other): ptr(mv(other.ptr)) {}
jpayne@69 452 template <typename U>
jpayne@69 453 inline Maybe(Own<U, D>&& other): ptr(mv(other)) {}
jpayne@69 454
jpayne@69 455 inline Maybe(decltype(nullptr)) noexcept: ptr(nullptr) {}
jpayne@69 456
jpayne@69 457 inline Own<T, D>& emplace(Own<T, D> value) {
jpayne@69 458 // Assign the Maybe to the given value and return the content. This avoids the need to do a
jpayne@69 459 // KJ_ASSERT_NONNULL() immediately after setting the Maybe just to read it back again.
jpayne@69 460 ptr = kj::mv(value);
jpayne@69 461 return ptr;
jpayne@69 462 }
jpayne@69 463
jpayne@69 464 template <typename U = T>
jpayne@69 465 inline operator NoInfer<Maybe<U&>>() { return ptr.get(); }
jpayne@69 466 template <typename U = T>
jpayne@69 467 inline operator NoInfer<Maybe<const U&>>() const { return ptr.get(); }
jpayne@69 468 // Implicit conversion to `Maybe<U&>`. The weird templating is to make sure that
jpayne@69 469 // `Maybe<Own<void>>` can be instantiated with the compiler complaining about forming references
jpayne@69 470 // to void -- the use of templates here will cause SFINAE to kick in and hide these, whereas if
jpayne@69 471 // they are not templates then SFINAE isn't applied and so they are considered errors.
jpayne@69 472
jpayne@69 473 inline Maybe& operator=(Maybe&& other) { ptr = kj::mv(other.ptr); return *this; }
jpayne@69 474
jpayne@69 475 inline bool operator==(decltype(nullptr)) const { return ptr == nullptr; }
jpayne@69 476 inline bool operator!=(decltype(nullptr)) const { return ptr != nullptr; }
jpayne@69 477
jpayne@69 478 Own<T, D>& orDefault(Own<T, D>& defaultValue) {
jpayne@69 479 if (ptr == nullptr) {
jpayne@69 480 return defaultValue;
jpayne@69 481 } else {
jpayne@69 482 return ptr;
jpayne@69 483 }
jpayne@69 484 }
jpayne@69 485 const Own<T, D>& orDefault(const Own<T, D>& defaultValue) const {
jpayne@69 486 if (ptr == nullptr) {
jpayne@69 487 return defaultValue;
jpayne@69 488 } else {
jpayne@69 489 return ptr;
jpayne@69 490 }
jpayne@69 491 }
jpayne@69 492
jpayne@69 493 template <typename F,
jpayne@69 494 typename Result = decltype(instance<bool>() ? instance<Own<T, D>>() : instance<F>()())>
jpayne@69 495 Result orDefault(F&& lazyDefaultValue) && {
jpayne@69 496 if (ptr == nullptr) {
jpayne@69 497 return lazyDefaultValue();
jpayne@69 498 } else {
jpayne@69 499 return kj::mv(ptr);
jpayne@69 500 }
jpayne@69 501 }
jpayne@69 502
jpayne@69 503 template <typename Func>
jpayne@69 504 auto map(Func&& f) & -> Maybe<decltype(f(instance<Own<T, D>&>()))> {
jpayne@69 505 if (ptr == nullptr) {
jpayne@69 506 return nullptr;
jpayne@69 507 } else {
jpayne@69 508 return f(ptr);
jpayne@69 509 }
jpayne@69 510 }
jpayne@69 511
jpayne@69 512 template <typename Func>
jpayne@69 513 auto map(Func&& f) const & -> Maybe<decltype(f(instance<const Own<T, D>&>()))> {
jpayne@69 514 if (ptr == nullptr) {
jpayne@69 515 return nullptr;
jpayne@69 516 } else {
jpayne@69 517 return f(ptr);
jpayne@69 518 }
jpayne@69 519 }
jpayne@69 520
jpayne@69 521 template <typename Func>
jpayne@69 522 auto map(Func&& f) && -> Maybe<decltype(f(instance<Own<T, D>&&>()))> {
jpayne@69 523 if (ptr == nullptr) {
jpayne@69 524 return nullptr;
jpayne@69 525 } else {
jpayne@69 526 return f(kj::mv(ptr));
jpayne@69 527 }
jpayne@69 528 }
jpayne@69 529
jpayne@69 530 template <typename Func>
jpayne@69 531 auto map(Func&& f) const && -> Maybe<decltype(f(instance<const Own<T, D>&&>()))> {
jpayne@69 532 if (ptr == nullptr) {
jpayne@69 533 return nullptr;
jpayne@69 534 } else {
jpayne@69 535 return f(kj::mv(ptr));
jpayne@69 536 }
jpayne@69 537 }
jpayne@69 538
jpayne@69 539 private:
jpayne@69 540 Own<T, D> ptr;
jpayne@69 541
jpayne@69 542 template <typename U>
jpayne@69 543 friend class Maybe;
jpayne@69 544 template <typename U, typename D2>
jpayne@69 545 friend _::OwnOwn<U, D2> _::readMaybe(Maybe<Own<U, D2>>&& maybe);
jpayne@69 546 template <typename U, typename D2>
jpayne@69 547 friend Own<U, D2>* _::readMaybe(Maybe<Own<U, D2>>& maybe);
jpayne@69 548 template <typename U, typename D2>
jpayne@69 549 friend const Own<U, D2>* _::readMaybe(const Maybe<Own<U, D2>>& maybe);
jpayne@69 550 };
jpayne@69 551
jpayne@69 552 namespace _ { // private
jpayne@69 553
jpayne@69 554 template <typename T>
jpayne@69 555 class HeapDisposer final: public Disposer {
jpayne@69 556 public:
jpayne@69 557 virtual void disposeImpl(void* pointer) const override { delete reinterpret_cast<T*>(pointer); }
jpayne@69 558
jpayne@69 559 static const HeapDisposer instance;
jpayne@69 560 };
jpayne@69 561
jpayne@69 562 #if _MSC_VER && _MSC_VER < 1920 && !defined(__clang__)
jpayne@69 563 template <typename T>
jpayne@69 564 __declspec(selectany) const HeapDisposer<T> HeapDisposer<T>::instance = HeapDisposer<T>();
jpayne@69 565 // On MSVC 2017 we suddenly started seeing a linker error on one specific specialization of
jpayne@69 566 // `HeapDisposer::instance` when seemingly-unrelated code was modified. Explicitly specifying
jpayne@69 567 // `__declspec(selectany)` seems to fix it. But why? Shouldn't template members have `selectany`
jpayne@69 568 // behavior by default? We don't know. It works and we're moving on.
jpayne@69 569 #else
jpayne@69 570 template <typename T>
jpayne@69 571 const HeapDisposer<T> HeapDisposer<T>::instance = HeapDisposer<T>();
jpayne@69 572 #endif
jpayne@69 573
jpayne@69 574 #if KJ_CPP_STD >= 202002L
jpayne@69 575 template <typename T, void(*F)(T*)>
jpayne@69 576 class CustomDisposer: public Disposer {
jpayne@69 577 public:
jpayne@69 578 void disposeImpl(void* pointer) const override {
jpayne@69 579 (*F)(reinterpret_cast<T*>(pointer));
jpayne@69 580 }
jpayne@69 581 };
jpayne@69 582
jpayne@69 583 template <typename T, void(*F)(T*)>
jpayne@69 584 static constexpr CustomDisposer<T, F> CUSTOM_DISPOSER_INSTANCE {};
jpayne@69 585 #else
jpayne@69 586 template <typename T, void(*F)(T*)>
jpayne@69 587 class CustomDisposer: public Disposer {
jpayne@69 588 public:
jpayne@69 589 static const CustomDisposer instance;
jpayne@69 590
jpayne@69 591 void disposeImpl(void* pointer) const override {
jpayne@69 592 (*F)(reinterpret_cast<T*>(pointer));
jpayne@69 593 }
jpayne@69 594 };
jpayne@69 595
jpayne@69 596 template <typename T, void(*F)(T*)>
jpayne@69 597 const CustomDisposer<T, F> CustomDisposer<T, F>::instance = CustomDisposer<T, F>();
jpayne@69 598 #endif
jpayne@69 599
jpayne@69 600 } // namespace _ (private)
jpayne@69 601
jpayne@69 602 template <typename T, typename... Params>
jpayne@69 603 Own<T> heap(Params&&... params) {
jpayne@69 604 // heap<T>(...) allocates a T on the heap, forwarding the parameters to its constructor. The
jpayne@69 605 // exact heap implementation is unspecified -- for now it is operator new, but you should not
jpayne@69 606 // assume this. (Since we know the object size at delete time, we could actually implement an
jpayne@69 607 // allocator that is more efficient than operator new.)
jpayne@69 608
jpayne@69 609 return Own<T>(new T(kj::fwd<Params>(params)...), _::HeapDisposer<T>::instance);
jpayne@69 610 }
jpayne@69 611
jpayne@69 612 template <typename T>
jpayne@69 613 Own<Decay<T>> heap(T&& orig) {
jpayne@69 614 // Allocate a copy (or move) of the argument on the heap.
jpayne@69 615 //
jpayne@69 616 // The purpose of this overload is to allow you to omit the template parameter as there is only
jpayne@69 617 // one argument and the purpose is to copy it.
jpayne@69 618
jpayne@69 619 typedef Decay<T> T2;
jpayne@69 620 return Own<T2>(new T2(kj::fwd<T>(orig)), _::HeapDisposer<T2>::instance);
jpayne@69 621 }
jpayne@69 622
jpayne@69 623 #if KJ_CPP_STD > 201402L
jpayne@69 624 #if KJ_CPP_STD < 202002L
jpayne@69 625 template <auto F, typename T>
jpayne@69 626 Own<T> disposeWith(T* ptr) {
jpayne@69 627 // Associate a pre-allocated raw pointer with a corresponding disposal function.
jpayne@69 628 // The first template parameter should be a function pointer e.g. disposeWith<freeInt>(new int(0)).
jpayne@69 629
jpayne@69 630 return Own<T>(ptr, _::CustomDisposer<T, F>::instance);
jpayne@69 631 }
jpayne@69 632 #else
jpayne@69 633 template <auto F, typename T>
jpayne@69 634 Own<T> disposeWith(T* ptr) {
jpayne@69 635 // Associate a pre-allocated raw pointer with a corresponding disposal function.
jpayne@69 636 // The first template parameter should be a function pointer e.g. disposeWith<freeInt>(new int(0)).
jpayne@69 637
jpayne@69 638 return Own<T>(ptr, _::CUSTOM_DISPOSER_INSTANCE<T, F>);
jpayne@69 639 }
jpayne@69 640 #endif
jpayne@69 641 #endif
jpayne@69 642
jpayne@69 643 template <typename T, typename... Attachments>
jpayne@69 644 Own<Decay<T>> attachVal(T&& value, Attachments&&... attachments);
jpayne@69 645 // Returns an Own<T> that takes ownership of `value` and `attachments`, and points to `value`.
jpayne@69 646 //
jpayne@69 647 // This is equivalent to heap(value).attach(attachments), but only does one allocation rather than
jpayne@69 648 // two.
jpayne@69 649
jpayne@69 650 template <typename T, typename... Attachments>
jpayne@69 651 Own<T> attachRef(T& value, Attachments&&... attachments);
jpayne@69 652 // Like attach() but `value` is not moved; the resulting Own<T> points to its existing location.
jpayne@69 653 // This is preferred if `value` is already owned by one of `attachments`.
jpayne@69 654 //
jpayne@69 655 // This is equivalent to Own<T>(&value, kj::NullDisposer::instance).attach(attachments), but
jpayne@69 656 // is easier to write and allocates slightly less memory.
jpayne@69 657
jpayne@69 658 // =======================================================================================
jpayne@69 659 // SpaceFor<T> -- assists in manual allocation
jpayne@69 660
jpayne@69 661 template <typename T>
jpayne@69 662 class SpaceFor {
jpayne@69 663 // A class which has the same size and alignment as T but does not call its constructor or
jpayne@69 664 // destructor automatically. Instead, call construct() to construct a T in the space, which
jpayne@69 665 // returns an Own<T> which will take care of calling T's destructor later.
jpayne@69 666
jpayne@69 667 public:
jpayne@69 668 inline SpaceFor() {}
jpayne@69 669 inline ~SpaceFor() {}
jpayne@69 670
jpayne@69 671 template <typename... Params>
jpayne@69 672 Own<T> construct(Params&&... params) {
jpayne@69 673 ctor(value, kj::fwd<Params>(params)...);
jpayne@69 674 return Own<T>(&value, DestructorOnlyDisposer<T>::instance);
jpayne@69 675 }
jpayne@69 676
jpayne@69 677 private:
jpayne@69 678 union {
jpayne@69 679 T value;
jpayne@69 680 };
jpayne@69 681 };
jpayne@69 682
jpayne@69 683 // =======================================================================================
jpayne@69 684 // Inline implementation details
jpayne@69 685
jpayne@69 686 template <typename T>
jpayne@69 687 struct Disposer::Dispose_<T, true> {
jpayne@69 688 static void dispose(T* object, const Disposer& disposer) {
jpayne@69 689 // Note that dynamic_cast<void*> does not require RTTI to be enabled, because the offset to
jpayne@69 690 // the top of the object is in the vtable -- as it obviously needs to be to correctly implement
jpayne@69 691 // operator delete.
jpayne@69 692 disposer.disposeImpl(dynamic_cast<void*>(object));
jpayne@69 693 }
jpayne@69 694 };
jpayne@69 695 template <typename T>
jpayne@69 696 struct Disposer::Dispose_<T, false> {
jpayne@69 697 static void dispose(T* object, const Disposer& disposer) {
jpayne@69 698 disposer.disposeImpl(static_cast<void*>(object));
jpayne@69 699 }
jpayne@69 700 };
jpayne@69 701
jpayne@69 702 template <typename T>
jpayne@69 703 void Disposer::dispose(T* object) const {
jpayne@69 704 Dispose_<T>::dispose(object, *this);
jpayne@69 705 }
jpayne@69 706
jpayne@69 707 namespace _ { // private
jpayne@69 708
jpayne@69 709 template <typename... T>
jpayne@69 710 struct OwnedBundle;
jpayne@69 711
jpayne@69 712 template <>
jpayne@69 713 struct OwnedBundle<> {};
jpayne@69 714
jpayne@69 715 template <typename First, typename... Rest>
jpayne@69 716 struct OwnedBundle<First, Rest...>: public OwnedBundle<Rest...> {
jpayne@69 717 OwnedBundle(First&& first, Rest&&... rest)
jpayne@69 718 : OwnedBundle<Rest...>(kj::fwd<Rest>(rest)...), first(kj::fwd<First>(first)) {}
jpayne@69 719
jpayne@69 720 // Note that it's intentional that `first` is destroyed before `rest`. This way, doing
jpayne@69 721 // ptr.attach(foo, bar, baz) is equivalent to ptr.attach(foo).attach(bar).attach(baz) in terms
jpayne@69 722 // of destruction order (although the former does fewer allocations).
jpayne@69 723 Decay<First> first;
jpayne@69 724 };
jpayne@69 725
jpayne@69 726 template <typename... T>
jpayne@69 727 struct DisposableOwnedBundle final: public Disposer, public OwnedBundle<T...> {
jpayne@69 728 DisposableOwnedBundle(T&&... values): OwnedBundle<T...>(kj::fwd<T>(values)...) {}
jpayne@69 729 void disposeImpl(void* pointer) const override { delete this; }
jpayne@69 730 };
jpayne@69 731
jpayne@69 732 template <typename T, typename StaticDisposer>
jpayne@69 733 class StaticDisposerAdapter final: public Disposer {
jpayne@69 734 // Adapts a static disposer to be called dynamically.
jpayne@69 735 public:
jpayne@69 736 virtual void disposeImpl(void* pointer) const override {
jpayne@69 737 StaticDisposer::dispose(reinterpret_cast<T*>(pointer));
jpayne@69 738 }
jpayne@69 739
jpayne@69 740 static const StaticDisposerAdapter instance;
jpayne@69 741 };
jpayne@69 742
jpayne@69 743 template <typename T, typename D>
jpayne@69 744 const StaticDisposerAdapter<T, D> StaticDisposerAdapter<T, D>::instance =
jpayne@69 745 StaticDisposerAdapter<T, D>();
jpayne@69 746
jpayne@69 747 } // namespace _ (private)
jpayne@69 748
jpayne@69 749 template <typename T>
jpayne@69 750 template <typename... Attachments>
jpayne@69 751 Own<T> Own<T>::attach(Attachments&&... attachments) {
jpayne@69 752 T* ptrCopy = ptr;
jpayne@69 753
jpayne@69 754 KJ_IREQUIRE(ptrCopy != nullptr, "cannot attach to null pointer");
jpayne@69 755
jpayne@69 756 // HACK: If someone accidentally calls .attach() on a null pointer in opt mode, try our best to
jpayne@69 757 // accomplish reasonable behavior: We turn the pointer non-null but still invalid, so that the
jpayne@69 758 // disposer will still be called when the pointer goes out of scope.
jpayne@69 759 if (ptrCopy == nullptr) ptrCopy = reinterpret_cast<T*>(1);
jpayne@69 760
jpayne@69 761 auto bundle = new _::DisposableOwnedBundle<Own<T>, Attachments...>(
jpayne@69 762 kj::mv(*this), kj::fwd<Attachments>(attachments)...);
jpayne@69 763 return Own<T>(ptrCopy, *bundle);
jpayne@69 764 }
jpayne@69 765
jpayne@69 766 template <typename T, typename... Attachments>
jpayne@69 767 Own<T> attachRef(T& value, Attachments&&... attachments) {
jpayne@69 768 auto bundle = new _::DisposableOwnedBundle<Attachments...>(kj::fwd<Attachments>(attachments)...);
jpayne@69 769 return Own<T>(&value, *bundle);
jpayne@69 770 }
jpayne@69 771
jpayne@69 772 template <typename T, typename... Attachments>
jpayne@69 773 Own<Decay<T>> attachVal(T&& value, Attachments&&... attachments) {
jpayne@69 774 auto bundle = new _::DisposableOwnedBundle<T, Attachments...>(
jpayne@69 775 kj::fwd<T>(value), kj::fwd<Attachments>(attachments)...);
jpayne@69 776 return Own<Decay<T>>(&bundle->first, *bundle);
jpayne@69 777 }
jpayne@69 778
jpayne@69 779 template <typename T>
jpayne@69 780 template <typename U, typename StaticDisposer, typename>
jpayne@69 781 inline Own<T>::Own(Own<U, StaticDisposer>&& other) noexcept
jpayne@69 782 : ptr(cast(other.ptr)) {
jpayne@69 783 if (_::castToVoid(other.ptr) != reinterpret_cast<void*>(other.ptr)) {
jpayne@69 784 // Oh dangit, there's some sort of multiple inheritance going on and `StaticDisposerAdapter`
jpayne@69 785 // won't actually work because it'll receive a pointer pointing to the top of the object, which
jpayne@69 786 // isn't exactly the same as the `U*` pointer it wants. We have no choice but to allocate
jpayne@69 787 // a dynamic disposer here.
jpayne@69 788 disposer = new _::DisposableOwnedBundle<Own<U, StaticDisposer>>(kj::mv(other));
jpayne@69 789 } else {
jpayne@69 790 disposer = &_::StaticDisposerAdapter<U, StaticDisposer>::instance;
jpayne@69 791 other.ptr = nullptr;
jpayne@69 792 }
jpayne@69 793 }
jpayne@69 794
jpayne@69 795 } // namespace kj
jpayne@69 796
jpayne@69 797 KJ_END_HEADER