#include <QtCore>
-class DPTR_CLASS_NAME(ClassWithDptr)
+class DPTR_CLASS_NAME(ClassWithDptr):public DPtr
{
public:
- static int cnt;
int num;
- Private(){num=cnt++;qDebug()<<"creates dptr"<<num;}
- Private(const Private&p){num=p.num;qDebug()<<"copyconst dptr"<<num;}
- ~Private(){qDebug()<<"delete dptr"<<num;}
- Private& operator=(const Private&p){
- qDebug()<<"copy dptr"<<p.num<<"onto"<<num;
- num=p.num;
- return *this;
- }
+ Private(){num=0;}
};
-int ClassWithDptr::Private::cnt=0;
DEFINE_DPTR(ClassWithDptr)
{
return QString("class with dptr %1").arg(d->num);
}
+int ClassWithDptr::num()const{return d->num;}
+void ClassWithDptr::setNum(int n){d->num=n;}
class DPTR_CLASS_NAME(ClassWithSDptr):public SharedDPtr
{
public:
- static int cnt;
int num;
- Private(){num=cnt++;qDebug()<<"creates dptr"<<num;}
- Private(const Private&p){num=p.num;qDebug()<<"copyconst dptr"<<num;}
- ~Private(){qDebug()<<"delete dptr"<<num;}
- Private& operator=(const Private&p){
- qDebug()<<"copy dptr"<<p.num<<"onto"<<num;
- num=p.num;
- return *this;
- }
+ Private(){num=0;}
};
-int ClassWithSDptr::Private::cnt=0;
DEFINE_SHARED_DPTR(ClassWithSDptr)
QString ClassWithSDptr::toString()const
{
return QString("class with shared dptr %1").arg(d->num);
}
+int ClassWithSDptr::num()const{return d->num;}
+void ClassWithSDptr::setNum(int n){d->num=n;}
void DPtrTest::simpleDP()
{
- qDebug()<<"dynamic"<<ClassWithDptr().toString();
- qDebug()<<"dynamic"<<ClassWithDptr().toString();
- ClassWithDptr so;
- qDebug()<<"static"<<so.toString();
- ClassWithDptr co(so);
- qDebug()<<"static copy"<<co.toString();
- qDebug()<<"dynamic copy"<<ClassWithDptr(so).toString();
- co=ClassWithDptr();
- qDebug()<<"overwritten"<<co.toString();
+ QCOMPARE(ClassWithDptr().num(),0);
+ ClassWithDptr o1;o1.setNum(1);
+ QCOMPARE(o1.num(),1);
+ ClassWithDptr o2;o2.setNum(2);
+ ClassWithDptr o3(o1);
+ QCOMPARE(o1.num(),1);
+ QCOMPARE(o2.num(),2);
+ QCOMPARE(o3.num(),1);
+ o1=o2;
+ QCOMPARE(o1.num(),2);
+ o2.setNum(3);
+ QCOMPARE(o1.num(),2);
+ QCOMPARE(o2.num(),3);
}
void DPtrTest::sharedDP()
{
- qDebug()<<"dynamic"<<ClassWithSDptr().toString();
- qDebug()<<"dynamic"<<ClassWithSDptr().toString();
- ClassWithSDptr so;
- qDebug()<<"static"<<so.toString();
- ClassWithSDptr co(so);
- qDebug()<<"static copy"<<co.toString();
- qDebug()<<"dynamic copy"<<ClassWithSDptr(so).toString();
- co=ClassWithSDptr();
- qDebug()<<"overwritten"<<co.toString();
- so=co;
- qDebug()<<"overwritten"<<so.toString();
+ QCOMPARE(ClassWithSDptr().num(),0);
+ ClassWithSDptr o1;o1.setNum(1);
+ QCOMPARE(o1.num(),1);
+ ClassWithSDptr o2;o2.setNum(2);
+ ClassWithSDptr o3(o1);
+ QCOMPARE(o1.num(),1);
+ QCOMPARE(o2.num(),2);
+ QCOMPARE(o3.num(),1);
+ o1.setNum(4);
+ QCOMPARE(o1.num(),4);
+ QCOMPARE(o2.num(),2);
+ QCOMPARE(o3.num(),4);
+ o2=o1;o2.setNum(5);
+ QCOMPARE(o1.num(),5);
+ QCOMPARE(o2.num(),5);
+ QCOMPARE(o3.num(),5);
}