| OLD | NEW |
| (Empty) | |
| 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 |
| 6 * are met: |
| 7 * 1. Redistributions of source code must retain the above copyright |
| 8 * notice, this list of conditions and the following disclaimer. |
| 9 * 2. Redistributions in binary form must reproduce the above copyright |
| 10 * notice, this list of conditions and the following disclaimer in the |
| 11 * documentation and/or other materials provided with the distribution. |
| 12 * |
| 13 * THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY |
| 14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR |
| 17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, |
| 18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, |
| 19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR |
| 20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY |
| 21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 24 */ |
| 25 |
| 26 #ifndef WTF_Ptr_h |
| 27 #define WTF_Ptr_h |
| 28 |
| 29 // Ptr is a simple wrapper for a raw pointer that provides the |
| 30 // interface (get, clear) of other pointer types such as RefPtr, |
| 31 // Persistent and Member. This is used for the Blink garbage |
| 32 // collection work in order to be able to write shared code that will |
| 33 // use reference counting or garbage collection based on a |
| 34 // compile-time flag. |
| 35 |
| 36 namespace WTF { |
| 37 |
| 38 template<typename T> |
| 39 class Ptr { |
| 40 public: |
| 41 Ptr(T* ptr) : m_ptr(ptr) { } |
| 42 Ptr(std::nullptr_t) : m_ptr(0) { } |
| 43 |
| 44 template<typename U> |
| 45 Ptr(const Ptr<U>& other) |
| 46 : m_ptr(other.get()) |
| 47 { |
| 48 } |
| 49 |
| 50 T* get() const { return m_ptr; } |
| 51 void clear() { m_ptr = 0; } |
| 52 |
| 53 Ptr& operator=(T* ptr) |
| 54 { |
| 55 m_ptr = ptr; |
| 56 return *this; |
| 57 } |
| 58 |
| 59 Ptr& operator=(std::nullptr_t) |
| 60 { |
| 61 m_ptr = 0; |
| 62 return *this; |
| 63 } |
| 64 |
| 65 operator T*() const { return m_ptr; } |
| 66 T& operator*() const { return *m_ptr; } |
| 67 T* operator->() const { return m_ptr; } |
| 68 bool operator!() const { return !m_ptr; } |
| 69 |
| 70 // This conversion operator allows implicit conversion to bool but |
| 71 // not to other integer types. |
| 72 typedef T* Ptr::*UnspecifiedBoolType; |
| 73 operator UnspecifiedBoolType() const { return m_ptr ? &m_ptr : 0; } |
| 74 |
| 75 private: |
| 76 T* m_ptr; |
| 77 }; |
| 78 |
| 79 } // namespace WTF |
| 80 |
| 81 using WTF::Ptr; |
| 82 |
| 83 #endif |
| OLD | NEW |