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>
11 // Copyright: (c) 2007 Software 2000 Ltd.
12 // Licence: wxWindows licence
13 /////////////////////////////////////////////////////////////////////////////
15 #ifndef __WX_COCOA_OBJC_CLASS_H__
16 #define __WX_COCOA_OBJC_CLASS_H__
18 #if wxUSE_OBJC_UNIQUIFYING
20 // objc_getClass and stuff
21 #include <objc/objc-runtime.h>
23 ////////////// Objective-C uniquifying implementation //////////////
25 template <typename ObjcType
>
26 class wxObjcClassInitializer
;
28 template <typename ObjcType
>
31 template <typename ObjcType
>
32 class wxObjcCompilerInformation
34 friend class wxObjcClassInitializer
<ObjcType
>;
35 friend class UniquifiedName
<ObjcType
>;
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();
41 // sm_theClassName must be partially specialized for each type
42 static const char sm_theClassName
[];
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();
51 template <typename ObjcType
>
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
)
60 snprintf(const_cast<char*>(m_theString
), sizeof(Type
), "%s@%p", wxObjcCompilerInformation
<ObjcType
>::sm_theClassName
, aClass
);
64 template <typename ObjcType
>
65 class wxObjcClassInitializer
68 static struct objc_class
* Get()
70 static wxObjcClassInitializer
<ObjcType
> s_theInstance
;
71 s_theInstance
.noop(); // Make the compiler think we need this instance
72 return wxObjcCompilerInformation
<ObjcType
>::GetCompiledClass();
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()
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.
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.
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.
96 struct objc_class
&theClassData
= *wxObjcCompilerInformation
<ObjcType
>::GetCompiledClass();
97 // Initialize the uniquified class name
98 UniquifiedName
<ObjcType
>::Init(sm_theUniquifiedClassName
, &theClassData
);
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
;
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
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
;
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
;
122 wxObjcClassInitializer(const wxObjcClassInitializer
&); // NO COPY
123 wxObjcClassInitializer
& operator =(const wxObjcClassInitializer
&); // NO ASSIGN
124 static typename UniquifiedName
<ObjcType
>::Type sm_theUniquifiedClassName
;
127 template<typename ObjcType
>
128 typename UniquifiedName
<ObjcType
>::Type wxObjcClassInitializer
<ObjcType
>::sm_theUniquifiedClassName
;
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();
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.
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; \
165 inline objc_class * wxObjcCompilerInformation<ObjcClass>::GetCompiledClass() \
167 return s_objc_class_ptr_## ObjcClass; \
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.
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) \
185 inline objc_class * wxObjcCompilerInformation<ObjcClass>::GetCompiledClass() \
187 register struct objc_class *retval; \
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" \
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.
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) \
214 inline objc_class * wxObjcCompilerInformation<ObjcClass>::GetCompiledClass() \
216 register struct objc_class *retval; \
219 "\n\tbcl 20, 31, \"LPC_FOR_GET_CLASS_" #ObjcClass "\"" \
220 "\n\"LPC_FOR_GET_CLASS_" #ObjcClass "\":" \
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)" \
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.
236 #warning "Don't know how to implement wxObjcCompilerInformation<ObjcClass>::GetCompiledClass on this platform"
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) \
246 inline objc_class* wxObjcCompilerInformation<ObjcClass>::GetSuperclass() \
248 return objc_getClass(#ObjcSuperClass); \
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) \
256 const char wxObjcCompilerInformation<ObjcClass>::sm_theClassName[] = #ObjcClass;
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() \
266 return wxObjcClassInitializer<ObjcClass>::Get(); \
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()
275 #else // wxUSE_OBJC_UNIQUIFYING
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)
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
289 #endif // wxUSE_OBJC_UNIQUIFYING
291 #endif //ndef __WX_COCOA_OBJC_CLASS_H__