1/*
2 * Copyright (C) 2014 Google Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
7 *
8 *     * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 *     * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
13 * distribution.
14 *     * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 */
30
31#ifndef WTF_RawPtr_h
32#define WTF_RawPtr_h
33
34#include <algorithm>
35#include <stdint.h>
36
37#include "wtf/HashTableDeletedValueType.h"
38
39// RawPtr is a simple wrapper for a raw pointer that provides the
40// interface (get, clear) of other pointer types such as RefPtr,
41// Persistent and Member. This is used for the Blink garbage
42// collection work in order to be able to write shared code that will
43// use reference counting or garbage collection based on a
44// compile-time flag.
45
46namespace WTF {
47
48template<typename T>
49class RawPtr {
50    WTF_DISALLOW_CONSTRUCTION_FROM_ZERO(RawPtr);
51    WTF_DISALLOW_ZERO_ASSIGNMENT(RawPtr);
52public:
53    RawPtr()
54    {
55#ifndef NDEBUG
56        m_ptr = reinterpret_cast<T*>(rawPtrZapValue);
57#endif
58    }
59    RawPtr(std::nullptr_t) : m_ptr(0) { }
60    RawPtr(T* ptr) : m_ptr(ptr) { }
61    explicit RawPtr(T& reference) : m_ptr(&reference) { }
62    RawPtr(const RawPtr& other)
63        : m_ptr(other.get())
64    {
65    }
66
67    template<typename U>
68    RawPtr(const RawPtr<U>& other)
69        : m_ptr(other.get())
70    {
71    }
72
73    // Hash table deleted values, which are only constructed and never copied or destroyed.
74    RawPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
75    bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
76
77    T* get() const { return m_ptr; }
78    void clear() { m_ptr = 0; }
79    // FIXME: oilpan: Remove release and leakRef once we remove RefPtrWillBeRawPtr.
80    RawPtr<T> release()
81    {
82        RawPtr<T> tmp = m_ptr;
83        m_ptr = 0;
84        return tmp;
85    }
86    T* leakRef()
87    {
88        T* ptr = m_ptr;
89        m_ptr = 0;
90        return ptr;
91    }
92
93    template<typename U>
94    RawPtr& operator=(U* ptr)
95    {
96        m_ptr = ptr;
97        return *this;
98    }
99
100    template<typename U>
101    RawPtr& operator=(RawPtr<U> ptr)
102    {
103        m_ptr = ptr.get();
104        return *this;
105    }
106
107    RawPtr& operator=(std::nullptr_t)
108    {
109        m_ptr = 0;
110        return *this;
111    }
112
113    operator T*() const { return m_ptr; }
114    T& operator*() const { return *m_ptr; }
115    T* operator->() const { return m_ptr; }
116    bool operator!() const { return !m_ptr; }
117
118    void swap(RawPtr& o)
119    {
120        std::swap(m_ptr, o.m_ptr);
121    }
122
123    static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); }
124
125private:
126    static const uintptr_t rawPtrZapValue = 0x3a3a3a3a;
127    T* m_ptr;
128};
129
130template<typename T, typename U> inline RawPtr<T> static_pointer_cast(const RawPtr<U>& p)
131{
132    return RawPtr<T>(static_cast<T*>(p.get()));
133}
134
135template<typename T> inline T* getPtr(const RawPtr<T>& p)
136{
137    return p.get();
138}
139
140} // namespace WTF
141
142using WTF::RawPtr;
143
144#endif
145