forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLifetimeTracked.swift
49 lines (42 loc) · 1.49 KB
/
LifetimeTracked.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//===--- LifetimeTracked.swift --------------------------------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2015 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See http://swift.org/LICENSE.txt for license information
// See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
public final class LifetimeTracked : ForwardIndexType, CustomStringConvertible {
public init(_ value: Int, identity: Int = 0) {
++LifetimeTracked.instances
serialNumber = ++LifetimeTracked._nextSerialNumber
self.value = value
self.identity = identity
}
deinit {
assert(serialNumber > 0, "double destruction!")
--LifetimeTracked.instances
serialNumber = -serialNumber
}
public var description: String {
assert(serialNumber > 0, "dead Tracked!")
return value.description
}
/// Returns the next consecutive value after `self`.
///
/// Requires: the next value is representable.
public func successor() -> LifetimeTracked {
return LifetimeTracked(self.value.successor())
}
public static var instances: Int = 0
internal static var _nextSerialNumber = 0
public let value: Int
public var identity: Int
public var serialNumber: Int = 0
}
public func == (x: LifetimeTracked, y: LifetimeTracked) -> Bool {
return x.value == y.value
}