Adding wrapper for CFArray, updating other wrappers

This commit is contained in:
Stefan Csomor 2018-07-30 16:32:12 +02:00
parent a02efd1fc7
commit 4bda27c96c
9 changed files with 332 additions and 141 deletions

View File

@ -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 = \

View File

@ -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
</set>
<!-- Base files used by OS X ports (not Carbon) -->

View File

@ -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

View File

@ -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

View File

@ -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 <CoreFoundation/CFArray.h>
template <typename T, typename E>
class wxCFArrayRefCommon : public wxCFRef<T>
{
public:
typedef wxCFRef<T> 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<E> at(size_type idx)
{
wxASSERT(idx < size());
CFTypeRef val = CFArrayGetValueAtIndex(this->m_ptr, idx);
if (val)
::CFRetain(val);
return wxCFRefFromGet<E>(val);
}
wxCFRef<E> operator[](size_type idx) { return at(idx); }
wxCFRef<E> front() { return at(0); }
wxCFRef<E> back() { return at(size() - 1); }
};
template <typename E>
class wxCFArrayRef : public wxCFArrayRefCommon<CFArrayRef, E>
{
public:
wxCFArrayRef(CFArrayRef r)
: wxCFArrayRefCommon<CFArrayRef, E>(r)
{
}
wxCFArrayRef(const wxCFArrayRef& otherRef)
: wxCFArrayRefCommon<CFArrayRef, E>(otherRef)
{
}
};
template <typename E>
class wxCFMutableArrayRef : public wxCFArrayRefCommon<CFMutableArrayRef, E>
{
public:
wxCFMutableArrayRef()
: wxCFArrayRefCommon<CFMutableArrayRef, E>(CFArrayCreateMutable(kCFAllocatorDefault, 0, &kCFTypeArrayCallBacks))
{
}
wxCFMutableArrayRef(CFMutableArrayRef r)
: wxCFArrayRefCommon<CFMutableArrayRef, E>(r)
{
}
wxCFMutableArrayRef(const wxCFMutableArrayRef& otherRef)
: wxCFArrayRefCommon<CFMutableArrayRef, E>(otherRef)
{
}
void push_back(E v)
{
CFArrayAppendValue(this->m_ptr, v);
}
void clear()
{
CFArrayRemoveAllValues(this->m_ptr);
}
};
#endif //ifndef _WX_OSX_COREFOUNDATION_CFARRAYREF_H__

View File

@ -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 <CoreFoundation/CFDictionary.h>
#ifndef WX_PRECOMP
#include "wx/string.h"
#endif
class wxCFTypeRef : public wxCFRef<CFTypeRef>
{
public:
typedef wxCFRef<CFTypeRef> super_type;
wxCFTypeRef(CFTypeRef d)
: super_type(d)
{
}
template <typename V>
bool GetValue(V* ptr) const;
template <typename V>
bool GetValue(V* ptr, V defaultValue) const
{
bool hasKey = GetValue(ptr);
if (!hasKey)
*ptr = defaultValue;
return hasKey;
}
template <typename V>
bool GetValue(V& ref) const
{
return GetValue(&ref);
}
template <typename V>
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<typename T> class wxCFDictionaryRefCommon : public wxCFRef< T >
template <typename T>
class wxCFDictionaryRefCommon : public wxCFRef<T>
{
public:
typedef wxCFRef<T> 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<CFDictionaryRef>
{
public:
wxCFDictionaryRef()
{
}
explicit wxCFDictionaryRef(CFDictionaryRef r)
: wxCFDictionaryRefCommon(r)
{}
: wxCFDictionaryRefCommon(r)
{
}
wxCFDictionaryRef& operator=(const wxCFMutableDictionaryRef& other);
};
class wxCFMutableDictionaryRef : public wxCFDictionaryRefCommon< CFMutableDictionaryRef >
class wxCFMutableDictionaryRef : public wxCFDictionaryRefCommon<CFMutableDictionaryRef>
{
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__

View File

@ -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 <class otherType>
explicit wxCFRef(otherType *p)
: m_ptr(p) // Implicit conversion from otherType* to refType should occur.

View File

@ -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<CFTypeRef>
{
public:
typedef wxCFRef<CFTypeRef> super_type;
wxCFTypeRef(CFTypeRef d)
: super_type(d)
{
}
template <typename V>
bool GetValue(V* ptr) const;
template <typename V>
bool GetValue(V* ptr, V defaultValue) const
{
bool hasKey = GetValue(ptr);
if (!hasKey)
*ptr = defaultValue;
return hasKey;
}
template <typename V>
bool GetValue(V& ref) const
{
return GetValue(&ref);
}
template <typename V>
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__

View File

@ -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