File: cfdictionary.h

package info (click to toggle)
wxpython4.0 4.2.3%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 221,752 kB
  • sloc: cpp: 962,555; python: 230,573; ansic: 170,731; makefile: 51,756; sh: 9,342; perl: 1,564; javascript: 584; php: 326; xml: 200
file content (137 lines) | stat: -rw-r--r-- 4,180 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
/////////////////////////////////////////////////////////////////////////////
// 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_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>

/*! @class wxCFDictionaryRef
 @discussion Properly retains/releases reference to CoreFoundation data objects
 */
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.
     @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 function.
     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&) = default;

    wxCFTypeRef GetValue(const void* key)
    {
        CFTypeRef val = CFDictionaryGetValue(this->m_ptr, key);
        if (val)
            ::CFRetain(val);
        return val;
    }
};

class wxCFMutableDictionaryRef;

class wxCFDictionaryRef : public wxCFDictionaryRefCommon<CFDictionaryRef>
{
public:
    wxCFDictionaryRef()
    {
    }

    wxCFDictionaryRef(CFDictionaryRef r)
        : wxCFDictionaryRefCommon(r)
    {
    }

    wxCFDictionaryRef& operator=(const wxCFMutableDictionaryRef& other);

    CFDictionaryRef CreateCopy() const
    {
        return CFDictionaryCreateCopy(kCFAllocatorDefault, this->m_ptr);
    }

    CFMutableDictionaryRef CreateMutableCopy() const
    {
        return CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, this->m_ptr);
    }
};

class wxCFMutableDictionaryRef : public wxCFDictionaryRefCommon<CFMutableDictionaryRef>
{
public:
    wxCFMutableDictionaryRef()
        : wxCFDictionaryRefCommon(CFDictionaryCreateMutable(kCFAllocatorDefault, 0, &kCFTypeDictionaryKeyCallBacks, &kCFTypeDictionaryValueCallBacks))
    {
    }

    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)
    {
        SetValue(key, wxCFNumberRef(v));
    }

    CFMutableDictionaryRef CreateCopy() const
    {
        return CFDictionaryCreateMutableCopy(kCFAllocatorDefault, 0, this->m_ptr);
    }

    friend class wxCFDictionaryRef;
};

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__