gpt4 book ai didi

c++ - 模型中虚拟索引实现的替代方案

转载 作者:行者123 更新时间:2023-11-30 03:42:18 25 4
gpt4 key购买 nike

于是我又遇到了QObject不能和模板混合(至少不能直接)的限制。基本上我有一个代理模型类,它使用索引将源位置转换为本地位置并返回。索引可以通过多种方式实现,目前我需要两个版本,一个使用QHash,一个使用QVector。索引的界面对两者都是通用的,只有在索引操作方面存在细微差别。使用模板这会很容易,我会将类设为模板,然后对这两种情况使用特化。

但是模型需要是一个 QObject 所以我似乎需要像这样使用多态性:

class IndexInterface;
class VectorIndex; //inherits IndexInterface
class HashIndex; //inherits IndexInterface

class ProxyModel : public QObject
{
Q_OBJECT
public:
enum IndexType { Vector, Hash };

explicit ProxyModel(IndexType indexType, QObject *parent = 0) :
QObject(parent),
index(indexType == Vector ? new VectorIndex : new HashIndex)
{
}
//...

private:
IndexInterface *index = nullptr;
};

我有几个问题。首先,它需要动态分配我想摆脱的索引。其次,由于使用指向 IndexInterace 的指针来分派(dispatch)对索引的调用,索引的任何方法都不会被内联(我查看了反汇编代码以确认这一点并尝试了各种优化等以没有用)。

理想情况下,如果没有索引的动态分配和对索引的虚拟调用,该设计的替代方案是什么?

最佳答案

使特定于索引类型的类成为基类之一:

template <typename Index> class IndexHandler {
};

using VectorIndexHandler = IndexHandler<QVector>;
using HashIndexHandler = IndexHandler<QHash>;

class VectorIndexProxy : public QAbstractItemModel, VectorIndexHandler {
... // should be very small
};

class HashIndexProxy : public QAbstractItemModel, HashIndexHandler {
... // should be very small
};

然后不将索引类型传递给构造函数,而是使用工厂函数:

QAbstractItemModel * proxyFactory(IndexType indexType, QObject * parent = 0) {
switch (indexType) {
case Foo::Vector:
return new VectorIndexProxy(parent);
...
}
}

如果您设想一个比 QAbstractItemModel 更广泛或不同的界面,当然,您需要编写这样一个基类并在具体实现中从中派生。

如果需要 IndexHandler,您可以使用 CRTP直接调用派生类的方法,使其更小:

template <typename Index, typename Derived> class IndexHandler {
Derived * derived() { return static_cast<Derived*>(this); }
const Derived * derived() const; // as above
void foo() {
derived()->setObjectName("Yay");
}
};

class VectorIndexProxy :
public QAbstractItemModel,
public VectorIndexHandler<QVector, VectorIndexProxy>
{
... // should be very small
};

您还可以将基类中的方法“提升”为 Qt 槽:

class VectorIndexProxy : ... {
#ifdef Q_MOC_RUN
Q_SLOT void foo();
#endif
};

参见 this question关于拥有一个带有信号和槽的非 QObject 基类。

最后,您可以使用 PIMPL idiom ,并根据需要具体实现固定类型。工厂将在构造函数中调用,您将在不同的 PIMPL 中交换不同的索引。这并不像您想象的那么昂贵,因为所有 Qt 类都已经动态分配了一个 PIMPL,因此您可以通过从 QObjectPrivate 派生 PIMPL 来利用该分配。 ( #include <private/qobject_p.h> ),并将 PIMPL 的实例传递给 protected QObject(QObjectPrivate&) .这种模式在 Qt 中无处不在,因此即使它是一个实现细节,它至少不会在 Qt 5 中消失。这是一个粗略的草图:

// ProxyModel.cpp
#include <private/qobject_p.h>

class ProxyModelPrivate : public QObjectPrivate {
// Note: you don't need a q-pointer, QObjectData already provides it
// for you! CAVEAT: q-pointer is not valid until the QObject-derived-class's
// constructor has returned. This would be the case even if you passed
// the q-pointer explicitly, of course.
...
}; // base class

class VectorProxyModelPrivate : public ProxyModelPrivate { ... };

class ProxyModel : public QObject
{
Q_OBJECT
Q_DECLARE_PRIVATE(ProxyModel)
ProxyModel * pimpl(IndexType indexType) {
switch (indexType) {
case Vector: return new VectorProxyModelPrivate();
...
}
public:
enum IndexType { Vector, Hash };

explicit ProxyModel(IndexType indexType, QObject *parent = 0) :
QObject(*pimpl(IndexType), parent)
{}
};

如果您是从 QAbstractItemModel 派生的,您的 PIMPL 将派生自 QAbstractItemModelPrivate , 以同样的方式;这适用于任何 QObject - 在 Qt 中派生类!

关于c++ - 模型中虚拟索引实现的替代方案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36899800/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com