1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: componentmodule.hxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _EXTENSIONS_COMPONENT_MODULE_HXX_
32 #define _EXTENSIONS_COMPONENT_MODULE_HXX_
34 /** you may find this file helpfull if you implement a component (in it's own library) which can't use
35 the usual infrastructure.<br/>
36 More precise, you find helper classes to ease the use of resources and the registration of services.
38 You need to define a preprocessor variable COMPMOD_NAMESPACE in order to use this file. Set it to a string
39 which should be used as namespace for the classes defined herein.</p>
42 #include <osl/mutex.hxx>
43 #include <tools/resid.hxx>
44 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
45 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
46 #include <com/sun/star/uno/Sequence.hxx>
47 #include <com/sun/star/registry/XRegistryKey.hpp>
48 #include <cppuhelper/factory.hxx>
49 #include <rtl/string.hxx>
53 //.........................................................................
54 namespace COMPMOD_NAMESPACE
56 //.........................................................................
58 typedef ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XSingleServiceFactory
> (SAL_CALL
*FactoryInstantiation
)
60 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rServiceManager
,
61 const ::rtl::OUString
& _rComponentName
,
62 ::cppu::ComponentInstantiation _pCreateFunction
,
63 const ::com::sun::star::uno::Sequence
< ::rtl::OUString
> & _rServiceNames
,
64 rtl_ModuleCount
* _pModuleCounter
67 //=========================================================================
69 //=========================================================================
73 friend class OModuleResourceClient
;
77 // not implemented. OModule is a static class
80 // resource administration
81 static ::osl::Mutex s_aMutex
; /// access safety
82 static sal_Int32 s_nClients
; /// number of registered clients
83 static OModuleImpl
* s_pImpl
; /// impl class. lives as long as at least one client for the module is registered
84 static ::rtl::OString s_sResPrefix
;
86 // auto registration administration
87 static ::com::sun::star::uno::Sequence
< ::rtl::OUString
>*
88 s_pImplementationNames
;
89 static ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< ::rtl::OUString
> >*
91 static ::com::sun::star::uno::Sequence
< sal_Int64
>*
92 s_pCreationFunctionPointers
;
93 static ::com::sun::star::uno::Sequence
< sal_Int64
>*
94 s_pFactoryFunctionPointers
;
97 // cna be set as long as no resource has been accessed ...
98 static void setResourceFilePrefix(const ::rtl::OString
& _rPrefix
);
100 /// get the vcl res manager of the module
101 static ResMgr
* getResManager();
103 /** register a component implementing a service with the given data.
104 @param _rImplementationName
105 the implementation name of the component
106 @param _rServiceNames
107 the services the component supports
108 @param _pCreateFunction
109 a function for creating an instance of the component
110 @param _pFactoryFunction
111 a function for creating a factory for that component
114 static void registerComponent(
115 const ::rtl::OUString
& _rImplementationName
,
116 const ::com::sun::star::uno::Sequence
< ::rtl::OUString
>& _rServiceNames
,
117 ::cppu::ComponentInstantiation _pCreateFunction
,
118 FactoryInstantiation _pFactoryFunction
);
120 /** revoke the registration for the specified component
121 @param _rImplementationName
122 the implementation name of the component
124 static void revokeComponent(
125 const ::rtl::OUString
& _rImplementationName
);
127 /** write the registration information of all known components
128 <p>writes the registration information of all components which are currently registered into the
129 specified registry.<p/>
130 <p>Usually used from within component_writeInfo.<p/>
131 @param _rxServiceManager
134 the registry key under which the information will be stored
136 sal_True if the registration of all implementations was successfull, sal_False otherwise
138 static sal_Bool
writeComponentInfos(
139 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rxServiceManager
,
140 const ::com::sun::star::uno::Reference
< ::com::sun::star::registry::XRegistryKey
>& _rRootKey
);
142 /** creates a Factory for the component with the given implementation name.
143 <p>Usually used from within component_getFactory.<p/>
144 @param _rxServiceManager
145 a pointer to an XMultiServiceFactory interface as got in component_getFactory
146 @param _pImplementationName
147 the implementation name of the component
149 the XInterface access to a factory for the component
151 static ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> getComponentFactory(
152 const ::rtl::OUString
& _rImplementationName
,
153 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rxServiceManager
157 /// register a client for the module
158 static void registerClient();
159 /// revoke a client for the module
160 static void revokeClient();
163 /** ensure that the impl class exists
164 @precond m_aMutex is guarded when this method gets called
166 static void ensureImpl();
169 //=========================================================================
170 //= OModuleResourceClient
171 //=========================================================================
172 /** base class for objects which uses any global module-specific ressources
174 class OModuleResourceClient
177 OModuleResourceClient() { OModule::registerClient(); }
178 ~OModuleResourceClient() { OModule::revokeClient(); }
181 //=========================================================================
183 //=========================================================================
184 /** specialized ResId, using the ressource manager provided by the global module
186 class ModuleRes
: public ::ResId
189 ModuleRes(USHORT _nId
) : ResId(_nId
, *OModule::getResManager()) { }
192 //==========================================================================
193 //= OMultiInstanceAutoRegistration
194 //==========================================================================
195 template <class TYPE
>
196 class OMultiInstanceAutoRegistration
199 /** automatically registeres a multi instance component
200 <p>Assumed that the template argument has the three methods
202 <li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
203 <li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
204 <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
205 Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
208 the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
210 The factory creation function used is <code>::cppu::createSingleFactory</code>.
211 @see OOneInstanceAutoRegistration
213 OMultiInstanceAutoRegistration();
214 ~OMultiInstanceAutoRegistration();
217 template <class TYPE
>
218 OMultiInstanceAutoRegistration
<TYPE
>::OMultiInstanceAutoRegistration()
220 OModule::registerComponent(
221 TYPE::getImplementationName_Static(),
222 TYPE::getSupportedServiceNames_Static(),
224 ::cppu::createSingleFactory
228 template <class TYPE
>
229 OMultiInstanceAutoRegistration
<TYPE
>::~OMultiInstanceAutoRegistration()
231 OModule::revokeComponent(TYPE::getImplementationName_Static());
234 //==========================================================================
235 //= OOneInstanceAutoRegistration
236 //==========================================================================
237 template <class TYPE
>
238 class OOneInstanceAutoRegistration
241 /** automatically registeres a single instance component
242 <p>Assumed that the template argument has the three methods
244 <li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
245 <li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
246 <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
247 Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
250 the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
252 The factory creation function used is <code>::cppu::createOneInstanceFactory</code>.
253 @see OOneInstanceAutoRegistration
255 OOneInstanceAutoRegistration();
256 ~OOneInstanceAutoRegistration();
259 template <class TYPE
>
260 OOneInstanceAutoRegistration
<TYPE
>::OOneInstanceAutoRegistration()
262 OModule::registerComponent(
263 TYPE::getImplementationName_Static(),
264 TYPE::getSupportedServiceNames_Static(),
266 ::cppu::createOneInstanceFactory
270 template <class TYPE
>
271 OOneInstanceAutoRegistration
<TYPE
>::~OOneInstanceAutoRegistration()
273 OModule::revokeComponent(TYPE::getImplementationName_Static());
276 //.........................................................................
277 } // namespace COMPMOD_NAMESPACE
278 //.........................................................................
280 #endif // _EXTENSIONS_COMPONENT_MODULE_HXX_