diff --git a/Makefile.in b/Makefile.in index a4792e10f8..f0363aebb4 100644 --- a/Makefile.in +++ b/Makefile.in @@ -2441,6 +2441,8 @@ COND_TOOLKIT_OSX_COCOA_BASE_OSX_HDR = \ wx/osx/core/colour.h \ wx/osx/carbon/region.h \ wx/osx/core/cfdictionary.h \ + wx/osx/core/cfarray.h \ + wx/osx/core/cftype.h \ wx/unix/app.h \ wx/unix/apptbase.h \ wx/unix/apptrait.h \ @@ -2474,7 +2476,9 @@ COND_TOOLKIT_COCOA_BASE_OSX_HDR = \ wx/osx/core/private.h \ wx/osx/core/colour.h \ wx/osx/carbon/region.h \ - wx/osx/core/cfdictionary.h + wx/osx/core/cfdictionary.h \ + wx/osx/core/cfarray.h \ + wx/osx/core/cftype.h @COND_TOOLKIT_COCOA@BASE_OSX_HDR = $(COND_TOOLKIT_COCOA_BASE_OSX_HDR) COND_TOOLKIT_GTK_BASE_OSX_HDR = \ wx/unix/app.h \ @@ -2497,7 +2501,9 @@ COND_TOOLKIT_GTK_BASE_OSX_HDR = \ wx/osx/core/private.h \ wx/osx/core/colour.h \ wx/osx/carbon/region.h \ - wx/osx/core/cfdictionary.h + wx/osx/core/cfdictionary.h \ + wx/osx/core/cfarray.h \ + wx/osx/core/cftype.h @COND_TOOLKIT_GTK@BASE_OSX_HDR = $(COND_TOOLKIT_GTK_BASE_OSX_HDR) COND_TOOLKIT_X11_BASE_OSX_HDR = \ wx/unix/app.h \ @@ -2520,7 +2526,9 @@ COND_TOOLKIT_X11_BASE_OSX_HDR = \ wx/osx/core/private.h \ wx/osx/core/colour.h \ wx/osx/carbon/region.h \ - wx/osx/core/cfdictionary.h + wx/osx/core/cfdictionary.h \ + wx/osx/core/cfarray.h \ + wx/osx/core/cftype.h @COND_TOOLKIT_X11@BASE_OSX_HDR = $(COND_TOOLKIT_X11_BASE_OSX_HDR) COND_TOOLKIT_MOTIF_BASE_OSX_HDR = \ wx/unix/app.h \ @@ -2543,7 +2551,9 @@ COND_TOOLKIT_MOTIF_BASE_OSX_HDR = \ wx/osx/core/private.h \ wx/osx/core/colour.h \ wx/osx/carbon/region.h \ - wx/osx/core/cfdictionary.h + wx/osx/core/cfdictionary.h \ + wx/osx/core/cfarray.h \ + wx/osx/core/cftype.h @COND_TOOLKIT_MOTIF@BASE_OSX_HDR = $(COND_TOOLKIT_MOTIF_BASE_OSX_HDR) COND_TOOLKIT__BASE_OSX_HDR = \ wx/unix/app.h \ @@ -2566,7 +2576,9 @@ COND_TOOLKIT__BASE_OSX_HDR = \ wx/osx/core/private.h \ wx/osx/core/colour.h \ wx/osx/carbon/region.h \ - wx/osx/core/cfdictionary.h + wx/osx/core/cfdictionary.h \ + wx/osx/core/cfarray.h \ + wx/osx/core/cftype.h @COND_TOOLKIT_@BASE_OSX_HDR = $(COND_TOOLKIT__BASE_OSX_HDR) @COND_PLATFORM_MACOSX_1@BASE_PLATFORM_HDR = $(BASE_OSX_HDR) COND_PLATFORM_UNIX_1_BASE_PLATFORM_HDR = \ diff --git a/build/bakefiles/files.bkl b/build/bakefiles/files.bkl index c464f898f0..ea64e2d1d6 100644 --- a/build/bakefiles/files.bkl +++ b/build/bakefiles/files.bkl @@ -200,6 +200,8 @@ IMPORTANT: please read docs/tech/tn0016.txt before modifying this file! wx/osx/core/colour.h wx/osx/carbon/region.h wx/osx/core/cfdictionary.h + wx/osx/core/cfarray.h + wx/osx/core/cftype.h diff --git a/build/cmake/files.cmake b/build/cmake/files.cmake index e503ef5777..c8edde689c 100644 --- a/build/cmake/files.cmake +++ b/build/cmake/files.cmake @@ -128,6 +128,8 @@ set(BASE_COREFOUNDATION_HDR wx/osx/core/objcid.h wx/osx/core/private.h wx/osx/core/cfdictionary.h + wx/osx/core/cfarray.h + wx/osx/core/cftype.h ) set(BASE_OSX_SHARED_SRC diff --git a/build/files b/build/files index 396ed5075a..9f7f68c0b5 100644 --- a/build/files +++ b/build/files @@ -145,7 +145,9 @@ BASE_COREFOUNDATION_SRC = BASE_COREFOUNDATION_HDR = wx/osx/carbon/region.h wx/osx/core/cfdataref.h + wx/osx/core/cfarray.h wx/osx/core/cfdictionary.h + wx/osx/core/cftype.h wx/osx/core/cfref.h wx/osx/core/cfstring.h wx/osx/core/colour.h diff --git a/include/wx/osx/core/cfarray.h b/include/wx/osx/core/cfarray.h new file mode 100644 index 0000000000..b16c075625 --- /dev/null +++ b/include/wx/osx/core/cfarray.h @@ -0,0 +1,110 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: wx/osx/core/cfarrayref.h +// Purpose: wxCFArrayRef class +// Author: Stefan Csomor +// Modified by: +// Created: 2018/07/27 +// Copyright: (c) 2018 Stefan Csomor +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// +/*! @header wx/osx/core/cfarrayref.h + @abstract wxCFArrayRef class + */ + +#ifndef _WX_OSX_COREFOUNDATION_CFARRAYREF_H__ +#define _WX_OSX_COREFOUNDATION_CFARRAYREF_H__ + +#include "wx/osx/core/cfref.h" +#include "wx/osx/core/cftype.h" + +#include + +template +class wxCFArrayRefCommon : public wxCFRef +{ +public: + typedef wxCFRef super_type; + typedef size_t size_type; + + wxCFArrayRefCommon(T r) + : super_type(r) + { + } + + wxCFArrayRefCommon(const wxCFArrayRefCommon& otherRef) + : super_type(otherRef) + { + } + + size_type size() const + { + return (size_type)CFArrayGetCount(this->m_ptr); + } + + bool empty() const + { + return size() == 0; + } + + wxCFRef at(size_type idx) + { + wxASSERT(idx < size()); + + CFTypeRef val = CFArrayGetValueAtIndex(this->m_ptr, idx); + if (val) + ::CFRetain(val); + return wxCFRefFromGet(val); + } + + wxCFRef operator[](size_type idx) { return at(idx); } + wxCFRef front() { return at(0); } + wxCFRef back() { return at(size() - 1); } +}; + + +template +class wxCFArrayRef : public wxCFArrayRefCommon +{ +public: + wxCFArrayRef(CFArrayRef r) + : wxCFArrayRefCommon(r) + { + } + + wxCFArrayRef(const wxCFArrayRef& otherRef) + : wxCFArrayRefCommon(otherRef) + { + } +}; + +template +class wxCFMutableArrayRef : public wxCFArrayRefCommon +{ +public: + wxCFMutableArrayRef() + : wxCFArrayRefCommon(CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks)) + { + } + + wxCFMutableArrayRef(CFMutableArrayRef r) + : wxCFArrayRefCommon(r) + { + } + + wxCFMutableArrayRef(const wxCFMutableArrayRef& otherRef) + : wxCFArrayRefCommon(otherRef) + { + } + + void push_back(E v) + { + CFArrayAppendValue(this->m_ptr, v); + } + + void clear() + { + CFArrayRemoveAllValues(this->m_ptr); + } +}; + +#endif //ifndef _WX_OSX_COREFOUNDATION_CFARRAYREF_H__ diff --git a/include/wx/osx/core/cfdictionary.h b/include/wx/osx/core/cfdictionary.h index e3aeec32bb..2088450060 100644 --- a/include/wx/osx/core/cfdictionary.h +++ b/include/wx/osx/core/cfdictionary.h @@ -11,134 +11,29 @@ @abstract wxCFDictionaryRef class */ -#ifndef _WX_OSX_COREFOUNDATION_CFICTIONARYREF_H__ -#define _WX_OSX_COREFOUNDATION_CFICTIONARYREF_H__ +#ifndef _WX_OSX_COREFOUNDATION_CFDICTIONARYREF_H__ +#define _WX_OSX_COREFOUNDATION_CFDICTIONARYREF_H__ #include "wx/osx/core/cfref.h" #include "wx/osx/core/cfstring.h" +#include "wx/osx/core/cftype.h" #include -#ifndef WX_PRECOMP -#include "wx/string.h" -#endif - -class wxCFTypeRef : public wxCFRef -{ -public: - typedef wxCFRef super_type; - - wxCFTypeRef(CFTypeRef d) - : super_type(d) - { - } - - template - bool GetValue(V* ptr) const; - - template - bool GetValue(V* ptr, V defaultValue) const - { - bool hasKey = GetValue(ptr); - - if (!hasKey) - *ptr = defaultValue; - - return hasKey; - } - - template - bool GetValue(V& ref) const - { - return GetValue(&ref); - } - - template - bool GetValue(V& ref, V defaultValue) const - { - bool hasKey = GetValue(ref); - - if (!hasKey) - ref = defaultValue; - - return hasKey; - } - - // spezialization through overload - - bool GetValue(CGFloat* ptr) const - { - if ( m_ptr ) - CFNumberGetValue((CFNumberRef)m_ptr, kCFNumberCGFloatType, ptr); - - return m_ptr; - } - - bool GetValue(int32_t* ptr) const - { - if ( m_ptr ) - CFNumberGetValue((CFNumberRef)m_ptr, kCFNumberSInt32Type, ptr); - - return m_ptr; - } - - bool GetValue(uint32_t* ptr) const - { - if ( m_ptr ) - CFNumberGetValue((CFNumberRef)m_ptr, kCFNumberSInt32Type, ptr); - - return m_ptr; - } - - bool GetValue(int64_t* ptr) const - { - if ( m_ptr ) - CFNumberGetValue((CFNumberRef)m_ptr, kCFNumberSInt64Type, ptr); - - return m_ptr; - } - - bool GetValue(uint64_t* ptr) const - { - if ( m_ptr ) - CFNumberGetValue((CFNumberRef)m_ptr, kCFNumberSInt64Type, ptr); - - return m_ptr; - } - - bool GetValue(wxString *s) const - { - if ( m_ptr ) - *s = wxCFStringRef::AsString((CFStringRef)m_ptr); - - return m_ptr; - - } - -}; - -class wxCFNumberRef : public wxCFTypeRef -{ -public: - wxCFNumberRef(CGFloat v) - : wxCFTypeRef(CFNumberCreate( NULL, kCFNumberCGFloatType, &v)) - { - } - - wxCFNumberRef(int v) - : wxCFTypeRef(CFNumberCreate( NULL, kCFNumberIntType, &v)) - { - } -}; - /*! @class wxCFDictionaryRef @discussion Properly retains/releases reference to CoreFoundation data objects */ -template class wxCFDictionaryRefCommon : public wxCFRef< T > +template +class wxCFDictionaryRefCommon : public wxCFRef { public: typedef wxCFRef super_type; + explicit wxCFDictionaryRefCommon() + : super_type() + { + } + /*! @method wxCFDictionaryRef @abstract Assumes ownership of r and creates a reference to it. @param r The dictionary reference to assume ownership of. May be NULL. @@ -151,8 +46,9 @@ public: using an operator refType() in a different ref-holding class type. */ explicit wxCFDictionaryRefCommon(T r) - : super_type(r) - {} + : super_type(r) + { + } /*! @method wxCFDictionaryRef @abstract Copies a ref holder of the same type @@ -161,47 +57,69 @@ public: the object will be explicitly retained by this new ref. */ wxCFDictionaryRefCommon(const wxCFDictionaryRefCommon& otherRef) - : super_type( otherRef ) - {} - - wxCFTypeRef GetValue(const void *key) + : super_type(otherRef) { - CFTypeRef val = CFDictionaryGetValue( this->m_ptr, key); - if ( val ) + } + + wxCFTypeRef GetValue(const void* key) + { + CFTypeRef val = CFDictionaryGetValue(this->m_ptr, key); + if (val) ::CFRetain(val); return val; } }; -class wxCFDictionaryRef : public wxCFDictionaryRefCommon< CFDictionaryRef > +class wxCFMutableDictionaryRef; + +class wxCFDictionaryRef : public wxCFDictionaryRefCommon { public: + wxCFDictionaryRef() + { + } + explicit wxCFDictionaryRef(CFDictionaryRef r) - : wxCFDictionaryRefCommon(r) - {} + : wxCFDictionaryRefCommon(r) + { + } + + wxCFDictionaryRef& operator=(const wxCFMutableDictionaryRef& other); }; -class wxCFMutableDictionaryRef : public wxCFDictionaryRefCommon< CFMutableDictionaryRef > +class wxCFMutableDictionaryRef : public wxCFDictionaryRefCommon { public: - wxCFMutableDictionaryRef() : wxCFDictionaryRefCommon(CFDictionaryCreateMutable(kCFAllocatorDefault, 0,&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)) + wxCFMutableDictionaryRef() + : wxCFDictionaryRefCommon(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)) { } explicit wxCFMutableDictionaryRef(CFMutableDictionaryRef r) - : wxCFDictionaryRefCommon(r) - {} + : wxCFDictionaryRefCommon(r) + { + } - void SetValue(const void *key, const void *data) + void SetValue(const void* key, const void* data) { - CFDictionarySetValue( this->m_ptr, key, data); + CFDictionarySetValue(this->m_ptr, key, data); } - - void SetValue(const void*key, CGFloat v) + + void SetValue(const void* key, CGFloat v) { - SetValue( key, wxCFNumberRef(v)); + SetValue(key, wxCFNumberRef(v)); } + + friend class wxCFDictionaryRef; }; -#endif //ifndef _WX_OSX_COREFOUNDATION_CFICTIONARYREF_H__ +inline wxCFDictionaryRef& wxCFDictionaryRef::operator=(const wxCFMutableDictionaryRef& otherRef) +{ + wxCFRetain(otherRef.m_ptr); + wxCFRelease(m_ptr); + m_ptr = (CFDictionaryRef)otherRef.m_ptr; + return *this; +} + +#endif //ifndef _WX_OSX_COREFOUNDATION_CFDICTIONARYREF_H__ diff --git a/include/wx/osx/core/cfref.h b/include/wx/osx/core/cfref.h index c4a6b3c17a..54580da695 100644 --- a/include/wx/osx/core/cfref.h +++ b/include/wx/osx/core/cfref.h @@ -165,6 +165,19 @@ public: : m_ptr(NULL) {} + /*! @method wxCFRef + @abstract Assumes ownership of p and creates a reference to it. + @param p The raw core foundation reference to assume ownership of. May be NULL. + @discussion Like shared_ptr, it is assumed that the caller has a strong reference to p and intends + to transfer ownership of that reference to this ref holder. If the object comes from + a Create or Copy method then this is the correct behaviour. If the object comes from + a Get method then you must CFRetain it yourself before passing it to this constructor. + A handy way to do this is to use the non-member wxCFRefFromGet factory funcion. + */ + wxCFRef(refType p) : m_ptr(p) + { + + } /*! @method wxCFRef @abstract Assumes ownership of p and creates a reference to it. @templatefield otherType Any type. @@ -177,6 +190,7 @@ public: This method is templated and takes an otherType *p. This prevents implicit conversion using an operator refType() in a different ref-holding class type. */ + template explicit wxCFRef(otherType *p) : m_ptr(p) // Implicit conversion from otherType* to refType should occur. diff --git a/include/wx/osx/core/cftype.h b/include/wx/osx/core/cftype.h new file mode 100644 index 0000000000..0b6fbc4c2f --- /dev/null +++ b/include/wx/osx/core/cftype.h @@ -0,0 +1,130 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: wx/osx/core/cftype.h +// Purpose: wxCFDictionaryRef class +// Author: Stefan Csomor +// Modified by: +// Created: 2018/07/27 +// Copyright: (c) 2018 Stefan Csomor +// Licence: wxWindows licence +///////////////////////////////////////////////////////////////////////////// +/*! @header wx/osx/core/cftype.h + @abstract wxCFTypeRef class and derived classes + */ + +#ifndef _WX_OSX_COREFOUNDATION_CFTYPEREF_H__ +#define _WX_OSX_COREFOUNDATION_CFTYPEREF_H__ + +#include "wx/osx/core/cfref.h" +#include "wx/osx/core/cfstring.h" + +#ifndef WX_PRECOMP +#include "wx/string.h" +#endif + +class wxCFTypeRef : public wxCFRef +{ +public: + typedef wxCFRef super_type; + + wxCFTypeRef(CFTypeRef d) + : super_type(d) + { + } + + template + bool GetValue(V* ptr) const; + + template + bool GetValue(V* ptr, V defaultValue) const + { + bool hasKey = GetValue(ptr); + + if (!hasKey) + *ptr = defaultValue; + + return hasKey; + } + + template + bool GetValue(V& ref) const + { + return GetValue(&ref); + } + + template + bool GetValue(V& ref, V defaultValue) const + { + bool hasKey = GetValue(ref); + + if (!hasKey) + ref = defaultValue; + + return hasKey; + } + + // spezialization through overload + + bool GetValue(CGFloat* ptr) const + { + if (m_ptr) + CFNumberGetValue((CFNumberRef)m_ptr, kCFNumberCGFloatType, ptr); + + return m_ptr; + } + + bool GetValue(int32_t* ptr) const + { + if (m_ptr) + CFNumberGetValue((CFNumberRef)m_ptr, kCFNumberSInt32Type, ptr); + + return m_ptr; + } + + bool GetValue(uint32_t* ptr) const + { + if (m_ptr) + CFNumberGetValue((CFNumberRef)m_ptr, kCFNumberSInt32Type, ptr); + + return m_ptr; + } + + bool GetValue(int64_t* ptr) const + { + if (m_ptr) + CFNumberGetValue((CFNumberRef)m_ptr, kCFNumberSInt64Type, ptr); + + return m_ptr; + } + + bool GetValue(uint64_t* ptr) const + { + if (m_ptr) + CFNumberGetValue((CFNumberRef)m_ptr, kCFNumberSInt64Type, ptr); + + return m_ptr; + } + + bool GetValue(wxString* s) const + { + if (m_ptr) + *s = wxCFStringRef::AsString((CFStringRef)m_ptr); + + return m_ptr; + } +}; + +class wxCFNumberRef : public wxCFTypeRef +{ +public: + wxCFNumberRef(CGFloat v) + : wxCFTypeRef(CFNumberCreate(NULL, kCFNumberCGFloatType, &v)) + { + } + + wxCFNumberRef(int v) + : wxCFTypeRef(CFNumberCreate(NULL, kCFNumberIntType, &v)) + { + } +}; + +#endif //ifndef _WX_OSX_COREFOUNDATION_CFTYPEREF_H__ diff --git a/include/wx/osx/core/private.h b/include/wx/osx/core/private.h index 3175109d38..d5acfa3e13 100644 --- a/include/wx/osx/core/private.h +++ b/include/wx/osx/core/private.h @@ -19,6 +19,7 @@ #include "wx/osx/core/cfstring.h" #include "wx/osx/core/cfdataref.h" +#include "wx/osx/core/cfarray.h" #include "wx/osx/core/cfdictionary.h" // platform specific Clang analyzer support