]>
git.saurik.com Git - wxWidgets.git/blob - include/wx/mac/corefoundation/cfref.h
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: wx/mac/corefoundation/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/mac/corefoundation/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 <CoreFoundation/CFBase.h>
21 /* Don't include CFBase.h such that this header can be included from public
22 * headers with minimal namespace pollution.
23 * Note that Darwin CF uses extern for CF_EXPORT. If we need this on Win32
24 * or non-Darwin Mac OS we'll need to define the appropriate __declspec.
27 typedef const void *CFTypeRef
;
28 extern /* CF_EXPORT */
29 CFTypeRef
CFRetain(CFTypeRef cf
);
30 extern /* CF_EXPORT */
31 void CFRelease(CFTypeRef cf
);
35 /*! @function wxCFRelease
36 @abstract A CFRelease variant that checks for NULL before releasing.
37 @discussion The parameter is template not for type safety but to ensure the argument
38 is a raw pointer and not a ref holder of any type.
41 inline void wxCFRelease(Type
*r
)
44 ::CFRelease((CFTypeRef
)r
);
47 /*! @function wxCFRetain
48 @abstract A typesafe CFRetain variant that checks for NULL.
51 inline Type
* wxCFRetain(Type
*r
)
53 // NOTE(DE): Setting r to the result of CFRetain improves efficiency on both x86 and PPC
54 // Casting r to CFTypeRef ensures we are calling the real C version defined in CFBase.h
55 // and not any possibly templated/overloaded CFRetain.
57 r
= (Type
*)::CFRetain((CFTypeRef
)r
);
61 template <class refType
>
64 /*! @class wxCFWeakRef
65 @templatefield refType The CF reference type (e.g. CFStringRef, CFRunLoopRef, etc.)
66 It should already be a pointer. This is different from
67 shared_ptr where the template parameter is the pointee type.
68 @discussion Wraps a raw pointer without any retain or release.
69 Provides a way to get what amounts to a raw pointer from a wxCFRef without
70 using a raw pointer. Unlike a raw pointer, constructing a wxCFRef from this
71 class will cause it to be retained because it is assumed that a wxCFWeakRef
72 does not own its pointer.
74 template <class refType
>
77 template <class refTypeA
, class otherRefType
>
78 friend wxCFWeakRef
<refTypeA
> static_cfref_cast(const wxCFRef
<otherRefType
> &otherRef
);
80 /*! @method wxCFWeakRef
81 @abstract Creates a NULL reference
87 // Default copy constructor is fine.
88 // Default destructor is fine but we'll set NULL to avoid bugs
92 // Do not implement a raw-pointer constructor.
94 /*! @method wxCFWeakRef
95 @abstract Copies another ref holder where its type can be converted to ours
96 @templatefield otherRefType Any type held by another wxCFWeakRef.
97 @param otherRef The other weak ref holder to copy.
98 @discussion This is merely a copy or implicit cast.
100 template <class otherRefType
>
101 wxCFWeakRef(const wxCFWeakRef
<otherRefType
>& otherRef
)
102 : m_ptr(otherRef
.get()) // Implicit conversion from otherRefType to refType should occur
105 /*! @method wxCFWeakRef
106 @abstract Copies a strong ref holder where its type can be converted to ours
107 @templatefield otherRefType Any type held by a wxCFRef.
108 @param otherRef The strong ref holder to copy.
109 @discussion This ref is merely a pointer copy, the strong ref still holds the pointer.
111 template <class otherRefType
>
112 wxCFWeakRef(const wxCFRef
<otherRefType
>& otherRef
)
113 : m_ptr(otherRef
.get()) // Implicit conversion from otherRefType to refType should occur
117 @abstract Explicit conversion to the underlying pointer type
118 @discussion Allows the caller to explicitly get the underlying pointer.
123 /*! @method operator refType
124 @abstract Implicit conversion to the underlying pointer type
125 @discussion Allows the ref to be used in CF function calls.
127 operator refType() const
131 /*! @method wxCFWeakRef
132 @abstract Constructs a weak reference to the raw pointer
133 @templatefield otherType Any type.
134 @param p The raw pointer to assume ownership of. May be NULL.
135 @discussion This method is private so that the friend static_cfref_cast can use it
137 template <class otherType
>
138 explicit wxCFWeakRef(otherType
*p
)
139 : m_ptr(p
) // Implicit conversion from otherType* to refType should occur.
142 /*! @var m_ptr The raw pointer.
148 @templatefield refType The CF reference type (e.g. CFStringRef, CFRunLoopRef, etc.)
149 It should already be a pointer. This is different from
150 shared_ptr where the template parameter is the pointee type.
151 @discussion Properly retains/releases reference to CoreFoundation objects
153 template <class refType
>
158 @abstract Creates a NULL reference
165 @abstract Assumes ownership of p and creates a reference to it.
166 @templatefield otherType Any type.
167 @param p The raw pointer to assume ownership of. May be NULL.
168 @discussion Like shared_ptr, it is assumed that the caller has a strong reference to p and intends
169 to transfer ownership of that reference to this ref holder. If the object comes from
170 a Create or Copy method then this is the correct behavior. If the object comes from
171 a Get method then you must CFRetain it yourself before passing it to this constructor.
172 A handy way to do this is to use the non-member wxCFRefFromGet factory funcion.
173 This method is templated and takes an otherType *p. This prevents implicit conversion
174 using an operator refType() in a different ref-holding class type.
176 template <class otherType
>
177 explicit wxCFRef(otherType
*p
)
178 : m_ptr(p
) // Implicit conversion from otherType* to refType should occur.
182 @abstract Copies a ref holder of the same type
183 @param otherRef The other ref holder to copy.
184 @discussion Ownership will be shared by the original ref and the newly created ref. That is,
185 the object will be explicitly retained by this new ref.
187 wxCFRef(const wxCFRef
& otherRef
)
188 : m_ptr(wxCFRetain(otherRef
.m_ptr
))
192 @abstract Copies a ref holder where its type can be converted to ours
193 @templatefield otherRefType Any type held by another wxCFRef.
194 @param otherRef The other ref holder to copy.
195 @discussion Ownership will be shared by the original ref and the newly created ref. That is,
196 the object will be explicitly retained by this new ref.
198 template <class otherRefType
>
199 wxCFRef(const wxCFRef
<otherRefType
>& otherRef
)
200 : m_ptr(wxCFRetain(otherRef
.get())) // Implicit conversion from otherRefType to refType should occur
204 @abstract Copies a weak ref holder where its type can be converted to ours
205 @templatefield otherRefType Any type held by a wxCFWeakRef.
206 @param otherRef The weak ref holder to copy.
207 @discussion Ownership will be taken by this newly created ref. That is,
208 the object will be explicitly retained by this new ref.
209 Ownership is most likely shared with some other ref as well.
211 template <class otherRefType
>
212 wxCFRef(const wxCFWeakRef
<otherRefType
>& otherRef
)
213 : m_ptr(wxCFRetain(otherRef
.get())) // Implicit conversion from otherRefType to refType should occur
217 @abstract Releases (potentially shared) ownership of the ref.
218 @discussion A ref holder instance is always assumed to have ownership so ownership is always
219 released (CFRelease called) upon destruction.
224 /*! @method operator=
225 @abstract Assigns the other ref's pointer to us when the otherRef is the same type.
226 @param otherRef The other ref holder to copy.
227 @discussion The incoming pointer is retained, the original pointer is released, and this object
228 is made to point to the new pointer.
230 wxCFRef
& operator=(const wxCFRef
& otherRef
)
232 wxCFRetain(otherRef
.m_ptr
);
234 m_ptr
= otherRef
.m_ptr
;
238 /*! @method operator=
239 @abstract Assigns the other ref's pointer to us when the other ref can be converted to our type.
240 @templatefield otherRefType Any type held by another wxCFRef
241 @param otherRef The other ref holder to copy.
242 @discussion The incoming pointer is retained, the original pointer is released, and this object
243 is made to point to the new pointer.
245 template <class otherRefType
>
246 wxCFRef
& operator=(const wxCFRef
<otherRefType
>& otherRef
)
248 wxCFRetain(otherRef
.get());
250 m_ptr
= otherRef
.get(); // Implicit conversion from otherRefType to refType should occur
255 @abstract Explicit conversion to the underlying pointer type
256 @discussion Allows the caller to explicitly get the underlying pointer.
261 /*! @method operator refType
262 @abstract Implicit conversion to the underlying pointer type
263 @discussion Allows the ref to be used in CF function calls.
265 operator refType() const
269 < // HeaderDoc is retarded and thinks the GT from operator-> is part of a template param.
270 // So give it that < outside of a comment to fake it out. (if 0 is not a comment to HeaderDoc)
273 /*! @method operator->
274 @abstract Implicit conversion to the underlying pointer type
275 @discussion This is nearly useless for CF types which are nearly always opaque
277 refType
operator-> () const
281 @abstract Nullifies the reference
282 @discussion Releases ownership (calls CFRelease) before nullifying the pointer.
291 @abstract Sets this to a new reference
292 @templatefield otherType Any type.
293 @param p The raw pointer to assume ownership of
294 @discussion The existing reference is released (like destruction). It is assumed that the caller
295 has a strong reference to the new p and intends to transfer ownership of that reference
296 to this ref holder. Take care to call CFRetain if you received the object from a Get method.
297 This method is templated and takes an otherType *p. This prevents implicit conversion
298 using an operator refType() in a different ref-holding class type.
300 template <class otherType
>
301 void reset(otherType
* p
)
304 m_ptr
= p
; // Automatic conversion should occur
307 /*! @var m_ptr The raw pointer.
312 /*! @function wxCFRefFromGet
313 @abstract Factory function to create wxCFRef from a raw pointer obtained from a Get-rule function
314 @param p The pointer to retain and create a wxCFRef from. May be NULL.
315 @discussion Unlike the wxCFRef raw pointer constructor, this function explicitly retains its
316 argument. This can be used for functions such as CFDictionaryGetValue() or
317 CFAttributedStringGetString() which return a temporary reference (Get-rule functions).
318 FIXME: Anybody got a better name?
320 template <typename Type
>
321 inline wxCFRef
<Type
*> wxCFRefFromGet(Type
*p
)
323 return wxCFRef
<Type
*>(wxCFRetain(p
));
326 /*! @function static_cfref_cast
327 @abstract Works like static_cast but with a wxCFRef as the argument.
328 @param refType Template parameter. The destination raw pointer type
329 @param otherRef Normal parameter. The source wxCFRef<> object.
330 @discussion This is intended to be a clever way to make static_cast work while allowing
331 the return value to be converted to either a strong ref or a raw pointer
332 while ensuring that the retain count is updated appropriately.
334 This is modeled after shared_ptr's static_pointer_cast. Just as wxCFRef is
335 parameterized on a pointer to an opaque type so is this class. Note that
336 this differs from shared_ptr which is parameterized on the pointee type.
338 FIXME: Anybody got a better name?
340 template <class refType
, class otherRefType
>
341 inline wxCFWeakRef
<refType
> static_cfref_cast(const wxCFRef
<otherRefType
> &otherRef
);
343 template <class refType
, class otherRefType
>
344 inline wxCFWeakRef
<refType
> static_cfref_cast(const wxCFRef
<otherRefType
> &otherRef
)
346 return wxCFWeakRef
<refType
>(static_cast<refType
>(otherRef
.get()));
349 /*! @function CFRelease
350 @abstract Overloads CFRelease so that the user is warned of bad behavior.
351 @discussion It is rarely appropriate to retain or release a wxCFRef. If one absolutely
352 must do it he can explicitly get() the raw pointer
353 Normally, this function is unimplemented resulting in a linker error if used.
356 inline void CFRelease(const wxCFRef
<T
*> & cfref
) DEPRECATED_ATTRIBUTE
;
358 /*! @function CFRetain
359 @abstract Overloads CFRetain so that the user is warned of bad behavior.
360 @discussion It is rarely appropriate to retain or release a wxCFRef. If one absolutely
361 must do it he can explicitly get() the raw pointer
362 Normally, this function is unimplemented resulting in a linker error if used.
365 inline void CFRetain(const wxCFRef
<T
*>& cfref
) DEPRECATED_ATTRIBUTE
;
367 // Change the 0 to a 1 if you want the functions to work (no link errors)
368 // Neither function will cause retain/release side-effects if implemented.
371 void CFRelease(const wxCFRef
<T
*> & cfref
)
373 CFRelease(cfref
.get());
377 void CFRetain(const wxCFRef
<T
*> & cfref
)
379 CFRetain(cfref
.get());
383 #endif //ndef _WX_MAC_COREFOUNDATION_CFREF_H__