Resource(3I)

πŸ’– Resource(3I) πŸ’–

okay bestie, pull up a chair, we're talking about Resource(3I) from the InterViews Reference Manual β€” yes, that's the page name, she's got a whole manual named after her, main character energy fr fr πŸŽ€


✨ NAME ✨

Resource β€” a shared object, and honestly? iconic for it πŸ’…


🌸 SYNOPSIS 🌸

#include <InterViews/resource.h>

πŸ¦‹ DESCRIPTION πŸ¦‹

okay so Resource is a class, and what she does is give you a simple form of reference counting for shared objects. like, she's keeping receipts on how many people are holding onto a thing πŸ“

every resource has a reference count. it starts out at zero β€” like, no cap, zero, nobody's claimed her yet β€” and you have to explicitly bump it up by calling Reference. that's not optional, bestie, you gotta do it yourself.

and here's the thing that trips people up: a resource should not be deleted like other objects. nope. πŸ™… instead, you use the static member function unref to unreference the object. that's the whole vibe.

when a resource gets unreferenced, the reference count goes down by one β€” unless it's already zero, in which case, nothing happens, she stays at zero, we don't do negative counts here. and if the count is zero? boom. πŸ’€ the object is destroyed.


πŸ‘‘ PUBLIC OPERATIONS πŸ‘‘

  • Resource() β€” creates a brand new resource object, and her reference count is set to zero. baby's first resource, she's fresh out the box 🎁
  • void ref() β€” increments the resource's reference count. bump it up, she's getting attention ✨
  • void unref() β€” decrements the resource's reference count, but only if it's positive, and then deletes this if the count is zero. so like, she deletes herself when nobody's left holding her 😭
  • static void ref(Resource*) β€” if the resource isn't nil, call ref on it. if it is nil, do absolutely nothing. minding her own business, periodt 🫢
  • static void unref(Resource*) β€” if the resource isn't nil, call unref on it. if it is nil, do nothing. same energy, different function πŸ’œ
  • void Reference() β€” this one is literally equivalent to ref. she's only here for backward compatibility, and heads up: she will be removed in a future version. so like, don't get attached πŸ™ˆ
  • void Unreference() β€” equivalent to unref, also just here for backward compatibility, and also will be removed in a future version. legacy queens, we love them, but they're on the way out πŸ’€

🧚 FRIEND OPERATIONS 🧚

  • void Unref(Resource*) β€” equivalent to static unref, and she's provided solely for backward compatibility. yep. will be removed in a future version. the friend who's moving away next semester 🌈

⚠️ BUGS ⚠️

okay this is the part where we get serious for a sec.

this whole scheme relies on manual referencing. which is more efficient β€” slay, faster, we love that for her β€” but it's also more dangerous. you're doing it by hand, bestie, nobody's catching you if you fumble it 😭

also, when garbage collection is supported by the runtime system, resources will be unnecessary. so like, one day she might not even need to exist. until then though, she's here and she's doing her job πŸ’–


and that's the whole page, bestie!! πŸŽ€βœ¨ every last detail, no skips, no cap. this was Resource(3I) from the InterViews Reference Manual β€” take care of your reference counts out there, and don't forget to unref your faves before you go πŸ¦‹πŸ’œ