From 342e4454238b32617fcd1fa49a1117d80a19237a Mon Sep 17 00:00:00 2001 From: Stefan Csomor Date: Sat, 28 Jul 2018 17:49:42 +0200 Subject: [PATCH] Adding wrapper for Core Foundation dictionaries --- include/wx/osx/core/cfdictionary.h | 203 +++++++++++++++++++++++++++++ include/wx/osx/core/private.h | 1 + 2 files changed, 204 insertions(+) create mode 100644 include/wx/osx/core/cfdictionary.h diff --git a/include/wx/osx/core/cfdictionary.h b/include/wx/osx/core/cfdictionary.h new file mode 100644 index 0000000000..a935791da3 --- /dev/null +++ b/include/wx/osx/core/cfdictionary.h @@ -0,0 +1,203 @@ +///////////////////////////////////////////////////////////////////////////// +// Name: wx/osx/core/cfdictionaryref.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/cfdictionaryref.h + @abstract wxCFDictionaryRef class + */ + +#ifndef _WX_OSX_COREFOUNDATION_CFICTIONARYREF_H__ +#define _WX_OSX_COREFOUNDATION_CFICTIONARYREF_H__ + +#include "wx/osx/core/cfref.h" +#include "wx/osx/core/cfstring.h" + +#include + +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 > +{ +public: + typedef wxCFRef 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. + @discussion Like shared_ptr, it is assumed that the caller has a strong reference to r 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. + This method is templated and takes an otherType *p. This prevents implicit conversion + using an operator refType() in a different ref-holding class type. + */ + explicit wxCFDictionaryRefCommon(T r) + : super_type(r) + {} + + /*! @method wxCFDictionaryRef + @abstract Copies a ref holder of the same type + @param otherRef The other ref holder to copy. + @discussion Ownership will be shared by the original ref and the newly created ref. That is, + the object will be explicitly retained by this new ref. + */ + wxCFDictionaryRefCommon(const wxCFDictionaryRefCommon& otherRef) + : super_type( otherRef ) + {} + + wxCFTypeRef GetValue(const void *key) + { + CFTypeRef val = CFDictionaryGetValue( this->m_ptr, key); + if ( val ) + ::CFRetain(val); + return val; + } +}; + +class wxCFDictionaryRef : public wxCFDictionaryRefCommon< CFDictionaryRef > +{ +public: + explicit wxCFDictionaryRef(CFDictionaryRef r) + : wxCFDictionaryRefCommon(r) + {} +}; + +class wxCFMutableDictionaryRef : public wxCFDictionaryRefCommon< CFMutableDictionaryRef > +{ +public: + wxCFMutableDictionaryRef() : wxCFDictionaryRefCommon(CFDictionaryCreateMutable(kCFAllocatorDefault, 0,&kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks)) + { + } + + explicit wxCFMutableDictionaryRef(CFMutableDictionaryRef r) + : wxCFDictionaryRefCommon(r) + {} + + void SetValue(const void *key, const void *data) + { + CFDictionarySetValue( this->m_ptr, key, data); + } + + void SetValue(const void*key, CGFloat v) + { + + } +}; + +#endif //ifndef _WX_OSX_COREFOUNDATION_CFICTIONARYREF_H__ + diff --git a/include/wx/osx/core/private.h b/include/wx/osx/core/private.h index 109f420124..3175109d38 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/cfdictionary.h" // platform specific Clang analyzer support #ifndef NS_RETURNS_RETAINED