1212
1313import SwiftShims
1414
15- /// A base class of `ManagedBuffer<Header, Element>`, used during
15+ /// A class whose instances contain a property of type `Header` and raw
16+ /// storage for an array of `Element`, whose size is determined at
1617/// instance creation.
1718///
18- /// During instance creation, in particular during
19- /// `ManagedBuffer.create`'s call to initialize, `ManagedBuffer`'s
20- /// `header` property is as-yet uninitialized, and therefore
21- /// `ManagedProtoBuffer` does not offer access to the as-yet
22- /// uninitialized `header` property of `ManagedBuffer`.
23- public class ManagedProtoBuffer < Header, Element> {
19+ /// Note that the `Element` array is suitably-aligned **raw memory**.
20+ /// You are expected to construct and---if necessary---destroy objects
21+ /// there yourself, using the APIs on `UnsafeMutablePointer<Element>`.
22+ /// Typical usage stores a count and capacity in `Header` and destroys
23+ /// any live elements in the `deinit` of a subclass.
24+ /// - Note: Subclasses must not have any stored properties; any storage
25+ /// needed should be included in `Header`.
26+ public class ManagedBuffer < Header, Element> {
27+
28+ /// Create a new instance of the most-derived class, calling
29+ /// `factory` on the partially-constructed object to generate
30+ /// an initial `Header`.
31+ public final class func create(
32+ minimumCapacity: Int ,
33+ makingHeaderWith factory: (
34+ ManagedBuffer < Header , Element > ) throws -> Header
35+ ) rethrows -> ManagedBuffer < Header , Element > {
36+
37+ let p = try ManagedBufferPointer < Header , Element > (
38+ bufferClass: self ,
39+ minimumCapacity: minimumCapacity,
40+ makingHeaderWith: { buffer, _ in
41+ try factory (
42+ unsafeDowncast ( buffer, to: ManagedBuffer< Header, Element> . self ) )
43+ } )
44+
45+ return unsafeDowncast ( p. buffer, to: ManagedBuffer< Header, Element> . self )
46+ }
47+
48+ /// Destroy the stored Header.
49+ deinit {
50+ ManagedBufferPointer ( self ) . withUnsafeMutablePointerToHeader {
51+ _ = $0. deinitialize ( )
52+ }
53+ }
54+
2455 /// The actual number of elements that can be stored in this object.
2556 ///
2657 /// This header may be nontrivial to compute; it is usually a good
@@ -64,56 +95,12 @@ public class ManagedProtoBuffer<Header, Element> {
6495 return try ManagedBufferPointer ( self ) . withUnsafeMutablePointers ( body)
6596 }
6697
67- //===--- internal/private API -------------------------------------------===//
68-
69- /// Make ordinary initialization unavailable
70- internal init ( _doNotCallMe: ( ) ) {
71- _sanityCheckFailure ( " Only initialize these by calling create " )
72- }
73- }
74-
75- /// A class whose instances contain a property of type `Header` and raw
76- /// storage for an array of `Element`, whose size is determined at
77- /// instance creation.
78- ///
79- /// Note that the `Element` array is suitably-aligned **raw memory**.
80- /// You are expected to construct and---if necessary---destroy objects
81- /// there yourself, using the APIs on `UnsafeMutablePointer<Element>`.
82- /// Typical usage stores a count and capacity in `Header` and destroys
83- /// any live elements in the `deinit` of a subclass.
84- /// - Note: Subclasses must not have any stored properties; any storage
85- /// needed should be included in `Header`.
86- public class ManagedBuffer < Header, Element>
87- : ManagedProtoBuffer < Header , Element > {
88-
89- /// Create a new instance of the most-derived class, calling
90- /// `factory` on the partially-constructed object to generate
91- /// an initial `Header`.
92- public final class func create(
93- minimumCapacity: Int ,
94- makingHeaderWith factory: (
95- ManagedProtoBuffer < Header , Element > ) throws -> Header
96- ) rethrows -> ManagedBuffer < Header , Element > {
97-
98- let p = try ManagedBufferPointer < Header , Element > (
99- bufferClass: self ,
100- minimumCapacity: minimumCapacity,
101- makingHeaderWith: { buffer, _ in
102- try factory (
103- unsafeDowncast ( buffer, to: ManagedProtoBuffer< Header, Element> . self ) )
104- } )
105-
106- return unsafeDowncast ( p. buffer, to: ManagedBuffer< Header, Element> . self )
107- }
108-
109- /// Destroy the stored Header.
110- deinit {
111- ManagedBufferPointer ( self ) . withUnsafeMutablePointerToHeader {
112- _ = $0. deinitialize ( )
113- }
114- }
115-
11698 /// The stored `Header` instance.
99+ ///
100+ /// During instance creation, in particular during
101+ /// `ManagedBuffer.create`'s call to initialize, `ManagedBuffer`'s
102+ /// `header` property is as-yet uninitialized, and therefore
103+ /// reading the `header` property during `ManagedBuffer.create` is undefined.
117104 public final var header : Header {
118105 addressWithNativeOwner {
119106 return (
@@ -128,6 +115,13 @@ public class ManagedBuffer<Header, Element>
128115 Builtin . castToNativeObject ( self ) )
129116 }
130117 }
118+
119+ //===--- internal/private API -------------------------------------------===//
120+
121+ /// Make ordinary initialization unavailable
122+ internal init ( _doNotCallMe: ( ) ) {
123+ _sanityCheckFailure ( " Only initialize these by calling create " )
124+ }
131125}
132126
133127/// Contains a buffer object, and provides access to an instance of
@@ -348,7 +342,7 @@ public struct ManagedBufferPointer<Header, Element> : Equatable {
348342 ///
349343 /// - Note: It is an error to use the `header` property of the resulting
350344 /// instance unless it has been initialized.
351- internal init ( _ buffer: ManagedProtoBuffer < Header , Element > ) {
345+ internal init ( _ buffer: ManagedBuffer < Header , Element > ) {
352346 _nativeBuffer = Builtin . castToNativeObject ( buffer)
353347 }
354348
0 commit comments