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_STRONG_POINTER_H
1889daad6bae798779e57f252e9da4fe4e62337124Tim Murray#define RS_STRONG_POINTER_H
1989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
2089daad6bae798779e57f252e9da4fe4e62337124Tim Murray//#include <cutils/atomic.h>
2189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
2289daad6bae798779e57f252e9da4fe4e62337124Tim Murray#include <stdint.h>
2389daad6bae798779e57f252e9da4fe4e62337124Tim Murray#include <sys/types.h>
2489daad6bae798779e57f252e9da4fe4e62337124Tim Murray#include <stdlib.h>
2589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
2689daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
2789daad6bae798779e57f252e9da4fe4e62337124Tim Murraynamespace android {
2889daad6bae798779e57f252e9da4fe4e62337124Tim Murraynamespace RSC {
2989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
3089daad6bae798779e57f252e9da4fe4e62337124Tim Murrayclass TextOutput;
3189daad6bae798779e57f252e9da4fe4e62337124Tim MurrayTextOutput& printStrongPointer(TextOutput& to, const void* val);
3289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
3389daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T> class wp;
3489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
3589daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
3689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
3789daad6bae798779e57f252e9da4fe4e62337124Tim Murray#define COMPARE(_op_)                                           \
3889daad6bae798779e57f252e9da4fe4e62337124Tim Murrayinline bool operator _op_ (const sp<T>& o) const {              \
3989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return m_ptr _op_ o.m_ptr;                                  \
4089daad6bae798779e57f252e9da4fe4e62337124Tim Murray}                                                               \
4189daad6bae798779e57f252e9da4fe4e62337124Tim Murrayinline bool operator _op_ (const T* o) const {                  \
4289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return m_ptr _op_ o;                                        \
4389daad6bae798779e57f252e9da4fe4e62337124Tim Murray}                                                               \
4489daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename U>                                            \
4589daad6bae798779e57f252e9da4fe4e62337124Tim Murrayinline bool operator _op_ (const sp<U>& o) const {              \
4689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return m_ptr _op_ o.m_ptr;                                  \
4789daad6bae798779e57f252e9da4fe4e62337124Tim Murray}                                                               \
4889daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename U>                                            \
4989daad6bae798779e57f252e9da4fe4e62337124Tim Murrayinline bool operator _op_ (const U* o) const {                  \
5089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return m_ptr _op_ o;                                        \
5189daad6bae798779e57f252e9da4fe4e62337124Tim Murray}                                                               \
5289daad6bae798779e57f252e9da4fe4e62337124Tim Murrayinline bool operator _op_ (const wp<T>& o) const {              \
5389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return m_ptr _op_ o.m_ptr;                                  \
5489daad6bae798779e57f252e9da4fe4e62337124Tim Murray}                                                               \
5589daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename U>                                            \
5689daad6bae798779e57f252e9da4fe4e62337124Tim Murrayinline bool operator _op_ (const wp<U>& o) const {              \
5789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return m_ptr _op_ o.m_ptr;                                  \
5889daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
5989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
6089daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
6189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
6289daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate <typename T>
6389daad6bae798779e57f252e9da4fe4e62337124Tim Murrayclass sp
6489daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
6589daad6bae798779e57f252e9da4fe4e62337124Tim Murraypublic:
6689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline sp() : m_ptr(0) { }
6789daad6bae798779e57f252e9da4fe4e62337124Tim Murray
6889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    sp(T* other);
6989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    sp(const sp<T>& other);
7089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U> sp(U* other);
7189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U> sp(const sp<U>& other);
7289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
7389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    ~sp();
7489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
7589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    // Assignment
7689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
7789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    sp& operator = (T* other);
7889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    sp& operator = (const sp<T>& other);
7989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
8089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U> sp& operator = (const sp<U>& other);
8189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename U> sp& operator = (U* other);
8289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
8389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    //! Special optimization for use by ProcessState (and nobody else).
8489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    void force_set(T* other);
8589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
8689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    // Reset
8789daad6bae798779e57f252e9da4fe4e62337124Tim Murray
8889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    void clear();
8989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
9089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    // Accessors
9189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
9289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline  T&      operator* () const  { return *m_ptr; }
9389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline  T*      operator-> () const { return m_ptr;  }
9489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    inline  T*      get() const         { return m_ptr; }
9589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
9689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    // Operators
9789daad6bae798779e57f252e9da4fe4e62337124Tim Murray
9889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    COMPARE(==)
9989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    COMPARE(!=)
10089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    COMPARE(>)
10189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    COMPARE(<)
10289daad6bae798779e57f252e9da4fe4e62337124Tim Murray    COMPARE(<=)
10389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    COMPARE(>=)
10489daad6bae798779e57f252e9da4fe4e62337124Tim Murray
10589daad6bae798779e57f252e9da4fe4e62337124Tim Murrayprivate:
10689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename Y> friend class sp;
10789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    template<typename Y> friend class wp;
10889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    void set_pointer(T* ptr);
10989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    T* m_ptr;
11089daad6bae798779e57f252e9da4fe4e62337124Tim Murray};
11189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
11289daad6bae798779e57f252e9da4fe4e62337124Tim Murray#undef COMPARE
11389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
11489daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate <typename T>
11589daad6bae798779e57f252e9da4fe4e62337124Tim MurrayTextOutput& operator<<(TextOutput& to, const sp<T>& val);
11689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
11789daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
11889daad6bae798779e57f252e9da4fe4e62337124Tim Murray// No user serviceable parts below here.
11989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
12089daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
12189daad6bae798779e57f252e9da4fe4e62337124Tim Murraysp<T>::sp(T* other)
12289daad6bae798779e57f252e9da4fe4e62337124Tim Murray: m_ptr(other)
12389daad6bae798779e57f252e9da4fe4e62337124Tim Murray  {
12489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (other) other->incStrong(this);
12589daad6bae798779e57f252e9da4fe4e62337124Tim Murray  }
12689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
12789daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
12889daad6bae798779e57f252e9da4fe4e62337124Tim Murraysp<T>::sp(const sp<T>& other)
12989daad6bae798779e57f252e9da4fe4e62337124Tim Murray: m_ptr(other.m_ptr)
13089daad6bae798779e57f252e9da4fe4e62337124Tim Murray  {
13189daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_ptr->incStrong(this);
13289daad6bae798779e57f252e9da4fe4e62337124Tim Murray  }
13389daad6bae798779e57f252e9da4fe4e62337124Tim Murray
13489daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T> template<typename U>
13589daad6bae798779e57f252e9da4fe4e62337124Tim Murraysp<T>::sp(U* other) : m_ptr(other)
13689daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
13789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (other) ((T*)other)->incStrong(this);
13889daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
13989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
14089daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T> template<typename U>
14189daad6bae798779e57f252e9da4fe4e62337124Tim Murraysp<T>::sp(const sp<U>& other)
14289daad6bae798779e57f252e9da4fe4e62337124Tim Murray: m_ptr(other.m_ptr)
14389daad6bae798779e57f252e9da4fe4e62337124Tim Murray  {
14489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_ptr->incStrong(this);
14589daad6bae798779e57f252e9da4fe4e62337124Tim Murray  }
14689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
14789daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
14889daad6bae798779e57f252e9da4fe4e62337124Tim Murraysp<T>::~sp()
14989daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
15089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_ptr->decStrong(this);
15189daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
15289daad6bae798779e57f252e9da4fe4e62337124Tim Murray
15389daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
15489daad6bae798779e57f252e9da4fe4e62337124Tim Murraysp<T>& sp<T>::operator = (const sp<T>& other) {
15589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    T* otherPtr(other.m_ptr);
15689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (otherPtr) otherPtr->incStrong(this);
15789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_ptr->decStrong(this);
15889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_ptr = otherPtr;
15989daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return *this;
16089daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
16189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
16289daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
16389daad6bae798779e57f252e9da4fe4e62337124Tim Murraysp<T>& sp<T>::operator = (T* other)
16489daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
16589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (other) other->incStrong(this);
16689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_ptr->decStrong(this);
16789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_ptr = other;
16889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return *this;
16989daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
17089daad6bae798779e57f252e9da4fe4e62337124Tim Murray
17189daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T> template<typename U>
17289daad6bae798779e57f252e9da4fe4e62337124Tim Murraysp<T>& sp<T>::operator = (const sp<U>& other)
17389daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
17489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    T* otherPtr(other.m_ptr);
17589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (otherPtr) otherPtr->incStrong(this);
17689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_ptr->decStrong(this);
17789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_ptr = otherPtr;
17889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return *this;
17989daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
18089daad6bae798779e57f252e9da4fe4e62337124Tim Murray
18189daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T> template<typename U>
18289daad6bae798779e57f252e9da4fe4e62337124Tim Murraysp<T>& sp<T>::operator = (U* other)
18389daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
18489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (other) ((T*)other)->incStrong(this);
18589daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) m_ptr->decStrong(this);
18689daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_ptr = other;
18789daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return *this;
18889daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
18989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
19089daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
19189daad6bae798779e57f252e9da4fe4e62337124Tim Murrayvoid sp<T>::force_set(T* other)
19289daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
19389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    other->forceIncStrong(this);
19489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_ptr = other;
19589daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
19689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
19789daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
19889daad6bae798779e57f252e9da4fe4e62337124Tim Murrayvoid sp<T>::clear()
19989daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
20089daad6bae798779e57f252e9da4fe4e62337124Tim Murray    if (m_ptr) {
20189daad6bae798779e57f252e9da4fe4e62337124Tim Murray        m_ptr->decStrong(this);
20289daad6bae798779e57f252e9da4fe4e62337124Tim Murray        m_ptr = 0;
20389daad6bae798779e57f252e9da4fe4e62337124Tim Murray    }
20489daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
20589daad6bae798779e57f252e9da4fe4e62337124Tim Murray
20689daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate<typename T>
20789daad6bae798779e57f252e9da4fe4e62337124Tim Murrayvoid sp<T>::set_pointer(T* ptr) {
20889daad6bae798779e57f252e9da4fe4e62337124Tim Murray    m_ptr = ptr;
20989daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
21089daad6bae798779e57f252e9da4fe4e62337124Tim Murray
21189daad6bae798779e57f252e9da4fe4e62337124Tim Murraytemplate <typename T>
21289daad6bae798779e57f252e9da4fe4e62337124Tim Murrayinline TextOutput& operator<<(TextOutput& to, const sp<T>& val)
21389daad6bae798779e57f252e9da4fe4e62337124Tim Murray{
21489daad6bae798779e57f252e9da4fe4e62337124Tim Murray    return printStrongPointer(to, val.get());
21589daad6bae798779e57f252e9da4fe4e62337124Tim Murray}
21689daad6bae798779e57f252e9da4fe4e62337124Tim Murray
21789daad6bae798779e57f252e9da4fe4e62337124Tim Murray}; // namespace RSC
21889daad6bae798779e57f252e9da4fe4e62337124Tim Murray}; // namespace android
21989daad6bae798779e57f252e9da4fe4e62337124Tim Murray
22089daad6bae798779e57f252e9da4fe4e62337124Tim Murray// ---------------------------------------------------------------------------
22189daad6bae798779e57f252e9da4fe4e62337124Tim Murray
22289daad6bae798779e57f252e9da4fe4e62337124Tim Murray#endif // RS_STRONG_POINTER_H
223