QGCSingleton.h 5.83 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
/*=====================================================================
 
 QGroundControl Open Source Ground Control Station
 
 (c) 2009 - 2015 QGROUNDCONTROL PROJECT <http://www.qgroundcontrol.org>
 
 This file is part of the QGROUNDCONTROL project
 
 QGROUNDCONTROL is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
 
 QGROUNDCONTROL is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
 
 You should have received a copy of the GNU General Public License
 along with QGROUNDCONTROL. If not, see <http://www.gnu.org/licenses/>.
 
 ======================================================================*/

/// @file
///     @author Don Gagne <don@thegagnes.com>

#ifndef QGCSINGLETON_H
#define QGCSINGLETON_H

#include <QObject>
Don Gagne's avatar
Don Gagne committed
31 32 33 34 35 36 37 38 39
#include <QMutex>

/// @def DECLARE_QGC_SINGLETON
/// Include this macro in your Derived Class definition
///     @param className Derived Class name
///     @param interfaceName If your class is accessed through an interface specify that, if not specify Derived class name.
///             For example DECLARE_QGC_SINGLETON(UASManager, UASManagerInterface)
#define DECLARE_QGC_SINGLETON(className, interfaceName) \
    public: \
40
        static interfaceName* instance(bool nullOk = false); \
Don Gagne's avatar
Don Gagne committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77
        static void setMockInstance(interfaceName* mock); \
    private: \
        static interfaceName* _createSingleton(void); \
        static void _deleteSingleton(void); \
        static interfaceName* _instance; \
        static interfaceName* _mockInstance; \
        static interfaceName* _realInstance; \
        friend class QGCApplication; \
        friend class UnitTest; \

/// @def IMPLEMENT_QGC_SINGLETON
/// Include this macro in your Derived Class implementation
///     @param className Derived Class name
///     @param interfaceName If your class is accessed through an interface specify that, if not specify Derived class name.
///             For example DECLARE_QGC_SINGLETON(UASManager, UASManagerInterface)
#define IMPLEMENT_QGC_SINGLETON(className, interfaceName) \
    interfaceName* className::_instance = NULL; \
    interfaceName* className::_mockInstance = NULL; \
    interfaceName* className::_realInstance = NULL; \
    \
    interfaceName* className::_createSingleton(void) \
    { \
        Q_ASSERT(_instance == NULL); \
        _instance = new className(qgcApp()); \
        return _instance; \
    } \
    \
    void className::_deleteSingleton(void) \
    { \
        if (className::_instance) { \
            className* instance = qobject_cast<className*>(className::_instance); \
            Q_ASSERT_X(instance != NULL, "QGCSingleton", "If you hit this assert you may have forgotten to clear a Mock instance"); \
            className::_instance = NULL; \
            delete instance; \
        } \
    } \
    \
78
    interfaceName* className::instance(bool nullOk) \
Don Gagne's avatar
Don Gagne committed
79
    { \
80
        if (!nullOk) { \
Don Gagne's avatar
Don Gagne committed
81
            Q_ASSERT_X(_instance, "QGCSingleton", "Request for singleton that is NULL. If you hit this, then you have likely run into a startup or shutdown sequence bug (possibly intermittent)."); \
82
        } \
Don Gagne's avatar
Don Gagne committed
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107
        return _instance; \
    } \
    \
    void className::setMockInstance(interfaceName* mock) \
    { \
        if (mock) { \
            Q_ASSERT(_instance); \
            Q_ASSERT(!_realInstance); \
            \
            _realInstance = _instance; \
            _instance = dynamic_cast<interfaceName*>(mock); \
            Q_ASSERT(_instance); \
            _mockInstance = mock; \
        } else { \
            Q_ASSERT(_instance); \
            Q_ASSERT(_realInstance); \
            \
            _instance = _realInstance; \
            _realInstance = NULL; \
            _mockInstance = NULL; \
        } \
    }

class QGCApplication;
class UnitTest;
108

Don Gagne's avatar
Don Gagne committed
109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147
/// This is the base class for all app global singletons
///
/// All global singletons are created/destroyed at boot time by QGCApplication::_createSingletons and destroyed by QGC::Application::_destroySingletons.
/// This is done in order to make sure they are all created on the main thread. As such no other code other than Unit Test
/// code has access to the constructor/destructor. QGCSingleton supports replacing singletons with a mock implementation.
/// In this case your object must derive from an interface which in turn derives from QGCSingleton. Youu can then use
/// the setMock method to add and remove you mock implementation. See UASManager example usage. In order to provide the
/// appropriate methods to make all this work you need to use the DECLARE_QGC_SINGLETON and IMPLEMENT_QGC_SINGLETON
/// macros as follows:
/// @code{.unparsed}
///     // Header file
///
///     class MySingleton : public QGCSingleton {
///         Q_OBJECT
///
///         DECLARE_QGC_SINGLETON(MySingleton, MySingleton)
///
///         ...
///
///     private:
///         // Constructor/Desctructor private since all access is through the singleton methods
///         MySingleton(QObject* parent == NULL);
///        ~MySingleton();
///
///         ...
///     }
///
///     // Code file
///
///     IMPLEMENT_QGC_SINGLETON(MySingleton, MySingleton)
///
///     MySingleton::MySingleton(QObject* parent) :
///         QGCSigleton(parent)
///     {
///     }
///
///     // Other class methods...
///
/// @endcode
Don Gagne's avatar
Don Gagne committed
148
/// The example above does not use an inteface so the second parameter to the macro is the class name as well.
149 150 151 152 153

class QGCSingleton : public QObject
{
    Q_OBJECT
    
Don Gagne's avatar
Don Gagne committed
154 155
protected:
    /// Constructor is private since all creation is done through _createInstance
156
    ///     @param parent Parent object
Don Gagne's avatar
Don Gagne committed
157
    QGCSingleton(QObject* parent = NULL);
158 159 160
};

#endif