ManagedBufferPointer

struct ManagedBufferPointer<Value, Element>

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

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 Value and any constructed Elements.

Example Buffer Class

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

   // All properties are *computed* based on members of the Value
   var count: Int {
     return Manager(unsafeBufferObject: self).value.0
   }
   var name: String {
     return Manager(unsafeBufferObject: self).value.1
   }
 }
Inheritance Equatable View Protocol Hierarchy →
Import import Swift

Initializers

init(bufferClass:minimumCapacity:initialValue:)

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

bufferClass: The class of the object used for storage.

minimumCapacity: The minimum number of Elements that must be able to be stored in the new buffer.

initialValue: A function that produces the initial Value 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.

Requires: 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 Value and any constructed Elements.

Declaration

init(bufferClass: AnyClass, minimumCapacity: Int, initialValue: (buffer: AnyObject, allocatedCount: (AnyObject) -> Int) -> Value)
init(unsafeBufferObject:)

Manage the given buffer.

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

Declaration

init(unsafeBufferObject buffer: AnyObject)

Instance Variables

var allocatedElementCount: Int

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 "value" area when an instance is created.

Declaration

var allocatedElementCount: Int { get }
var buffer: AnyObject

Returns the object instance being used for storage.

Declaration

var buffer: AnyObject { get }
var value: Value

The stored Value instance.

Declaration

var value: Value { get set }

Instance Methods

mutating func holdsUniqueOrPinnedReference()

Returns true iff either self holds the only strong reference to its buffer or the pinned has been 'pinned'.

See isUniquelyReferenced for details.

Declaration

mutating func holdsUniqueOrPinnedReference() -> Bool
mutating func holdsUniqueReference()

Returns true iff self holds the only strong reference to its buffer.

See isUniquelyReferenced for details.

Declaration

mutating func holdsUniqueReference() -> Bool
func withUnsafeMutablePointerToElements(_:)

Call body with an UnsafeMutablePointer to the Element storage.

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

Declaration

func withUnsafeMutablePointerToElements<R>(body: (UnsafeMutablePointer<Element>) -> R) -> R
func withUnsafeMutablePointerToValue(_:)

Call body with an UnsafeMutablePointer to the stored Value.

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

Declaration

func withUnsafeMutablePointerToValue<R>(body: (UnsafeMutablePointer<Value>) -> R) -> R
func withUnsafeMutablePointers(_:)

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

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

Declaration

func withUnsafeMutablePointers<R>(body: (UnsafeMutablePointer<Value>, UnsafeMutablePointer<Element>) -> R) -> R