class Prism::ReferencePool(T)
- Prism::ReferencePool(T)
- Reference
- Object
Overview
A pool of items which are managed with Prism::ReferenceCounter
s.
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.crConstructors
-
.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.
Instance Method Summary
-
#add(key : String, item : T)
Adds an item to the pool.
-
#has_key?(key : String) : Bool
Checks of the pool contains the item
-
#size : Int32
Returns how many items are in the pool
-
#trash(key : String)
Trashes a reference to an item in the pool.
-
#use(key) : T
Retrieves an item from the pool.
Macro Summary
-
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.
Constructor Detail
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.
Instance Method Detail
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.
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.
Macro Detail
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.