1 ///////////////////////////////////////////////////////////////////////////////
2 // Name: wx/mac/corefoundation/cfwrappers.h
3 // Purpose: Wrappers around some complex core foundation types
8 // Copyright: (c) 2005 Ryan Norton
9 // Licence: wxWindows licence
10 ///////////////////////////////////////////////////////////////////////////////
12 #ifndef __WX_CFWRAPPERS_H__
13 #define __WX_CFWRAPPERS_H__
15 #include "wx/mac/corefoundation/cfstring.h"
16 #include "wx/intl.h" //wxLocale for wxCFString
18 #define wxCF_RELEASE true
19 #define wxCF_RETAIN false
21 // ----------------------------------------------------------------------------
23 // ----------------------------------------------------------------------------
28 wxCFDictionary(CFTypeRef ref
, bool bRetain
= wxCF_RELEASE
)
30 m_cfmdRef
= (CFMutableDictionaryRef
) ref
;
31 if(bRetain
== wxCF_RETAIN
&& ref
)
35 wxCFDictionary(CFIndex cfiSize
= 0)
37 CFDictionaryKeyCallBacks kcbs
;
38 CFDictionaryValueCallBacks vcbs
;
39 BuildKeyCallbacks(&kcbs
);
40 BuildValueCallbacks(&vcbs
);
42 m_cfmdRef
= CFDictionaryCreateMutable(
43 kCFAllocatorDefault
, cfiSize
, &kcbs
, &vcbs
);
51 {if(m_cfmdRef
) CFRelease(m_cfmdRef
);}
53 static const void* RetainProc(CFAllocatorRef
, const void* v
)
54 { return (const void*) CFRetain(v
); }
56 static void ReleaseProc(CFAllocatorRef
, const void* v
)
59 void MakeMutable(CFIndex cfiSize
= 0)
61 CFDictionaryRef oldref
= (CFDictionaryRef
) m_cfmdRef
;
63 m_cfmdRef
= CFDictionaryCreateMutableCopy(
71 void BuildKeyCallbacks(CFDictionaryKeyCallBacks
* pCbs
)
74 pCbs
->retain
= RetainProc
;
75 pCbs
->release
= ReleaseProc
;
76 pCbs
->copyDescription
= NULL
;
81 void BuildValueCallbacks(CFDictionaryValueCallBacks
* pCbs
)
84 pCbs
->retain
= RetainProc
;
85 pCbs
->release
= ReleaseProc
;
86 pCbs
->copyDescription
= NULL
;
90 operator CFTypeRef () const
91 { return (CFTypeRef
)m_cfmdRef
; }
93 CFDictionaryRef
GetCFDictionary() const
94 { return (CFDictionaryRef
)m_cfmdRef
; }
96 CFMutableDictionaryRef
GetCFMutableDictionary()
97 { return (CFMutableDictionaryRef
) m_cfmdRef
; }
99 CFTypeRef
operator [] (CFTypeRef cftEntry
) const
102 return (CFTypeRef
) CFDictionaryGetValue((CFDictionaryRef
)m_cfmdRef
, cftEntry
);
105 CFIndex
GetCount() const
108 return CFDictionaryGetCount((CFDictionaryRef
)m_cfmdRef
);
111 void Add(CFTypeRef cftKey
, CFTypeRef cftValue
)
114 wxASSERT(Exists(cftKey
) == false);
115 CFDictionaryAddValue(m_cfmdRef
, cftKey
, cftValue
);
118 void Remove(CFTypeRef cftKey
)
121 wxASSERT(Exists(cftKey
));
122 CFDictionaryRemoveValue(m_cfmdRef
, cftKey
);
125 void Set(CFTypeRef cftKey
, CFTypeRef cftValue
)
128 wxASSERT(Exists(cftKey
));
129 CFDictionarySetValue(m_cfmdRef
, cftKey
, cftValue
);
132 bool Exists(CFTypeRef cftKey
) const
135 return CFDictionaryContainsKey((CFDictionaryRef
)m_cfmdRef
, cftKey
) == true;
138 bool IsOk() const {return m_cfmdRef
!= NULL
; }
142 return IsOk() && CFGetTypeID((CFTypeRef
)m_cfmdRef
) == CFDictionaryGetTypeID();
145 void PrintOut(wxString
& sMessage
)
147 PrintOutDictionary(sMessage
, m_cfmdRef
);
150 static void PrintOutDictionary(wxString
& sMessage
, CFDictionaryRef cfdRef
)
152 CFIndex cfiCount
= CFDictionaryGetCount(cfdRef
);
153 CFTypeRef
* pKeys
= new CFTypeRef
[cfiCount
];
154 CFTypeRef
* pValues
= new CFTypeRef
[cfiCount
];
156 CFDictionaryGetKeysAndValues(cfdRef
, pKeys
, pValues
);
158 for(CFIndex i
= 0; i
< cfiCount
; ++i
)
160 wxString sKey
= wxMacCFStringHolder(CFCopyTypeIDDescription(CFGetTypeID(pKeys
[i
]))).AsString();
161 wxString sValue
= wxMacCFStringHolder(CFCopyTypeIDDescription(CFGetTypeID(pValues
[i
]))).AsString();
164 wxString::Format(wxT("[{#%d} Key : %s]"), (int) i
,
167 PrintOutType(sMessage
, sKey
, pKeys
[i
]);
170 wxString::Format(wxT("\n\t[Value : %s]"),
173 PrintOutType(sMessage
, sValue
, pValues
[i
]);
175 sMessage
<< wxT("\n");
182 static void PrintOutArray(wxString
& sMessage
, CFArrayRef cfaRef
)
185 for(CFIndex i
= 0; i
< CFArrayGetCount(cfaRef
); ++i
)
187 wxString sValue
= wxMacCFStringHolder(CFCopyTypeIDDescription(CFGetTypeID(
188 CFArrayGetValueAtIndex(cfaRef
, i
)
192 wxString::Format(wxT("\t\t[{#%d} ArrayValue : %s]\n"), (int) i
,
195 PrintOutType(sMessage
, sValue
, CFArrayGetValueAtIndex(cfaRef
, i
));
199 static void PrintOutType(wxString
& sMessage
, wxString sValue
, CFTypeRef cfRef
)
201 sMessage
<< wxT(" {");
203 if(sValue
== wxT("CFString"))
205 sMessage
<< wxMacCFStringHolder((CFStringRef
)cfRef
, false).AsString();
207 else if(sValue
== wxT("CFNumber"))
210 CFNumberGetValue((CFNumberRef
)cfRef
, kCFNumberIntType
, &nOut
);
213 else if(sValue
== wxT("CFDictionary"))
215 PrintOutDictionary(sMessage
, (CFDictionaryRef
)cfRef
);
217 else if(sValue
== wxT("CFArray"))
219 PrintOutArray(sMessage
, (CFArrayRef
)cfRef
);
221 else if(sValue
== wxT("CFBoolean"))
223 sMessage
<< (cfRef
== kCFBooleanTrue
? wxT("true") : wxT("false"));
225 else if(sValue
== wxT("CFURL"))
227 sMessage
<< wxMacCFStringHolder(CFURLCopyPath((CFURLRef
) cfRef
)).AsString();
231 sMessage
<< wxT("*****UNKNOWN TYPE******");
234 sMessage
<< wxT("} ");
241 CFTypeRef
WriteAsXML()
243 return CFPropertyListCreateXMLData(kCFAllocatorDefault
, m_cfmdRef
);
246 bool ReadAsXML(CFTypeRef cfData
, wxString
* pErrorMsg
= NULL
)
249 CFStringRef cfsError
=NULL
;
250 m_cfmdRef
= (CFMutableDictionaryRef
) CFPropertyListCreateFromXMLData(
253 kCFPropertyListMutableContainersAndLeaves
,
258 *pErrorMsg
= wxMacCFStringHolder(cfsError
).AsString();
263 return m_cfmdRef
!= NULL
;
266 CFMutableDictionaryRef m_cfmdRef
;
269 // ----------------------------------------------------------------------------
271 // ----------------------------------------------------------------------------
276 wxCFArray(CFTypeRef ref
, bool bRetain
= wxCF_RELEASE
)
278 m_cfmaRef
= (CFMutableArrayRef
)ref
;
279 if(bRetain
== wxCF_RETAIN
&& ref
)
283 wxCFArray(CFIndex cfiSize
= 0) : m_cfmaRef(NULL
)
289 void MakeMutable(CFIndex cfiSize
= 0)
293 CFMutableArrayRef oldref
= m_cfmaRef
;
294 m_cfmaRef
= CFArrayCreateMutableCopy(
301 void BuildCallbacks(CFArrayCallBacks
* pCbs
)
304 pCbs
->retain
= RetainProc
;
305 pCbs
->release
= ReleaseProc
;
306 pCbs
->copyDescription
= NULL
;
310 void Create(CFIndex cfiSize
= 0)
316 m_cfmaRef
= CFArrayCreateMutable(kCFAllocatorDefault
, cfiSize
, &cb
);
320 {if(m_cfmaRef
) CFRelease(m_cfmaRef
);}
322 static const void* RetainProc(CFAllocatorRef
, const void* v
)
323 { return (const void*) CFRetain(v
); }
325 static void ReleaseProc(CFAllocatorRef
, const void* v
)
328 operator CFTypeRef () const
329 { return (CFTypeRef
)m_cfmaRef
; }
331 CFArrayRef
GetCFArray() const
332 { return (CFArrayRef
)m_cfmaRef
; }
334 CFMutableArrayRef
GetCFMutableArray()
335 { return (CFMutableArrayRef
) m_cfmaRef
; }
337 CFTypeRef
operator [] (CFIndex cfiIndex
) const
340 return (CFTypeRef
) CFArrayGetValueAtIndex((CFArrayRef
)m_cfmaRef
, cfiIndex
);
346 return CFArrayGetCount((CFArrayRef
)m_cfmaRef
);
349 void Add(CFTypeRef cftValue
)
352 CFArrayAppendValue(m_cfmaRef
, cftValue
);
355 void Remove(CFIndex cfiIndex
)
358 wxASSERT(cfiIndex
< GetCount());
359 CFArrayRemoveValueAtIndex(m_cfmaRef
, cfiIndex
);
362 void Set(CFIndex cfiIndex
, CFTypeRef cftValue
)
365 wxASSERT(cfiIndex
< GetCount());
366 CFArraySetValueAtIndex(m_cfmaRef
, cfiIndex
, cftValue
);
369 bool IsOk() const {return m_cfmaRef
!= NULL
; }
373 return IsOk() && CFGetTypeID((CFTypeRef
)m_cfmaRef
) == CFArrayGetTypeID();
381 CFMutableArrayRef m_cfmaRef
;
384 // ----------------------------------------------------------------------------
386 // ----------------------------------------------------------------------------
391 wxCFString(CFTypeRef ref
, bool bRetain
= wxCF_RELEASE
) : m_Holder((CFStringRef
)ref
, bRetain
== wxCF_RELEASE
)
394 wxCFString(const wxChar
* szString
) : m_Holder(wxString(szString
), wxLocale::GetSystemEncoding())
397 wxCFString(const wxString
& sString
) : m_Holder(sString
, wxLocale::GetSystemEncoding())
400 operator CFTypeRef() const
401 {return (CFTypeRef
) ((CFStringRef
) m_Holder
); }
403 bool IsOk() { return ((CFTypeRef
)(*this)) != NULL
; }
405 wxString
BuildWXString() {return m_Holder
.AsString(); }
408 wxMacCFStringHolder m_Holder
;
411 // ----------------------------------------------------------------------------
413 // ----------------------------------------------------------------------------
418 wxCFNumber(int nValue
)
420 m_cfnRef
= CFNumberCreate(kCFAllocatorDefault
,
421 kCFNumberIntType
, &nValue
);
424 wxCFNumber(CFTypeRef ref
, bool bRetain
= wxCF_RELEASE
) : m_cfnRef((CFNumberRef
)ref
)
426 if(bRetain
== wxCF_RETAIN
&& ref
)
431 { if(m_cfnRef
) CFRelease(m_cfnRef
); }
434 operator CFTypeRef() const
435 { return (CFTypeRef
) m_cfnRef
; }
449 bool IsOk() { return m_cfnRef
!= NULL
; }
452 CFNumberRef m_cfnRef
;
455 // ----------------------------------------------------------------------------
457 // ----------------------------------------------------------------------------
462 wxCFURL(CFTypeRef ref
, bool bRetain
= wxCF_RELEASE
) : m_cfurlRef((CFURLRef
)ref
)
464 if(bRetain
== wxCF_RETAIN
&& ref
)
467 wxCFURL(const wxCFString
& URLString
, CFTypeRef BaseURL
= NULL
)
469 m_cfurlRef
= CFURLCreateWithString(
471 (CFStringRef
)(CFTypeRef
)URLString
,
474 ~wxCFURL() {if(m_cfurlRef
) CFRelease(m_cfurlRef
);}
476 wxString
BuildWXString()
478 return wxCFString(CFURLCopyPath(m_cfurlRef
)).BuildWXString();
481 operator CFTypeRef() const
482 { return (CFTypeRef
)m_cfurlRef
; }
484 bool IsOk() { return m_cfurlRef
!= NULL
; }
489 // ----------------------------------------------------------------------------
491 // ----------------------------------------------------------------------------
493 #define wxCFDATA_RELEASEBUFFER 1
494 #define wxCFDATA_RETAINBUFFER 0
499 wxCFData(CFTypeRef ref
, bool bRetain
= wxCF_RELEASE
) : m_cfdaRef((CFDataRef
)ref
)
501 if(bRetain
== wxCF_RETAIN
&& ref
)
504 wxCFData(const UInt8
* pBytes
, CFIndex len
, bool bKeep
= wxCFDATA_RELEASEBUFFER
)
506 if(bKeep
== wxCFDATA_RELEASEBUFFER
)
508 m_cfdaRef
= CFDataCreateWithBytesNoCopy
509 (kCFAllocatorDefault
, pBytes
, len
, kCFAllocatorDefault
);
513 m_cfdaRef
= CFDataCreate(kCFAllocatorDefault
, pBytes
, len
);
516 ~wxCFData() {if(m_cfdaRef
) CFRelease(m_cfdaRef
);}
518 const UInt8
* GetValue()
520 return CFDataGetBytePtr(m_cfdaRef
);
525 return CFDataGetLength(m_cfdaRef
);
528 operator CFTypeRef() const
529 { return (CFTypeRef
)m_cfdaRef
; }
531 bool IsOk() { return m_cfdaRef
!= NULL
; }
536 #endif //__WX_CFWRAPPERS_H__