当前位置: 首页 > news >正文

必应站长平台做导购型网站

必应站长平台,做导购型网站,音乐网站html模板,中国室内设计网联盟一、什么是隐式共享 Qt 的隐式共享(implicit sharing)机制是一种设计模式,用于在进行数据拷贝时提高效率和减少内存占用。 在 Qt 中,许多类(如 QString、QList 等)都使用了隐式共享机制。这意味着当这些类…

一、什么是隐式共享

Qt 的隐式共享(implicit sharing)机制是一种设计模式,用于在进行数据拷贝时提高效率和减少内存占用。

Qt 中,许多类(如 QStringQList 等)都使用了隐式共享机制。这意味着当这些类的实例被拷贝时,实际上并不会立即进行数据的深拷贝,而是共享同一份数据。只有在其中一个实例发生修改时,才会进行实际的数据复制,以确保数据的独立性,即Copy-On-Write

隐式共享机制通过引用计数(reference counting)来实现。每个共享的实例都包含一个引用计数,用于记录当前有多少个实例共享同一份数据。当一个实例被拷贝时,引用计数会增加;当一个实例被销毁时,引用计数会减少。只有当引用计数为 1 时,才会进行实际的数据复制。

这种设计模式可以提高程序的性能和内存利用率,特别是在处理大量数据拷贝的情况下。同时,开发者也无需过多关注数据的共享和拷贝,从而简化了程序的设计和实现。

总之,Qt 的隐式共享机制是一种高效的数据共享和拷贝方式,它通过引用计数来实现数据的延迟复制,从而提高了程序的性能和内存利用率。

二、隐式共享代码示例

下面是一个简单的例子,展示了QString如何使用隐式共享:

#include <QString>
#include <QDebug>int main() {QString str1 = "Hello, Qt!";QString str2 = str1; // 这里并没有发生真正的数据复制,str1和str2共享同一份数据,拷贝赋值使用的是浅拷贝的方式qDebug() << str1; // 输出 "Hello, Qt!"qDebug() << str2; // 输出 "Hello, Qt!"str2[0] = 'h'; // 在这里,由于str2要修改数据,所以发生了真正的数据复制,str1和str2不再共享数据qDebug() << str1; // 输出 "Hello, Qt!"qDebug() << str2; // 输出 "hello, Qt!"return 0;
}

在上述代码中,当我们创建str2并将其初始化为str1时,并没有发生真正的数据复制,str1和str2实际上是共享同一份数据的。只有当我们试图修改str2的数据时,才会发生真正的数据复制,这就是所谓的写时复制。

这种技术的优点是可以大大减少不必要的数据复制,从而提高程序的性能。但是,它也有一些缺点,例如在多线程环境中可能需要额外的同步操作,以防止数据竞争。

在Qt的源码中,这种技术的实现主要依赖于引用计数和深拷贝。每个可以共享数据的对象都有一个引用计数,当引用计数为1时,表示只有一个对象在使用这份数据,可以直接修改。当引用计数大于1时,表示有多个对象在共享这份数据,如果有一个对象要修改数据,就需要先进行深拷贝,然后再修改新的数据,这样就不会影响到其他对象。

三、自定义一个使用隐式共享技术的数据类型

Qt中,你可以通过使用QSharedDataQSharedDataPointer类来实现隐式共享(也称为写时复制)。

以下是一个简单的例子,定义了一个自定义的数据类型MyData,它使用了隐式共享技术:

#include <QSharedData>
#include <QSharedDataPointer>class MyData : public QSharedData {
public:MyData() : x(0), y(0) {}MyData(int x, int y) : x(x), y(y) {}int x, y;
};class MySharedType {
public:MySharedType() : data(new MyData) {}MySharedType(int x, int y) : data(new MyData(x, y)) {}MySharedType(const MySharedType &other) : data(other.data) {}MySharedType &operator=(const MySharedType &other) {if (this != &other)data = other.data;return *this;}int x() const { return data->x; }int y() const { return data->y; }void setX(int x) { if (data->x != x) detach(); data->x = x; }void setY(int y) { if (data->y != y) detach(); data->y = y; }private:void detach() { if (data->ref != 1) data = new MyData(*data); }QSharedDataPointer<MyData> data;
};

在这个例子中,MyData类是实际存储数据的类,它继承自QSharedDataMySharedType类是用户使用的类,它包含一个QSharedDataPointer,指向MyData实例。当需要修改数据时,detach方法会被调用,如果有多个MySharedType实例共享同一个MyData实例,那么detach方法会创建一个新的MyData实例,以实现写时复制。

顺便看看 QSharedDataQSharedDataPointer 的源码实现:

#ifndef QSHAREDDATA_H
#define QSHAREDDATA_H#include <QtCore/qglobal.h>
#include <QtCore/qatomic.h>
#if QT_DEPRECATED_SINCE(5, 6)
#include <QtCore/qhash.h>
#endif
#include <QtCore/qhashfunctions.h>QT_BEGIN_NAMESPACEtemplate <class T> class QSharedDataPointer;class
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
Q_CORE_EXPORT
#endif
QSharedData
{
public:mutable QAtomicInt ref;   /// 原子计算inline QSharedData() noexcept : ref(0) { }inline QSharedData(const QSharedData &) noexcept : ref(0) { }// using the assignment operator would lead to corruption in the ref-countingQSharedData &operator=(const QSharedData &) = delete;~QSharedData() = default;
};template <class T> class QSharedDataPointer
{
public:typedef T Type;typedef T *pointer;/***************************************************//// 分离数据inline void detach() { if (d && d->ref.loadRelaxed() != 1) detach_helper(); }/***************************************************/inline T &operator*() { detach(); return *d; }inline const T &operator*() const { return *d; }inline T *operator->() { detach(); return d; }inline const T *operator->() const { return d; }inline operator T *() { detach(); return d; }inline operator const T *() const { return d; }inline T *data() { detach(); return d; }inline const T *data() const { return d; }inline const T *constData() const { return d; }inline bool operator==(const QSharedDataPointer<T> &other) const { return d == other.d; }inline bool operator!=(const QSharedDataPointer<T> &other) const { return d != other.d; }inline QSharedDataPointer() { d = nullptr; }inline ~QSharedDataPointer() { if (d && !d->ref.deref()) delete d; }explicit QSharedDataPointer(T *data) noexcept;inline QSharedDataPointer(const QSharedDataPointer<T> &o) : d(o.d) { if (d) d->ref.ref(); }inline QSharedDataPointer<T> & operator=(const QSharedDataPointer<T> &o) {if (o.d != d) {if (o.d)o.d->ref.ref();T *old = d;d = o.d;if (old && !old->ref.deref())delete old;}return *this;}inline QSharedDataPointer &operator=(T *o) {if (o != d) {if (o)o->ref.ref();T *old = d;d = o;if (old && !old->ref.deref())delete old;}return *this;}QSharedDataPointer(QSharedDataPointer &&o) noexcept : d(o.d) { o.d = nullptr; }inline QSharedDataPointer<T> &operator=(QSharedDataPointer<T> &&other) noexcept{QSharedDataPointer moved(std::move(other));swap(moved);return *this;}inline bool operator!() const { return !d; }inline void swap(QSharedDataPointer &other) noexcept{ qSwap(d, other.d); }protected:T *clone();private:void detach_helper();T *d;
};template <class T> inline bool operator==(std::nullptr_t p1, const QSharedDataPointer<T> &p2)
{Q_UNUSED(p1);return !p2;
}template <class T> inline bool operator==(const QSharedDataPointer<T> &p1, std::nullptr_t p2)
{Q_UNUSED(p2);return !p1;
}template <class T> class QExplicitlySharedDataPointer
{
public:typedef T Type;typedef T *pointer;inline T &operator*() const { return *d; }inline T *operator->() { return d; }inline T *operator->() const { return d; }inline T *data() const { return d; }inline const T *constData() const { return d; }inline T *take() { T *x = d; d = nullptr; return x; }inline void detach() { if (d && d->ref.loadRelaxed() != 1) detach_helper(); }inline void reset(){if(d && !d->ref.deref())delete d;d = nullptr;}inline operator bool () const { return d != nullptr; }inline bool operator==(const QExplicitlySharedDataPointer<T> &other) const { return d == other.d; }inline bool operator!=(const QExplicitlySharedDataPointer<T> &other) const { return d != other.d; }inline bool operator==(const T *ptr) const { return d == ptr; }inline bool operator!=(const T *ptr) const { return d != ptr; }inline QExplicitlySharedDataPointer() { d = nullptr; }inline ~QExplicitlySharedDataPointer() { if (d && !d->ref.deref()) delete d; }explicit QExplicitlySharedDataPointer(T *data) noexcept;inline QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<T> &o) : d(o.d) { if (d) d->ref.ref(); }template<class X>inline QExplicitlySharedDataPointer(const QExplicitlySharedDataPointer<X> &o)
#ifdef QT_ENABLE_QEXPLICITLYSHAREDDATAPOINTER_STATICCAST: d(static_cast<T *>(o.data()))
#else: d(o.data())
#endif{if(d)d->ref.ref();}inline QExplicitlySharedDataPointer<T> & operator=(const QExplicitlySharedDataPointer<T> &o) {if (o.d != d) {if (o.d)o.d->ref.ref();T *old = d;d = o.d;if (old && !old->ref.deref())delete old;}return *this;}inline QExplicitlySharedDataPointer &operator=(T *o) {if (o != d) {if (o)o->ref.ref();T *old = d;d = o;if (old && !old->ref.deref())delete old;}return *this;}inline QExplicitlySharedDataPointer(QExplicitlySharedDataPointer &&o) noexcept : d(o.d) { o.d = nullptr; }inline QExplicitlySharedDataPointer<T> &operator=(QExplicitlySharedDataPointer<T> &&other) noexcept{QExplicitlySharedDataPointer moved(std::move(other));swap(moved);return *this;}inline bool operator!() const { return !d; }inline void swap(QExplicitlySharedDataPointer &other) noexcept{ qSwap(d, other.d); }protected:T *clone();private:void detach_helper();T *d;
};template <class T>
Q_INLINE_TEMPLATE QSharedDataPointer<T>::QSharedDataPointer(T *adata) noexcept: d(adata)
{ if (d) d->ref.ref(); }template <class T>
Q_INLINE_TEMPLATE T *QSharedDataPointer<T>::clone()
{return new T(*d);
}template <class T>
Q_OUTOFLINE_TEMPLATE void QSharedDataPointer<T>::detach_helper()
{T *x = clone();x->ref.ref();if (!d->ref.deref())delete d;d = x;
}template <class T>
Q_INLINE_TEMPLATE T *QExplicitlySharedDataPointer<T>::clone()
{return new T(*d);
}template <class T>
Q_OUTOFLINE_TEMPLATE void QExplicitlySharedDataPointer<T>::detach_helper()
{T *x = clone();x->ref.ref();if (!d->ref.deref())delete d;d = x;
}template <class T>
Q_INLINE_TEMPLATE QExplicitlySharedDataPointer<T>::QExplicitlySharedDataPointer(T *adata) noexcept: d(adata)
{ if (d) d->ref.ref(); }template <class T> inline bool operator==(std::nullptr_t p1, const QExplicitlySharedDataPointer<T> &p2)
{Q_UNUSED(p1);return !p2;
}template <class T> inline bool operator==(const QExplicitlySharedDataPointer<T> &p1, std::nullptr_t p2)
{Q_UNUSED(p2);return !p1;
}template <class T>
Q_INLINE_TEMPLATE void swap(QSharedDataPointer<T> &p1, QSharedDataPointer<T> &p2)
{ p1.swap(p2); }template <class T>
Q_INLINE_TEMPLATE void swap(QExplicitlySharedDataPointer<T> &p1, QExplicitlySharedDataPointer<T> &p2)
{ p1.swap(p2); }template <class T>
Q_INLINE_TEMPLATE uint qHash(const QSharedDataPointer<T> &ptr, uint seed = 0) noexcept
{return qHash(ptr.data(), seed);
}
template <class T>
Q_INLINE_TEMPLATE uint qHash(const QExplicitlySharedDataPointer<T> &ptr, uint seed = 0) noexcept
{return qHash(ptr.data(), seed);
}template<typename T> Q_DECLARE_TYPEINFO_BODY(QSharedDataPointer<T>, Q_MOVABLE_TYPE);
template<typename T> Q_DECLARE_TYPEINFO_BODY(QExplicitlySharedDataPointer<T>, Q_MOVABLE_TYPE);QT_END_NAMESPACE#endif // QSHAREDDATA_H
http://www.yayakq.cn/news/551760/

相关文章:

  • 美化网站代码亿级流量网站架构
  • 重庆平台网站建设工作合肥在线设计
  • 用ps做一份网站iphone wordpress
  • 红叶网站建设方案网站建设 天佩营销
  • 微信分享网站短链接怎么做网站建设小江
  • 自己做博客网站网站优化建设苏州
  • 如何给网站做301跳转网页
  • 做设计怎么进公司网站智能小程序平台
  • 河南省住房和城乡建设厅二维码网站界面设计案例图片
  • 企业网站更新频率杭州建设教育网站
  • php除了做网站还能做什么本标准在住房城乡建设部门户网站
  • 大型网站开发价格一家只做外卖的网站
  • 在线制作图片网站品牌画册设计公司网址
  • 购物网站排名前100哈尔滨网站优化推广公司
  • 网站的营销方案企业网站建设公司价格
  • 做电销要在哪个网站上找资源东莞市一箭天网络科技有限公司
  • 网站被k表现网页制作详细教程
  • 建设工程 质量 协会网站网络推广内容策划
  • 重庆市设计公司网站网站怎么进行优化
  • 51一起做网站网站关键词更改
  • 东莞企业网站定制设计南昌有哪些做网站的公司
  • 影楼网站怎么做wordpress 3.5 下载
  • 评网网站建设成都网站开发公司
  • 大作业做网站代理记账包含哪些业务
  • 给别人做网站别人违法经营6第三方维护公司
  • 驻马店市建设工程网站优化培训学校
  • 怎么查看网站ftp地址课程精品网站开发
  • 执业医师变更注册网站艺术字体转换器在线转换器
  • 彩妆做推广的网站网站建设方案书的内容
  • 网站新闻列表怎么做齐家网装修