]> git.saurik.com Git - apple/libdispatch.git/blob - src/swift/IO.swift
libdispatch-703.1.4.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: (data: DispatchData, error: Int32) -> Void) {
38 dispatch_read(fromFileDescriptor, maxLength, queue.__wrapped) { (data: dispatch_data_t, error: Int32) in
39 handler(data: DispatchData(data: data), error: error)
40 }
41 }
42
43 public class func write(fromFileDescriptor: Int32, data: DispatchData, runningHandlerOn queue: DispatchQueue, handler: (data: DispatchData?, error: Int32) -> Void) {
44 dispatch_write(fromFileDescriptor, data.__wrapped, queue.__wrapped) { (data: dispatch_data_t?, error: Int32) in
45 handler(data: data.flatMap { DispatchData(data: $0) }, error: error)
46 }
47 }
48
49 public convenience init(
50 type: StreamType,
51 fileDescriptor: Int32,
52 queue: DispatchQueue,
53 cleanupHandler: (error: Int32) -> Void)
54 {
55 self.init(__type: type.rawValue, fd: fileDescriptor, queue: queue, handler: cleanupHandler)
56 }
57
58 public convenience init(
59 type: StreamType,
60 path: UnsafePointer<Int8>,
61 oflag: Int32,
62 mode: mode_t,
63 queue: DispatchQueue,
64 cleanupHandler: (error: Int32) -> Void)
65 {
66 self.init(__type: type.rawValue, path: path, oflag: oflag, mode: mode, queue: queue, handler: cleanupHandler)
67 }
68
69 public convenience init(
70 type: StreamType,
71 io: DispatchIO,
72 queue: DispatchQueue,
73 cleanupHandler: (error: Int32) -> Void)
74 {
75 self.init(__type: type.rawValue, io: io, queue: queue, handler: cleanupHandler)
76 }
77
78 public func read(offset: off_t, length: Int, queue: DispatchQueue, ioHandler: (done: Bool, data: DispatchData?, error: Int32) -> Void) {
79 dispatch_io_read(self.__wrapped, offset, length, queue.__wrapped) { (done: Bool, data: dispatch_data_t?, error: Int32) in
80 ioHandler(done: done, data: data.flatMap { DispatchData(data: $0) }, error: error)
81 }
82 }
83
84 public func write(offset: off_t, data: DispatchData, queue: DispatchQueue, ioHandler: (done: Bool, data: DispatchData?, error: Int32) -> Void) {
85 dispatch_io_write(self.__wrapped, offset, data.__wrapped, queue.__wrapped) { (done: Bool, data: dispatch_data_t?, error: Int32) in
86 ioHandler(done: done, data: data.flatMap { DispatchData(data: $0) }, error: error)
87 }
88 }
89
90 public func setInterval(interval: DispatchTimeInterval, flags: IntervalFlags = []) {
91 dispatch_io_set_interval(self.__wrapped, interval.rawValue, flags.rawValue)
92 }
93
94 public func close(flags: CloseFlags = []) {
95 dispatch_io_close(self.__wrapped, flags.rawValue)
96 }
97 }
98
99 extension DispatchIO {
100 @available(*, deprecated, renamed: "DispatchIO.read(fromFileDescriptor:maxLength:runningHandlerOn:handler:)")
101 public class func read(fd: Int32, length: Int, queue: DispatchQueue, handler: (DispatchData, Int32) -> Void) {
102 DispatchIO.read(fromFileDescriptor: fd, maxLength: length, runningHandlerOn: queue, handler: handler)
103 }
104
105 @available(*, deprecated, renamed: "DispatchIO.write(fromFileDescriptor:data:runningHandlerOn:handler:)")
106 public class func write(fd: Int32, data: DispatchData, queue: DispatchQueue, handler: (DispatchData?, Int32) -> Void) {
107 DispatchIO.write(fromFileDescriptor: fd, data: data, runningHandlerOn: queue, handler: handler)
108 }
109
110 @available(*, deprecated, renamed: "DispatchIO.barrier(self:execute:)")
111 public func withBarrier(barrier work: () -> ()) {
112 barrier(execute: work)
113 }
114
115 @available(*, deprecated, renamed: "DispatchIO.setLimit(self:highWater:)")
116 public func setHighWater(highWater: Int) {
117 setLimit(highWater: highWater)
118 }
119
120 @available(*, deprecated, renamed: "DispatchIO.setLimit(self:lowWater:)")
121 public func setLowWater(lowWater: Int) {
122 setLimit(lowWater: lowWater)
123 }
124
125 @available(*, deprecated, renamed: "DispatchIO.setInterval(self:interval:flags:)")
126 public func setInterval(interval: UInt64, flags: IntervalFlags) {
127 setInterval(interval: .nanoseconds(Int(interval)), flags: flags)
128 }
129 }