ObjcRef.h 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235
  1. /////////////////////////////////////////////////////////////////////////////
  2. // Name: wx/cocoa/ObjcRef.h
  3. // Purpose: wxObjcAutoRef template class
  4. // Author: David Elliott
  5. // Modified by:
  6. // Created: 2004/03/28
  7. // Copyright: (c) 2004 David Elliott <dfe@cox.net>
  8. // Licence: wxWindows licence
  9. /////////////////////////////////////////////////////////////////////////////
  10. #ifndef _WX_COCOA_OBJCREF_H__
  11. #define _WX_COCOA_OBJCREF_H__
  12. // Reuse wxCFRef-related code (e.g. wxCFRetain/wxCFRelease)
  13. #include "wx/osx/core/cfref.h"
  14. // NOTE WELL: We can only know whether or not GC can be used when compiling Objective-C.
  15. // Therefore we cannot implement these functions except when compiling Objective-C.
  16. #ifdef __OBJC__
  17. /*! @function wxGCSafeRetain
  18. @templatefield Type (implicit) An Objective-C class type
  19. @arg r Pointer to Objective-C object. May be null.
  20. @abstract Retains the Objective-C object, even when using Apple's garbage collector
  21. @discussion
  22. When Apple's garbage collector is enabled, the usual [obj retain] and [obj release] messages
  23. are ignored. Instead the collector with help from compiler-generated write-barriers tracks
  24. reachable objects. The write-barriers are generated when setting i-vars of C++ classes but
  25. they are ignored by the garbage collector unless the C++ object is in GC-managed memory.
  26. The simple solution is to use CFRetain on the Objective-C object which has been enhanced in
  27. GC mode to forcibly retain the object. In Retain/Release (RR) mode the CFRetain function has
  28. the same effect as [obj retain]. Note that GC vs. RR is selected at runtime.
  29. Take care that wxGCSafeRetain must be balanced with wxGCSafeRelease and that conversely
  30. wxGCSafeRelease must only be called on objects to balance wxGCSafeRetain. In particular when
  31. receiving an Objective-C object from an alloc or copy method take care that you must retain
  32. it with wxGCSafeRetain and balance the initial alloc with a standard release.
  33. Example:
  34. wxGCSafeRelease(m_obj); // release current object (if any)
  35. NSObject *obj = [[NSObject alloc] init];
  36. m_obj = wxGCSafeRetain(obj);
  37. [obj release];
  38. Alternatively (same effect, perhaps less clear):
  39. wxGCSafeRelease(m_obj); // release current object (if any)
  40. m_obj = wxGCSafeRetain([[NSObject alloc] init]);
  41. [m_obj release]; // balance alloc
  42. Consider the effect on the retain count from each statement (alloc, CFRetain, release)
  43. In RR mode: retainCount = 1, +1, -1
  44. In GC mode: strongRetainCount = 0, +1, -0
  45. This is a template function to ensure it is used on raw pointers and never on pointer-holder
  46. objects via implicit conversion operators.
  47. */
  48. template <class Type>
  49. inline Type * wxGCSafeRetain(Type *r)
  50. {
  51. #ifdef __OBJC_GC__
  52. return static_cast<Type*>(wxCFRetain(r));
  53. #else
  54. return [r retain];
  55. #endif
  56. }
  57. /*! @function wxGCSafeRelease
  58. @templatefield Type (implicit) An Objective-C class type
  59. @arg r Pointer to Objective-C object. May be null.
  60. @abstract Balances wxGCSafeRetain. Particularly useful with the Apple Garbage Collector.
  61. @discussion
  62. See the wxGCSafeRetain documentation for more details.
  63. Example (from wxGCSafeRetain documentation):
  64. wxGCSafeRelease(m_obj); // release current object (if any)
  65. m_obj = wxGCSafeRetain([[NSObject alloc] init]);
  66. [m_obj release]; // balance alloc
  67. When viewed from the start, m_obj ought to start as nil. However, the second time through
  68. the wxGCSafeRelease call becomes critical as it releases the retain from the first time
  69. through.
  70. In the destructor for this C++ object with the m_obj i-var you ought to do the following:
  71. wxGCSafeRelease(m_obj);
  72. m_obj = nil; // Not strictly needed, but safer.
  73. Under no circumstances should you balance an alloc or copy with a wxGCSafeRelease.
  74. */
  75. template <class Type>
  76. inline void wxGCSafeRelease(Type *r)
  77. {
  78. #ifdef __OBJC_GC__
  79. wxCFRelease(r);
  80. #else
  81. [r release];
  82. #endif
  83. }
  84. #else
  85. // NOTE: When not compiling Objective-C, declare these functions such that they can be
  86. // used by other inline-implemented methods. Since those methods in turn will not actually
  87. // be used from non-ObjC code the compiler ought not emit them. If it emits an out of
  88. // line copy of those methods then presumably it will have also emitted at least one
  89. // out of line copy of these functions from at least one Objective-C++ translation unit.
  90. // That means the out of line implementation will be available at link time.
  91. template <class Type>
  92. inline Type * wxGCSafeRetain(Type *r);
  93. template <class Type>
  94. inline void wxGCSafeRelease(Type *r);
  95. #endif //def __OBJC__
  96. /*
  97. wxObjcAutoRefFromAlloc: construct a reference to an object that was
  98. [NSObject -alloc]'ed and thus does not need a retain
  99. wxObjcAutoRef: construct a reference to an object that was
  100. either autoreleased or is retained by something else.
  101. */
  102. struct objc_object;
  103. // We must do any calls to Objective-C from an Objective-C++ source file
  104. class wxObjcAutoRefBase
  105. {
  106. protected:
  107. /*! @function ObjcRetain
  108. @abstract Simply does [p retain].
  109. */
  110. static struct objc_object* ObjcRetain(struct objc_object*);
  111. /*! @function ObjcRelease
  112. @abstract Simply does [p release].
  113. */
  114. static void ObjcRelease(struct objc_object*);
  115. };
  116. /*! @class wxObjcAutoRefFromAlloc
  117. @templatefield T The type of _pointer_ (e.g. NSString*, NSRunLoop*)
  118. @abstract Pointer-holder for Objective-C objects
  119. @discussion
  120. When constructing this object from a raw pointer, the pointer is assumed to have
  121. come from an alloc-style method. That is, once you construct this object from
  122. the pointer you must not balance your alloc with a call to release.
  123. This class has been carefully designed to work with both the traditional Retain/Release
  124. and the new Garbage Collected modes. In RR-mode it will prevent the object from being
  125. released by managing the reference count using the retain/release semantics. In GC-mode
  126. it will use a method (currently CFRetain/CFRelease) to ensure the object will never be
  127. finalized until this object is destroyed.
  128. */
  129. template <class T>
  130. class wxObjcAutoRefFromAlloc: wxObjcAutoRefBase
  131. {
  132. public:
  133. wxObjcAutoRefFromAlloc(T p = 0)
  134. : m_ptr(p)
  135. // NOTE: this is from alloc. Do NOT retain
  136. {
  137. // CFRetain
  138. // GC: Object is strongly retained and prevented from being collected
  139. // non-GC: Simply realizes it's an Objective-C object and calls [p retain]
  140. wxGCSafeRetain(p);
  141. // ObjcRelease (e.g. [p release])
  142. // GC: Objective-C retain/release mean nothing in GC mode
  143. // non-GC: This is a normal release call, balancing the retain
  144. ObjcRelease(static_cast<T>(p));
  145. // The overall result:
  146. // GC: Object is strongly retained
  147. // non-GC: Retain count is the same as it was (retain then release)
  148. }
  149. wxObjcAutoRefFromAlloc(const wxObjcAutoRefFromAlloc& otherRef)
  150. : m_ptr(otherRef.m_ptr)
  151. { wxGCSafeRetain(m_ptr); }
  152. ~wxObjcAutoRefFromAlloc()
  153. { wxGCSafeRelease(m_ptr); }
  154. wxObjcAutoRefFromAlloc& operator=(const wxObjcAutoRefFromAlloc& otherRef)
  155. { wxGCSafeRetain(otherRef.m_ptr);
  156. wxGCSafeRelease(m_ptr);
  157. m_ptr = otherRef.m_ptr;
  158. return *this;
  159. }
  160. operator T() const
  161. { return static_cast<T>(m_ptr); }
  162. T operator->() const
  163. { return static_cast<T>(m_ptr); }
  164. protected:
  165. /*! @field m_ptr The pointer to the Objective-C object
  166. @discussion
  167. The pointer to the Objective-C object is typed as void* to avoid compiler-generated write
  168. barriers as would be used for implicitly __strong object pointers and to avoid the similar
  169. read barriers as would be used for an explicitly __weak object pointer. The write barriers
  170. are useless unless this object is located in GC-managed heap which is highly unlikely.
  171. Since we guarantee strong reference via CFRetain/CFRelease the write-barriers are not needed
  172. at all, even if this object does happen to be allocated in GC-managed heap.
  173. */
  174. void *m_ptr;
  175. };
  176. /*!
  177. @class wxObjcAutoRef
  178. @description
  179. A pointer holder that does retain its argument.
  180. NOTE: It is suggest that you instead use wxObjcAutoRefFromAlloc<T> foo([aRawPointer retain])
  181. */
  182. template <class T>
  183. class wxObjcAutoRef: public wxObjcAutoRefFromAlloc<T>
  184. {
  185. public:
  186. /*! @method wxObjcAutoRef
  187. @description
  188. Uses the underlying wxObjcAutoRefFromAlloc and simply does a typical [p retain] such that
  189. in RR-mode the object is in effectively the same retain-count state as it would have been
  190. coming straight from an alloc method.
  191. */
  192. wxObjcAutoRef(T p = 0)
  193. : wxObjcAutoRefFromAlloc<T>(p)
  194. { // NOTE: ObjcRetain is correct because in GC-mode it balances ObjcRelease in our superclass constructor
  195. // In RR mode it does retain and the superclass does retain/release thus resulting in an overall retain.
  196. ObjcRetain(static_cast<T>(wxObjcAutoRefFromAlloc<T>::m_ptr));
  197. }
  198. ~wxObjcAutoRef() {}
  199. wxObjcAutoRef(const wxObjcAutoRef& otherRef)
  200. : wxObjcAutoRefFromAlloc<T>(otherRef)
  201. {}
  202. wxObjcAutoRef(const wxObjcAutoRefFromAlloc<T>& otherRef)
  203. : wxObjcAutoRefFromAlloc<T>(otherRef)
  204. {}
  205. wxObjcAutoRef& operator=(const wxObjcAutoRef& otherRef)
  206. { return wxObjcAutoRefFromAlloc<T>::operator=(otherRef); }
  207. };
  208. #endif //ndef _WX_COCOA_OBJCREF_H__