1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2000, 2010 Oracle and/or its affiliates.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * This file is part of OpenOffice.org.
11 * OpenOffice.org is free software: you can redistribute it and/or modify
12 * it under the terms of the GNU Lesser General Public License version 3
13 * only, as published by the Free Software Foundation.
15 * OpenOffice.org is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU Lesser General Public License version 3 for more details
19 * (a copy is included in the LICENSE file that accompanied this code).
21 * You should have received a copy of the GNU Lesser General Public License
22 * version 3 along with OpenOffice.org. If not, see
23 * <http://www.openoffice.org/license.html>
24 * for a copy of the LGPLv3 License.
26 ************************************************************************/
28 #ifndef _EXTENSIONS_COMPONENT_MODULE_HXX_
29 #define _EXTENSIONS_COMPONENT_MODULE_HXX_
31 /** you may find this file helpfull if you implement a component (in it's own library) which can't use
32 the usual infrastructure.<br/>
33 More precise, you find helper classes to ease the use of resources and the registration of services.
35 You need to define a preprocessor variable COMPMOD_NAMESPACE in order to use this file. Set it to a string
36 which should be used as namespace for the classes defined herein.</p>
39 #include <osl/mutex.hxx>
40 #include <tools/resid.hxx>
41 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
43 #include <com/sun/star/uno/Sequence.hxx>
44 #include <com/sun/star/registry/XRegistryKey.hpp>
45 #include <cppuhelper/factory.hxx>
46 #include <rtl/string.hxx>
50 //.........................................................................
51 namespace COMPMOD_NAMESPACE
53 //.........................................................................
55 typedef ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XSingleServiceFactory
> (SAL_CALL
*FactoryInstantiation
)
57 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rServiceManager
,
58 const ::rtl::OUString
& _rComponentName
,
59 ::cppu::ComponentInstantiation _pCreateFunction
,
60 const ::com::sun::star::uno::Sequence
< ::rtl::OUString
> & _rServiceNames
,
61 rtl_ModuleCount
* _pModuleCounter
64 //=========================================================================
66 //=========================================================================
70 friend class OModuleResourceClient
;
74 // not implemented. OModule is a static class
77 // resource administration
78 static ::osl::Mutex s_aMutex
; /// access safety
79 static sal_Int32 s_nClients
; /// number of registered clients
80 static OModuleImpl
* s_pImpl
; /// impl class. lives as long as at least one client for the module is registered
81 static ::rtl::OString s_sResPrefix
;
83 // auto registration administration
84 static ::com::sun::star::uno::Sequence
< ::rtl::OUString
>*
85 s_pImplementationNames
;
86 static ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< ::rtl::OUString
> >*
88 static ::com::sun::star::uno::Sequence
< sal_Int64
>*
89 s_pCreationFunctionPointers
;
90 static ::com::sun::star::uno::Sequence
< sal_Int64
>*
91 s_pFactoryFunctionPointers
;
94 // cna be set as long as no resource has been accessed ...
95 static void setResourceFilePrefix(const ::rtl::OString
& _rPrefix
);
97 /// get the vcl res manager of the module
98 static ResMgr
* getResManager();
100 /** register a component implementing a service with the given data.
101 @param _rImplementationName
102 the implementation name of the component
103 @param _rServiceNames
104 the services the component supports
105 @param _pCreateFunction
106 a function for creating an instance of the component
107 @param _pFactoryFunction
108 a function for creating a factory for that component
111 static void registerComponent(
112 const ::rtl::OUString
& _rImplementationName
,
113 const ::com::sun::star::uno::Sequence
< ::rtl::OUString
>& _rServiceNames
,
114 ::cppu::ComponentInstantiation _pCreateFunction
,
115 FactoryInstantiation _pFactoryFunction
);
117 /** revoke the registration for the specified component
118 @param _rImplementationName
119 the implementation name of the component
121 static void revokeComponent(
122 const ::rtl::OUString
& _rImplementationName
);
124 /** write the registration information of all known components
125 <p>writes the registration information of all components which are currently registered into the
126 specified registry.<p/>
127 <p>Usually used from within component_writeInfo.<p/>
128 @param _rxServiceManager
131 the registry key under which the information will be stored
133 sal_True if the registration of all implementations was successfull, sal_False otherwise
135 static sal_Bool
writeComponentInfos(
136 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rxServiceManager
,
137 const ::com::sun::star::uno::Reference
< ::com::sun::star::registry::XRegistryKey
>& _rRootKey
);
139 /** creates a Factory for the component with the given implementation name.
140 <p>Usually used from within component_getFactory.<p/>
141 @param _rxServiceManager
142 a pointer to an XMultiServiceFactory interface as got in component_getFactory
143 @param _pImplementationName
144 the implementation name of the component
146 the XInterface access to a factory for the component
148 static ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> getComponentFactory(
149 const ::rtl::OUString
& _rImplementationName
,
150 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rxServiceManager
154 /// register a client for the module
155 static void registerClient();
156 /// revoke a client for the module
157 static void revokeClient();
160 /** ensure that the impl class exists
161 @precond m_aMutex is guarded when this method gets called
163 static void ensureImpl();
166 //=========================================================================
167 //= OModuleResourceClient
168 //=========================================================================
169 /** base class for objects which uses any global module-specific ressources
171 class OModuleResourceClient
174 OModuleResourceClient() { OModule::registerClient(); }
175 ~OModuleResourceClient() { OModule::revokeClient(); }
178 //=========================================================================
180 //=========================================================================
181 /** specialized ResId, using the ressource manager provided by the global module
183 class ModuleRes
: public ::ResId
186 ModuleRes(USHORT _nId
) : ResId(_nId
, *OModule::getResManager()) { }
189 //==========================================================================
190 //= OMultiInstanceAutoRegistration
191 //==========================================================================
192 template <class TYPE
>
193 class OMultiInstanceAutoRegistration
196 /** automatically registeres a multi instance component
197 <p>Assumed that the template argument has the three methods
199 <li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
200 <li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
201 <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
202 Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
205 the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
207 The factory creation function used is <code>::cppu::createSingleFactory</code>.
208 @see OOneInstanceAutoRegistration
210 OMultiInstanceAutoRegistration();
211 ~OMultiInstanceAutoRegistration();
214 template <class TYPE
>
215 OMultiInstanceAutoRegistration
<TYPE
>::OMultiInstanceAutoRegistration()
217 OModule::registerComponent(
218 TYPE::getImplementationName_Static(),
219 TYPE::getSupportedServiceNames_Static(),
221 ::cppu::createSingleFactory
225 template <class TYPE
>
226 OMultiInstanceAutoRegistration
<TYPE
>::~OMultiInstanceAutoRegistration()
228 OModule::revokeComponent(TYPE::getImplementationName_Static());
231 //==========================================================================
232 //= OOneInstanceAutoRegistration
233 //==========================================================================
234 template <class TYPE
>
235 class OOneInstanceAutoRegistration
238 /** automatically registeres a single instance component
239 <p>Assumed that the template argument has the three methods
241 <li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
242 <li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
243 <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
244 Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
247 the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
249 The factory creation function used is <code>::cppu::createOneInstanceFactory</code>.
250 @see OOneInstanceAutoRegistration
252 OOneInstanceAutoRegistration();
253 ~OOneInstanceAutoRegistration();
256 template <class TYPE
>
257 OOneInstanceAutoRegistration
<TYPE
>::OOneInstanceAutoRegistration()
259 OModule::registerComponent(
260 TYPE::getImplementationName_Static(),
261 TYPE::getSupportedServiceNames_Static(),
263 ::cppu::createOneInstanceFactory
267 template <class TYPE
>
268 OOneInstanceAutoRegistration
<TYPE
>::~OOneInstanceAutoRegistration()
270 OModule::revokeComponent(TYPE::getImplementationName_Static());
273 //.........................................................................
274 } // namespace COMPMOD_NAMESPACE
275 //.........................................................................
277 #endif // _EXTENSIONS_COMPONENT_MODULE_HXX_