| 1 | /* |
| 2 | * Copyright (C) 2003-2017 Apple Inc. All rights reserved. |
| 3 | * |
| 4 | * This library is free software; you can redistribute it and/or |
| 5 | * modify it under the terms of the GNU Library General Public |
| 6 | * License as published by the Free Software Foundation; either |
| 7 | * version 2 of the License, or (at your option) any later version. |
| 8 | * |
| 9 | * This library is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | * Library General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU Library General Public License |
| 15 | * along with this library; see the file COPYING.LIB. If not, write to |
| 16 | * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 17 | * Boston, MA 02110-1301, USA. |
| 18 | * |
| 19 | */ |
| 20 | |
| 21 | #pragma once |
| 22 | |
| 23 | #include <wtf/Ref.h> |
| 24 | |
| 25 | namespace WebCore { |
| 26 | |
| 27 | template <typename T> class DataRef { |
| 28 | public: |
| 29 | DataRef(Ref<T>&& data) |
| 30 | : m_data(WTFMove(data)) |
| 31 | { |
| 32 | } |
| 33 | |
| 34 | DataRef(const DataRef& other) |
| 35 | : m_data(other.m_data.copyRef()) |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | DataRef& operator=(const DataRef& other) |
| 40 | { |
| 41 | m_data = other.m_data.copyRef(); |
| 42 | return *this; |
| 43 | } |
| 44 | |
| 45 | DataRef(DataRef&&) = default; |
| 46 | DataRef& operator=(DataRef&&) = default; |
| 47 | |
| 48 | DataRef replace(DataRef&& other) |
| 49 | { |
| 50 | return m_data.replace(WTFMove(other.m_data)); |
| 51 | } |
| 52 | |
| 53 | operator const T&() const |
| 54 | { |
| 55 | return m_data; |
| 56 | } |
| 57 | |
| 58 | const T* ptr() const |
| 59 | { |
| 60 | return m_data.ptr(); |
| 61 | } |
| 62 | |
| 63 | const T& get() const |
| 64 | { |
| 65 | return m_data; |
| 66 | } |
| 67 | |
| 68 | const T& operator*() const |
| 69 | { |
| 70 | return m_data; |
| 71 | } |
| 72 | |
| 73 | const T* operator->() const |
| 74 | { |
| 75 | return m_data.ptr(); |
| 76 | } |
| 77 | |
| 78 | T& access() |
| 79 | { |
| 80 | if (!m_data->hasOneRef()) |
| 81 | m_data = m_data->copy(); |
| 82 | return m_data; |
| 83 | } |
| 84 | |
| 85 | bool operator==(const DataRef& other) const |
| 86 | { |
| 87 | return m_data.ptr() == other.m_data.ptr() || m_data.get() == other.m_data.get(); |
| 88 | } |
| 89 | |
| 90 | bool operator!=(const DataRef& other) const |
| 91 | { |
| 92 | return !(*this == other); |
| 93 | } |
| 94 | |
| 95 | private: |
| 96 | Ref<T> m_data; |
| 97 | }; |
| 98 | |
| 99 | } // namespace WebCore |
| 100 | |