1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 #ifndef UNOTOOLS_INC_UNOTOOLS_COMPONENTRESMODULE_HXX 24 #define UNOTOOLS_INC_UNOTOOLS_COMPONENTRESMODULE_HXX 25 26 #include <comphelper/componentmodule.hxx> 27 #include <unotools/unotoolsdllapi.h> 28 29 /** === begin UNO includes === **/ 30 /** === end UNO includes === **/ 31 #include <tools/resid.hxx> 32 33 #include <memory> 34 35 class ResMgr; 36 37 //........................................................................ 38 namespace utl 39 { 40 //........................................................................ 41 42 class OComponentResModuleImpl; 43 44 //==================================================================== 45 //= OComponentResourceModule 46 //==================================================================== 47 /** extends the <type scope="comphelper">OModule</type> implementation with 48 simply resource access 49 */ 50 class UNOTOOLS_DLLPUBLIC OComponentResourceModule : public ::comphelper::OModule 51 { 52 private: 53 typedef ::comphelper::OModule BaseClass; 54 55 private: 56 ::std::auto_ptr< OComponentResModuleImpl > m_pImpl; 57 58 public: 59 OComponentResourceModule( const ::rtl::OString& _rResFilePrefix ); 60 ~OComponentResourceModule(); 61 62 /// get the vcl res manager of the module 63 ResMgr* getResManager(); 64 65 protected: 66 // OModule overridables 67 virtual void onFirstClient(); 68 virtual void onLastClient(); 69 }; 70 71 //========================================================================= 72 //= ModuleRes 73 //========================================================================= 74 /** specialized ResId, using the ressource manager provided by a given <type>OModule</type> 75 */ 76 class UNOTOOLS_DLLPUBLIC ModuleRes : public ::ResId 77 { 78 public: ModuleRes(sal_uInt16 _nId,OComponentResourceModule & _rModule)79 ModuleRes( sal_uInt16 _nId, OComponentResourceModule& _rModule ) : ResId( _nId, *_rModule.getResManager() ) { } 80 }; 81 82 //==================================================================== 83 //= defining a concrete module 84 //==================================================================== 85 #define DEFINE_MODULE( ModuleClass, ClientClass, ResClass ) \ 86 /* -------------------------------------------------------------------- */ \ 87 class ModuleClass : public ::utl::OComponentResourceModule \ 88 { \ 89 friend struct CreateModuleClass; \ 90 typedef ::utl::OComponentResourceModule BaseClass; \ 91 \ 92 public: \ 93 static ModuleClass& getInstance(); \ 94 \ 95 private: \ 96 ModuleClass(); \ 97 }; \ 98 \ 99 /* -------------------------------------------------------------------- */ \ 100 class ClientClass : public ::comphelper::OModuleClient \ 101 { \ 102 private: \ 103 typedef ::comphelper::OModuleClient BaseClass; \ 104 \ 105 public: \ 106 ClientClass() : BaseClass( ModuleClass::getInstance() ) \ 107 { \ 108 } \ 109 }; \ 110 \ 111 /* -------------------------------------------------------------------- */ \ 112 class ResClass : public ::utl::ModuleRes \ 113 { \ 114 private: \ 115 typedef ::utl::ModuleRes BaseClass; \ 116 \ 117 public: \ 118 ResClass( sal_uInt16 _nId ) : BaseClass( _nId, ModuleClass::getInstance() ) \ 119 { \ 120 } \ 121 }; \ 122 \ 123 /* -------------------------------------------------------------------- */ \ 124 template < class TYPE > \ 125 class OAutoRegistration : public ::comphelper::OAutoRegistration< TYPE > \ 126 { \ 127 private: \ 128 typedef ::comphelper::OAutoRegistration< TYPE > BaseClass; \ 129 \ 130 public: \ 131 OAutoRegistration() : BaseClass( ModuleClass::getInstance() ) \ 132 { \ 133 } \ 134 }; \ 135 \ 136 /* -------------------------------------------------------------------- */ \ 137 template < class TYPE > \ 138 class OSingletonRegistration : public ::comphelper::OSingletonRegistration< TYPE > \ 139 { \ 140 private: \ 141 typedef ::comphelper::OSingletonRegistration< TYPE > BaseClass; \ 142 \ 143 public: \ 144 OSingletonRegistration() : BaseClass( ModuleClass::getInstance() ) \ 145 { \ 146 } \ 147 }; \ 148 \ 149 /* -------------------------------------------------------------------- */ \ 150 template < class TYPE > \ 151 class OLegacySingletonRegistration : public ::comphelper::OLegacySingletonRegistration< TYPE > \ 152 { \ 153 private: \ 154 typedef ::comphelper::OLegacySingletonRegistration< TYPE > BaseClass; \ 155 \ 156 public: \ 157 OLegacySingletonRegistration() : BaseClass( ModuleClass::getInstance() ) \ 158 { \ 159 } \ 160 }; 161 162 163 //==================================================================== 164 //= implementing a concrete module 165 //==================================================================== 166 #define IMPLEMENT_MODULE( ModuleClass, resprefix ) \ 167 struct CreateModuleClass \ 168 { \ 169 ModuleClass* operator()() \ 170 { \ 171 static ModuleClass* pModule = new ModuleClass; \ 172 return pModule; \ 173 /* yes, in theory, this is a resource leak, since the ModuleClass \ 174 will never be cleaned up. However, using a non-heap instance of ModuleClass \ 175 would not work: It would be cleaned up when the module is unloaded. \ 176 This might happen (and is likely to happen) *after* the tools-library \ 177 has been unloaded. However, the module's dtor is where we would delete \ 178 our resource manager (in case not all our clients de-registered) - which \ 179 would call into the already-unloaded tools-library. */ \ 180 } \ 181 }; \ 182 \ 183 ModuleClass::ModuleClass() \ 184 :BaseClass( ::rtl::OString( resprefix ) ) \ 185 { \ 186 } \ 187 \ 188 ModuleClass& ModuleClass::getInstance() \ 189 { \ 190 return *rtl_Instance< ModuleClass, CreateModuleClass, ::osl::MutexGuard, ::osl::GetGlobalMutex >:: \ 191 create( CreateModuleClass(), ::osl::GetGlobalMutex() ); \ 192 } \ 193 194 //........................................................................ 195 } // namespace utl 196 //........................................................................ 197 198 #endif // UNOTOOLS_INC_UNOTOOLS_COMPONENTRESMODULE_HXX 199 200