]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/osx/core/cfref.h
90c7ae415831d80529e73fa3adeea678422c23e6
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/osx/core/cfref.h
3 // Purpose: wxCFRef template class
4 // Author: David Elliott <dfe@cox.net>
5 // Modified by: Stefan Csomor
8 // Copyright: (c) 2007 David Elliott <dfe@cox.net>, Stefan Csomor
9 // Licence: wxWindows licence
10 // Notes: See http://developer.apple.com/documentation/CoreFoundation/Conceptual/CFMemoryMgmt/index.html
11 /////////////////////////////////////////////////////////////////////////////
12 /*! @header wx/osx/core/cfref.h
13 @abstract wxCFRef template class
14 @discussion FIXME: Convert doc tags to something less buggy with C++
17 #ifndef _WX_MAC_COREFOUNDATION_CFREF_H__
18 #define _WX_MAC_COREFOUNDATION_CFREF_H__
20 // Include unistd to ensure that NULL is defined
22 // Include AvailabilityMacros for DEPRECATED_ATTRIBUTE
23 #include <AvailabilityMacros.h>
25 // #include <CoreFoundation/CFBase.h>
26 /* Don't include CFBase.h such that this header can be included from public
27 * headers with minimal namespace pollution.
28 * Note that Darwin CF uses extern for CF_EXPORT. If we need this on Win32
29 * or non-Darwin Mac OS we'll need to define the appropriate __declspec.
31 typedef const void *CFTypeRef
;
33 extern /* CF_EXPORT */
34 CFTypeRef
CFRetain(CFTypeRef cf
);
35 extern /* CF_EXPORT */
36 void CFRelease(CFTypeRef cf
);
40 /*! @function wxCFRelease
41 @abstract A CFRelease variant that checks for NULL before releasing.
42 @discussion The parameter is template not for type safety but to ensure the argument
43 is a raw pointer and not a ref holder of any type.
46 inline void wxCFRelease(Type
*r
)
49 ::CFRelease((CFTypeRef
)r
);
52 /*! @function wxCFRetain
53 @abstract A typesafe CFRetain variant that checks for NULL.
56 inline Type
* wxCFRetain(Type
*r
)
58 // NOTE(DE): Setting r to the result of CFRetain improves efficiency on both x86 and PPC
59 // Casting r to CFTypeRef ensures we are calling the real C version defined in CFBase.h
60 // and not any possibly templated/overloaded CFRetain.
62 r
= (Type
*)::CFRetain((CFTypeRef
)r
);
66 template <class refType
>
69 /*! @class wxCFWeakRef
70 @templatefield refType The CF reference type (e.g. CFStringRef, CFRunLoopRef, etc.)
71 It should already be a pointer. This is different from
72 shared_ptr where the template parameter is the pointee type.
73 @discussion Wraps a raw pointer without any retain or release.
74 Provides a way to get what amounts to a raw pointer from a wxCFRef without
75 using a raw pointer. Unlike a raw pointer, constructing a wxCFRef from this
76 class will cause it to be retained because it is assumed that a wxCFWeakRef
77 does not own its pointer.
79 template <class refType
>
82 template <class refTypeA
, class otherRefType
>
83 friend wxCFWeakRef
<refTypeA
> static_cfref_cast(const wxCFRef
<otherRefType
> &otherRef
);
85 /*! @method wxCFWeakRef
86 @abstract Creates a NULL reference
92 // Default copy constructor is fine.
93 // Default destructor is fine but we'll set NULL to avoid bugs
97 // Do not implement a raw-pointer constructor.
99 /*! @method wxCFWeakRef
100 @abstract Copies another ref holder where its type can be converted to ours
101 @templatefield otherRefType Any type held by another wxCFWeakRef.
102 @param otherRef The other weak ref holder to copy.
103 @discussion This is merely a copy or implicit cast.
105 template <class otherRefType
>
106 wxCFWeakRef(const wxCFWeakRef
<otherRefType
>& otherRef
)
107 : m_ptr(otherRef
.get()) // Implicit conversion from otherRefType to refType should occur
110 /*! @method wxCFWeakRef
111 @abstract Copies a strong ref holder where its type can be converted to ours
112 @templatefield otherRefType Any type held by a wxCFRef.
113 @param otherRef The strong ref holder to copy.
114 @discussion This ref is merely a pointer copy, the strong ref still holds the pointer.
116 template <class otherRefType
>
117 wxCFWeakRef(const wxCFRef
<otherRefType
>& otherRef
)
118 : m_ptr(otherRef
.get()) // Implicit conversion from otherRefType to refType should occur
122 @abstract Explicit conversion to the underlying pointer type
123 @discussion Allows the caller to explicitly get the underlying pointer.
128 /*! @method operator refType
129 @abstract Implicit conversion to the underlying pointer type
130 @discussion Allows the ref to be used in CF function calls.
132 operator refType() const
136 /*! @method wxCFWeakRef
137 @abstract Constructs a weak reference to the raw pointer
138 @templatefield otherType Any type.
139 @param p The raw pointer to assume ownership of. May be NULL.
140 @discussion This method is private so that the friend static_cfref_cast can use it
142 template <class otherType
>
143 explicit wxCFWeakRef(otherType
*p
)
144 : m_ptr(p
) // Implicit conversion from otherType* to refType should occur.
147 /*! @var m_ptr The raw pointer.
153 @templatefield refType The CF reference type (e.g. CFStringRef, CFRunLoopRef, etc.)
154 It should already be a pointer. This is different from
155 shared_ptr where the template parameter is the pointee type.
156 @discussion Properly retains/releases reference to CoreFoundation objects
158 template <class refType
>
163 @abstract Creates a NULL reference
170 @abstract Assumes ownership of p and creates a reference to it.
171 @templatefield otherType Any type.
172 @param p The raw pointer to assume ownership of. May be NULL.
173 @discussion Like shared_ptr, it is assumed that the caller has a strong reference to p and intends
174 to transfer ownership of that reference to this ref holder. If the object comes from
175 a Create or Copy method then this is the correct behavior. If the object comes from
176 a Get method then you must CFRetain it yourself before passing it to this constructor.
177 A handy way to do this is to use the non-member wxCFRefFromGet factory funcion.
178 This method is templated and takes an otherType *p. This prevents implicit conversion
179 using an operator refType() in a different ref-holding class type.
181 template <class otherType
>
182 explicit wxCFRef(otherType
*p
)
183 : m_ptr(p
) // Implicit conversion from otherType* to refType should occur.
187 @abstract Copies a ref holder of the same type
188 @param otherRef The other ref holder to copy.
189 @discussion Ownership will be shared by the original ref and the newly created ref. That is,
190 the object will be explicitly retained by this new ref.
192 wxCFRef(const wxCFRef
& otherRef
)
193 : m_ptr(wxCFRetain(otherRef
.m_ptr
))
197 @abstract Copies a ref holder where its type can be converted to ours
198 @templatefield otherRefType Any type held by another wxCFRef.
199 @param otherRef The other ref holder to copy.
200 @discussion Ownership will be shared by the original ref and the newly created ref. That is,
201 the object will be explicitly retained by this new ref.
203 template <class otherRefType
>
204 wxCFRef(const wxCFRef
<otherRefType
>& otherRef
)
205 : m_ptr(wxCFRetain(otherRef
.get())) // Implicit conversion from otherRefType to refType should occur
209 @abstract Copies a weak ref holder where its type can be converted to ours
210 @templatefield otherRefType Any type held by a wxCFWeakRef.
211 @param otherRef The weak ref holder to copy.
212 @discussion Ownership will be taken by this newly created ref. That is,
213 the object will be explicitly retained by this new ref.
214 Ownership is most likely shared with some other ref as well.
216 template <class otherRefType
>
217 wxCFRef(const wxCFWeakRef
<otherRefType
>& otherRef
)
218 : m_ptr(wxCFRetain(otherRef
.get())) // Implicit conversion from otherRefType to refType should occur
222 @abstract Releases (potentially shared) ownership of the ref.
223 @discussion A ref holder instance is always assumed to have ownership so ownership is always
224 released (CFRelease called) upon destruction.
229 /*! @method operator=
230 @abstract Assigns the other ref's pointer to us when the otherRef is the same type.
231 @param otherRef The other ref holder to copy.
232 @discussion The incoming pointer is retained, the original pointer is released, and this object
233 is made to point to the new pointer.
235 wxCFRef
& operator=(const wxCFRef
& otherRef
)
237 if (this != &otherRef
)
239 wxCFRetain(otherRef
.m_ptr
);
241 m_ptr
= otherRef
.m_ptr
;
246 /*! @method operator=
247 @abstract Assigns the other ref's pointer to us when the other ref can be converted to our type.
248 @templatefield otherRefType Any type held by another wxCFRef
249 @param otherRef The other ref holder to copy.
250 @discussion The incoming pointer is retained, the original pointer is released, and this object
251 is made to point to the new pointer.
253 template <class otherRefType
>
254 wxCFRef
& operator=(const wxCFRef
<otherRefType
>& otherRef
)
256 wxCFRetain(otherRef
.get());
258 m_ptr
= otherRef
.get(); // Implicit conversion from otherRefType to refType should occur
263 @abstract Explicit conversion to the underlying pointer type
264 @discussion Allows the caller to explicitly get the underlying pointer.
269 /*! @method operator refType
270 @abstract Implicit conversion to the underlying pointer type
271 @discussion Allows the ref to be used in CF function calls.
273 operator refType() const
277 < // HeaderDoc is retarded and thinks the GT from operator-> is part of a template param.
278 // So give it that < outside of a comment to fake it out. (if 0 is not a comment to HeaderDoc)
281 /*! @method operator->
282 @abstract Implicit conversion to the underlying pointer type
283 @discussion This is nearly useless for CF types which are nearly always opaque
285 refType
operator-> () const
289 @abstract Nullifies the reference
290 @discussion Releases ownership (calls CFRelease) before nullifying the pointer.
299 @abstract Sets this to a new reference
300 @templatefield otherType Any type.
301 @param p The raw pointer to assume ownership of
302 @discussion The existing reference is released (like destruction). It is assumed that the caller
303 has a strong reference to the new p and intends to transfer ownership of that reference
304 to this ref holder. Take care to call CFRetain if you received the object from a Get method.
305 This method is templated and takes an otherType *p. This prevents implicit conversion
306 using an operator refType() in a different ref-holding class type.
308 template <class otherType
>
309 void reset(otherType
* p
)
312 m_ptr
= p
; // Automatic conversion should occur
315 // Release the pointer, i.e. give up its ownership.
324 /*! @var m_ptr The raw pointer.
329 /*! @function wxCFRefFromGet
330 @abstract Factory function to create wxCFRef from a raw pointer obtained from a Get-rule function
331 @param p The pointer to retain and create a wxCFRef from. May be NULL.
332 @discussion Unlike the wxCFRef raw pointer constructor, this function explicitly retains its
333 argument. This can be used for functions such as CFDictionaryGetValue() or
334 CFAttributedStringGetString() which return a temporary reference (Get-rule functions).
335 FIXME: Anybody got a better name?
337 template <typename Type
>
338 inline wxCFRef
<Type
*> wxCFRefFromGet(Type
*p
)
340 return wxCFRef
<Type
*>(wxCFRetain(p
));
343 /*! @function static_cfref_cast
344 @abstract Works like static_cast but with a wxCFRef as the argument.
345 @param refType Template parameter. The destination raw pointer type
346 @param otherRef Normal parameter. The source wxCFRef<> object.
347 @discussion This is intended to be a clever way to make static_cast work while allowing
348 the return value to be converted to either a strong ref or a raw pointer
349 while ensuring that the retain count is updated appropriately.
351 This is modeled after shared_ptr's static_pointer_cast. Just as wxCFRef is
352 parameterized on a pointer to an opaque type so is this class. Note that
353 this differs from shared_ptr which is parameterized on the pointee type.
355 FIXME: Anybody got a better name?
357 template <class refType
, class otherRefType
>
358 inline wxCFWeakRef
<refType
> static_cfref_cast(const wxCFRef
<otherRefType
> &otherRef
);
360 template <class refType
, class otherRefType
>
361 inline wxCFWeakRef
<refType
> static_cfref_cast(const wxCFRef
<otherRefType
> &otherRef
)
363 return wxCFWeakRef
<refType
>(static_cast<refType
>(otherRef
.get()));
366 /*! @function CFRelease
367 @abstract Overloads CFRelease so that the user is warned of bad behavior.
368 @discussion It is rarely appropriate to retain or release a wxCFRef. If one absolutely
369 must do it he can explicitly get() the raw pointer
370 Normally, this function is unimplemented resulting in a linker error if used.
373 inline void CFRelease(const wxCFRef
<T
*> & cfref
) DEPRECATED_ATTRIBUTE
;
375 /*! @function CFRetain
376 @abstract Overloads CFRetain so that the user is warned of bad behavior.
377 @discussion It is rarely appropriate to retain or release a wxCFRef. If one absolutely
378 must do it he can explicitly get() the raw pointer
379 Normally, this function is unimplemented resulting in a linker error if used.
382 inline void CFRetain(const wxCFRef
<T
*>& cfref
) DEPRECATED_ATTRIBUTE
;
384 // Change the 0 to a 1 if you want the functions to work (no link errors)
385 // Neither function will cause retain/release side-effects if implemented.
388 void CFRelease(const wxCFRef
<T
*> & cfref
)
390 CFRelease(cfref
.get());
394 void CFRetain(const wxCFRef
<T
*> & cfref
)
396 CFRetain(cfref
.get());
400 #endif //ndef _WX_MAC_COREFOUNDATION_CFREF_H__