RefBase.h revision 89daad6bae798779e57f252e9da4fe4e62337124
189daad6bae798779e57f252e9da4fe4e62337124Tim Murray/*
289daad6bae798779e57f252e9da4fe4e62337124Tim Murray * Copyright (C) 2013 The Android Open Source Project
389daad6bae798779e57f252e9da4fe4e62337124Tim Murray *
489daad6bae798779e57f252e9da4fe4e62337124Tim Murray * Licensed under the Apache License, Version 2.0 (the "License");
589daad6bae798779e57f252e9da4fe4e62337124Tim Murray * you may not use this file except in compliance with the License.
689daad6bae798779e57f252e9da4fe4e62337124Tim Murray * You may obtain a copy of the License at
789daad6bae798779e57f252e9da4fe4e62337124Tim Murray *
889daad6bae798779e57f252e9da4fe4e62337124Tim Murray *      http://www.apache.org/licenses/LICENSE-2.0
989daad6bae798779e57f252e9da4fe4e62337124Tim Murray *
1089daad6bae798779e57f252e9da4fe4e62337124Tim Murray * Unless required by applicable law or agreed to in writing, software
1189daad6bae798779e57f252e9da4fe4e62337124Tim Murray * distributed under the License is distributed on an "AS IS" BASIS,
1289daad6bae798779e57f252e9da4fe4e62337124Tim Murray * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1389daad6bae798779e57f252e9da4fe4e62337124Tim Murray * See the License for the specific language governing permissions and
1489daad6bae798779e57f252e9da4fe4e62337124Tim Murray * limitations under the License.
1589daad6bae798779e57f252e9da4fe4e62337124Tim Murray */
1689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
1789daad6bae798779e57f252e9da4fe4e62337124Tim Murray#ifndef RS_REF_BASE_H
1889daad6bae798779e57f252e9da4fe4e62337124Tim Murray#define RS_REF_BASE_H
1989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
2089daad6bae798779e57f252e9da4fe4e62337124Tim Murray
2189daad6bae798779e57f252e9da4fe4e62337124Tim Murray#include <stdint.h>
2289daad6bae798779e57f252e9da4fe4e62337124Tim Murray#include <sys/types.h>
2389daad6bae798779e57f252e9da4fe4e62337124Tim Murray#include <stdlib.h>
2489daad6bae798779e57f252e9da4fe4e62337124Tim Murray#include <string.h>
2589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
2689daad6bae798779e57f252e9da4fe4e62337124Tim Murray#include "StrongPointer.h"
2789daad6bae798779e57f252e9da4fe4e62337124Tim Murray#include "TypeHelpers.h"
2889daad6bae798779e57f252e9da4fe4e62337124Tim Murray
2989daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
3089daad6bae798779e57f252e9da4fe4e62337124Tim Murraynamespace android{
3189daad6bae798779e57f252e9da4fe4e62337124Tim Murraynamespace RSC {
3289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
3389daad6bae798779e57f252e9da4fe4e62337124Tim Murrayclass TextOutput;
3489daad6bae798779e57f252e9da4fe4e62337124Tim MurrayTextOutput& printWeakPointer(TextOutput& to, const void* val);
3589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
3689daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
3789daad6bae798779e57f252e9da4fe4e62337124Tim Murray
3889daad6bae798779e57f252e9da4fe4e62337124Tim Murray#define COMPARE_WEAK(_op_)                                      \
3989daad6bae798779e57f252e9da4fe4e62337124Tim Murrayinline bool operator _op_ (const sp<T>& o) const {              \
4089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return m_ptr _op_ o.m_ptr;                                  \
4189daad6bae798779e57f252e9da4fe4e62337124Tim Murray}                                                               \
4289daad6bae798779e57f252e9da4fe4e62337124Tim Murrayinline bool operator _op_ (const T* o) const {                  \
4389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return m_ptr _op_ o;                                        \
4489daad6bae798779e57f252e9da4fe4e62337124Tim Murray}                                                               \
4589daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename U>                                            \
4689daad6bae798779e57f252e9da4fe4e62337124Tim Murrayinline bool operator _op_ (const sp<U>& o) const {              \
4789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return m_ptr _op_ o.m_ptr;                                  \
4889daad6bae798779e57f252e9da4fe4e62337124Tim Murray}                                                               \
4989daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename U>                                            \
5089daad6bae798779e57f252e9da4fe4e62337124Tim Murrayinline bool operator _op_ (const U* o) const {                  \
5189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return m_ptr _op_ o;                                        \
5289daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
5389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
5489daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
5589daad6bae798779e57f252e9da4fe4e62337124Tim Murrayclass ReferenceMover;
5689daad6bae798779e57f252e9da4fe4e62337124Tim Murrayclass ReferenceConverterBase {
5789daad6bae798779e57f252e9da4fe4e62337124Tim Murraypublic:
5889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    virtual size_t getReferenceTypeSize() const = 0;
5989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    virtual void* getReferenceBase(void const*) const = 0;
6089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline virtual ~ReferenceConverterBase() { }
6189daad6bae798779e57f252e9da4fe4e62337124Tim Murray};
6289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
6389daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
6489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
6589daad6bae798779e57f252e9da4fe4e62337124Tim Murrayclass RefBase
6689daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
6789daad6bae798779e57f252e9da4fe4e62337124Tim Murraypublic:
6889daad6bae798779e57f252e9da4fe4e62337124Tim Murray            void            incStrong(const void* id) const;
6989daad6bae798779e57f252e9da4fe4e62337124Tim Murray            void            decStrong(const void* id) const;
7089daad6bae798779e57f252e9da4fe4e62337124Tim Murray
7189daad6bae798779e57f252e9da4fe4e62337124Tim Murray            void            forceIncStrong(const void* id) const;
7289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
7389daad6bae798779e57f252e9da4fe4e62337124Tim Murray            //! DEBUGGING ONLY: Get current strong ref count.
7489daad6bae798779e57f252e9da4fe4e62337124Tim Murray            int32_t         getStrongCount() const;
7589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
7689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    class weakref_type
7789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    {
7889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    public:
7989daad6bae798779e57f252e9da4fe4e62337124Tim Murray        RefBase*            refBase() const;
8089daad6bae798779e57f252e9da4fe4e62337124Tim Murray
8189daad6bae798779e57f252e9da4fe4e62337124Tim Murray        void                incWeak(const void* id);
8289daad6bae798779e57f252e9da4fe4e62337124Tim Murray        void                decWeak(const void* id);
8389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
8489daad6bae798779e57f252e9da4fe4e62337124Tim Murray        // acquires a strong reference if there is already one.
8589daad6bae798779e57f252e9da4fe4e62337124Tim Murray        bool                attemptIncStrong(const void* id);
8689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
8789daad6bae798779e57f252e9da4fe4e62337124Tim Murray        // acquires a weak reference if there is already one.
8889daad6bae798779e57f252e9da4fe4e62337124Tim Murray        // This is not always safe. see ProcessState.cpp and BpBinder.cpp
8989daad6bae798779e57f252e9da4fe4e62337124Tim Murray        // for proper use.
9089daad6bae798779e57f252e9da4fe4e62337124Tim Murray        bool                attemptIncWeak(const void* id);
9189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
9289daad6bae798779e57f252e9da4fe4e62337124Tim Murray        //! DEBUGGING ONLY: Get current weak ref count.
9389daad6bae798779e57f252e9da4fe4e62337124Tim Murray        int32_t             getWeakCount() const;
9489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
9589daad6bae798779e57f252e9da4fe4e62337124Tim Murray        //! DEBUGGING ONLY: Print references held on object.
9689daad6bae798779e57f252e9da4fe4e62337124Tim Murray        void                printRefs() const;
9789daad6bae798779e57f252e9da4fe4e62337124Tim Murray
9889daad6bae798779e57f252e9da4fe4e62337124Tim Murray        //! DEBUGGING ONLY: Enable tracking for this object.
9989daad6bae798779e57f252e9da4fe4e62337124Tim Murray        // enable -- enable/disable tracking
10089daad6bae798779e57f252e9da4fe4e62337124Tim Murray        // retain -- when tracking is enable, if true, then we save a stack trace
10189daad6bae798779e57f252e9da4fe4e62337124Tim Murray        //           for each reference and dereference; when retain == false, we
10289daad6bae798779e57f252e9da4fe4e62337124Tim Murray        //           match up references and dereferences and keep only the
10389daad6bae798779e57f252e9da4fe4e62337124Tim Murray        //           outstanding ones.
10489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
10589daad6bae798779e57f252e9da4fe4e62337124Tim Murray        void                trackMe(bool enable, bool retain);
10689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    };
10789daad6bae798779e57f252e9da4fe4e62337124Tim Murray
10889daad6bae798779e57f252e9da4fe4e62337124Tim Murray            weakref_type*   createWeak(const void* id) const;
10989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
11089daad6bae798779e57f252e9da4fe4e62337124Tim Murray            weakref_type*   getWeakRefs() const;
11189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
11289daad6bae798779e57f252e9da4fe4e62337124Tim Murray            //! DEBUGGING ONLY: Print references held on object.
11389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline  void            printRefs() const { getWeakRefs()->printRefs(); }
11489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
11589daad6bae798779e57f252e9da4fe4e62337124Tim Murray            //! DEBUGGING ONLY: Enable tracking of object.
11689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline  void            trackMe(bool enable, bool retain)
11789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    {
11889daad6bae798779e57f252e9da4fe4e62337124Tim Murray        getWeakRefs()->trackMe(enable, retain);
11989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
12089daad6bae798779e57f252e9da4fe4e62337124Tim Murray
12189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    typedef RefBase basetype;
12289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
12389daad6bae798779e57f252e9da4fe4e62337124Tim Murrayprotected:
12489daad6bae798779e57f252e9da4fe4e62337124Tim Murray                            RefBase();
12589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    virtual                 ~RefBase();
12689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
12789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    //! Flags for extendObjectLifetime()
12889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    enum {
12989daad6bae798779e57f252e9da4fe4e62337124Tim Murray        OBJECT_LIFETIME_STRONG  = 0x0000,
13089daad6bae798779e57f252e9da4fe4e62337124Tim Murray        OBJECT_LIFETIME_WEAK    = 0x0001,
13189daad6bae798779e57f252e9da4fe4e62337124Tim Murray        OBJECT_LIFETIME_MASK    = 0x0001
13289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    };
13389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
13489daad6bae798779e57f252e9da4fe4e62337124Tim Murray            void            extendObjectLifetime(int32_t mode);
13589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
13689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    //! Flags for onIncStrongAttempted()
13789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    enum {
13889daad6bae798779e57f252e9da4fe4e62337124Tim Murray        FIRST_INC_STRONG = 0x0001
13989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    };
14089daad6bae798779e57f252e9da4fe4e62337124Tim Murray
14189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    virtual void            onFirstRef();
14289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    virtual void            onLastStrongRef(const void* id);
14389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    virtual bool            onIncStrongAttempted(uint32_t flags, const void* id);
14489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    virtual void            onLastWeakRef(const void* id);
14589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
14689daad6bae798779e57f252e9da4fe4e62337124Tim Murrayprivate:
14789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    friend class ReferenceMover;
14889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    static void moveReferences(void* d, void const* s, size_t n,
14989daad6bae798779e57f252e9da4fe4e62337124Tim Murray            const ReferenceConverterBase& caster);
15089daad6bae798779e57f252e9da4fe4e62337124Tim Murray
15189daad6bae798779e57f252e9da4fe4e62337124Tim Murrayprivate:
15289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    friend class weakref_type;
15389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    class weakref_impl;
15489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
15589daad6bae798779e57f252e9da4fe4e62337124Tim Murray                            RefBase(const RefBase& o);
15689daad6bae798779e57f252e9da4fe4e62337124Tim Murray            RefBase&        operator=(const RefBase& o);
15789daad6bae798779e57f252e9da4fe4e62337124Tim Murray
15889daad6bae798779e57f252e9da4fe4e62337124Tim Murray        weakref_impl* const mRefs;
15989daad6bae798779e57f252e9da4fe4e62337124Tim Murray};
16089daad6bae798779e57f252e9da4fe4e62337124Tim Murray
16189daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
16289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
16389daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate <class T>
16489daad6bae798779e57f252e9da4fe4e62337124Tim Murrayclass LightRefBase
16589daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
16689daad6bae798779e57f252e9da4fe4e62337124Tim Murraypublic:
16789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline LightRefBase() : mCount(0) { }
16889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline void incStrong(__attribute__((unused)) const void* id) const {
16989daad6bae798779e57f252e9da4fe4e62337124Tim Murray        __sync_fetch_and_add(&mCount, 1);
17089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
17189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline void decStrong(__attribute__((unused)) const void* id) const {
17289daad6bae798779e57f252e9da4fe4e62337124Tim Murray        if (__sync_fetch_and_sub(&mCount, 1) == 1) {
17389daad6bae798779e57f252e9da4fe4e62337124Tim Murray            delete static_cast<const T*>(this);
17489daad6bae798779e57f252e9da4fe4e62337124Tim Murray        }
17589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
17689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    //! DEBUGGING ONLY: Get current strong ref count.
17789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline int32_t getStrongCount() const {
17889daad6bae798779e57f252e9da4fe4e62337124Tim Murray        return mCount;
17989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
18089daad6bae798779e57f252e9da4fe4e62337124Tim Murray
18189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    typedef LightRefBase<T> basetype;
18289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
18389daad6bae798779e57f252e9da4fe4e62337124Tim Murrayprotected:
18489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline ~LightRefBase() { }
18589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
18689daad6bae798779e57f252e9da4fe4e62337124Tim Murrayprivate:
18789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    friend class ReferenceMover;
18889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline static void moveReferences(void* d, void const* s, size_t n,
18989daad6bae798779e57f252e9da4fe4e62337124Tim Murray            const ReferenceConverterBase& caster) { }
19089daad6bae798779e57f252e9da4fe4e62337124Tim Murray
19189daad6bae798779e57f252e9da4fe4e62337124Tim Murrayprivate:
19289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    mutable volatile int32_t mCount;
19389daad6bae798779e57f252e9da4fe4e62337124Tim Murray};
19489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
19589daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
19689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
19789daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate <typename T>
19889daad6bae798779e57f252e9da4fe4e62337124Tim Murrayclass wp
19989daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
20089daad6bae798779e57f252e9da4fe4e62337124Tim Murraypublic:
20189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    typedef typename RefBase::weakref_type weakref_type;
20289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
20389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline wp() : m_ptr(0) { }
20489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
20589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    wp(T* other);
20689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    wp(const wp<T>& other);
20789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    wp(const sp<T>& other);
20889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U> wp(U* other);
20989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U> wp(const sp<U>& other);
21089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U> wp(const wp<U>& other);
21189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
21289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    ~wp();
21389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
21489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    // Assignment
21589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
21689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    wp& operator = (T* other);
21789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    wp& operator = (const wp<T>& other);
21889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    wp& operator = (const sp<T>& other);
21989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
22089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U> wp& operator = (U* other);
22189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U> wp& operator = (const wp<U>& other);
22289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U> wp& operator = (const sp<U>& other);
22389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
22489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    void set_object_and_refs(T* other, weakref_type* refs);
22589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
22689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    // promotion to sp
22789daad6bae798779e57f252e9da4fe4e62337124Tim Murray
22889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    sp<T> promote() const;
22989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
23089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    // Reset
23189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
23289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    void clear();
23389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
23489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    // Accessors
23589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
23689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline  weakref_type* get_refs() const { return m_refs; }
23789daad6bae798779e57f252e9da4fe4e62337124Tim Murray
23889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline  T* unsafe_get() const { return m_ptr; }
23989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
24089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    // Operators
24189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
24289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    COMPARE_WEAK(==)
24389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    COMPARE_WEAK(!=)
24489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    COMPARE_WEAK(>)
24589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    COMPARE_WEAK(<)
24689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    COMPARE_WEAK(<=)
24789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    COMPARE_WEAK(>=)
24889daad6bae798779e57f252e9da4fe4e62337124Tim Murray
24989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline bool operator == (const wp<T>& o) const {
25089daad6bae798779e57f252e9da4fe4e62337124Tim Murray        return (m_ptr == o.m_ptr) && (m_refs == o.m_refs);
25189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
25289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U>
25389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline bool operator == (const wp<U>& o) const {
25489daad6bae798779e57f252e9da4fe4e62337124Tim Murray        return m_ptr == o.m_ptr;
25589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
25689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
25789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline bool operator > (const wp<T>& o) const {
25889daad6bae798779e57f252e9da4fe4e62337124Tim Murray        return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
25989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
26089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U>
26189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline bool operator > (const wp<U>& o) const {
26289daad6bae798779e57f252e9da4fe4e62337124Tim Murray        return (m_ptr == o.m_ptr) ? (m_refs > o.m_refs) : (m_ptr > o.m_ptr);
26389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
26489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
26589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline bool operator < (const wp<T>& o) const {
26689daad6bae798779e57f252e9da4fe4e62337124Tim Murray        return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
26789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
26889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U>
26989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline bool operator < (const wp<U>& o) const {
27089daad6bae798779e57f252e9da4fe4e62337124Tim Murray        return (m_ptr == o.m_ptr) ? (m_refs < o.m_refs) : (m_ptr < o.m_ptr);
27189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
27289daad6bae798779e57f252e9da4fe4e62337124Tim Murray                         inline bool operator != (const wp<T>& o) const { return m_refs != o.m_refs; }
27389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U> inline bool operator != (const wp<U>& o) const { return !operator == (o); }
27489daad6bae798779e57f252e9da4fe4e62337124Tim Murray                         inline bool operator <= (const wp<T>& o) const { return !operator > (o); }
27589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U> inline bool operator <= (const wp<U>& o) const { return !operator > (o); }
27689daad6bae798779e57f252e9da4fe4e62337124Tim Murray                         inline bool operator >= (const wp<T>& o) const { return !operator < (o); }
27789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U> inline bool operator >= (const wp<U>& o) const { return !operator < (o); }
27889daad6bae798779e57f252e9da4fe4e62337124Tim Murray
27989daad6bae798779e57f252e9da4fe4e62337124Tim Murrayprivate:
28089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename Y> friend class sp;
28189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename Y> friend class wp;
28289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
28389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    T*              m_ptr;
28489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    weakref_type*   m_refs;
28589daad6bae798779e57f252e9da4fe4e62337124Tim Murray};
28689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
28789daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate <typename T>
28889daad6bae798779e57f252e9da4fe4e62337124Tim MurrayTextOutput& operator<<(TextOutput& to, const wp<T>& val);
28989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
29089daad6bae798779e57f252e9da4fe4e62337124Tim Murray#undef COMPARE_WEAK
29189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
29289daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
29389daad6bae798779e57f252e9da4fe4e62337124Tim Murray// No user serviceable parts below here.
29489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
29589daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
29689daad6bae798779e57f252e9da4fe4e62337124Tim Murraywp<T>::wp(T* other)
29789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    : m_ptr(other)
29889daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
29989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (other) m_refs = other->createWeak(this);
30089daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
30189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
30289daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
30389daad6bae798779e57f252e9da4fe4e62337124Tim Murraywp<T>::wp(const wp<T>& other)
30489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    : m_ptr(other.m_ptr), m_refs(other.m_refs)
30589daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
30689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_refs->incWeak(this);
30789daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
30889daad6bae798779e57f252e9da4fe4e62337124Tim Murray
30989daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
31089daad6bae798779e57f252e9da4fe4e62337124Tim Murraywp<T>::wp(const sp<T>& other)
31189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    : m_ptr(other.m_ptr)
31289daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
31389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) {
31489daad6bae798779e57f252e9da4fe4e62337124Tim Murray        m_refs = m_ptr->createWeak(this);
31589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
31689daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
31789daad6bae798779e57f252e9da4fe4e62337124Tim Murray
31889daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T> template<typename U>
31989daad6bae798779e57f252e9da4fe4e62337124Tim Murraywp<T>::wp(U* other)
32089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    : m_ptr(other)
32189daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
32289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (other) m_refs = other->createWeak(this);
32389daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
32489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
32589daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T> template<typename U>
32689daad6bae798779e57f252e9da4fe4e62337124Tim Murraywp<T>::wp(const wp<U>& other)
32789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    : m_ptr(other.m_ptr)
32889daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
32989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) {
33089daad6bae798779e57f252e9da4fe4e62337124Tim Murray        m_refs = other.m_refs;
33189daad6bae798779e57f252e9da4fe4e62337124Tim Murray        m_refs->incWeak(this);
33289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
33389daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
33489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
33589daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T> template<typename U>
33689daad6bae798779e57f252e9da4fe4e62337124Tim Murraywp<T>::wp(const sp<U>& other)
33789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    : m_ptr(other.m_ptr)
33889daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
33989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) {
34089daad6bae798779e57f252e9da4fe4e62337124Tim Murray        m_refs = m_ptr->createWeak(this);
34189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
34289daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
34389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
34489daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
34589daad6bae798779e57f252e9da4fe4e62337124Tim Murraywp<T>::~wp()
34689daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
34789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_refs->decWeak(this);
34889daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
34989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
35089daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
35189daad6bae798779e57f252e9da4fe4e62337124Tim Murraywp<T>& wp<T>::operator = (T* other)
35289daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
35389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    weakref_type* newRefs =
35489daad6bae798779e57f252e9da4fe4e62337124Tim Murray        other ? other->createWeak(this) : 0;
35589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_refs->decWeak(this);
35689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_ptr = other;
35789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_refs = newRefs;
35889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return *this;
35989daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
36089daad6bae798779e57f252e9da4fe4e62337124Tim Murray
36189daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
36289daad6bae798779e57f252e9da4fe4e62337124Tim Murraywp<T>& wp<T>::operator = (const wp<T>& other)
36389daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
36489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    weakref_type* otherRefs(other.m_refs);
36589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    T* otherPtr(other.m_ptr);
36689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (otherPtr) otherRefs->incWeak(this);
36789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_refs->decWeak(this);
36889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_ptr = otherPtr;
36989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_refs = otherRefs;
37089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return *this;
37189daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
37289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
37389daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
37489daad6bae798779e57f252e9da4fe4e62337124Tim Murraywp<T>& wp<T>::operator = (const sp<T>& other)
37589daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
37689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    weakref_type* newRefs =
37789daad6bae798779e57f252e9da4fe4e62337124Tim Murray        other != NULL ? other->createWeak(this) : 0;
37889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    T* otherPtr(other.m_ptr);
37989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_refs->decWeak(this);
38089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_ptr = otherPtr;
38189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_refs = newRefs;
38289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return *this;
38389daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
38489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
38589daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T> template<typename U>
38689daad6bae798779e57f252e9da4fe4e62337124Tim Murraywp<T>& wp<T>::operator = (U* other)
38789daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
38889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    weakref_type* newRefs =
38989daad6bae798779e57f252e9da4fe4e62337124Tim Murray        other ? other->createWeak(this) : 0;
39089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_refs->decWeak(this);
39189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_ptr = other;
39289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_refs = newRefs;
39389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return *this;
39489daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
39589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
39689daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T> template<typename U>
39789daad6bae798779e57f252e9da4fe4e62337124Tim Murraywp<T>& wp<T>::operator = (const wp<U>& other)
39889daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
39989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    weakref_type* otherRefs(other.m_refs);
40089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    U* otherPtr(other.m_ptr);
40189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (otherPtr) otherRefs->incWeak(this);
40289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_refs->decWeak(this);
40389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_ptr = otherPtr;
40489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_refs = otherRefs;
40589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return *this;
40689daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
40789daad6bae798779e57f252e9da4fe4e62337124Tim Murray
40889daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T> template<typename U>
40989daad6bae798779e57f252e9da4fe4e62337124Tim Murraywp<T>& wp<T>::operator = (const sp<U>& other)
41089daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
41189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    weakref_type* newRefs =
41289daad6bae798779e57f252e9da4fe4e62337124Tim Murray        other != NULL ? other->createWeak(this) : 0;
41389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    U* otherPtr(other.m_ptr);
41489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_refs->decWeak(this);
41589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_ptr = otherPtr;
41689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_refs = newRefs;
41789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return *this;
41889daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
41989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
42089daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
42189daad6bae798779e57f252e9da4fe4e62337124Tim Murrayvoid wp<T>::set_object_and_refs(T* other, weakref_type* refs)
42289daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
42389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (other) refs->incWeak(this);
42489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_refs->decWeak(this);
42589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_ptr = other;
42689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_refs = refs;
42789daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
42889daad6bae798779e57f252e9da4fe4e62337124Tim Murray
42989daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
43089daad6bae798779e57f252e9da4fe4e62337124Tim Murraysp<T> wp<T>::promote() const
43189daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
43289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    sp<T> result;
43389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr && m_refs->attemptIncStrong(&result)) {
43489daad6bae798779e57f252e9da4fe4e62337124Tim Murray        result.set_pointer(m_ptr);
43589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
43689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return result;
43789daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
43889daad6bae798779e57f252e9da4fe4e62337124Tim Murray
43989daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
44089daad6bae798779e57f252e9da4fe4e62337124Tim Murrayvoid wp<T>::clear()
44189daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
44289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) {
44389daad6bae798779e57f252e9da4fe4e62337124Tim Murray        m_refs->decWeak(this);
44489daad6bae798779e57f252e9da4fe4e62337124Tim Murray        m_ptr = 0;
44589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
44689daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
44789daad6bae798779e57f252e9da4fe4e62337124Tim Murray
44889daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate <typename T>
44989daad6bae798779e57f252e9da4fe4e62337124Tim Murrayinline TextOutput& operator<<(TextOutput& to, const wp<T>& val)
45089daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
45189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return printWeakPointer(to, val.unsafe_get());
45289daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
45389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
45489daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
45589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
45689daad6bae798779e57f252e9da4fe4e62337124Tim Murray// this class just serves as a namespace so TYPE::moveReferences can stay
45789daad6bae798779e57f252e9da4fe4e62337124Tim Murray// private.
45889daad6bae798779e57f252e9da4fe4e62337124Tim Murray
45989daad6bae798779e57f252e9da4fe4e62337124Tim Murrayclass ReferenceMover {
46089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    // StrongReferenceCast and WeakReferenceCast do the impedance matching
46189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    // between the generic (void*) implementation in Refbase and the strongly typed
46289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    // template specializations below.
46389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
46489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template <typename TYPE>
46589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    struct StrongReferenceCast : public ReferenceConverterBase {
46689daad6bae798779e57f252e9da4fe4e62337124Tim Murray        virtual size_t getReferenceTypeSize() const { return sizeof( sp<TYPE> ); }
46789daad6bae798779e57f252e9da4fe4e62337124Tim Murray        virtual void* getReferenceBase(void const* p) const {
46889daad6bae798779e57f252e9da4fe4e62337124Tim Murray            sp<TYPE> const* sptr(reinterpret_cast<sp<TYPE> const*>(p));
46989daad6bae798779e57f252e9da4fe4e62337124Tim Murray            return static_cast<typename TYPE::basetype *>(sptr->get());
47089daad6bae798779e57f252e9da4fe4e62337124Tim Murray        }
47189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    };
47289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
47389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template <typename TYPE>
47489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    struct WeakReferenceCast : public ReferenceConverterBase {
47589daad6bae798779e57f252e9da4fe4e62337124Tim Murray        virtual size_t getReferenceTypeSize() const { return sizeof( wp<TYPE> ); }
47689daad6bae798779e57f252e9da4fe4e62337124Tim Murray        virtual void* getReferenceBase(void const* p) const {
47789daad6bae798779e57f252e9da4fe4e62337124Tim Murray            wp<TYPE> const* sptr(reinterpret_cast<wp<TYPE> const*>(p));
47889daad6bae798779e57f252e9da4fe4e62337124Tim Murray            return static_cast<typename TYPE::basetype *>(sptr->unsafe_get());
47989daad6bae798779e57f252e9da4fe4e62337124Tim Murray        }
48089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    };
48189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
48289daad6bae798779e57f252e9da4fe4e62337124Tim Murraypublic:
48389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename TYPE> static inline
48489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    void move_references(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
48589daad6bae798779e57f252e9da4fe4e62337124Tim Murray        memmove(d, s, n*sizeof(sp<TYPE>));
48689daad6bae798779e57f252e9da4fe4e62337124Tim Murray        StrongReferenceCast<TYPE> caster;
48789daad6bae798779e57f252e9da4fe4e62337124Tim Murray        TYPE::moveReferences(d, s, n, caster);
48889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
48989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename TYPE> static inline
49089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    void move_references(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
49189daad6bae798779e57f252e9da4fe4e62337124Tim Murray        memmove(d, s, n*sizeof(wp<TYPE>));
49289daad6bae798779e57f252e9da4fe4e62337124Tim Murray        WeakReferenceCast<TYPE> caster;
49389daad6bae798779e57f252e9da4fe4e62337124Tim Murray        TYPE::moveReferences(d, s, n, caster);
49489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
49589daad6bae798779e57f252e9da4fe4e62337124Tim Murray};
49689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
49789daad6bae798779e57f252e9da4fe4e62337124Tim Murray// specialization for moving sp<> and wp<> types.
49889daad6bae798779e57f252e9da4fe4e62337124Tim Murray// these are used by the [Sorted|Keyed]Vector<> implementations
49989daad6bae798779e57f252e9da4fe4e62337124Tim Murray// sp<> and wp<> need to be handled specially, because they do not
50089daad6bae798779e57f252e9da4fe4e62337124Tim Murray// have trivial copy operation in the general case (see RefBase.cpp
50189daad6bae798779e57f252e9da4fe4e62337124Tim Murray// when DEBUG ops are enabled), but can be implemented very
50289daad6bae798779e57f252e9da4fe4e62337124Tim Murray// efficiently in most cases.
50389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
50489daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename TYPE> inline
50589daad6bae798779e57f252e9da4fe4e62337124Tim Murrayvoid move_forward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
50689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    ReferenceMover::move_references(d, s, n);
50789daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
50889daad6bae798779e57f252e9da4fe4e62337124Tim Murray
50989daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename TYPE> inline
51089daad6bae798779e57f252e9da4fe4e62337124Tim Murrayvoid move_backward_type(sp<TYPE>* d, sp<TYPE> const* s, size_t n) {
51189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    ReferenceMover::move_references(d, s, n);
51289daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
51389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
51489daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename TYPE> inline
51589daad6bae798779e57f252e9da4fe4e62337124Tim Murrayvoid move_forward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
51689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    ReferenceMover::move_references(d, s, n);
51789daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
51889daad6bae798779e57f252e9da4fe4e62337124Tim Murray
51989daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename TYPE> inline
52089daad6bae798779e57f252e9da4fe4e62337124Tim Murrayvoid move_backward_type(wp<TYPE>* d, wp<TYPE> const* s, size_t n) {
52189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    ReferenceMover::move_references(d, s, n);
52289daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
52389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
52489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
52589daad6bae798779e57f252e9da4fe4e62337124Tim Murray}; // namespace RSC
52689daad6bae798779e57f252e9da4fe4e62337124Tim Murray}; // namespace android
52789daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
52889daad6bae798779e57f252e9da4fe4e62337124Tim Murray
52989daad6bae798779e57f252e9da4fe4e62337124Tim Murray#endif // RS_REF_BASE_H
530