SignalBlocker.h

Go to the documentation of this file.
00001 /*
00002  * The information in this file is
00003  * Copyright(c) 2007 Ball Aerospace & Technologies Corporation
00004  * and is subject to the terms and conditions of the
00005  * GNU Lesser General Public License Version 2.1
00006  * The license text is available from   
00007  * http://www.gnu.org/licenses/lgpl.html
00008  */
00009 
00010 #ifndef SIGNALBLOCKER_H
00011 #define SIGNALBLOCKER_H
00012 
00013 #include "SafePtr.h"
00014 #include "Subject.h"
00015 
00016 /**
00017  * SignalEnabler is an RAII class for enabling/disabling signals on a Subject,
00018  * guaranteeing that the previous state is restored when the SignalEnabler
00019  * goes out of scope.
00020  *
00021  * @warning  Disabling signals can be very dangerous as it hides details from
00022  *           any attached slots which may depend on being notified to
00023  *           function properly.
00024  *
00025  * @see Subject, Subject::signalsEnabled
00026  */
00027 class SignalEnabler
00028 {
00029 public:
00030    /**
00031     * Creates the RAII object and enables or disables signals on the specified
00032     * Subject.
00033     *
00034     * @param subject
00035     *         The Subject to enable/disable
00036     *
00037     * @param enable
00038     *         The new enabled state
00039     *
00040     * @warning  Disabling signals can be very dangerous as it hides details from
00041     *           any attached slots which may depend on being notified to
00042     *           function properly.
00043     */
00044    SignalEnabler(Subject& subject, bool enable) :
00045       mpSubject(&subject),
00046       mWereEnabled(mpSubject->signalsEnabled())
00047    {
00048       mpSubject->enableSignals(enable);
00049    }
00050 
00051    /**
00052     * Creates the RAII object, leaving the enabled state unchanged.
00053     *
00054     * @param subject
00055     *         The Subject to restore upon destruction of the SignalEnabler.
00056     */
00057    explicit SignalEnabler(Subject& subject) :
00058       mpSubject(&subject),
00059       mWereEnabled(mpSubject->signalsEnabled())
00060    {
00061    }
00062 
00063    /**
00064     * Destroys the SignalEnabler, restoring the enabled state of the Subject to 
00065     * what it was when the SignalEnabler was created.
00066     */
00067    ~SignalEnabler()
00068    {
00069       if (mpSubject.get() != NULL)
00070       {
00071          mpSubject->enableSignals(mWereEnabled);
00072       }
00073    }
00074 
00075    /**
00076     * Returns the enabled state the Subject had when the SignalEnabler was
00077     * created.
00078     */
00079    bool wereEnabled() const
00080    {
00081       return mWereEnabled;
00082    }
00083 
00084 private:
00085    SignalEnabler& operator=(const SignalEnabler&); // prevents assignment
00086    SignalEnabler(const SignalEnabler&); // prevents copying
00087 
00088    SafePtr<Subject> mpSubject;
00089    bool mWereEnabled;
00090 };
00091 
00092 /**
00093  * Blocks signals on the given Subject.  The given Subject will have it's
00094  * signals blocked when SignalBlocker is constructed.  When SignalBlocker is
00095  * destructed, the state of signal blocking will return to the state it had prior
00096  * to the construction of SignalBlocker.
00097  *
00098  * A convenience class. The same as <code>SignalEnabler(subject, false)</code>.
00099  *
00100  * @warning  Disabling signals can be very dangerous as it hides details from
00101  *           any attached slots which may be depending on being notified to
00102  *           function properly.
00103  *
00104  * @see SignalEnabler, Subject, Subject::signalsEnabled
00105  */
00106 class SignalBlocker : SignalEnabler
00107 {
00108 public:
00109    /**
00110     * Creates the RAII object. Disables signals on the specified Subject.
00111     *
00112     * @param subject
00113     *         The Subject to disable
00114     *
00115     * @warning  Disabling signals can be very dangerous as it hides details from
00116     *           any attached slots which may depend on being notified to
00117     *           function properly.
00118     */
00119    explicit SignalBlocker(Subject& subject) :
00120       SignalEnabler(subject, false)
00121    {
00122    }
00123 
00124 private:
00125    SignalBlocker(const SignalBlocker& rhs);
00126    SignalBlocker& operator=(const SignalBlocker& rhs);
00127 };
00128 
00129 #endif

Software Development Kit - Opticks 4.9.0 Build 16218