12
12
13
13
import SwiftShims
14
14
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
16
17
/// instance creation.
17
18
///
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
+
24
55
/// The actual number of elements that can be stored in this object.
25
56
///
26
57
/// This header may be nontrivial to compute; it is usually a good
@@ -64,56 +95,12 @@ public class ManagedProtoBuffer<Header, Element> {
64
95
return try ManagedBufferPointer ( self ) . withUnsafeMutablePointers ( body)
65
96
}
66
97
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
-
116
98
/// 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.
117
104
public final var header : Header {
118
105
addressWithNativeOwner {
119
106
return (
@@ -128,6 +115,13 @@ public class ManagedBuffer<Header, Element>
128
115
Builtin . castToNativeObject ( self ) )
129
116
}
130
117
}
118
+
119
+ //===--- internal/private API -------------------------------------------===//
120
+
121
+ /// Make ordinary initialization unavailable
122
+ internal init ( _doNotCallMe: ( ) ) {
123
+ _sanityCheckFailure ( " Only initialize these by calling create " )
124
+ }
131
125
}
132
126
133
127
/// Contains a buffer object, and provides access to an instance of
@@ -348,7 +342,7 @@ public struct ManagedBufferPointer<Header, Element> : Equatable {
348
342
///
349
343
/// - Note: It is an error to use the `header` property of the resulting
350
344
/// instance unless it has been initialized.
351
- internal init ( _ buffer: ManagedProtoBuffer < Header , Element > ) {
345
+ internal init ( _ buffer: ManagedBuffer < Header , Element > ) {
352
346
_nativeBuffer = Builtin . castToNativeObject ( buffer)
353
347
}
354
348
0 commit comments