]> git.saurik.com Git - wxWidgets.git/blob - include/wx/cocoa/objc/objc_uniquifying.h
wxOSX/Carbon compilation fix after wxListBoxBase changes.
[wxWidgets.git] / include / wx / cocoa / objc / objc_uniquifying.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/cocoa/objc/objc_uniquifying.h
3 // Purpose: Allows wxWidgets code to get a direct pointer to a compiled
4 // Objective-C class and provides a method to fix up the
5 // name to include a unique identifier (currently the address
6 // of the objc_class structure).
7 // Author: David Elliott <dfe@cox.net>
8 // Modified by:
9 // Created: 2007/05/15
10 // RCS-ID: $Id$
11 // Copyright: (c) 2007 Software 2000 Ltd.
12 // Licence: wxWindows licence
13 /////////////////////////////////////////////////////////////////////////////
14
15 #ifndef __WX_COCOA_OBJC_CLASS_H__
16 #define __WX_COCOA_OBJC_CLASS_H__
17
18 /* A note about this header:
19 Nothing in here is guaranteed to exist in future versions of wxCocoa. There
20 are other ways of adding Objective-C classes at runtime and a future wxCocoa
21 might use these instead of this ugly hack. You may use this header file in
22 your own wxCocoa code if you need your own Objective-C classes to be
23 unqiuified.
24
25 You cannot turn this on for 64-bit mode. It will not compile due to opaque
26 Objective-C data structures and it is not needed because it is a workaround
27 for a bug that does not exist in the 64-bit runtime.
28
29 You should not use this when wxCocoa is built as a dynamic library. This has
30 only been tested for the case when wxCocoa is built as a static library and
31 statically linked to user code to form a loadable bundle (e.g. a Cocoa plugin).
32 It forces each plugin (when multiple wxCocoa-using plugins are used) to use
33 its own internal Objective-C classes which is desirable when wxCocoa is
34 statically linked to the rest of the code.
35
36 Do not use uniquifying on your principal class. That one should be named
37 differently for different bundles.
38 */
39
40 #if wxUSE_OBJC_UNIQUIFYING
41
42 // objc_getClass and stuff
43 #include <objc/objc-runtime.h>
44
45 ////////////// Objective-C uniquifying implementation //////////////
46
47 template <typename ObjcType>
48 class wxObjcClassInitializer;
49
50 template <typename ObjcType>
51 class UniquifiedName;
52
53 template <typename ObjcType>
54 class wxObjcCompilerInformation
55 {
56 friend class wxObjcClassInitializer<ObjcType>;
57 friend class UniquifiedName<ObjcType>;
58 private:
59 // GetCompiledClass must be partially specialized for an ObjcType
60 // If you're not using it, implement an inline returning NULL
61 inline static struct objc_class * GetCompiledClass();
62
63 // sm_theClassName must be partially specialized for each type
64 static const char sm_theClassName[];
65
66 // GetSuperclass must be specialized. Typically one of two ways:
67 // 1. objc_getClass("SomeRealClassName")
68 // 2. wxGetObjcClass_SomeWxClassName();
69 inline static struct objc_class *GetSuperclass();
70 };
71
72
73 template <typename ObjcType>
74 struct UniquifiedName
75 {
76 // We're going for OriginalClassName@ClassStructureAddress
77 // Therefore our size is the sizeof the original class name constant string (which includes the terminating NULL)
78 // plus the sizeof a pointer to struct objc_class times two (two hex digits for each byte) plus 3 for "@0x"
79 typedef char Type[sizeof(wxObjcCompilerInformation<ObjcType>::sm_theClassName) + (sizeof(struct objc_class*)<<1) + 3];
80 static void Init(Type m_theString, const objc_class *aClass)
81 {
82 snprintf(const_cast<char*>(m_theString), sizeof(Type), "%s@%p", wxObjcCompilerInformation<ObjcType>::sm_theClassName, aClass);
83 }
84 };
85
86 /*! @function HidePointerFromGC
87 @abstract Returns an l-value whose location the compiler cannot know.
88 @discussion
89 The compiler-generated Objective-C class structures are located in the static data area.
90 They are by design Objective-C objects in their own right which makes the compiler issue
91 write barriers as if they were located in the GC-managed heap as most Objective-C objects.
92
93 By accepting and returning a reference to any pointer type we can set any i-var of an
94 Objective-C object that is a pointer to another Objective-C object without the compiler
95 generating an objc_assign_ivar write barrier. It will instad generate an
96 objc_assign_strongCast write barrier which is the appropriate write-barrier when assigning
97 pointers to Objective-C objects located in unknown memory.
98
99 For instance:
100 Class *someClass = ...;
101 HidePointerFromGC(someClass->isa) = ...;
102 */
103 template <typename ObjcType>
104 inline ObjcType * & HidePointerFromGC(ObjcType * &p) __attribute__((always_inline));
105
106 template <typename ObjcType>
107 inline ObjcType * & HidePointerFromGC(ObjcType * &p)
108 {
109 return p;
110 }
111
112 template <typename ObjcType>
113 class wxObjcClassInitializer
114 {
115 public:
116 static struct objc_class* Get()
117 {
118 static wxObjcClassInitializer<ObjcType> s_theInstance;
119 s_theInstance.noop(); // Make the compiler think we need this instance
120 return wxObjcCompilerInformation<ObjcType>::GetCompiledClass();
121 }
122 private:
123 void noop()
124 {}
125 // This "constructor" operates solely on static data
126 // It exists so that we can take advantage of a function-static
127 // "instance" of this class to do the static data initialization.
128 wxObjcClassInitializer()
129 {
130 // Objective-C class initialization occurs before C++ static initialization because the
131 // libobjc.dylib gets notified directly by dyld on Tiger.
132 // Therefore, even though we change the name, the class is still registered with the
133 // original name. We unfortunately can't change that.
134
135 // The first time the class is loaded, Objective-C will already have fixed up the super_class
136 // and isa->isa and isa->super_class variables so much of this won't do anything. But
137 // the next time the class is loaded, Objective-C will ignore it and thus we need to
138 // initialize the data structures appropriately.
139
140 // Ideally we'd have some sort of lock here, but we depend on the fact that we get called
141 // just before the first time someone wants to send a class message so it should be
142 // reasonably safe to do this without any locks.
143
144 struct objc_class &theClassData = *wxObjcCompilerInformation<ObjcType>::GetCompiledClass();
145 // Initialize the uniquified class name
146 UniquifiedName<ObjcType>::Init(sm_theUniquifiedClassName, &theClassData);
147
148 //////// Class Initialization ////////
149 // Use objc_getClass to fix up the superclass pointer
150 theClassData.super_class = wxObjcCompilerInformation<ObjcType>::GetSuperclass();
151 // Fix up the compiler generated class struct to use the new name
152 theClassData.name = sm_theUniquifiedClassName;
153
154 //////// Meta-Class Initialization ////////
155 // theClassData.isa is the metaclass pointer
156 // Globals on Darwin use PC-relative access (slow) so it's quicker to use theClassData.isa
157
158 // In any object hierarchy a metaclass's metaclass is always the root class's metaclass
159 // Therefore, our superclass's metaclass's metaclass should already be the root class's metaclass
160 HidePointerFromGC(theClassData.isa->isa) = theClassData.super_class->isa->isa;
161 // A metaclass's superclass is always the superclass's metaclass.
162 HidePointerFromGC(theClassData.isa->super_class) = theClassData.super_class->isa;
163 // Fix up the compiler generated metaclass struct to use the new name
164 theClassData.isa->name = sm_theUniquifiedClassName;
165
166 // We need to set the initialized flag because after we change the name, Objective-C can't
167 // look us up by name because we're only registered with the original name.
168 theClassData.isa->info |= CLS_INITIALIZED;
169 }
170 wxObjcClassInitializer(const wxObjcClassInitializer&); // NO COPY
171 wxObjcClassInitializer& operator =(const wxObjcClassInitializer&); // NO ASSIGN
172 static typename UniquifiedName<ObjcType>::Type sm_theUniquifiedClassName;
173 };
174
175 template<typename ObjcType>
176 typename UniquifiedName<ObjcType>::Type wxObjcClassInitializer<ObjcType>::sm_theUniquifiedClassName;
177
178 // WX_DECLARE_GET_OBJC_CLASS
179 // Declares a function to get a direct pointer to an objective-C class.
180 // The class is guaranteed to be usable.
181 // When wxCocoa is built into a Mach-O bundle this function allows the wxCocoa
182 // code to get a reference to the Objective-C class structure located in the
183 // same bundle. This allows a static wxCocoa library to be built into
184 // two different Mach-O bundles without having one bundle's Objective-C
185 // classes trample on the other's.
186 // Right now we toss the ObjcSuperClass parameter, but we might use it later.
187 #define WX_DECLARE_GET_OBJC_CLASS(ObjcClass,ObjcSuperClass) \
188 struct objc_class* wx_GetObjcClass_ ## ObjcClass();
189
190 // WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass)
191 // Provides an architecture-dependent way to get the direct pointer to the
192 // objc_class structure in the __OBJC segment.
193 // This takes advantage of the fact that the Objective-C compiler uses guessable
194 // local assembler labels for the class structures.
195 // Those class structures are only available on the Objective-C file containing the
196 // @implementation block.
197
198 #if 1
199 // Generic implementation - Tested on i386 and PPC. Should work in all cases.
200 // This is a hack that depends on GCC asm symbol names.
201 // The static variable winds up being initialized with a direct reference to the appropriate
202 // L_OBJC_CLASS and no global symbol reference is generated because nothing uses the global symbol
203 // except for the static initializer which does it directly.
204 // The generated assembler for s_objc_class_ptr is basically like this:
205 // _s_objc_class_ptr_ObjcClass:
206 // .long L_OBJC_CLASS_ObjcClass
207 // Once that static symbol is defined, the function implementation is easy for GCC to generate.
208 // Do note that return &s_objc_class_data_ObjcClass won't work. The code is wrong in the case.
209 #define WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \
210 extern "C" objc_class s_objc_class_data_ ## ObjcClass asm("L_OBJC_CLASS_" #ObjcClass); \
211 static objc_class * s_objc_class_ptr_ ## ObjcClass = &s_objc_class_data_ ## ObjcClass; \
212 template<> \
213 inline objc_class * wxObjcCompilerInformation<ObjcClass>::GetCompiledClass() \
214 { \
215 return s_objc_class_ptr_## ObjcClass; \
216 }
217
218 #elif defined(__i386__)
219 // Not used because the generic implementation seems to work fine.
220 // But this is here since it was written beforehand and it also works.
221
222 // This is based on the code GCC generates for accessing file-static data on i386.
223 // The i386 PC-relative addressing happens in this manner
224 // 1. The program counter is placed into ecx using the code that GCC should have
225 // already generated.
226 // 2. A label is placed directly after the call to get the program counter.
227 // 3. The Load Effective Address instruction is used to add the offset of the
228 // local assembler label we're interested in minus the local assembler label
229 // from step 2 to the program counter register in ecx and place the result
230 // into the result register (typically eax if not inlined).
231 #define WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \
232 template<> \
233 inline objc_class * wxObjcCompilerInformation<ObjcClass>::GetCompiledClass() \
234 { \
235 register struct objc_class *retval; \
236 asm \
237 ( "call ___i686.get_pc_thunk.cx\n" \
238 "\"LPC_FOR_GET_CLASS_" #ObjcClass "\":\n\t" \
239 "leal L_OBJC_CLASS_" #ObjcClass "-\"LPC_FOR_GET_CLASS_" #ObjcClass "\"(%%ecx), %0" \
240 : "=r"(retval) \
241 : \
242 : "ecx" \
243 ); \
244 return retval; \
245 }
246
247 #elif defined(__ppc__)
248 // Not used because the generic implementation seems to work fine.
249 // But this is here since it was written beforehand and it also works.
250
251 // This is based on the code GCC generates for accessing file-static data on PPC.
252 // The PowerPC PC-relative addressing happens in this manner
253 // 1. The link register is saved (mflr) to a temporary (we re-use the output register for this)
254 // 2. An unconditional branch instruction (bcl) "branches" to the following address (labeled)
255 // 3. The link register (filled in by bcl) is saved to r10 (a temporary)
256 // 4. The previous link register is restored (mtlr) (from the output register we were using as a temporary)
257 // 5. The address of the LPC label as executed is added to the high 16 bits of the offset between that label and the static data we want
258 // and stored in a temporary register (r2)
259 // 6. That temporary register plus the low 16 bits of the offset are stored into the result register.
260 #define WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \
261 template<> \
262 inline objc_class * wxObjcCompilerInformation<ObjcClass>::GetCompiledClass() \
263 { \
264 register struct objc_class *retval; \
265 asm \
266 ( "mflr %0" \
267 "\n\tbcl 20, 31, \"LPC_FOR_GET_CLASS_" #ObjcClass "\"" \
268 "\n\"LPC_FOR_GET_CLASS_" #ObjcClass "\":" \
269 "\n\tmflr r10" \
270 "\n\tmtlr %0" \
271 "\n\taddis r2,r10,ha16(L_OBJC_CLASS_" #ObjcClass "-\"LPC_FOR_GET_CLASS_" #ObjcClass "\")" \
272 "\n\tla %0,lo16(L_OBJC_CLASS_" #ObjcClass "-\"LPC_FOR_GET_CLASS_" #ObjcClass "\")(r2)" \
273 : "=r" (retval) \
274 : \
275 : "r10","r2" \
276 ); \
277 return retval; \
278 }
279
280 // TODO: __x86_64__, __ppc64__
281 #else // Can't wrie inline asm to bust into __OBJC segment
282 // This won't be used since the generic implementation takes precedence.
283
284 #warning "Don't know how to implement wxObjcCompilerInformation<ObjcClass>::GetCompiledClass on this platform"
285
286 #endif // platforms
287
288 // The WX_IMPLEMENT_OBJC_GET_SUPERCLASS macro implements the template specialization
289 // to get the superclass. This only works if it's a real superclass. If you are
290 // deriving from a class that's already being uniquified then you'd need to
291 // implement the specialization to call the appropriate get method instead.
292 #define WX_IMPLEMENT_OBJC_GET_SUPERCLASS(ObjcClass,ObjcSuperClass) \
293 template <> \
294 inline objc_class* wxObjcCompilerInformation<ObjcClass>::GetSuperclass() \
295 { \
296 return objc_getClass(#ObjcSuperClass); \
297 }
298
299 // The WX_IMPLEMENT_OBJC_GET_UNIQUIFIED_SUPERCLASS macro implements the template
300 // specialization to get the superclass when the superclass is another uniquified
301 // Objective-C class.
302 #define WX_IMPLEMENT_OBJC_GET_UNIQUIFIED_SUPERCLASS(ObjcClass,ObjcSuperClass) \
303 template <> \
304 inline objc_class* wxObjcCompilerInformation<ObjcClass>::GetSuperclass() \
305 { \
306 return wx_GetObjcClass_ ## ObjcSuperClass(); \
307 }
308
309 // The WX_IMPLEMENT_OBJC_CLASS_NAME macro implements the template specialization
310 // of the sm_theClassName constant. As soon as this specialization is in place
311 // sizeof(sm_theClassName) will return the number of bytes at compile time.
312 #define WX_IMPLEMENT_OBJC_CLASS_NAME(ObjcClass) \
313 template <> \
314 const char wxObjcCompilerInformation<ObjcClass>::sm_theClassName[] = #ObjcClass;
315
316 // The WX_IMPLEMENT_OBJC_GET_OBJC_CLASS macro is the final one that actually provides
317 // the wx_GetObjcClass_XXX function that will be called in lieu of asking the Objective-C
318 // runtime for the class. All the others are really machinery to make this happen.
319 #define WX_IMPLEMENT_OBJC_GET_OBJC_CLASS(ObjcClass) \
320 objc_class* wx_GetObjcClass_ ## ObjcClass() \
321 { \
322 return wxObjcClassInitializer<ObjcClass>::Get(); \
323 }
324
325 // The WX_IMPLEMENT_GET_OBJC_CLASS macro combines all of these together
326 // for the case when the superclass is a non-uniquified class.
327 #define WX_IMPLEMENT_GET_OBJC_CLASS(ObjcClass,ObjcSuperClass) \
328 WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \
329 WX_IMPLEMENT_OBJC_GET_SUPERCLASS(ObjcClass,ObjcSuperClass) \
330 WX_IMPLEMENT_OBJC_CLASS_NAME(ObjcClass) \
331 WX_IMPLEMENT_OBJC_GET_OBJC_CLASS(ObjcClass)
332
333 // The WX_IMPLEMENT_GET_OBJC_CLASS_WITH_UNIQUIFIED_SUPERCLASS macro combines all
334 // of these together for the case when the superclass is another uniquified class.
335 #define WX_IMPLEMENT_GET_OBJC_CLASS_WITH_UNIQUIFIED_SUPERCLASS(ObjcClass,ObjcSuperClass) \
336 WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \
337 WX_IMPLEMENT_OBJC_GET_UNIQUIFIED_SUPERCLASS(ObjcClass,ObjcSuperClass) \
338 WX_IMPLEMENT_OBJC_CLASS_NAME(ObjcClass) \
339 WX_IMPLEMENT_OBJC_GET_OBJC_CLASS(ObjcClass)
340
341 // The WX_GET_OBJC_CLASS macro is intended to wrap the class name when the class
342 // is used as a message receiver (e.g. for calling class methods). When
343 // class name uniquifying is used, this calls the global function implemented
344 // in the Objective-C file containing the class @implementation.
345 #define WX_GET_OBJC_CLASS(ObjcClass) wx_GetObjcClass_ ## ObjcClass()
346
347 #else // wxUSE_OBJC_UNIQUIFYING
348
349 // Define WX_DECLARE_GET_OBJC_CLASS as nothing
350 #define WX_DECLARE_GET_OBJC_CLASS(ObjcClass,ObjcSuperClass)
351 // Define WX_IMPLEMENT_GET_OBJC_CLASS as nothing
352 #define WX_IMPLEMENT_GET_OBJC_CLASS(ObjcClass,ObjcSuperClass)
353 // Define WX_IMPLEMENT_GET_OBJC_CLASS_WITH_UNIQUIFIED_SUPERCLASS as nothing
354 #define WX_IMPLEMENT_GET_OBJC_CLASS_WITH_UNIQUIFIED_SUPERCLASS(ObjcClass,ObjcSuperClass)
355
356 // Define WX_GET_OBJC_CLASS macro to output the class name and let the compiler do the normal thing
357 // The WX_GET_OBJC_CLASS macro is intended to wrap the class name when the class
358 // is used as a message receiver (e.g. for calling class methods). When
359 // class name uniquifying is not used, this is simply defined to be the class
360 // name which will allow the compiler to do the normal thing.
361 #define WX_GET_OBJC_CLASS(ObjcClass) ObjcClass
362
363 #endif // wxUSE_OBJC_UNIQUIFYING
364
365 #endif //ndef __WX_COCOA_OBJC_CLASS_H__