Registry
public class Registry<T>
Registry
is a generic class designed to keep track of items (of type T
)
in a thread-safe way.
The lifetime of an item in the registry is determined by the Receipt
that
was returned when the item was registered.
You must maintain at least one strong reference to the returned Receipt
for
the duration of the time that you want the item it represents to remain
registered. Once the Receipt
deallocates, the associated item is removed
from the registry.
-
The signature of a function to be called when a registrant is being de-registered.
Declaration
Swift
public typealias DeregistrationHookFunction = (_ registry: Registry<T>, _ registrant: T, _ receipt: Receipt) -> Void
-
The number of registered items.
Declaration
Swift
public var count: Int
-
An optional
DeregistrationHookFunction
which, if set, will be called when an item is being de-registered.Declaration
Swift
public var deregistrationHook: DeregistrationHookFunction?
-
Creates a new
Registry
using the specifiedLockMechanism
.Declaration
Swift
public init(lock mechanism: LockMechanism = .readWrite)
Parameters
mechanism
A
LockMechanism
value that governs the type of lock used for protecting concurrent access to the registry. -
Adds an item to the registry.
You must maintain a reference to the returned
Receipt
for as long as you wish to haveitem
registered. When theReceipt
deallocates,item
will be deregistered. You can also force deregistration prior to deallocation by calling theReceipt
‘sderegister()
function.Declaration
Swift
public func register(_ item: T) -> Receipt
Parameters
item
The item to register.
Return Value
A
Receipt
that controls the duration ofitem
’s registration with the receiver. -
The items currently in the
Registry
.Declaration
Swift
public var registrants: [T]
-
Performs the given function once for each registered object.
Declaration
Swift
public func withEachRegistrant(perform function: (T) -> Void)
Parameters
function
A function to perform on each object currently registered with the receiver.