~mir-team/mir/x11-multi-window

« back to all changes in this revision

Viewing changes to 3rd_party/android-deps/utils/RefBase.h

  • Committer: Andreas Pokorny
  • Date: 2017-02-15 14:45:41 UTC
  • mto: This revision was merged to the branch mainline in revision 4036.
  • Revision ID: andreas.pokorny@canonical.com-20170215144541-f0i749qairmbt9yv
mirserver: remove android-input and mirserver parts no longer neede

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright (C) 2005 The Android Open Source Project
3
 
 *
4
 
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 
 * you may not use this file except in compliance with the License.
6
 
 * You may obtain a copy of the License at
7
 
 *
8
 
 *      http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
10
 
 * Unless required by applicable law or agreed to in writing, software
11
 
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 
 * See the License for the specific language governing permissions and
14
 
 * limitations under the License.
15
 
 */
16
 
 
17
 
#ifndef ANDROID_REF_BASE_H
18
 
#define ANDROID_REF_BASE_H
19
 
 
20
 
 
21
 
#include <std/atomic.h>
22
 
 
23
 
#include <stdint.h>
24
 
#include <sys/types.h>
25
 
#include <stdlib.h>
26
 
#include <string.h>
27
 
 
28
 
#include <std/StrongPointer.h>
29
 
#include <std/TypeHelpers.h>
30
 
 
31
 
// ---------------------------------------------------------------------------
32
 
namespace android {
33
 
 
34
 
// ---------------------------------------------------------------------------
35
 
 
36
 
#define COMPARE_WEAK(_op_)                                      \
37
 
inline bool operator _op_ (const sp<T>& o) const {              \
38
 
    return m_ptr _op_ o.m_ptr;                                  \
39
 
}                                                               \
40
 
inline bool operator _op_ (const T* o) const {                  \
41
 
    return m_ptr _op_ o;                                        \
42
 
}                                                               \
43
 
template<typename U>                                            \
44
 
inline bool operator _op_ (const sp<U>& o) const {              \
45
 
    return m_ptr _op_ o.m_ptr;                                  \
46
 
}                                                               \
47
 
template<typename U>                                            \
48
 
inline bool operator _op_ (const U* o) const {                  \
49
 
    return m_ptr _op_ o;                                        \
50
 
}
51
 
 
52
 
// ---------------------------------------------------------------------------
53
 
class ReferenceMover;
54
 
class ReferenceConverterBase {
55
 
public:
56
 
    virtual size_t getReferenceTypeSize() const = 0;
57
 
    virtual void* getReferenceBase(void const*) const = 0;
58
 
