Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / RawPtr.h
blobc3a82ec4b72d51c961d40dfce960f477d3e713c3
1 /*
2 * Copyright (C) 2014 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
6 * met:
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.
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.
31 #ifndef WTF_RawPtr_h
32 #define WTF_RawPtr_h
34 #include "wtf/HashTableDeletedValueType.h"
35 #include "wtf/TypeTraits.h"
36 #include <algorithm>
37 #include <stdint.h>
38 #include <utility>
40 // RawPtr is a simple wrapper for a raw pointer that provides the
41 // interface (get, clear) of other pointer types such as RefPtr,
42 // Persistent and Member. This is used for the Blink garbage
43 // collection work in order to be able to write shared code that will
44 // use reference counting or garbage collection based on a
45 // compile-time flag.
47 namespace WTF {
49 template<typename T>
50 class RawPtr {
51 public:
52 RawPtr()
54 #if ENABLE(ASSERT)
55 m_ptr = reinterpret_cast<T*>(rawPtrZapValue);
56 #endif
58 RawPtr(std::nullptr_t) : m_ptr(0) { }
59 RawPtr(T* ptr) : m_ptr(ptr) { }
60 explicit RawPtr(T& reference) : m_ptr(&reference) { }
61 RawPtr(const RawPtr& other)
62 : m_ptr(other.get())
66 template<typename U>
67 RawPtr(const RawPtr<U>& other, EnsurePtrConvertibleArgDecl(U, T))
68 : m_ptr(other.get())
72 // Hash table deleted values, which are only constructed and never copied or destroyed.
73 RawPtr(HashTableDeletedValueType) : m_ptr(hashTableDeletedValue()) { }
74 bool isHashTableDeletedValue() const { return m_ptr == hashTableDeletedValue(); }
76 T* get() const { return m_ptr; }
77 void clear() { m_ptr = 0; }
78 RawPtr<T> release()
80 RawPtr<T> tmp = m_ptr;
81 m_ptr = 0;
82 return tmp;
84 T* leakRef()
86 T* ptr = m_ptr;
87 m_ptr = 0;
88 return ptr;
90 T* leakPtr()
92 T* ptr = m_ptr;
93 m_ptr = 0;
94 return ptr;
97 template<typename U>
98 RawPtr& operator=(U* ptr)
100 m_ptr = ptr;
101 return *this;
104 template<typename U>
105 RawPtr& operator=(RawPtr<U> ptr)
107 m_ptr = ptr.get();
108 return *this;
111 RawPtr& operator=(std::nullptr_t)
113 m_ptr = 0;
114 return *this;
117 operator T*() const { return m_ptr; }
118 T& operator*() const { return *m_ptr; }
119 T* operator->() const { return m_ptr; }
120 bool operator!() const { return !m_ptr; }
122 void swap(RawPtr& o)
124 std::swap(m_ptr, o.m_ptr);
127 static T* hashTableDeletedValue() { return reinterpret_cast<T*>(-1); }
129 private:
130 static const uintptr_t rawPtrZapValue = 0x3a3a3a3a;
131 T* m_ptr;
134 template<typename T, typename U> inline RawPtr<T> static_pointer_cast(const RawPtr<U>& p)
136 return RawPtr<T>(static_cast<T*>(p.get()));
139 template<typename T> inline T* getPtr(const RawPtr<T>& p)
141 return p.get();
144 } // namespace WTF
146 using WTF::RawPtr;
148 #endif