class Prism::ReferencePool(T)

Overview

A pool of items which are managed with Prism::ReferenceCounters. Items in the pool are available for re-use, and items without any references are removed and garbage collected.

You should try to pool mostly raw data. E.g. an OpenGL texture id instead of the whole texture class. Because whatever is using the pooled must be able to implement a finalize method in which it can #trash the pooled item.

Defined in:

prism/reference_pool/reference_pool.cr

Constructors

Instance Method Summary

Macro Summary

Constructor Detail

def self.new(&clean_callback : String, T -> Nil) #

Creates a new pool with a clean_callback that will be called any time an item is orphaned and removed. Use this callback to perform final cleanup operations such as freeing OpenGL resources.


[View source]

Instance Method Detail

def add(key : String, item : T) #

Adds an item to the pool. This will set the reference counter to 0. You must then call #use to get your first reference.

WARNING do not use the item you passed in without calling #use or you will mess up the reference counter.


[View source]
def has_key?(key : String) : Bool #

Checks of the pool contains the item


[View source]
def size : Int32 #

Returns how many items are in the pool


[View source]
def trash(key : String) #

Trashes a reference to an item in the pool. The item will be removed from the pool if there are no more references to it.


[View source]
def use(key) : T #

Retrieves an item from the pool. This will increment the reference counter.


[View source]

Macro Detail

macro create_persistent_pool(type, &block) #

Generates a new typed ReferencePool that is unique to the class (and sub-classes) in which this macro is called. The ReferencePool is wrapped in a singleton so that it persists to all sub-classes. The pool itself will be made available from the class method #pool.

In your finalize method you should trash the pooled item so that it's reference can be recovered.

Example

def finalize
  pool.trash(@pool_key)
end

Where you should be keeping track of the @pool_key yourself.


[View source]