mirror of
https://github.com/apple/swift.git
synced 2025-12-21 12:14:44 +01:00
toString(x) => String(x) toDebugString(x) => String(reflecting: x) Printable => CustomStringConvertible DebugPrintable => CustomDebugStringConvertible Also updated comments to clarify these protocols Swift SVN r27090
51 lines
1.4 KiB
Swift
51 lines
1.4 KiB
Swift
//===--- 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
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
var trackedCount = 0
|
|
var nextTrackedSerialNumber = 0
|
|
|
|
public final class LifetimeTracked : ForwardIndexType, CustomStringConvertible {
|
|
public init(_ value: Int) {
|
|
++trackedCount
|
|
serialNumber = ++nextTrackedSerialNumber
|
|
self.value = value
|
|
}
|
|
|
|
deinit {
|
|
assert(serialNumber > 0, "double destruction!")
|
|
--trackedCount
|
|
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 class var instances: Int {
|
|
return trackedCount
|
|
}
|
|
|
|
public let value: Int
|
|
public var serialNumber: Int = 0
|
|
}
|
|
|
|
public func == (x: LifetimeTracked, y: LifetimeTracked) -> Bool {
|
|
return x.value == y.value
|
|
}
|