~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to JavaScriptCore/wtf/RetainPtr.h

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// -*- mode: c++; c-basic-offset: 4 -*-
 
2
/*
 
3
 *  This file is part of the KDE libraries
 
4
 *  Copyright (C) 2005, 2006 Apple Computer, Inc.
 
5
 *
 
6
 *  This library is free software; you can redistribute it and/or
 
7
 *  modify it under the terms of the GNU Library General Public
 
8
 *  License as published by the Free Software Foundation; either
 
9
 *  version 2 of the License, or (at your option) any later version.
 
10
 *
 
11
 *  This library is distributed in the hope that it will be useful,
 
12
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 
14
 *  Library General Public License for more details.
 
15
 *
 
16
 *  You should have received a copy of the GNU Library General Public License
 
17
 *  along with this library; see the file COPYING.LIB.  If not, write to
 
18
 *  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
 
19
 *  Boston, MA 02110-1301, USA.
 
20
 *
 
21
 */
 
22
 
 
23
#ifndef RetainPtr_h
 
24
#define RetainPtr_h
 
25
 
 
26
#include <algorithm>
 
27
#include <CoreFoundation/CoreFoundation.h>
 
28
 
 
29
#ifdef __OBJC__
 
30
#import <Foundation/Foundation.h>
 
31
#endif
 
32
 
 
33
namespace WTF {
 
34
 
 
35
    template <typename T> struct RemovePointer {
 
36
        typedef T type;
 
37
    };
 
38
 
 
39
    template <typename T> struct RemovePointer<T*> {
 
40
        typedef T type;
 
41
    };
 
42
 
 
43
    // Unlike most most of our smart pointers, RetainPtr can take either the pointer type or the pointed-to type,
 
44
    // so both RetainPtr<NSDictionary> and RetainPtr<CFDictionaryRef> will work.
 
45
 
 
46
    enum AdoptCFTag { AdoptCF };
 
47
    enum AdoptNSTag { AdoptNS };
 
48
    
 
49
#ifdef __OBJC__
 
50
    inline void adoptNSReference(id ptr)
 
51
    {
 
52
        if (ptr) {
 
53
            CFRetain(ptr);
 
54
            [ptr release];
 
55
        }
 
56
    }
 
57
#endif
 
58
 
 
59
    template <typename T> class RetainPtr
 
60
    {
 
61
    public:
 
62
        typedef typename RemovePointer<T>::type ValueType;
 
63
        typedef ValueType* PtrType;
 
64
 
 
65
        RetainPtr() : m_ptr(0) {}
 
66
        RetainPtr(PtrType ptr) : m_ptr(ptr) { if (ptr) CFRetain(ptr); }
 
67
 
 
68
        RetainPtr(AdoptCFTag, PtrType ptr) : m_ptr(ptr) { }
 
69
        RetainPtr(AdoptNSTag, PtrType ptr) : m_ptr(ptr) { adoptNSReference(ptr); }
 
70
        
 
71
        RetainPtr(const RetainPtr& o) : m_ptr(o.m_ptr) { if (PtrType ptr = m_ptr) CFRetain(ptr); }
 
72
 
 
73
        ~RetainPtr() { if (PtrType ptr = m_ptr) CFRelease(ptr); }
 
74
        
 
75
        template <typename U> RetainPtr(const RetainPtr<U>& o) : m_ptr(o.get()) { if (PtrType ptr = m_ptr) CFRetain(ptr); }
 
76
        
 
77
        PtrType get() const { return m_ptr; }
 
78
        
 
79
        PtrType releaseRef() { PtrType tmp = m_ptr; m_ptr = 0; return tmp; }
 
80
        
 
81
        PtrType operator->() const { return m_ptr; }
 
82
        
 
83
        bool operator!() const { return !m_ptr; }
 
84
    
 
85
        // This conversion operator allows implicit conversion to bool but not to other integer types.
 
86
        typedef PtrType (RetainPtr::*UnspecifiedBoolType)() const;
 
87
        operator UnspecifiedBoolType() const { return m_ptr ? &RetainPtr::get : 0; }
 
88
        
 
89
        RetainPtr& operator=(const RetainPtr&);
 
90
        template <typename U> RetainPtr& operator=(const RetainPtr<U>&);
 
91
        RetainPtr& operator=(PtrType);
 
92
        template <typename U> RetainPtr& operator=(U*);
 
93
 
 
94
        void adoptCF(PtrType);
 
95
        void adoptNS(PtrType);
 
96
        
 
97
        void swap(RetainPtr&);
 
98
 
 
99
    private:
 
100
        PtrType m_ptr;
 
101
    };
 
102
    
 
103
    template <typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<T>& o)
 
