2 Copyright (C) 2001-2006, William Joseph.
5 This file is part of GtkRadiant.
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.
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.
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
22 #if !defined( INCLUDED_MODULESYSTEM_SINGLETONMODULE_H )
23 #define INCLUDED_MODULESYSTEM_SINGLETONMODULE_H
25 #include "modulesystem.h"
27 #include "debugging/debugging.h"
28 #include "modulesystem/moduleregistry.h"
29 #include "generic/reference.h"
31 template<typename API, typename Dependencies>
32 class DefaultAPIConstructor
35 const char* getName(){
36 return typename API::Name();
39 API* constructAPI( Dependencies& dependencies ){
42 void destroyAPI( API* api ){
47 template<typename API, typename Dependencies>
48 class DependenciesAPIConstructor
51 const char* getName(){
52 return typename API::Name();
55 API* constructAPI( Dependencies& dependencies ){
56 return new API( dependencies );
58 void destroyAPI( API* api ){
63 class NullDependencies
68 template<typename API, typename Dependencies = NullDependencies, typename APIConstructor = DefaultAPIConstructor<API, Dependencies> >
69 class SingletonModule : public APIConstructor, public Module, public ModuleRegisterable
71 Dependencies* m_dependencies;
73 std::size_t m_refcount;
74 bool m_dependencyCheck;
77 typedef typename API::Type Type;
80 : m_dependencies( 0 ), m_api( 0 ), m_refcount( 0 ), m_dependencyCheck( false ), m_cycleCheck( false ){
82 explicit SingletonModule( const APIConstructor& constructor )
83 : APIConstructor( constructor ), m_dependencies( 0 ), m_api( 0 ), m_refcount( 0 ), m_dependencyCheck( false ), m_cycleCheck( false ){
86 ASSERT_MESSAGE( m_refcount == 0, "module still referenced at shutdown" );
90 globalModuleServer().registerModule( typename Type::Name(), typename Type::Version(), APIConstructor::getName(), *this );
93 Dependencies& getDependencies(){
94 return *m_dependencies;
98 return m_api->getTable();
103 if ( ++m_refcount == 1 ) {
104 globalOutputStream() << "Module Initialising: '" << typename Type::Name() << "' '" << APIConstructor::getName() << "'\n";
105 m_dependencies = new Dependencies();
106 m_dependencyCheck = !globalModuleServer().getError();
107 if ( m_dependencyCheck ) {
108 m_api = APIConstructor::constructAPI( *m_dependencies );
109 globalOutputStream() << "Module Ready: '" << typename Type::Name() << "' '" << APIConstructor::getName() << "'\n";
113 globalOutputStream() << "Module Dependencies Failed: '" << typename Type::Name() << "' '" << APIConstructor::getName() << "'\n";
118 ASSERT_MESSAGE( m_cycleCheck, "cyclic dependency detected" );
121 if ( --m_refcount == 0 ) {
122 if ( m_dependencyCheck ) {
123 APIConstructor::destroyAPI( m_api );
125 delete m_dependencies;