]>
Commit | Line | Data |
---|---|---|
931d7698 DE |
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 | #if wxUSE_OBJC_UNIQUIFYING | |
19 | ||
20 | // objc_getClass and stuff | |
21 | #include <objc/objc-runtime.h> | |
22 | ||
23 | ////////////// Objective-C uniquifying implementation ////////////// | |
24 | ||
25 | template <typename ObjcType> | |
26 | class wxObjcClassInitializer; | |
27 | ||
28 | template <typename ObjcType> | |
29 | class UniquifiedName; | |
30 | ||
31 | template <typename ObjcType> | |
32 | class wxObjcCompilerInformation | |
33 | { | |
34 | friend class wxObjcClassInitializer<ObjcType>; | |
35 | friend class UniquifiedName<ObjcType>; | |
36 | private: | |
37 | // GetCompiledClass must be partially specialized for an ObjcType | |
38 | // If you're not using it, implement an inline returning NULL | |
39 | inline static struct objc_class * GetCompiledClass(); | |
40 | ||
41 | // sm_theClassName must be partially specialized for each type | |
42 | static const char sm_theClassName[]; | |
43 | ||
44 | // GetSuperclass must be specialized. Typically one of two ways: | |
45 | // 1. objc_getClass("SomeRealClassName") | |
46 | // 2. wxGetObjcClass_SomeWxClassName(); | |
47 | inline static struct objc_class *GetSuperclass(); | |
48 | }; | |
49 | ||
50 | ||
51 | template <typename ObjcType> | |
52 | struct UniquifiedName | |
53 | { | |
54 | // We're going for OriginalClassName@ClassStructureAddress | |
55 | // Therefore our size is the sizeof the original class name constant string (which includes the terminating NULL) | |
56 | // plus the sizeof a pointer to struct objc_class times two (two hex digits for each byte) plus 3 for "@0x" | |
57 | typedef char Type[sizeof(wxObjcCompilerInformation<ObjcType>::sm_theClassName) + (sizeof(struct objc_class*)<<1) + 3]; | |
58 | static void Init(Type m_theString, const objc_class *aClass) | |
59 | { | |
60 | snprintf(const_cast<char*>(m_theString), sizeof(Type), "%s@%p", wxObjcCompilerInformation<ObjcType>::sm_theClassName, aClass); | |
61 | } | |
62 | }; | |
63 | ||
64 | template <typename ObjcType> | |
65 | class wxObjcClassInitializer | |
66 | { | |
67 | public: | |
68 | static struct objc_class* Get() | |
69 | { | |
70 | static wxObjcClassInitializer<ObjcType> s_theInstance; | |
71 | s_theInstance.noop(); // Make the compiler think we need this instance | |
72 | return wxObjcCompilerInformation<ObjcType>::GetCompiledClass(); | |
73 | } | |
74 | private: | |
75 | void noop() | |
76 | {} | |
77 | // This "constructor" operates solely on static data | |
78 | // It exists so that we can take advantage of a function-static | |
79 | // "instance" of this class to do the static data initialization. | |
80 | wxObjcClassInitializer() | |
81 | { | |
82 | // Objective-C class initialization occurs before C++ static initialization because the | |
83 | // libobjc.dylib gets notified directly by dyld on Tiger. | |
84 | // Therefore, even though we change the name, the class is still registered with the | |
85 | // original name. We unfortunately can't change that. | |
86 | ||
87 | // The first time the class is loaded, Objective-C will already have fixed up the super_class | |
88 | // and isa->isa and isa->super_class variables so much of this won't do anything. But | |
89 | // the next time the class is loaded, Objective-C will ignore it and thus we need to | |
90 | // initialize the data structures appropriately. | |
91 | ||
92 | // Ideally we'd have some sort of lock here, but we depend on the fact that we get called | |
93 | // just before the first time someone wants to send a class message so it should be | |
94 | // reasonably safe to do this without any locks. | |
95 | ||
96 | struct objc_class &theClassData = *wxObjcCompilerInformation<ObjcType>::GetCompiledClass(); | |
97 | // Initialize the uniquified class name | |
98 | UniquifiedName<ObjcType>::Init(sm_theUniquifiedClassName, &theClassData); | |
99 | ||
100 | //////// Class Initialization //////// | |
101 | // Use objc_getClass to fix up the superclass pointer | |
102 | theClassData.super_class = wxObjcCompilerInformation<ObjcType>::GetSuperclass(); | |
103 | // Fix up the compiler generated class struct to use the new name | |
104 | theClassData.name = sm_theUniquifiedClassName; | |
105 | ||
106 | //////// Meta-Class Initialization //////// | |
107 | // theClassData.isa is the metaclass pointer | |
108 | // Globals on Darwin use PC-relative access (slow) so it's quicker to use theClassData.isa | |
109 | ||
110 | // In any object hierarchy a metaclass's metaclass is always the root class's metaclass | |
111 | // Therefore, our superclass's metaclass's metaclass should already be the root class's metaclass | |
112 | theClassData.isa->isa = theClassData.super_class->isa->isa; | |
113 | // A metaclass's superclass is always the superclass's metaclass. | |
114 | theClassData.isa->super_class = theClassData.super_class->isa; | |
115 | // Fix up the compiler generated metaclass struct to use the new name | |
116 | theClassData.isa->name = sm_theUniquifiedClassName; | |
117 | ||
118 | // We need to set the initialized flag because after we change the name, Objective-C can't | |
119 | // look us up by name because we're only registered with the original name. | |
120 | theClassData.isa->info |= CLS_INITIALIZED; | |
121 | } | |
122 | wxObjcClassInitializer(const wxObjcClassInitializer&); // NO COPY | |
123 | wxObjcClassInitializer& operator =(const wxObjcClassInitializer&); // NO ASSIGN | |
124 | static typename UniquifiedName<ObjcType>::Type sm_theUniquifiedClassName; | |
125 | }; | |
126 | ||
127 | template<typename ObjcType> | |
128 | typename UniquifiedName<ObjcType>::Type wxObjcClassInitializer<ObjcType>::sm_theUniquifiedClassName; | |
129 | ||
130 | // WX_DECLARE_GET_OBJC_CLASS | |
131 | // Declares a function to get a direct pointer to an objective-C class. | |
132 | // The class is guaranteed to be usable. | |
133 | // When wxCocoa is built into a Mach-O bundle this function allows the wxCocoa | |
134 | // code to get a reference to the Objective-C class structure located in the | |
135 | // same bundle. This allows a static wxCocoa library to be built into | |
136 | // two different Mach-O bundles without having one bundle's Objective-C | |
137 | // classes trample on the other's. | |
138 | // Right now we toss the ObjcSuperClass parameter, but we might use it later. | |
139 | #define WX_DECLARE_GET_OBJC_CLASS(ObjcClass,ObjcSuperClass) \ | |
140 | struct objc_class* wx_GetObjcClass_ ## ObjcClass(); | |
141 | ||
142 | // WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) | |
143 | // Provides an architecture-dependent way to get the direct pointer to the | |
144 | // objc_class structure in the __OBJC segment. | |
145 | // This takes advantage of the fact that the Objective-C compiler uses guessable | |
146 | // local assembler labels for the class structures. | |
147 | // Those class structures are only available on the Objective-C file containing the | |
148 | // @implementation block. | |
149 | ||
150 | #if 1 | |
151 | // Generic implementation - Tested on i386 and PPC. Should work in all cases. | |
152 | // This is a hack that depends on GCC asm symbol names. | |
153 | // The static variable winds up being initialized with a direct reference to the appropriate | |
154 | // L_OBJC_CLASS and no global symbol reference is generated because nothing uses the global symbol | |
155 | // except for the static initializer which does it directly. | |
156 | // The generated assembler for s_objc_class_ptr is basically like this: | |
157 | // _s_objc_class_ptr_ObjcClass: | |
158 | // .long L_OBJC_CLASS_ObjcClass | |
159 | // Once that static symbol is defined, the function implementation is easy for GCC to generate. | |
160 | // Do note that return &s_objc_class_data_ObjcClass won't work. The code is wrong in the case. | |
161 | #define WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \ | |
162 | extern "C" objc_class s_objc_class_data_ ## ObjcClass asm("L_OBJC_CLASS_" #ObjcClass); \ | |
163 | static objc_class * s_objc_class_ptr_ ## ObjcClass = &s_objc_class_data_ ## ObjcClass; \ | |
164 | template<> \ | |
165 | inline objc_class * wxObjcCompilerInformation<ObjcClass>::GetCompiledClass() \ | |
166 | { \ | |
167 | return s_objc_class_ptr_## ObjcClass; \ | |
168 | } | |
169 | ||
170 | #elif defined(__i386__) | |
171 | // Not used because the generic implementation seems to work fine. | |
172 | // But this is here since it was written beforehand and it also works. | |
173 | ||
174 | // This is based on the code GCC generates for accessing file-static data on i386. | |
175 | // The i386 PC-relative addressing happens in this manner | |
176 | // 1. The program counter is placed into ecx using the code that GCC should have | |
177 | // already generated. | |
178 | // 2. A label is placed directly after the call to get the program counter. | |
179 | // 3. The Load Effective Address instruction is used to add the offset of the | |
180 | // local assembler label we're interested in minus the local assembler label | |
181 | // from step 2 to the program counter register in ecx and place the result | |
182 | // into the result register (typically eax if not inlined). | |
183 | #define WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \ | |
184 | template<> \ | |
185 | inline objc_class * wxObjcCompilerInformation<ObjcClass>::GetCompiledClass() \ | |
186 | { \ | |
187 | register struct objc_class *retval; \ | |
188 | asm \ | |
189 | ( "call ___i686.get_pc_thunk.cx\n" \ | |
190 | "\"LPC_FOR_GET_CLASS_" #ObjcClass "\":\n\t" \ | |
191 | "leal L_OBJC_CLASS_" #ObjcClass "-\"LPC_FOR_GET_CLASS_" #ObjcClass "\"(%%ecx), %0" \ | |
192 | : "=r"(retval) \ | |
193 | : \ | |
194 | : "ecx" \ | |
195 | ); \ | |
196 | return retval; \ | |
197 | } | |
198 | ||
199 | #elif defined(__ppc__) | |
200 | // Not used because the generic implementation seems to work fine. | |
201 | // But this is here since it was written beforehand and it also works. | |
202 | ||
203 | // This is based on the code GCC generates for accessing file-static data on PPC. | |
204 | // The PowerPC PC-relative addressing happens in this manner | |
205 | // 1. The link register is saved (mflr) to a temporary (we re-use the output register for this) | |
206 | // 2. An unconditional branch instruction (bcl) "branches" to the following address (labeled) | |
207 | // 3. The link register (filled in by bcl) is saved to r10 (a temporary) | |
208 | // 4. The previous link register is restored (mtlr) (from the output register we were using as a temporary) | |
209 | // 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 | |
210 | // and stored in a temporary register (r2) | |
211 | // 6. That temporary register plus the low 16 bits of the offset are stored into the result register. | |
212 | #define WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \ | |
213 | template<> \ | |
214 | inline objc_class * wxObjcCompilerInformation<ObjcClass>::GetCompiledClass() \ | |
215 | { \ | |
216 | register struct objc_class *retval; \ | |
217 | asm \ | |
218 | ( "mflr %0" \ | |
219 | "\n\tbcl 20, 31, \"LPC_FOR_GET_CLASS_" #ObjcClass "\"" \ | |
220 | "\n\"LPC_FOR_GET_CLASS_" #ObjcClass "\":" \ | |
221 | "\n\tmflr r10" \ | |
222 | "\n\tmtlr %0" \ | |
223 | "\n\taddis r2,r10,ha16(L_OBJC_CLASS_" #ObjcClass "-\"LPC_FOR_GET_CLASS_" #ObjcClass "\")" \ | |
224 | "\n\tla %0,lo16(L_OBJC_CLASS_" #ObjcClass "-\"LPC_FOR_GET_CLASS_" #ObjcClass "\")(r2)" \ | |
225 | : "=r" (retval) \ | |
226 | : \ | |
227 | : "r10","r2" \ | |
228 | ); \ | |
229 | return retval; \ | |
230 | } | |
231 | ||
232 | // TODO: __x86_64__, __ppc64__ | |
233 | #else // Can't wrie inline asm to bust into __OBJC segment | |
234 | // This won't be used since the generic implementation takes precedence. | |
235 | ||
236 | #warning "Don't know how to implement wxObjcCompilerInformation<ObjcClass>::GetCompiledClass on this platform" | |
237 | ||
238 | #endif // platforms | |
239 | ||
240 | // The WX_IMPLEMENT_OBJC_GET_SUPERCLASS macro implements the template specialization | |
241 | // to get the superclass. This only works if it's a real superclass. If you are | |
242 | // deriving from a class that's already being uniquified then you'd need to | |
243 | // implement the specialization to call the appropriate get method instead. | |
244 | #define WX_IMPLEMENT_OBJC_GET_SUPERCLASS(ObjcClass,ObjcSuperClass) \ | |
245 | template <> \ | |
246 | inline objc_class* wxObjcCompilerInformation<ObjcClass>::GetSuperclass() \ | |
247 | { \ | |
248 | return objc_getClass(#ObjcSuperClass); \ | |
249 | } | |
250 | ||
251 | // The WX_IMPLEMENT_OBJC_CLASS_NAME macro implements the template specialization | |
252 | // of the sm_theClassName constant. As soon as this specialization is in place | |
253 | // sizeof(sm_theClassName) will return the number of bytes at compile time. | |
254 | #define WX_IMPLEMENT_OBJC_CLASS_NAME(ObjcClass) \ | |
255 | template <> \ | |
256 | const char wxObjcCompilerInformation<ObjcClass>::sm_theClassName[] = #ObjcClass; | |
257 | ||
258 | // The WX_IMPLEMENT_GET_OBJC_CLASS macro combines all of these together and adds | |
259 | // a global wx_GetObjcClass_ObjcClass() function. | |
260 | #define WX_IMPLEMENT_GET_OBJC_CLASS(ObjcClass,ObjcSuperClass) \ | |
261 | WX_IMPLEMENT_OBJC_GET_COMPILED_CLASS(ObjcClass) \ | |
262 | WX_IMPLEMENT_OBJC_GET_SUPERCLASS(ObjcClass,ObjcSuperClass) \ | |
263 | WX_IMPLEMENT_OBJC_CLASS_NAME(ObjcClass) \ | |
264 | objc_class* wx_GetObjcClass_ ## ObjcClass() \ | |
265 | { \ | |
266 | return wxObjcClassInitializer<ObjcClass>::Get(); \ | |
267 | } | |
268 | ||
269 | // The WX_GET_OBJC_CLASS macro is intended to wrap the class name when the class | |
270 | // is used as a message receiver (e.g. for calling class methods). When | |
271 | // class name uniquifying is used, this calls the global function implemented | |
272 | // in the Objective-C file containing the class @implementation. | |
273 | #define WX_GET_OBJC_CLASS(ObjcClass) wx_GetObjcClass_ ## ObjcClass() | |
274 | ||
275 | #else // wxUSE_OBJC_UNIQUIFYING | |
276 | ||
277 | // Define WX_DECLARE_GET_OBJC_CLASS as nothing | |
278 | #define WX_DECLARE_GET_OBJC_CLASS(ObjcClass,ObjcSuperClass) | |
279 | // Define WX_IMPLEMENT_GET_OBJC_CLASS as nothing | |
280 | #define WX_IMPLEMENT_GET_OBJC_CLASS(ObjcClass,ObjcSuperClass) | |
281 | ||
282 | // Define WX_GET_OBJC_CLASS macro to output the class name and let the compiler do the normal thing | |
283 | // The WX_GET_OBJC_CLASS macro is intended to wrap the class name when the class | |
284 | // is used as a message receiver (e.g. for calling class methods). When | |
285 | // class name uniquifying is not used, this is simply defined to be the class | |
286 | // name which will allow the compiler to do the normal thing. | |
287 | #define WX_GET_OBJC_CLASS(ObjcClass) ObjcClass | |
288 | ||
289 | #endif // wxUSE_OBJC_UNIQUIFYING | |
290 | ||
291 | #endif //ndef __WX_COCOA_OBJC_CLASS_H__ |