    inline virtual ~ReferenceConverterBase() { }
59
 
};
60
 
 
61
 
// ---------------------------------------------------------------------------
62
 
 
63
 
class RefBase
64
 
{
65
 
public:
66
 
            void            incStrong(const void* id) const;
67
 
            void            decStrong(const void* id) const;
68
 
    
69
 
            void            forceIncStrong(const void* id) const;
70
 
 
71
 
            //! DEBUGGING ONLY: Get current strong ref count.
72
 
            int32_t         getStrongCount() const;
73
 
 
74
 
    class weakref_type
75
 
    {
76
 
    public:
77
 
        RefBase*            refBase() const;
78
 
        
79
 
        void                incWeak(const void* id);
80
 
        void                decWeak(const void* id);
81
 
        
82
 
        // acquires a strong reference if there is already one.
83
 
        bool                attemptIncStrong(const void* id);
84
 
        
85
 
        // acquires a weak reference if there is already one.
86
 
        // This is not always safe. see ProcessState.cpp and BpBinder.cpp
87
 
        // for proper use.
88
 
        bool                attemptIncWeak(const void* id);
89
 
 
90
 
        //! DEBUGGING ONLY: Get current weak ref count.
91
 
        int32_t             getWeakCount() const;
92
 
 
93
 
        //! DEBUGGING ONLY: Print references held on object.
94
 
        void                printRefs() const;
95
 
 
96
 
        //! DEBUGGING ONLY: Enable tracking for this object.
97
 
        // enable -- enable/disable tracking
98
 
        // retain -- when tracking is enable, if true, then we save a stack trace
99
 
        //           for each reference and dereference; when retain == false, we
100
 
        //           match up references and dereferences and keep only the 
101
 
        //           outstanding ones.
102
 
        
103
 
        void                trackMe(bool enable, bool retain);
104
 
    };
105
 
    
106
 
            weakref_type*   createWeak(const void* id) const;
107
 
            
108
 
            weakref_type*   getWeakRefs() const;
109
 
 
110
 
            //! DEBUGGING ONLY: Print references held on object.
111
 
    inline  void            printRefs() const { getWeakRefs()->printRefs(); }
112
 
 
113
 
            //! DEBUGGING ONLY: Enable tracking of object.
114
 
    inline  void            trackMe(bool enable, bool retain)
115
 
    { 
116
 
        getWeakRefs()->trackMe(enable, retain); 
117
 
    }
118
 
 
119
 
    typedef RefBase basetype;
120
 
 
121
 
protected:
122
 
                            RefBase();
123
 
    virtual                 ~RefBase();
124
 
 
125
 
    //! Flags for extendObjectLifetime()
126
 
    enum {
127
 
        OBJECT_LIFETIME_STRONG  = 0x0000,
128
 
        OBJECT_LIFETIME_WEAK    = 0x0001,
129
 
        OBJECT_LIFETIME_MASK    = 0x0001
130
 
    };
131
 
    
132
 
            void            extendObjectLifetime(int32_t mode);
133
 
            
134
 
    //! Flags for onIncStrongAttempted()
135
 
    enum {
136
 
        FIRST_INC_STRONG = 0x0001
137
 
    };
138
 
    
139
 
    virtual void            onFirstRef();
140
 
    virtual void            onLastStrongRef(const void* id);
141
 
    virtual bool            onIncStrongAttempted(uint32_t flags, const void* id);
142
 
    virtual void            onLastWeakRef(const void* id);
143
 
 
144
 
private:
145
 
    friend class ReferenceMover;
146
 
    static void moveReferences(void* d, void const* s, size_t n,
147
 
            const ReferenceConverterBase& caster);
148
 
 
149
 
private:
150
 
    friend class weakref_type;
151
 
    class weakref_impl;
152
 
    
153
 
                            RefBase(const RefBase& o);
154
 
            RefBase&        operator=(const RefBase& o);
155
 
 
156
 
        weakref_impl* const mRefs;
157
 
};
158
 
 
159
 
// ---------------------------------------------------------------------------
160
 
 
161
 
template <typename T>
162
 
class wp
163
 
{
164
 
public:
165
 
    typedef typename RefBase::weakref_type weakref_type;
166
 
    
167
 
    inline wp() : m_ptr(0) { }
168
 
 
169
 
    wp(T* other);
170
 
    wp(const wp<T>& other);
171
 
    wp(const sp<T>& other);
172
 
    template<typename U> wp(U* other);
173
 
    template<typename U> wp(const sp<U>& other);
174
 
    template<typename U> wp(const wp<U>& other);
175
 
 
176
 
    ~wp();
177
 
    
178
 
    // Assignment
179
 
 
180
 
    wp& operator = (T* other);
181
 
    wp& operator = (const wp<T>& other);
182
 
    wp& operator = (const sp<T>& other);
183
 
    
184
 
    template<typename U> wp& operator = (U* other);
185
 
    template<typename U> wp& operator = (const wp<U>& other);
186
 
    template<typename U> wp& operator = (const sp<U>& other);
187
 
    
188
 
    void set_object_and_refs(T* other, weakref_type* refs);
189
 
 
190
 
    // promotion to sp
191
 
    
192
 
    sp<T> promote() const;
193
 
 
194
 
    // Reset
195
 
    
196
 
    void clear();
197
 
 
198
 
    // Accessors
199
 
    
200
 
    inline  weakref_type* get_refs() const { return m_refs; }
201
 
    
202
 
    inline  T* unsafe_get() const { return m_ptr; }
203
 
 
204
 
    // Operators
205
 
 
206
 
    COMPARE_WEAK(==)
207
 
    COMPARE_WEAK(!=)
208
 
    COMPARE_WEAK(>)
209
 
    COMPARE_WEAK(<)
210
 
    COMPARE_WEAK(<=)
211
 
    COMPARE_WEAK(>=)
212
 
 
213
 
    inline bool operator == (const wp<T>& o) const {
214
 
        return (m_ptr == o.m_ptr) && (m_refs == o.m_refs);
215
 
    }
216
 
    template<typename U>
217
 
    inline bool operator == (const wp<U>& o) const {
218
 
        return m_ptr == o.m_ptr;
219
 
    }
220
 
 
221
 
    inline bool operator > (const wp<T>& o) const {
222
 
        return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
223
 
    }
224
 
    template<typename U>
225
 
    inline bool operator > (const wp<U>& o) const {
226
 
        return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
227
 
    }
228
 
 
229
 
    inline bool operator < (const wp<T>& o) const {
230
 
        return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
231
 
    }
232
 
    template<typename U>
233
 
    inline bool operator < (const wp<U>& o) const {
234
 
        return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
235
 
    }
236
 
                         inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; }
237
 
