]> de.git.xonotic.org Git - xonotic/netradiant.git/blob - libs/modulesystem/singletonmodule.h
Merge commit '515673c08f8718a237e90c2130a1f5294f966d6a'
[xonotic/netradiant.git] / libs / modulesystem / singletonmodule.h
1 /*
2 Copyright (C) 2001-2006, William Joseph.
3 All Rights Reserved.
4
5 This file is part of GtkRadiant.
6
7 GtkRadiant is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
11
12 GtkRadiant is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 GNU General Public License for more details.
16
17 You should have received a copy of the GNU General Public License
18 along with GtkRadiant; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
20 */
21
22 #if !defined(INCLUDED_MODULESYSTEM_SINGLETONMODULE_H)
23 #define INCLUDED_MODULESYSTEM_SINGLETONMODULE_H
24
25 #include "modulesystem.h"
26 #include <cstddef>
27 #include "debugging/debugging.h"
28 #include "modulesystem/moduleregistry.h"
29 #include "generic/reference.h"
30
31 template<typename API, typename Dependencies>
32 class DefaultAPIConstructor
33 {
34 public:
35   const char* getName()
36   {
37     return typename API::Name();
38   }
39
40   API* constructAPI(Dependencies& dependencies)
41   {
42     return new API;
43   }
44   void destroyAPI(API* api)
45   {
46     delete api;
47   }
48 };
49
50 template<typename API, typename Dependencies>
51 class DependenciesAPIConstructor
52 {
53 public:
54   const char* getName()
55   {
56     return typename API::Name();
57   }
58
59   API* constructAPI(Dependencies& dependencies)
60   {
61     return new API(dependencies);
62   }
63   void destroyAPI(API* api)
64   {
65     delete api;
66   }
67 };
68
69 class NullDependencies
70 {
71 };
72
73
74 template<typename API, typename Dependencies = NullDependencies, typename APIConstructor = DefaultAPIConstructor<API, Dependencies> >
75 class SingletonModule : public APIConstructor, public Module, public ModuleRegisterable
76 {
77   Dependencies* m_dependencies;
78   API* m_api;
79   std::size_t m_refcount;
80   bool m_dependencyCheck;
81   bool m_cycleCheck;
82 public:
83   typedef typename API::Type Type;
84
85   SingletonModule()
86     : m_dependencies(0), m_api(0), m_refcount(0), m_dependencyCheck(false), m_cycleCheck(false)
87   {
88   }
89   explicit SingletonModule(const APIConstructor& constructor)
90     : APIConstructor(constructor), m_dependencies(0), m_api(0), m_refcount(0), m_dependencyCheck(false), m_cycleCheck(false)
91   {
92   }
93   ~SingletonModule()
94   {
95     ASSERT_MESSAGE(m_refcount == 0, "module still referenced at shutdown");
96   }
97
98   void selfRegister()
99   {
100     globalModuleServer().registerModule(typename Type::Name(), typename Type::Version(), APIConstructor::getName(), *this);
101   }
102   
103   Dependencies& getDependencies()
104   {
105     return *m_dependencies;
106   }
107   void* getTable()
108   {
109     if(m_api != 0)
110     {
111       return m_api->getTable();
112     }
113     return 0;
114   }
115   void capture()
116   {
117     if(++m_refcount == 1)
118     {
119       globalOutputStream() << "Module Initialising: '" << typename Type::Name() << "' '" << APIConstructor::getName() << "'\n";
120       m_dependencies = new Dependencies();
121       m_dependencyCheck = !globalModuleServer().getError();
122       if(m_dependencyCheck)
123       {
124         m_api = APIConstructor::constructAPI(*m_dependencies);
125         globalOutputStream() << "Module Ready: '" << typename Type::Name() << "' '" << APIConstructor::getName() << "'\n";
126       }
127       else
128       {
129         globalOutputStream() << "Module Dependencies Failed: '" << typename Type::Name() << "' '" << APIConstructor::getName() << "'\n";
130       }
131       m_cycleCheck = true;
132     }
133
134     ASSERT_MESSAGE(m_cycleCheck, "cyclic dependency detected");
135   }
136   void release()
137   {
138     if(--m_refcount == 0)
139     {
140       if(m_dependencyCheck)
141       {
142         APIConstructor::destroyAPI(m_api);
143       }
144       delete m_dependencies;
145     }
146   }
147 };
148
149
150 #endif