forked from Quick/Quick
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[refactoring] Re-implement qck_enumerateSubclasses in Swift
- Loading branch information
Showing
5 changed files
with
35 additions
and
53 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,39 +1,46 @@ | ||
import Foundation | ||
import XCTest | ||
|
||
// NOTE: This file is not intended to be included in the Xcode project or CocoaPods. | ||
// It is picked up by the Swift Package Manager during its build process. | ||
|
||
#if SWIFT_PACKAGE | ||
|
||
open class QuickConfiguration: NSObject { | ||
open class func configure(_ configuration: Configuration) {} | ||
} | ||
|
||
#endif | ||
|
||
#if canImport(Darwin) | ||
|
||
internal func qck_enumerateSubclasses<T: AnyObject>(_ klass: T.Type, block: (T.Type) -> Void) { | ||
var classesCount = objc_getClassList(nil, 0) | ||
extension QuickConfiguration { | ||
|
||
guard classesCount > 0 else { | ||
return | ||
} | ||
/// Finds all direct subclasses of QuickConfiguration and passes them to the block provided. | ||
/// The classes are iterated over in the order that objc_getClassList returns them. | ||
/// | ||
/// - parameter block: A block that takes a QuickConfiguration.Type. | ||
/// This block will be executed once for each subclass of QuickConfiguration. | ||
@objc static func enumerateSubclasses(_ block: (QuickConfiguration.Type) -> Void) { | ||
var classesCount = objc_getClassList(nil, 0) | ||
|
||
let classes = UnsafeMutablePointer<AnyClass?>.allocate(capacity: Int(classesCount)) | ||
classesCount = objc_getClassList(AutoreleasingUnsafeMutablePointer(classes), classesCount) | ||
guard classesCount > 0 else { | ||
return | ||
} | ||
|
||
var subclass: AnyClass! | ||
for i in 0..<classesCount { | ||
subclass = classes[Int(i)] | ||
let classes = UnsafeMutablePointer<AnyClass?>.allocate(capacity: Int(classesCount)) | ||
defer { free(classes) } | ||
|
||
guard let superclass = class_getSuperclass(subclass), superclass == klass else { continue } | ||
classesCount = objc_getClassList(AutoreleasingUnsafeMutablePointer(classes), classesCount) | ||
|
||
block(subclass as! T.Type) // swiftlint:disable:this force_cast | ||
} | ||
for i in 0..<classesCount { | ||
guard | ||
let subclass = classes[Int(i)], | ||
let superclass = class_getSuperclass(subclass), | ||
superclass == QuickConfiguration.self | ||
else { continue } | ||
|
||
free(classes) | ||
// swiftlint:disable:next force_cast | ||
block(subclass as! QuickConfiguration.Type) | ||
} | ||
} | ||
} | ||
|
||
#endif | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters