c++ - 如何创建自定义引用容器?

标签 c++ qt c++11 reference qlist

免责声明:

我已经明白 C++ 委员会有理由不支持它,所以请不要告诉我“这是个坏主意”或任何类似的答案,我主要是为了好玩而这样做,我个人发现了一个我想使用它的情况。程序员总是会为这种事情争论,所以请不要争论。

问题:

现在进入正题……我本来想使用 QList,却发现 C++ 不支持引用容器,所以我想也许我可以创建一个自定义的引用容器,它实际上存储了指针(无论如何,这实际上是引用的本质)。这样做的最佳方法是什么? (而且我不会接受“没有好的办法”作为答案,让我们保持开放的心态吧!)

答案:

我设法让一个实现工作,我认为它工作得很好,我使用 QList,但这个概念对于任何容器类型都应该是相同的。我在这里为它创建了一个 Github Gist:ReferenceList.hpp .或者您可以在下面的答案中查看。

即使我回答了我自己的问题,也不要害怕发表其他关于如何实现这一点的想法!

最佳答案

/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
 * ReferenceList.hpp                                                           *
 *                                                                             *
 * Copyright 2014 Tory Gaurnier <tory.gaurnier@linuxmail.org>                  *
 *                                                                             *
 * This program is free software; you can redistribute it and/or modify        *
 * it under the terms of the GNU Lesser General Public License as published by *
 * the Free Software Foundation; version 3.                                    *
 *                                                                             *
 * This program is distributed in the hope that it will be useful,             *
 * but WITHOUT ANY WARRANTY; without even the implied warranty of              *
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the               *
 * GNU Lesser General Public License for more details.                         *
 *                                                                             *
 * You should have received a copy of the GNU Lesser General Public License    *
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.       *
 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */


#ifndef REFERENCELIST_HPP
#define REFERENCELIST_HPP


#include <QDataStream>
#include <QDebug>
#include <QList>


/**
 * class ReferenceList
 *
 * Uses QList under the hood to store pointers, on the surface recieves and returns nothing but the
 * references.
 *
 * NOTE: Any method that calls it's QList<T*> counterpart with parameter T, T must not be const, if
 * it is then QList<T*> would have to be QList<const T *const> instead.
 */
template<class T>
class ReferenceList : public QList<T*> {
    public:
        // Forward declare iterators
        class const_iterator;
        class iterator;

        // Set iterators as friends
        friend class const_iterator;
        friend class iterator;


        ReferenceList() {}
        ReferenceList(const QList<T*> &other) : QList<T*>(other) {  }
        ReferenceList(const ReferenceList<T> &other) : QList<T*>(other) {  }
        ReferenceList(ReferenceList<T> &&other) : QList<T*>(std::move(other)) {  }
        ~ReferenceList() {}

        void append(T &value) {
            QList<T*>::append(&value);
        }

        void append(const ReferenceList<T> other) {
            QList<T*>::append(other);
        }

        const T & at(int i) const {
            return *QList<T*>::at(i);
        }

        T & back() {
            return *QList<T*>::back();
        }

        const T & back() const {
            return *QList<T*>::back();
        }

        iterator begin() {
            return iterator(iterator(QList<T*>::begin()));
        }

        const_iterator begin() const {
            return const_iterator(QList<T*>::begin());
        }

        const_iterator cbegin() const {
            return const_iterator(QList<T*>::cbegin());
        }

        const_iterator cend() const {
            return const_iterator(QList<T*>::cend());
        }

        void clear() {
            QList<T*>::clear();
        }

        const_iterator constBegin() const {
            return const_iterator(QList<T*>::constBegin());
        }

        const_iterator constEnd() const {
            return const_iterator(QList<T*>::constEnd());
        }

        bool contains(T &value) const {
            return QList<T*>::contains(&value);
        }

        int count(T &value) const {
            return QList<T*>::count(&value);
        }

        int count() const {
            return QList<T*>::count();
        }

        bool empty() const {
            return QList<T*>::empty();
        }

        iterator end() {
            return iterator(QList<T*>::end());
        }

        const_iterator end() const {
            return const_iterator(QList<T*>::end());
        }

        bool endsWith(T &value) const {
            return QList<T*>::endsWith(&value);
        }

        iterator erase(iterator pos) {
            return iterator(QList<T*>::erase(pos));
        }

        iterator erase(iterator begin, iterator end) {
            return iterator(QList<T*>::erase(begin, end));

        }

        T & first() {
            return *QList<T*>::first();
        }

        const T & first() const {
            return *QList<T*>::first();
        }

        /**
         * Inherited "from" methods are unsupported.
         */
        static ReferenceList<T> fromSet(const QSet<T> & set) = delete;
        static ReferenceList<T> fromStdList(const std::list<T> & list) = delete;
        static ReferenceList<T> fromVector(const QVector<T> & vector) = delete;

        T & front() {
            return *QList<T*>::front();
        }

        const T & front() const {
            return *QList<T*>::front();
        }

        int indexOf(T &value, int from = 0) const {
            return QList<T*>::indexOf(&value, from);
        }

        void insert(int i, T &value) {
            QList<T*>::insert(i, &value);
        }

        iterator insert(iterator before, T &value) {
            return iterator(QList<T*>::insert(before, &value));
        }

        bool isEmpty() const {
            return QList<T*>::isEmpty();
        }

        T & last() {
            return *QList<T*>::last();
        }

        const T & last() const {
            return *QList<T*>::last();
        }

        int lastIndexOf(T &value, int from = -1) const {
            return QList<T*>::lastIndexOf(&value, from);
        }

        int length() const {
            return QList<T*>::length();
        }

        ReferenceList<T> mid(int pos, int length = -1) const {
            return ReferenceList<T>(QList<T*>::mid(pos, length));
        }

        void move(int from, int to) {
            QList<T*>::move(from, to);
        }

        void pop_back() {
            QList<T*>::pop_back();
        }

        void pop_front() {
            QList<T*>::pop_front();
        }

        void prepend(T &value) {
            QList<T*>::prepend(&value);
        }

        void push_back(T &value) {
            QList<T*>::push_back(&value);
        }

        void push_front(T &value) {
            QList<T*>::push_front(&value);
        }

        int removeAll(T &value) {
            return QList<T*>::removeAll(&value);
        }

        void removeAt(int i) {
            QList<T*>::removeAt(i);
        }

        void removeFirst() {
            QList<T*>::removeFirst();
        }

        void removeLast() {
            QList<T*>::removeLast();
        }

        bool removeOne(T &value) {
            return QList<T*>::removeOne(&value);
        }

        void replace(int i, T &value) {
            QList<T*>::replace(i, &value);
        }

        void reserve(int alloc) {
            QList<T*>::reserve(alloc);
        }

        int size() const {
            return QList<T*>::size();
        }

        bool startsWith(T &value) const {
            return QList<T*>::startsWith(&value);
        }

        void swap(ReferenceList<T> &other) {
            QList<T*>::swap(other);
        }

        void swap(int i, int j) {
            QList<T*>::swap(i, j);
        }

        T & takeAt(int i) {
            return *QList<T*>::takeAt(i);
        }

        T & takeFirst() {
            return *QList<T*>::takeFirst();
        }

        T & takeLast() {
            return *QList<T*>::takeLast();
        }

        /**
         * Inherited "to" methods are not supported.
         */
        QSet<T> toSet() const = delete;
        std::list<T> toStdList() const = delete;
        QVector<T> toVector() const = delete;

        T & value(int i) const {
            return *QList<T*>::value(i);
        }

        T & value(int i, T &default_value) const {
            return *QList<T*>::value(i, &default_value);
        }

        bool operator!=(const ReferenceList<T> &other) const {
            return QList<T*>::operator!=(other);
        }

        ReferenceList<T> operator+(const ReferenceList<T> &other) const {
            return ReferenceList<T>(QList<T*>::operator+(other));
        }

