QGCSingleton.h 5.42 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
#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.
#define DECLARE_QGC_SINGLETON(className, interfaceName) \
    public: \
39
        static interfaceName* instance(bool nullOk = false); \
Don Gagne's avatar
Don Gagne committed
40 41 42
        static void setMockInstance(interfaceName* mock); \
        static interfaceName* _createSingleton(void); \
        static void _deleteSingleton(void); \
43
    private: \
Don Gagne's avatar
Don Gagne committed
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
        static interfaceName* _instance; \
        static interfaceName* _mockInstance; \
        static interfaceName* _realInstance; \

/// @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.
#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); \
60
        _instance = new className; \
Don Gagne's avatar
Don Gagne committed
61 62 63 64 65 66 67 68 69 70 71 72 73
        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; \
        } \
    } \
    \
74
    interfaceName* className::instance(bool nullOk) \
Don Gagne's avatar
Don Gagne committed
75
    { \
76
        if (!nullOk) { \
Don Gagne's avatar
Don Gagne committed
77
            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)."); \
78
        } \
Don Gagne's avatar
Don Gagne committed
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
        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;
104

Don Gagne's avatar
Don Gagne committed
105 106 107 108 109 110
/// 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
111
/// the setMock method to add and remove you mock implementation. See HomePositionManager example usage. In order to provide the
Don Gagne's avatar
Don Gagne committed
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
/// 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
144
/// The example above does not use an inteface so the second parameter to the macro is the class name as well.
145 146 147 148 149 150 151 152

class QGCSingleton : public QObject
{
    Q_OBJECT
    
};

#endif