]> git.saurik.com Git - apple/libdispatch.git/blob - src/swift/IO.swift
libdispatch-913.1.6.tar.gz
[apple/libdispatch.git] / src / swift / IO.swift
1 //===----------------------------------------------------------------------===//
2 //
3 // This source file is part of the Swift.org open source project
4 //
5 // Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
6 // Licensed under Apache License v2.0 with Runtime Library Exception
7 //
8 // See http://swift.org/LICENSE.txt for license information
9 // See http://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
10 //
11 //===----------------------------------------------------------------------===//
12
13 import CDispatch
14
15 public extension DispatchIO {
16
17 public enum StreamType : UInt {
18 case stream = 0
19 case random = 1
20 }
21
22 public struct CloseFlags : OptionSet, RawRepresentable {
23 public let rawValue: UInt
24 public init(rawValue: UInt) { self.rawValue = rawValue }
25
26 public static let stop = CloseFlags(rawValue: 1)
27 }
28
29 public struct IntervalFlags : OptionSet, RawRepresentable {
30 public let rawValue: UInt
31 public init(rawValue: UInt) { self.rawValue = rawValue }
32 public init(nilLiteral: ()) { self.rawValue = 0 }
33
34 public static let strictInterval = IntervalFlags(rawValue: 1)
35 }
36
37 public class func read(fromFileDescriptor: Int32, maxLength: Int, runningHandlerOn queue: DispatchQueue, handler: @escaping (_ data: DispatchData, _ error: Int32) -> Void) {
38 dispatch_read(fromFileDescriptor, maxLength, queue.__wrapped) { (data: dispatch_data_t, error: Int32) in
39 handler(DispatchData(borrowedData: data), error)
40 }
41 }
42
43 public class func write(toFileDescriptor: Int32, data: DispatchData, runningHandlerOn queue: DispatchQueue, handler: @escaping (_ data: DispatchData?, _ error: Int32) -> Void) {
44 dispatch_write(toFileDescriptor, data.__wrapped.__wrapped, queue.__wrapped) { (data: dispatch_data_t?, error: Int32) in
45 handler(data.flatMap { DispatchData(borrowedData: $0) }, error)
46 }
47 }
48
49 public convenience init(
50 type: StreamType,
51 fileDescriptor: Int32,
52 queue: DispatchQueue,
53 cleanupHandler: @escaping (_ error: Int32) -> Void)
54 {
55 self.init(__type: type.rawValue, fd: fileDescriptor, queue: queue, handler: cleanupHandler)
56 }
57
58 @available(swift, obsoleted: 4)
59 public convenience init(
60 type: StreamType,
61 path: UnsafePointer<Int8>,
62 oflag: Int32,
63 mode: mode_t,
64 queue: DispatchQueue,
65 cleanupHandler: @escaping (_ error: Int32) -> Void)
66 {
67 self.init(__type: type.rawValue, path: path, oflag: oflag, mode: mode, queue: queue, handler: cleanupHandler)
68 }
69
70 @available(swift, introduced: 4)
71 public convenience init?(
72 type: StreamType,
73 path: UnsafePointer<Int8>,
74 oflag: Int32,
75 mode: mode_t,
76 queue: DispatchQueue,
77 cleanupHandler: @escaping (_ error: Int32) -> Void)
78 {
79 self.init(__type: type.rawValue, path: path, oflag: oflag, mode: mode, queue: queue, handler: cleanupHandler)
80 }
81
82 public convenience init(
83 type: StreamType,
84 io: DispatchIO,
85 queue: DispatchQueue,
86 cleanupHandler: @escaping (_ error: Int32) -> Void)
87 {
88 self.init(__type: type.rawValue, io: io, queue: queue, handler: cleanupHandler)
89 }
90
91 public func read(offset: off_t, length: Int, queue: DispatchQueue, ioHandler: @escaping (_ done: Bool, _ data: DispatchData?, _ error: Int32) -> Void) {
92 dispatch_io_read(self.__wrapped, offset, length, queue.__wrapped) { (done: Bool, data: dispatch_data_t?, error: Int32) in
93 ioHandler(done, data.flatMap { DispatchData(borrowedData: $0) }, error)
94 }
95 }
96
97 public func write(offset: off_t, data: DispatchData, queue: DispatchQueue, ioHandler: @escaping (_ done: Bool, _ data: DispatchData?, _ error: Int32) -> Void) {
98 dispatch_io_write(self.__wrapped, offset, data.__wrapped.__wrapped, queue.__wrapped) { (done: Bool, data: dispatch_data_t?, error: Int32) in
99 ioHandler(done, data.flatMap { DispatchData(borrowedData: $0) }, error)
100 }
101 }
102
103 public func setInterval(interval: DispatchTimeInterval, flags: IntervalFlags = []) {
104 dispatch_io_set_interval(self.__wrapped, UInt64(interval.rawValue), flags.rawValue)
105 }
106
107 public func close(flags: CloseFlags = []) {
108 dispatch_io_close(self.__wrapped, flags.rawValue)
109 }
110 }