summaryrefslogtreecommitdiff
path: root/src/util/cpointers.h
blob: 500567294645785e29a77997c6fec8ead6919e87 (plain)
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
31
32
33
34
35
36
37
38
39
40
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
78
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
104
105
106
107
108
109
110
111
112
113
114
115
/*********
*
* This file is part of BibleTime's source code, http://www.bibletime.info/.
*
* Copyright 1999-2008 by the BibleTime developers.
* The BibleTime source code is licensed under the GNU General Public License version 2.0.
*
**********/

#ifndef CPOINTERS_H
#define CPOINTERS_H

#include "backend/managers/clanguagemgr.h"


class CSwordBackend;
class CLanguageMgr;
class CDisplayTemplateMgr;

namespace InfoDisplay {
class CInfoDisplay;
}

/** Holds the pointers to important classes like modules, backend etc.
*/
class CPointers {
    protected:
        friend class BibleTime; //BibleTime may initialize this object
        friend class BibleTimeApp; //BibleTimeApp may initialize this object
        friend int main(int argc, char* argv[]); //main may set the printer

        //Empty virtuaual destructor
        virtual ~CPointers() {}

        /** Set the backend.
        * @param backend Pointer to the new application-wide Sword backend
        */
        static void setBackend(CSwordBackend* const backend);
        /** Set the info display.
        * @param iDisplay The pointer to the new info display.
        */
        static void setInfoDisplay(InfoDisplay::CInfoDisplay* const iDisplay);

        /** Delete the backend. Should be called by BibleTimeApp,
        * because the backend should be deleted as late as possible.
        */
        static void deleteBackend();
        /** Delete the printer. Should be called by BibleTimeApp,
        * because the printer should be deleted as late as possible.
        */
        static void deletePrinter();
        /** Delete the language manager. Should be called by BibleTimeApp,
        * because the language manager should be deleted as late as possible.
        */
        static void deleteLanguageMgr();
        /** Delete the display template manager. Should be called by BibleTimeApp,
        * because the template manager should be deleted as late as possible.
        */
        static void deleteDisplayTemplateMgr();

    public: // Public methods
        /** Returns a pointer to the backend
        * @return The backend pointer.
        */
        inline static CSwordBackend* backend();
        /** Returns a pointer to the language manager
        * @return The language manager
        */
        inline static CLanguageMgr* languageMgr();
        /** Returns a pointer to the info display.
        * @return The backend pointer.
        */
        inline static InfoDisplay::CInfoDisplay* infoDisplay();
        /** Returns a pointer to the application's display template manager
        * @return The backend pointer.
        */
        static CDisplayTemplateMgr* displayTemplateManager();

        struct PointerCache {
            PointerCache() {
                backend = 0;
                langMgr = 0;
                infoDisplay = 0;
                displayTemplateMgr = 0;
            };

            CSwordBackend* backend;
            CLanguageMgr*  langMgr;
            InfoDisplay::CInfoDisplay*  infoDisplay;
            CDisplayTemplateMgr* displayTemplateMgr;
        };
};

extern CPointers::PointerCache m_pointerCache;

/** Returns a pointer to the backend ... */
inline CSwordBackend* CPointers::backend() {
    return m_pointerCache.backend;
}

/** Returns a pointer to the backend ... */
inline CLanguageMgr* CPointers::languageMgr() {
    if (!m_pointerCache.langMgr) {
        m_pointerCache.langMgr = new CLanguageMgr();
    }
    return m_pointerCache.langMgr;
}

/** Returns a pointer to the printer object. */
inline InfoDisplay::CInfoDisplay* CPointers::infoDisplay() {
    return m_pointerCache.infoDisplay;
}


#endif