ManagedBufferPointer

struct ManagedBufferPointer

Contains a buffer object, and provides access to an instance of Header and contiguous storage for an arbitrary number of Element instances stored in that buffer.

Inheritance Equatable

For most purposes, the ManagedBuffer class works fine for this purpose, and can simply be used on its own. However, in cases where objects of various different classes must serve as storage, ManagedBufferPointer is needed.

A valid buffer class is non-@objc, with no declared stored properties. Its deinit must destroy its stored Header and any constructed Elements.

Example Buffer Class

 class MyBuffer<Element> { // non-@objc
   typealias Manager = ManagedBufferPointer<(Int, String), Element>
   deinit {
     Manager(unsafeBufferObject: self).withUnsafeMutablePointers {
       (pointerToHeader, pointerToElements) -> Void in
       pointerToElements.deinitialize(count: self.count)
       pointerToHeader.deinitialize(count: 1)
     }
   }

   // All properties are *computed* based on members of the Header
   var count: Int {
     return Manager(unsafeBufferObject: self).header.0
   }
   var name: String {
     return Manager(unsafeBufferObject: self).header.1
   }
 }

Initializers

init init(bufferClass:minimumCapacity:makingHeaderWith:) Required

Create with new storage containing an initial Header and space for at least minimumCapacity elements.

  • parameter bufferClass: The class of the object used for storage.
  • parameter minimumCapacity: The minimum number of Elements that must be able to be stored in the new buffer.
  • parameter factory: A function that produces the initial Header instance stored in the buffer, given the buffer object and a function that can be called on it to get the actual number of allocated elements.

Precondition: minimumCapacity >= 0, and the type indicated by bufferClass is a non-@objc class with no declared stored properties. The deinit of bufferClass must destroy its stored Header and any constructed Elements.

Declaration

@inlinable public init(bufferClass: AnyClass, minimumCapacity: Int, makingHeaderWith factory: (AnyObject, (AnyObject) -> Int) throws -> Header) rethrows
init init(unsafeBufferObject:) Required

Manage the given buffer.

Precondition: buffer is an instance of a non-@objc class whose deinit destroys its stored Header and any constructed Elements.

Declaration

@inlinable public init(unsafeBufferObject buffer: AnyObject)

Instance Variables

var buffer Required

Returns the object instance being used for storage.

Declaration

var buffer: AnyObject
var capacity Required

The actual number of elements that can be stored in this object.

This value may be nontrivial to compute; it is usually a good idea to store this information in the "header" area when an instance is created.

Declaration

var capacity: Int
var header Required

The stored Header instance.

Declaration

var header: Header

Instance Methods

func isUniqueReference() -> Bool Required

Returns true if self holds the only strong reference to its buffer. Otherwise, returns false.

See isKnownUniquelyReferenced for details.

Declaration

@inlinable public mutating func isUniqueReference() -> Bool
func withUnsafeMutablePointerToElements(_ body: (UnsafeMutablePointer<Element>) throws -> R) rethrows -> R Required

Call body with an UnsafeMutablePointer to the Element storage.

Note: This pointer is valid only for the duration of the call to body.

Declaration

@inlinable public func withUnsafeMutablePointerToElements<R>(_ body: (UnsafeMutablePointer<Element>) throws -> R) rethrows -> R
func withUnsafeMutablePointerToHeader(_ body: (UnsafeMutablePointer<Header>) throws -> R) rethrows -> R Required

Call body with an UnsafeMutablePointer to the stored Header.

Note: This pointer is valid only for the duration of the call to body.

Declaration

@inlinable public func withUnsafeMutablePointerToHeader<R>(_ body: (UnsafeMutablePointer<Header>) throws -> R) rethrows -> R
func withUnsafeMutablePointers(_ body: (UnsafeMutablePointer<Header>, UnsafeMutablePointer<Element>) throws -> R) rethrows -> R Required

Call body with UnsafeMutablePointers to the stored Header and raw Element storage.

Note: These pointers are valid only for the duration of the call to body.

Declaration

@inlinable public func withUnsafeMutablePointers<R>(_ body: (UnsafeMutablePointer<Header>, UnsafeMutablePointer<Element>) throws -> R) rethrows -> R

Type Methods

func !=(lhs: Self, rhs: Self) -> Bool Required

Declaration

public static func !=(lhs: Self, rhs: Self) -> Bool
func ==(lhs: ManagedBufferPointer<Header, Element>, rhs: ManagedBufferPointer<Header, Element>) -> Bool Required

Returns a Boolean value indicating whether two values are equal.

Equality is the inverse of inequality. For any values a and b, a == b implies that a != b is false.

Declaration

@inlinable public static func ==(lhs: ManagedBufferPointer<Header, Element>, rhs: ManagedBufferPointer<Header, Element>) -> Bool