comparison dstep/objc/bridge/Capsule.d @ 2:9fd439a28ce3

Adapted the scripts for the new bridge + a lot more
author Jacob Carlborg <doob@me.com>
date Sun, 05 Jul 2009 17:16:19 +0200
parents 033d260cfc9b
children 19885b43130e
comparison
equal deleted inserted replaced
1:033d260cfc9b 2:9fd439a28ce3
22 import std.math : log2; 22 import std.math : log2;
23 } 23 }
24 24
25 import dstep.internal.String; 25 import dstep.internal.String;
26 import dstep.objc.bridge.Bridge; 26 import dstep.objc.bridge.Bridge;
27 import dstep.objc.bridge.Type;
27 import dstep.objc.bridge.TypeEncoding; 28 import dstep.objc.bridge.TypeEncoding;
28 import dstep.objc.bridge.Wrapper; 29 import dstep.objc.bridge.Wrapper;
29 import dstep.objc.message; 30 import dstep.objc.message;
30 import dstep.objc.objc; 31 import dstep.objc.objc;
31 import dstep.objc.runtime; 32 import dstep.objc.runtime;
32 import bindings = dstep.objc.bindings; 33 import bindings = dstep.objc.bindings;
33 34
34 import mambo.io; 35 import mambo.io;
35
36 template ObjcType (T)
37 {
38 static if (needsEncapsulation!(T))
39 alias id ObjcType;
40
41 else
42 alias T ObjcType;
43 }
44 36
45 Class capsuleClass () 37 Class capsuleClass ()
46 { 38 {
47 if (!Capsule.capsuleClass) 39 if (!Capsule.capsuleClass)
48 initCapsuleClass; 40 initCapsuleClass;
117 109
118 Class cls = capsule.isa; 110 Class cls = capsule.isa;
119 return cls.getInstanceVariable!(Bridge.dObjectVar) !is null; 111 return cls.getInstanceVariable!(Bridge.dObjectVar) !is null;
120 } 112 }
121 113
122 template needsEncapsulation (T)
123 {
124 static if (is(T == class))
125 const needsEncapsulation = true;
126
127 else
128 const needsEncapsulation = false;
129 }
130
131 ObjcType!(T) encapsule (T) (T value) 114 ObjcType!(T) encapsule (T) (T value)
132 { 115 {
133 static if (needsEncapsulation!(T)) 116 static if (needsEncapsulation!(T))
134 { 117 {
135 if (!value) 118 if (!value)
136 return null; 119 return null;
137 120
143 126
144 else 127 else
145 return value; 128 return value;
146 } 129 }
147 130
148 template decapsule (T) 131 T decapsule (T) (ObjcType!(T) value)
149 { 132 {
150 T decapsule (ObjcType!(T) value) 133 static if (needsEncapsulation!(T))
151 { 134 {
152 static if (needsEncapsulation!(T)) 135 if (isCapsule(value))
136 return cast(T) Bridge.getDObject(value);
137
138 else
153 { 139 {
154 if (isCapsule(value)) 140 static if (is(T : ObjcWrapper))
155 return cast(T) Bridge.getDObject(value); 141 new T(value);
156 142
157 else 143 else
158 { 144 return null;
159 static if (is(T : ObjcWrapper))
160 new T(value);
161
162 else
163 return null;
164 }
165 } 145 }
166
167 else
168 return value;
169 } 146 }
147
148 else
149 return value;
170 } 150 }
171 151
172 private id encapsuleString (string str) 152 private id encapsuleString (string str)
173 { 153 {
174 enum { NSUTF8StringEncoding = 4 } 154 enum { NSUTF8StringEncoding = 4 }