        ReferenceList<T> & operator+=(const ReferenceList<T> &other) {
            QList<T*>::operator+=(other);
            return *this;
        }

        ReferenceList<T> & operator+=(T &value) {
            QList<T*>::operator+=(&value);
            return *this;
        }

        ReferenceList<T> & operator<<(const ReferenceList<T> &other) {
            QList<T*>::operator<<(other);
            return *this;
        }

        ReferenceList<T> & operator<<(T &value) {
            QList<T*>::operator<<(&value);
            return *this;
        }

        ReferenceList<T> & operator=(ReferenceList<T> &other) {
            QList<T*>::operator=(other);
            return *this;
        }

        ReferenceList & operator=(ReferenceList<T> &&other) {
            QList<T*>::operator=(std::move(other));
            return *this;
        }

        bool operator==(const ReferenceList<T> &other) const {
            return QList<T*>::operator==(other);
        }

        T & operator[](int i) {
            return *QList<T*>::operator[](i);
        }

        const T & operator[](int i) const {
            return *QList<T*>::operator[](i);
        }


        class iterator : public QList<T*>::iterator {
            public:
                iterator() {  }
                iterator(const typename QList<T*>::iterator &other)
                    : QList<T*>::iterator(other) {  }
                iterator(const iterator &other) : QList<T*>::iterator(other) {  }
                T & operator*() const { return *QList<T*>::iterator::operator*(); }
                T * operator->() const { return *QList<T*>::iterator::operator->(); }
                T & operator[](int j) const { return *QList<T*>::iterator::operator[](j); }
        };

        class const_iterator : public QList<T*>::const_iterator {
            public:
                const_iterator() {  }
                const_iterator(const typename QList<T*>::const_iterator &other)
                    : QList<T*>::const_iterator(other) {  }
                const_iterator(const const_iterator &other)
                    : QList<T*>::const_iterator(other) {  }
                const_iterator(const iterator &other)
                    : QList<T*>::const_iterator(other) {  }
                T & operator*() const { return *QList<T*>::const_iterator::operator*(); }
                T * operator->() const { return *QList<T*>::const_iterator::operator->(); }
                T & operator[](int j) const { return *QList<T*>::const_iterator::operator[](j); }
        };
};


/**
 * Implement QDebug << operator so that it will print out values rather than pointer addresses (do
 * to it implicitly converting to QList<T*>).
 */
template <class T>
QDebug operator<<(QDebug debug, const ReferenceList<T> &list) {
    debug.nospace() << '(';
    for(typename QList<T>::size_type i = 0; i < list.count(); ++i) {
        if(i) debug << ", ";
        debug << list.at(i);
    }

    debug << ')';

    return debug.space();
}


/**
* QDataStream << ReferenceList<T> should have same output as the QDataStream << QList<T>.
*/
template<class T>
QDataStream & operator<<(QDataStream &out, const ReferenceList<T>& list) {
    out << quint32(list.size());
    for(int i = 0; i < list.size(); ++i) out << list.at(i);
    return out;
}


/**
* Make sure QDataStream >> is not callable, there is no scenario where it would be useful with a
* ReferenceList.
*/
template<class T>
QDataStream & operator>>(QDataStream &out, const ReferenceList<T>& list) = delete;

#endif

关于c++ - 如何创建自定义引用容器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26319110/

相关文章:

c++ - 什么链接到 MSVCRTD?

android - 无法在模拟器中启动 AVD :QT library not found

c++ - 使用和不使用指针复制 char 数组

c++ - C++ 中的欧拉方法;值变得太大太快

c++ - 如何停止应用程序执行

c++ - 我可以打开多个 QWebView 实例来模拟打开 N 个浏览器吗?

c++ - 如何使 QChar.unicode() 报告组合字符的 utf-16 表示形式?

c++ - 延长临时生命周期,适用于 block 范围的聚合,但不能通过 `new` ;为什么?

c++ - 为每个模板(int)参数向类添加方法

c++ - 检测字符串中的 Unicode 字符