File indexing completed on 2024-04-28 15:23:52

0001 /*
0002  * Copyright (C) 2000 Lars Knoll (knoll@kde.org)
0003  *           (C) 2000 Antti Koivisto (koivisto@kde.org)
0004  *           (C) 2000 Dirk Mueller (mueller@kde.org)
0005  * Copyright (C) 2003, 2005, 2008 Apple Inc. All rights reserved.
0006  *
0007  * This library is free software; you can redistribute it and/or
0008  * modify it under the terms of the GNU Library General Public
0009  * License as published by the Free Software Foundation; either
0010  * version 2 of the License, or (at your option) any later version.
0011  *
0012  * This library is distributed in the hope that it will be useful,
0013  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0014  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0015  * Library General Public License for more details.
0016  *
0017  * You should have received a copy of the GNU Library General Public License
0018  * along with this library; see the file COPYING.LIB.  If not, write to
0019  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0020  * Boston, MA 02110-1301, USA.
0021  *
0022  */
0023 
0024 #ifndef DataRef_h
0025 #define DataRef_h
0026 
0027 #include <wtf/RefPtr.h>
0028 
0029 namespace khtml
0030 {
0031 
0032 template <class DATA>
0033 class DataRef
0034 {
0035 public:
0036     DataRef()
0037     {
0038         data = nullptr;
0039     }
0040 
0041     DataRef(const DataRef<DATA> &d)
0042     {
0043         data = d.data;
0044         data->ref();
0045     }
0046 
0047     ~DataRef()
0048     {
0049         if (data) {
0050             data->deref();
0051         }
0052     }
0053 
0054     const DATA *operator->() const
0055     {
0056         return data;
0057     }
0058 
0059     const DATA *get() const
0060     {
0061         return data;
0062     }
0063 
0064     DATA *access()
0065     {
0066         if (!data->hasOneRef()) {
0067             data->deref();
0068             data = new DATA(*data);
0069             data->ref();
0070         }
0071         return data;
0072     }
0073 
0074     void init()
0075     {
0076         data = new DATA;
0077         data->ref();
0078     }
0079 
0080     DataRef<DATA> &operator=(const DataRef<DATA> &d)
0081     {
0082         if (data == d.data) {
0083             return *this;
0084         }
0085         if (data) {
0086             data->deref();
0087         }
0088         data = d.data;
0089 
0090         data->ref();
0091 
0092         return *this;
0093     }
0094 
0095     bool operator == (const DataRef<DATA> &o) const
0096     {
0097         return (*data == *(o.data));
0098     }
0099 
0100     bool operator != (const DataRef<DATA> &o) const
0101     {
0102         return (*data != *(o.data));
0103     }
0104 
0105 private:
0106     DATA *data;
0107 };
0108 
0109 } // namespace khtml
0110 
0111 #endif // DataRef_h