    template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); }
238
 
                         inline bool operator <= (const wp<T>& o) const { return !operator > (o); }
239
 
    template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); }
240
 
                         inline bool operator >= (const wp<T>& o) const { return !operator < (o); }
241
 
    template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); }
242
 
 
243
 
private:
244
 
    template<typename Y> friend class sp;
245
 
    template<typename Y> friend class wp;
246
 
 
247
 
    T*              m_ptr;
248
 
    weakref_type*   m_refs;
249
 
};
250
 
 
251
 
#undef COMPARE_WEAK
252
 
 
253
 
// ---------------------------------------------------------------------------
254
 
// No user serviceable parts below here.
255
 
 
256
 
template<typename T>
257
 
wp<T>::wp(T* other)
258
 
    : m_ptr(other)
259
 
{
260
 
    if (other) m_refs = other->createWeak(this);
261
 
}
262
 
 
263
 
template<typename T>
264
 
wp<T>::wp(const wp<T>& other)
265
 
    : m_ptr(other.m_ptr), m_refs(other.m_refs)
266
 
{
267
 
    if (m_ptr) m_refs->incWeak(this);
268
 
}
269
 
 
270
 
template<typename T>
271
 
wp<T>::wp(const sp<T>& other)
272
 
    : m_ptr(other.m_ptr)
273
 
{
274
 
    if (m_ptr) {
275
 
        m_refs = m_ptr->createWeak(this);
276
 
    }
277
 
}
278
 
 
279
 
template<typename T> template<typename U>
280
 
wp<T>::wp(U* other)
281
 
    : m_ptr(other)
282
 
{
283
 
    if (other) m_refs = other->createWeak(this);
284
 
}
285
 
 
286
 
template<typename T> template<typename U>
287
 
wp<T>::wp(const wp<U>& other)
288
 
    : m_ptr(other.m_ptr)
289
 
{
290
 
    if (m_ptr) {
291
 
        m_refs = other.m_refs;
292
 
        m_refs->incWeak(this);
293
 
    }
294
 
}
295
 
 
296
 
template<typename T> template<typename U>
297
 
wp<T>::wp(const sp<U>& other)
298
 
    : m_ptr(other.m_ptr)
299
 
{
300
 
    if (m_ptr) {
301
 
        m_refs = m_ptr->createWeak(this);
302
 
    }
303
 
}
304
 
 
305
 
template<typename T>
306
 
wp<T>::~wp()
307
 
{
308
 
    if (m_ptr) m_refs->decWeak(this);
309
 
}
310
 
 
311
 
