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 specified LockMechanism.

    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.

  • Creates a new Registry using the specified Lock.

    Declaration

    Swift

    public init(lock: Lock)

    Parameters

    lock

    The Lock instance that will be 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 have item registered. When the Receipt deallocates, item will be deregistered. You can also force deregistration prior to deallocation by calling the Receipt‘s deregister() function.

    Declaration

    Swift

    public func register(_ item: T)
            -> Receipt

    Parameters

    item

    The item to register.

    Return Value

    A Receipt that controls the duration of item’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.