Generic Structure

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.

Declaration

@frozen struct ManagedBufferPointer<Header, Element>

Overview

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
   }
 }

Topics

Creating a Buffer

init(bufferClass: AnyClass, minimumCapacity: Int, makingHeaderWith: (AnyObject, (AnyObject) -> Int) -> Header)

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

init(unsafeBufferObject: AnyObject)

Manage the given buffer.

Inspecting a Buffer

var capacity: Int

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

var header: Header

The stored Header instance.

var buffer: AnyObject

Returns the object instance being used for storage.

func isUniqueReference() -> Bool

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

Accessing Buffer Contents

func withUnsafeMutablePointerToElements<R>((UnsafeMutablePointer<Element>) -> R) -> R

Call body with an UnsafeMutablePointer to the Element storage.

func withUnsafeMutablePointerToHeader<R>((UnsafeMutablePointer<Header>) -> R) -> R

Call body with an UnsafeMutablePointer to the stored Header.

func withUnsafeMutablePointers<R>((UnsafeMutablePointer<Header>, UnsafeMutablePointer<Element>) -> R) -> R

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

Comparing Buffers

static func != (ManagedBufferPointer<Header, Element>, ManagedBufferPointer<Header, Element>) -> Bool

Returns a Boolean value indicating whether two values are not equal.

Operator Functions

Relationships

Conforms To

See Also

Buffer Implementation

class ManagedBuffer

A class whose instances contain a property of type Header and raw storage for an array of Element, whose size is determined at instance creation.