15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)/*
25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *           (C) 2000 Antti Koivisto (koivisto@kde.org)
45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *           (C) 2000 Dirk Mueller (mueller@kde.org)
55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Copyright (C) 2003, 2005, 2008 Apple Inc. All rights reserved.
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This library is free software; you can redistribute it and/or
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * modify it under the terms of the GNU Library General Public
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * License as published by the Free Software Foundation; either
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * version 2 of the License, or (at your option) any later version.
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * This library is distributed in the hope that it will be useful,
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * but WITHOUT ANY WARRANTY; without even the implied warranty of
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Library General Public License for more details.
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * You should have received a copy of the GNU Library General Public License
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * along with this library; see the file COPYING.LIB.  If not, write to
195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) * Boston, MA 02110-1301, USA.
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) *
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) */
235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef DataRef_h
255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define DataRef_h
265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "wtf/RefPtr.h"
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace blink {
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)template <typename T> class DataRef {
32public:
33    const T* get() const { return m_data.get(); }
34
35    const T& operator*() const { return *get(); }
36    const T* operator->() const { return get(); }
37
38    T* access()
39    {
40        if (!m_data->hasOneRef())
41            m_data = m_data->copy();
42        return m_data.get();
43    }
44
45    void init()
46    {
47        ASSERT(!m_data);
48        m_data = T::create();
49    }
50
51    bool operator==(const DataRef<T>& o) const
52    {
53        ASSERT(m_data);
54        ASSERT(o.m_data);
55        return m_data == o.m_data || *m_data == *o.m_data;
56    }
57
58    bool operator!=(const DataRef<T>& o) const
59    {
60        ASSERT(m_data);
61        ASSERT(o.m_data);
62        return m_data != o.m_data && *m_data != *o.m_data;
63    }
64
65private:
66    RefPtr<T> m_data;
67};
68
69} // namespace blink
70
71#endif // DataRef_h
72