公开类的内部组件,以防止编写过多代码和影响性能
Exposing internal component of a class in order to prevent writing too much code and impact on performance
假设我有一个类
class B : public class QObject {
Q_OBJECT
public:
B(QObject* parent=Q_NULLPTR);
signals:
void signalData(int data);
public slots:
void slotGetData();
private:
}
slotGetData()
是在外部触发的,基本上是从某个地方检索一些数据并使用signalData(int data)
将其发送回。另一方面,我还有另一节课
class A : public class QObject {
Q_OBJECT
public:
A(QObject* parent=Q_NULLPTR) {
// Init B, move to thread, setup timer, connect timer's timeout to B's slotGetData()
// Connect B to A
connect(this->B, SIGNAL(signalData(int)), this, SLOT(slotGetData(int)));
}
signals:
// Emit signal containing data to another Qt component
void signalData(int x);
private slots:
// Connect B::signalData(int x) to this slot and re-emit the data using A::signalData(int x). Don't do anything with the data!
void slotGetData(int x);
private:
B* workerObj;
QThread worker;
QTimer workerTimer;
}
它基本上负责实例化workerObj
,将其移动到线程worker
并将workerTimer
的timeout()
信号连接到B::slotGetData()
。
此类的目的是在想要使用它的第三方类中实现B
的正确集成(多线程),例如:
class ThirdParty : public class QWidget {
Q_OBJECT
public:
ThirdParty(QObject* parent=Q_NULLPTR) {
// Init A
// Connect to B through A
connect(this->integrationObj, SIGNAL(signalData(int)), this, SLOT(slotGetData(int)));
}
private slots:
// Connect A::signalData(int x) to this slot and do something with the x (change UI, whatever)
void slotGetData(int x);
private:
A* integrationObj;
}
类ThirdParty
通过A
间接访问B
的特定功能。
现在我面临的困境如下:
- 我应该只是中继来自
B
到A
的数据信号并将其公开为A
信号还是 - 我是否应该只返回对
B
的const
引用以允许其中具有A
的类(以便它可以使用B
)直接连接到B
的signalData(int x)
?
在第一种情况下(我有),我基本上必须镜像B
想要提供给外部内部A
的每个信号(通过在A
中提供各自的专用插槽以及与B
基本相同的信号)。不用说,这会导致有太多相同的东西,并且还会产生一些(即使只是轻微的)性能影响,因为我得到了 2 个信号发射(从B
到A
,然后从A
到任何其他对象都有A
)和 2 个时隙调用(一个在A
中从B
获取信号,一个在任何其他对象中A
从CC_获取信号(第32页)。
第二种情况看起来不错,但我担心我会暴露B
的功能,而包含A
的类可能无法访问这些功能
如果实现第二种情况,我会有这样的东西(B
不会改变):
class A : public class QObject {
Q_OBJECT
public:
A(QObject* parent=Q_NULLPTR);
const B* getB() const; // Return a reference to B that cannot be changed but can be used to expose B's slots and signals
signals:
private slots:
private:
B* workerObj;
QThread worker;
QTimer workerTimer;
}
class ThirdParty : public class QWidget {
Q_OBJECT
public:
ThirdParty(QObject* parent=Q_NULLPTR) {
// Init A
// Connect to B directly!
connect(this->A->getB(), SIGNAL(signalData(int)), this, SLOT(slotGetData(int)));
}
private slots:
// Connect B::signalData(int x) to this slot and do something with the x (change UI, whatever)
void slotGetData(int x);
private:
A* integrationObj;
}
我应该在这里做什么?可悲的是,没有私人信号,因此所有B
信号以及其所有公共插槽都将暴露。如果要使用所有这些,那么这种程度的暴露没有问题,但除此之外......没那么多。
脸掌我已经在我的另一个项目中有了解决方案。 :D我只需要在A
内部提供一个connectTo(QObject* thirdparty)
,它在内部仅将thirdparty
插槽连接到特定的B
信号,从而不会暴露B
中不打算暴露的东西!
另一个更好的解决方案是使用信令到信号连接(connect(const QObject *sender, const char *signal, const char *method, Qt::ConnectionType type)
)或使用子父关系,尽管在后者中,我必须确保父级实际设置。这也称为信号转发,需要 2 个信号,但没有额外的插槽。由于A
对ThirdParty
可见,我可以做(这里this
代表ThirdParty
)connect(this, SIGNAL(signalFromThirdPartyToB()), this->a, SIGNAL(signalFromAToB()))
设置ThirdParty
信号的转发,通过A
B
,然后通过调用(这里this
表示A
)connect(this, SIGNAL(signalFromAToB()), this->b, SLOT(slotB()))
,在A
转发信号到B
中的插槽之间进行实际的信号槽连接。
- GCC 和 Clang 代码性能的巨大差异
- 如何使用本征提高性能?(包括示例代码)
- 在.cpp文件中定义方法而不是在 C++ 的 .h 文件中定义方法时,如何提高代码的性能?
- 使用 const double* const 作为模板参数 - 代码性能问题
- 如何在没有性能命中的情况下抽象SIMD代码来处理不同的数据类型
- 为什么 C++ 代码实现的性能不比 python 实现更好?
- 如何提高此 OpenCL 缩减内核代码的性能?
- 相同的代码在不同的 gcc 编译器中存在巨大的性能差异
- 如何基准C 代码的性能
- 相同的代码执行两次:性能差异
- 附加到 C++/CLI dll 的性能探查器无法访问本机C++代码
- 牢记干净的代码的性能,什么更好
- 在将其尺寸较大的向量移动到容量较小的向量之前,是否可以通过使用Reserve()来提高代码性能
- 如何提高四叉树代码的性能以防止程序冻结
- 从编译器优化和代码性能的角度来看,"if constexpr"与"if"
- 内存泄漏和代码性能
- C++代码性能字符串进行比较
- Dalvik 对原生 C++ 代码性能的影响?
- 为什么添加一条从未执行过的指令会导致我的代码性能下降
- 粒子模拟的并行化OpenMP代码性能差