Skip to content

ObjCBool: Add boolValue property #1223

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Sep 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions Foundation/FileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ open class FileManager : NSObject {
} else if let attr = attributes {
try self.setAttributes(attr, ofItemAtPath: path)
}
} else if isDir {
} else if isDir.boolValue {
return
} else {
throw _NSErrorWithErrno(EEXIST, reading: false, path: path)
Expand Down Expand Up @@ -406,9 +406,9 @@ open class FileManager : NSObject {
}

open func linkItem(atPath srcPath: String, toPath dstPath: String) throws {
var isDir = false
var isDir: ObjCBool = false
if self.fileExists(atPath: srcPath, isDirectory: &isDir) {
if !isDir {
if !isDir.boolValue {
// TODO: Symlinks should be copied instead of hard-linked.
if link(srcPath, dstPath) == -1 {
throw _NSErrorWithErrno(errno, reading: false, path: srcPath)
Expand Down Expand Up @@ -532,12 +532,13 @@ open class FileManager : NSObject {
if let isDirectory = isDirectory {
if (s.st_mode & S_IFMT) == S_IFLNK {
if stat(path, &s) >= 0 {
isDirectory.pointee = (s.st_mode & S_IFMT) == S_IFDIR
isDirectory.pointee = ObjCBool((s.st_mode & S_IFMT) == S_IFDIR)
} else {
return false
}
} else {
isDirectory.pointee = (s.st_mode & S_IFMT) == S_IFDIR
let isDir = (s.st_mode & S_IFMT) == S_IFDIR
isDirectory.pointee = ObjCBool(isDir)
}
}

Expand Down
8 changes: 4 additions & 4 deletions Foundation/NSAttributedString.swift
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ open class NSAttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCo
attributesInRange = attributes(at: currentIndex, longestEffectiveRange: &attributesEffectiveRange, in: enumerationRange)
}

var shouldStop = false
var shouldStop: ObjCBool = false
block(attributesInRange, attributesEffectiveRange, &shouldStop)
stop.pointee = shouldStop

Expand All @@ -133,7 +133,7 @@ open class NSAttributedString: NSObject, NSCopying, NSMutableCopying, NSSecureCo
attributeInRange = attribute(attrName, at: currentIndex, longestEffectiveRange: &attributeEffectiveRange, in: enumerationRange)
}

var shouldStop = false
var shouldStop: ObjCBool = false
block(attributeInRange, attributeEffectiveRange, &shouldStop)
stop.pointee = shouldStop

Expand Down Expand Up @@ -232,10 +232,10 @@ private extension NSAttributedString {
func _enumerate(in enumerationRange: NSRange, reversed: Bool, using block: (Int, UnsafeMutablePointer<ObjCBool>) -> NSRange) {
var attributeEnumerationRange = AttributeEnumerationRange(range: enumerationRange, reversed: reversed)
while attributeEnumerationRange.hasMore {
var stop = false
var stop: ObjCBool = false
let effectiveRange = block(attributeEnumerationRange.currentIndex, &stop)
attributeEnumerationRange.advance(step: effectiveRange.length)
if stop {
if stop.boolValue {
break
}
}
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSDictionary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -467,11 +467,11 @@ open class NSDictionary : NSObject, NSCopying, NSMutableCopying, NSSecureCoding,
lock.lock()
var stop = sharedStop
lock.unlock()
if stop { return }
if stop.boolValue { return }

closure(keys[idx], objects[idx], &stop)

if stop {
if stop.boolValue {
lock.lock()
sharedStop = stop
lock.unlock()
Expand Down
12 changes: 6 additions & 6 deletions Foundation/NSIndexSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -396,9 +396,9 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
let iteration = withoutActuallyEscaping(block) { (closure: @escaping (P, UnsafeMutablePointer<ObjCBool>) -> R) -> (Int) -> Void in
return { (rangeIdx) in
lock.lock()
var stop = sharedStop
var stop = ObjCBool(sharedStop)
lock.unlock()
if stop { return }
if stop.boolValue { return }

let idx = rangeSequence.index(rangeSequence.startIndex, offsetBy: Int64(rangeIdx))
let curRange = rangeSequence[idx]
Expand All @@ -407,9 +407,9 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
if intersection.length > 0 {
let _ = closure(intersection as! P, &stop)
}
if stop {
if stop.boolValue {
lock.lock()
sharedStop = stop
sharedStop = stop.boolValue
lock.unlock()
return
}
Expand All @@ -426,9 +426,9 @@ open class NSIndexSet : NSObject, NSCopying, NSMutableCopying, NSSecureCoding {
} else {
let _ = closure(idx as! P, &stop)
}
if stop {
if stop.boolValue {
lock.lock()
sharedStop = stop
sharedStop = stop.boolValue
lock.unlock()
return
}
Expand Down
4 changes: 2 additions & 2 deletions Foundation/NSPathUtilities.swift
Original file line number Diff line number Diff line change
Expand Up @@ -408,9 +408,9 @@ public extension NSString {
return false
}

var isDirectory = false
var isDirectory: ObjCBool = false
let exists = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
return exists && isDirectory
return exists && isDirectory.boolValue
}

internal typealias _FileNamePredicate = (String?) -> Bool
Expand Down
2 changes: 1 addition & 1 deletion Foundation/NSSet.swift
Original file line number Diff line number Diff line change
Expand Up @@ -255,7 +255,7 @@ extension NSSet {
withUnsafeMutablePointer(to: &stop) { stop in
block(obj, stop)
}
if stop {
if stop.boolValue {
break
}
}
Expand Down
58 changes: 57 additions & 1 deletion Foundation/NSSwiftRuntime.swift
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,63 @@ import CoreFoundation
}
#endif

public typealias ObjCBool = Bool
/// The Objective-C BOOL type.
///
/// On 64-bit iOS, the Objective-C BOOL type is a typedef of C/C++
/// bool. Elsewhere, it is "signed char". The Clang importer imports it as
/// ObjCBool.
@_fixed_layout
public struct ObjCBool : ExpressibleByBooleanLiteral {
#if os(OSX) || (os(iOS) && (arch(i386) || arch(arm)))
// On OS X and 32-bit iOS, Objective-C's BOOL type is a "signed char".
var _value: Int8

init(_ value: Int8) {
self._value = value
}

public init(_ value: Bool) {
self._value = value ? 1 : 0
}

#else
// Everywhere else it is C/C++'s "Bool"
var _value: Bool

public init(_ value: Bool) {
self._value = value
}
#endif

/// The value of `self`, expressed as a `Bool`.
public var boolValue: Bool {
#if os(OSX) || (os(iOS) && (arch(i386) || arch(arm)))
return _value != 0
#else
return _value
#endif
}

/// Create an instance initialized to `value`.
@_transparent
public init(booleanLiteral value: Bool) {
self.init(value)
}
}

extension ObjCBool : CustomReflectable {
/// Returns a mirror that reflects `self`.
public var customMirror: Mirror {
return Mirror(reflecting: boolValue)
}
}

extension ObjCBool : CustomStringConvertible {
/// A textual representation of `self`.
public var description: String {
return self.boolValue.description
}
}

internal class __NSCFType : NSObject {
private var _cfinfo : Int32
Expand Down
16 changes: 8 additions & 8 deletions Foundation/NSURL.swift
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {

let thePath = _standardizedPath(path)

var isDir : Bool = false
var isDir: ObjCBool = false
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand why this is necessary... It's only going to get passed to an API which takes bool so can't it stay as bool?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because it calls
let _ = FileManager.default.fileExists(atPath: absolutePath, isDirectory: &isDir)
and thats defined as:
open func fileExists(atPath path: String, isDirectory: UnsafeMutablePointer<ObjCBool>?) -> Bool

and ObjCBool is not a simple Bool anymore.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ohhhh, thanks.

if thePath.hasSuffix("/") {
isDir = true
} else {
Expand All @@ -323,7 +323,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
let _ = FileManager.default.fileExists(atPath: absolutePath, isDirectory: &isDir)
}

self.init(fileURLWithPath: thePath, isDirectory: isDir, relativeTo: baseURL)
self.init(fileURLWithPath: thePath, isDirectory: isDir.boolValue, relativeTo: baseURL)
}

public convenience init(fileURLWithPath path: String, isDirectory isDir: Bool) {
Expand All @@ -339,7 +339,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
thePath = path
}

var isDir : Bool = false
var isDir: ObjCBool = false
if thePath.hasSuffix("/") {
isDir = true
} else {
Expand All @@ -348,7 +348,7 @@ open class NSURL : NSObject, NSSecureCoding, NSCopying {
}
}
super.init()
_CFURLInitWithFileSystemPathRelativeToBase(_cfObject, thePath._cfObject, kCFURLPOSIXPathStyle, isDir, nil)
_CFURLInitWithFileSystemPathRelativeToBase(_cfObject, thePath._cfObject, kCFURLPOSIXPathStyle, isDir.boolValue, nil)
}

public convenience init(fileURLWithFileSystemRepresentation path: UnsafePointer<Int8>, isDirectory isDir: Bool, relativeTo baseURL: URL?) {
Expand Down Expand Up @@ -779,8 +779,8 @@ extension NSURL {
var result : URL? = appendingPathComponent(pathComponent, isDirectory: false)
if !pathComponent.hasSuffix("/") && isFileURL {
if let urlWithoutDirectory = result {
var isDir : Bool = false
if FileManager.default.fileExists(atPath: urlWithoutDirectory.path, isDirectory: &isDir) && isDir {
var isDir: ObjCBool = false
if FileManager.default.fileExists(atPath: urlWithoutDirectory.path, isDirectory: &isDir) && isDir.boolValue {
result = self.appendingPathComponent(pathComponent, isDirectory: true)
}
}
Expand Down Expand Up @@ -859,14 +859,14 @@ extension NSURL {
}

// It might be a responsibility of NSURL(fileURLWithPath:). Check it.
var isExistingDirectory = false
var isExistingDirectory: ObjCBool = false
let _ = FileManager.default.fileExists(atPath: resolvedPath, isDirectory: &isExistingDirectory)

if excludeSystemDirs {
resolvedPath = resolvedPath._tryToRemovePathPrefix("/private") ?? resolvedPath
}

if isExistingDirectory && !resolvedPath.hasSuffix("/") {
if isExistingDirectory.boolValue && !resolvedPath.hasSuffix("/") {
resolvedPath += "/"
}

Expand Down
12 changes: 6 additions & 6 deletions TestFoundation/TestFileManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ class TestFileManager : XCTestCase {
// Ensure attempting to create the directory again fails gracefully.
XCTAssertNil(try? fm.createDirectory(atPath: path, withIntermediateDirectories:false, attributes:nil))

var isDir = false
var isDir: ObjCBool = false
let exists = fm.fileExists(atPath: path, isDirectory: &isDir)
XCTAssertTrue(exists)
XCTAssertTrue(isDir)
XCTAssertTrue(isDir.boolValue)

do {
try fm.removeItem(atPath: path)
Expand All @@ -74,10 +74,10 @@ class TestFileManager : XCTestCase {

XCTAssertTrue(fm.createFile(atPath: path, contents: Data(), attributes: nil))

var isDir = false
var isDir: ObjCBool = false
let exists = fm.fileExists(atPath: path, isDirectory: &isDir)
XCTAssertTrue(exists)
XCTAssertFalse(isDir)
XCTAssertFalse(isDir.boolValue)

do {
try fm.removeItem(atPath: path)
Expand Down Expand Up @@ -467,9 +467,9 @@ class TestFileManager : XCTestCase {
}

func directoryExists(atPath path: String) -> Bool {
var isDir = false
var isDir: ObjCBool = false
let exists = fm.fileExists(atPath: path, isDirectory: &isDir)
return exists && isDir
return exists && isDir.boolValue
}

func createDirectory(atPath path: String) {
Expand Down
2 changes: 1 addition & 1 deletion TestFoundation/TestHTTPCookieStorage.swift
Original file line number Diff line number Diff line change
Expand Up @@ -238,7 +238,7 @@ class TestHTTPCookieStorage: XCTestCase {
destPath = NSHomeDirectory() + "/.local/share" + bundleName + "/.cookies.shared"
}
let fm = FileManager.default
var isDir = false
var isDir: ObjCBool = false
let exists = fm.fileExists(atPath: destPath, isDirectory: &isDir)
XCTAssertTrue(exists)
//Test by setting the environmental variable
Expand Down
2 changes: 1 addition & 1 deletion TestFoundation/TestUtils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func ensureFiles(_ fileNames: [String]) -> Bool {
print(err)
return false
}
} else if !isDir {
} else if !isDir.boolValue {
return false
}

Expand Down
2 changes: 1 addition & 1 deletion TestFoundation/XDGTestHelper.swift
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class XDGCheck {
storage.setCookie(simpleCookie!)
let fm = FileManager.default
let destPath = xdg_data_home! + "/xdgTestHelper/.cookies.shared"
var isDir = false
var isDir: ObjCBool = false
let exists = fm.fileExists(atPath: destPath, isDirectory: &isDir)
if (!exists) {
print("Expected cookie path: ", destPath)
Expand Down