104
    {
 
105
        PtrType optr = o.get();
 
106
        if (optr)
 
107
            CFRetain(optr);
 
108
        PtrType ptr = m_ptr;
 
109
        m_ptr = optr;
 
110
        if (ptr)
 
111
            CFRelease(ptr);
 
112
        return *this;
 
113
    }
 
114
    
 
115
    template <typename T> template <typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(const RetainPtr<U>& o)
 
116
    {
 
117
        PtrType optr = o.get();
 
118
        if (optr)
 
119
            CFRetain(optr);
 
120
        PtrType ptr = m_ptr;
 
121
        m_ptr = optr;
 
122
        if (ptr)
 
123
            CFRelease(ptr);
 
124
        return *this;
 
125
    }
 
126
    
 
127
    template <typename T> inline RetainPtr<T>& RetainPtr<T>::operator=(PtrType optr)
 
128
    {
 
129
        if (optr)
 
130
            CFRetain(optr);
 
131
        PtrType ptr = m_ptr;
 
132
        m_ptr = optr;
 
133
        if (ptr)
 
134
            CFRelease(ptr);
 
135
        return *this;
 
136
    }
 
137
 
 
138
    template <typename T> inline void RetainPtr<T>::adoptCF(PtrType optr)
 
139
    {
 
140
        PtrType ptr = m_ptr;
 
141
        m_ptr = optr;
 
142
        if (ptr)
 
143
            CFRelease(ptr);
 
144
    }
 
145
 
 
146
    template <typename T> inline void RetainPtr<T>::adoptNS(PtrType optr)
 
147
    {
 
148
        adoptNSReference(optr);
 
149
        
 
150
        PtrType ptr = m_ptr;
 
151
        m_ptr = optr;
 
152
        if (ptr)
 
153
            CFRelease(ptr);
 
154
    }
 
155
    
 
156
    template <typename T> template <typename U> inline RetainPtr<T>& RetainPtr<T>::operator=(U* optr)
 
157
    {
 
158
        if (optr)
 
159
            CFRetain(optr);
 
160
        PtrType ptr = m_ptr;
 
161
        m_ptr = optr;
 
162
        if (ptr)
 
163
            CFRelease(ptr);
 
164
        return *this;
 
165
    }
 
166
 
 
167
    template <class T> inline void RetainPtr<T>::swap(RetainPtr<T>& o)
 
168
    {
 
169
        std::swap(m_ptr, o.m_ptr);
 
170
    }
 
171
 
 
172
    template <class T> inline void swap(RetainPtr<T>& a, RetainPtr<T>& b)
 
173
    {
 
174
        a.swap(b);
 
175
    }
 
176
 
 
177
    template <typename T, typename U> inline bool operator==(const RetainPtr<T>& a, const RetainPtr<U>& b)
 
178
    { 
 
179
        return a.get() == b.get(); 
 
180
    }
 
181
 
 
182
    template <typename T, typename U> inline bool operator==(const RetainPtr<T>& a, U* b)
 
183
    { 
 
184
        return a.get() == b; 
 
185
    }
 
186
    
 
187
    template <typename T, typename U> inline bool operator==(T* a, const RetainPtr<U>& b) 
 
188
    {
 
189
        return a == b.get(); 
 
190
    }
 
191
    
 
192
    template <typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, const RetainPtr<U>& b)
 
193
    { 
 
194
        return a.get() != b.get(); 
 
195
    }
 
196
 
 
197
    template <typename T, typename U> inline bool operator!=(const RetainPtr<T>& a, U* b)
 
198
    {
 
199
        return a.get() != b; 
 
200
    }
 
201
 
 
202
    template <typename T, typename U> inline bool operator!=(T* a, const RetainPtr<U>& b)
 
203
    { 
 
204
        return a != b.get(); 
 
205
    }
 
206
 
 
207
} // namespace WTF
 
208
 
 
209
using WTF::AdoptCF;
 
210
using WTF::AdoptNS;
 
211
using WTF::RetainPtr;
 
212
 
 
213
#endif // WTF_RetainPtr_h