Ezen az oldalon

Utils::Guard Class

class Utils::Guard

A Guard osztály egy zármechanizmussal rendelkező rekurzív őrzőt valósít meg. Tovább...

Header: #include <Guard>

Részletes leírás

QSignalBlocker alternatívájaként használható. A QSignalBlocker blokkolja az objektum összes jelét, ami általában nem kívánatos. Blokkolhat olyan jeleket is, amelyekre az objektumnak belsőleg szüksége van. A Guard és a GuardLocker osztályok egyáltalán nem blokkolják a jeleket.

Ha egy objektum olyan metódusát hívja meg, amely viszont olyan jelet bocsáthat ki, amelyhez Ön csatlakozik, és ezt az értesítést figyelmen kívül szeretné hagyni, akkor a Guard objektumot osztálytagként kell tárolnia, és a GuardLocker objektumot közvetlenül az említett metódus meghívása előtt kell deklarálnia, például így:

class MyClass : public QObject
{
\dots
private:
    Guard updateGuard; // member of your class
};

\dots

void MyClass::updateOtherObject()
{
    GuardLocker updatelocker(updateGuard);
    otherObject->update(); // this may trigger a signal
}

Egy olyan sloton belül, amely a másik objektum jeléhez van csatlakoztatva, ellenőrizhetjük, hogy a Guard zárolva van-e, és ebben az esetben figyelmen kívül hagyhatjuk a további műveleteket:

void MyClass::otherObjectUpdated()
{
    if (updateGuard.isLocked())
        return;

    // we didn't trigger the update
    // so do update now
    \dots
}

A GuardLocker a destruktorában feloldja a Guard zárolását.

A Guard objektum rekurzív: ugyanazon Guard-példányhoz több GuardLocker objektumot is deklarálhat, és a Guard addig lesz lezárva, amíg a Guard számára létrehozott GuardLocker objektumok közül legalább egy a hatókörben van.

Copyright © The Qt Company Ltd. and other contributors. Documentation contributions included herein are the copyrights of their respective owners. The documentation provided herein is licensed under the terms of the GNU Free Documentation License version 1.3 as published by the Free Software Foundation. Qt and respective logos are trademarks of The Qt Company Ltd in Finland and/or other countries worldwide. All other trademarks are property of their respective owners.