template<typename T>
312
 
wp<T>& wp<T>::operator = (T* other)
313
 
{
314
 
    weakref_type* newRefs =
315
 
        other ? other->createWeak(this) : 0;
316
 
    if (m_ptr) m_refs->decWeak(this);
317
 
    m_ptr = other;
318
 
    m_refs = newRefs;
319
 
    return *this;
320
 
}
321
 
 
322
 
template<typename T>
323
 
wp<T>& wp<T>::operator = (const wp<T>& other)
324
 
{
325
 
    weakref_type* otherRefs(other.m_refs);
326
 
    T* otherPtr(other.m_ptr);
327
 
    if (otherPtr) otherRefs->incWeak(this);
328
 
    if (m_ptr) m_refs->decWeak(this);
329
 
    m_ptr = otherPtr;
330
 
    m_refs = otherRefs;
331
 
    return *this;
332
 
}
333
 
 
334
 
template<typename T>
335
 
wp<T>& wp<T>::operator = (const sp<T>& other)
336
 
{
337
 
    weakref_type* newRefs =
338
 
        other != NULL ? other->createWeak(this) : 0;
339
 
    T* otherPtr(other.m_ptr);
340
 
    if (m_ptr) m_refs->decWeak(this);
341
 
    m_ptr = otherPtr;
342
 
    m_refs = newRefs;
343
 
    return *this;
344
 
}
345
 
 
346
 
template<typename T> template<typename U>
347
 
wp<T>& wp<T>::operator = (U* other)
348
 
{
349
 
    weakref_type* newRefs =
350
 
        other ? other->createWeak(this) : 0;
351
 
    if (m_ptr) m_refs->decWeak(this);
352
 
    m_ptr = other;
353
 
    m_refs = newRefs;
354
 
    return *this;
355
 
}
356
 
 
357
 
template<typename T> template<typename U>
358
 
wp<T>& wp<T>::operator = (const wp<U>& other)
359
 
{
360
 
    weakref_type* otherRefs(other.m_refs);
361
 
    U* otherPtr(other.m_ptr);
362
 
    if (otherPtr) otherRefs->incWeak(this);
363
 
    if (m_ptr) m_refs->decWeak(this);
364
 
    m_ptr = otherPtr;
365
 
    m_refs = otherRefs;
366
 
    return *this;
367
 
}
368
 
 
369
 
template<typename T> template<typename U>
370
 
wp<T>& wp<T>::operator = (const sp<U>& other)
371
 
{
372
 
    weakref_type* newRefs =
373
 
        other != NULL ? other->createWeak(this) : 0;
374
 
    U* otherPtr(other.m_ptr);
375
 
    if (m_ptr) m_refs->decWeak(this);
376
 
    m_ptr = otherPtr;
377
 
    m_refs = newRefs;
378
 
    return *this;
379
 
}
380
 
 
381
 
template<typename T>
382
 
void wp<T>::set_object_and_refs(T* other, weakref_type* refs)
383
 
{
384
 
    if (other) refs->incWeak(this);
385
 
    if (m_ptr) m_refs->decWeak(this);
386
 
    m_ptr = other;
387
 
    m_refs = refs;
388
 
}
389
 
 
390
 
template<typename T>
391
 
sp<T> wp<T>::promote() const
392
 
{
393
 
    sp<T> result;
394
 
    if (m_ptr && m_refs->attemptIncStrong(&result)) {
395
 
        result.set_pointer(m_ptr);
396
 
    }
397
 
    return result;
398
 
}
399
 
 
400
 
template<typename T>
401
 
void wp<T>::clear()
402
 
{
403
 
    if (m_ptr) {
404
 
        m_refs->decWeak(this);
405
 
        m_ptr = 0;
406
 
    }
407
 
}
408
 
 
409
 
} // namespace android
410
 
 
411
 
// ---------------------------------------------------------------------------
412
 
 
413
 
#endif // ANDROID_REF_BASE_H