forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFoundationMirrors.swift.gyb
156 lines (120 loc) · 4.54 KB
/
FoundationMirrors.swift.gyb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
//===----------------------------------------------------------*- 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
//
//===----------------------------------------------------------------------===//
%import gyb
%TMirrorDecl = gyb.parseTemplate("../../common/MirrorDecl.gyb")
%TMirrorConformance = gyb.parseTemplate("../../common/MirrorConformance.gyb")
%TMirrorBoilerplate = gyb.parseTemplate("../../common/MirrorBoilerplate.gyb")
// helper functions - these Mirrors are dissimilar enough that it's
// probably not worth trying to write one unique generator - let's
// just use these helpers manually and write the bulk of each one
%{
def getMirrorConformance(Self,Disp = None):
return gyb.executeTemplate(
TMirrorConformance,introspecteeType=Self,disposition=Disp)
def getMirrorBoilerplate(Self,Disp = None):
return gyb.executeTemplate(
TMirrorBoilerplate,introspecteeType=Self,disposition=Disp)
def getMirrorDecl(Self,Disp = None):
return gyb.executeTemplate(TMirrorDecl,introspecteeType=Self,disposition=Disp)
}%
// actual Mirrors
${getMirrorDecl("NSURL")} {
${getMirrorBoilerplate("NSURL")}
var count: Int { get { return 0 } }
subscript(_: Int) -> (String, _MirrorType) {
_preconditionFailure("_MirrorType access out of bounds")
}
var summary: String { get { return _value.absoluteString } }
var quickLookObject: PlaygroundQuickLook? { return .Some(.URL(summary)) }
}
${getMirrorDecl("NSRange")} {
${getMirrorBoilerplate("NSRange")}
var count: Int { get { return 2 } }
subscript(i: Int) -> (String, _MirrorType) {
switch i {
case 0: return ("location", _reflect(_value.location))
case 1: return ("length", _reflect(_value.length))
default: _preconditionFailure("_MirrorType access out of bounds")
}
}
var summary: String { return "(\(_value.location),\(_value.length))" }
var quickLookObject: PlaygroundQuickLook? {
return .Some(.Range(Int64(_value.location),Int64(_value.length)))
}
}
${getMirrorDecl("NSDate")} {
${getMirrorBoilerplate("NSDate")}
var count: Int { get { return 0 } }
subscript(i: Int) -> (String, _MirrorType) {
_preconditionFailure("_MirrorType access out of bounds")
}
var summary: String {
let df = NSDateFormatter()
df.dateStyle = .MediumStyle
df.timeStyle = .ShortStyle
return df.stringFromDate(_value)
}
var quickLookObject: PlaygroundQuickLook? { return .Some(.Text(summary)) }
}
${getMirrorDecl("NSSet","MembershipContainer")} {
var _a : NSArray!
var _value: NSSet
init(_ x: NSSet) {
_value = x
_a = _value.allObjects as NSArray
}
var disposition: _MirrorDisposition { return .MembershipContainer }
var value: Any { return _value }
var valueType: Any.Type { return (_value as Any).dynamicType }
var objectIdentifier: ObjectIdentifier? { return .None }
// this is the only member that needs to validate _a - others either don't touch it or call into this
var count: Int {
if _a != nil {
return _a.count
}
return 0
}
subscript(i: Int) -> (String, _MirrorType) {
_precondition(i >= 0 && i < count, "_MirrorType access out of bounds")
return ("[\(i)]", _reflect(_a[i]))
}
var summary: String { return "\(count) elements" }
var quickLookObject: PlaygroundQuickLook? { return nil }
}
${getMirrorDecl("NSString")} {
${getMirrorBoilerplate("NSString")}
var count: Int { get { return 0 } }
subscript(_: Int) -> (String, _MirrorType) {
_preconditionFailure("_MirrorType access out of bounds")
}
var summary: String { get { return _value as String } }
var quickLookObject: PlaygroundQuickLook? { return .Some(.Text(summary)) }
}
// conformances
${getMirrorConformance("NSURL")}
${getMirrorConformance("NSRange")}
${getMirrorConformance("NSDate")}
${getMirrorConformance("NSSet","MembershipContainer")}
${getMirrorConformance("NSString")}
extension NSArray : _Reflectable {
/// Returns a mirror that reflects `self`.
public func _getMirror() -> _MirrorType {
return _reflect(self as [AnyObject])
}
}
extension NSDictionary : _Reflectable {
/// Returns a mirror that reflects `self`.
public func _getMirror() -> _MirrorType {
let dict: [NSObject : AnyObject] = _convertNSDictionaryToDictionary(self)
return _reflect(dict)
}
}