File indexing completed on 2024-04-28 04:50:50

0001 /*
0002  * shareddata.h
0003  *
0004  * Copyright (C) 2011 Christoph Pfister <christophpfister@gmail.com>
0005  *
0006  * This program is free software; you can redistribute it and/or modify
0007  * it under the terms of the GNU General Public License as published by
0008  * the Free Software Foundation; either version 2 of the License, or
0009  * (at your option) any later version.
0010  *
0011  * This program is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0014  * GNU General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License along
0017  * with this program; if not, write to the Free Software Foundation, Inc.,
0018  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
0019  */
0020 
0021 #ifndef SHAREDDATA_H
0022 #define SHAREDDATA_H
0023 
0024 #include <QSharedData>
0025 
0026 class SharedData : public QSharedData
0027 {
0028 public:
0029     SharedData() { }
0030     SharedData(const SharedData &sh) : QSharedData(sh) { }
0031     ~SharedData() { }
0032 
0033     SharedData &operator=(const SharedData &)
0034     {
0035         return *this;
0036     }
0037 };
0038 
0039 template<class T> class ExplicitlySharedDataPointer
0040 {
0041 public:
0042     explicit ExplicitlySharedDataPointer(T *data_ = NULL) : data(data_) { }
0043     ~ExplicitlySharedDataPointer() { }
0044 
0045     bool isValid() const
0046     {
0047         return (data.constData() != NULL);
0048     }
0049 
0050     const T *constData() const
0051     {
0052         return data.constData();
0053     }
0054 
0055     const T &operator*() const
0056     {
0057         return *data;
0058     }
0059 
0060     const T *operator->() const
0061     {
0062         return data.constData();
0063     }
0064 
0065     bool operator==(const ExplicitlySharedDataPointer<T> &other) const
0066     {
0067         return (data.constData() == other.data.constData());
0068     }
0069 
0070     bool operator!=(const ExplicitlySharedDataPointer<T> &other) const
0071     {
0072         return (data.constData() != other.data.constData());
0073     }
0074 
0075     bool operator<(const ExplicitlySharedDataPointer<T> &other) const
0076     {
0077         return (data.constData() < other.data.constData());
0078     }
0079 
0080     friend uint qHash(const ExplicitlySharedDataPointer<T> &pointer)
0081     {
0082         return qHash(pointer.data.constData());
0083     }
0084 
0085 private:
0086     QExplicitlySharedDataPointer<T> data;
0087 };
0088 
0089 #endif /* SHAREDDATA_H */