Qpointer of QT source code analysis

Source: Internet
Author: User

Http://blog.csdn.net/oowgsoo/article/details/1529424

Qpointer is a pointer encapsulation class. Its function is similar to smart pointers, but its biggest feature should be pointer control. It wants a QT pointer (of course derived from qobject) can be owned by multiple classes at the same time.
Interface Programming is of course a very common thing, but when this pointer is deleted, we do not want to find the two interface classes and then notify them, on the contrary, we hope these two interface classes can directly determine the isnull method in qpointer.
Naturally, we know that the original pointer does not exist.

1. TestCode:
# Include <qapplication>
# Include <qpushbutton>
# Include <qpointer>

Int main (INT argc, char * argv [])
{
Qapplication app (argc, argv );

Qpushbutton * pbutton = new qpushbutton ("WGS ");
Qpointer <qpushbutton> button = pbutton;
Delete pbutton;
If (! Button. isnull ())
{
Button-> settext ("www ");
}
Return app.exe C ();
}

A short piece of code, here you need to note the use of the qpointer pointer
Template <class T>
Class qpointer
{
Qobject * O;
Public:
Inline qpointer (): O (0 ){}
Inline qpointer (T * P): O (P)
{Qmetaobject: addguard (& O );}
Inline qpointer (const qpointer <t> & P): O (P. O)
{Qmetaobject: addguard (& O );}
Inline ~ Qpointer ()
{Qmetaobject: removeguard (& O );}
Inline qpointer <t> & operator = (const qpointer <t> & P)
{If (this! = & P) qmetaobject: changeguard (& O, P. o); return * This ;}
Inline qpointer <t> & operator = (T * P)
{If (o! = P) qmetaobject: changeguard (& O, P); return * This ;}

Inline bool isnull () const
{Return! O ;}

inline T * operator-> () const
{return static_cast (const_cast (o ));}
inline T & operator * () const
{return * static_cast (const_cast (o ));}
inline operator T * () const
{return static_cast (const_cast (O) ;}< BR> };

qpointer is just a simple template that encapsulates qmetaobject-related operations. The basic idea here is
calling qmetaobject during qpointer construction :: addguard (& O) adds the T pointer to a hash table in qmetaobject.
call qmetaobject: removeguard (& O) during qpointer analysis ), delete the T pointer from the hash table
This is a static member of qmetaobject. The definition of this hash table is as follows:
typedef qmultihash guardhash;
the hash table stores pointer values and pointer addresses, so the code added is as follows:
void qmetaobject: addguard (qobject ** PTR)
{< br> If (! * PTR)
return;
guardhash * hash = guardhash ();
If (! Hash) {
* PTR = 0;
return;
}< br> qwritelocker locker (guardhashlock ());
hash-> insert (* PTR, PTR);
}

Why not just save one pointer? It was originally intended to prevent accidental deletion. Let's look at the deleted code:
void qmetaobject: removeguard (qobject ** PTR)
{< br> If (! * PTR)
return;
guardhash * hash = guardhash ();
If (! Hash)
return;
qwritelocker locker (guardhashlock ();
guardhash: iterator it = hash-> Find (* PTR );
const guardhash: iterator end = hash-> end ();
for (; it. key () = * PTR & it! = End; ++ it) {
If (it. value () = PTR) {
(void) hash-> erase (it);
break;
}< BR >}< br> only in it. value () = PTR will be deleted

But wait. How can I update this hash table when I delete a normal pointer?
Delete pbutton; how to notify qmetaobject to update the hash table?
The answer is in qobject. Pay attention to the qobject destructor.
Qobject ::~ Qobject ()
{
Q_d (qobject );
If (D-> wasdeleted ){
# If defined (qt_debug)
Qwarning ("Double qobject deletion detected ");
# Endif
Return;
}
D-> wasdeleted = true;

D-> blocksig = 0; // unblock signals so we always emit destroyed ()

// Set all qpointers for this object to zero
Guardhash * hash =: guardhash ();
If (hash ){
Qwritelocker locker (guardhashlock ());
Guardhash: iterator it = hash-> Find (this );
Const guardhash: iterator end = hash-> end ();
While (it. Key () = This & it! = END ){
* It. Value () = 0;
It = hash-> erase (it );
}
}

Emit destroyed (this );

qconnectionlist * List =: connectionlist ();
If (list) {
qwritelocker locker (& list-> lock );
list-> remove (this);
}

If (D-> pendtimer ){
// Have pending timers
Qthread * thr = thread ();
If (THR | D-> thread = 0 ){
// Don't unregister timers in the wrong thread
Qiniacteventdispatcher * eventdispatcher = qiniacteventdispatcher: instance (THR );
If (eventdispatcher)
Eventdispatcher-> unregistertimers (this );
}
}

D-> eventfilters. Clear ();

// Delete children objects
If (! D-> children. isempty ()){
Qdeleteall (D-> children );
D-> children. Clear ();
}

{
Qwritelocker locker (qobjectprivate: readwritelock ());
: Qt_removeobject (this );

/*
Theoretically, we cannot check D-> postedevents
Holding the posteventlist. mutex for the object's thread,
But since we hold the qobjectprivate: readwritelock (),
Nothing can go into qcoreapplication: postevent (), which
Effectively means noone can post new events, which is what
We are trying to prevent. This means we can safely check
D-> postedevents, since we are fairly sure it will not
Change (it cocould, but only by decreasing, I. e. Removing
Posted events from a differebnt thread)
*/
If (D-> postedevents> 0)
Qcoreapplication: removepostedevents (this );
}

If (D-> parent) // remove it from parent object
D-> setparent_helper (0 );

Delete D;
D_ptr = 0;
}
Here we have done a lot of things, including the code
// Set all qpointers for this object to zero
Guardhash * hash =: guardhash ();
If (hash ){
Qwritelocker locker (guardhashlock ());
Guardhash: iterator it = hash-> Find (this );
Const guardhash: iterator end = hash-> end ();
While (it. Key () = This & it! = END ){
* It. Value () = 0;
It = hash-> erase (it );
}
}

Update the hash table in qmetaobject

This is the benefit of a single base class, and many things can be controlled here

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.