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 /** creates a Factory for the component with the given implementation name.
125 <p>Usually used from within component_getFactory.<p/>
126 @param _rxServiceManager
127 a pointer to an XMultiServiceFactory interface as got in component_getFactory
128 @param _pImplementationName
129 the implementation name of the component
131 the XInterface access to a factory for the component
133 static ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> getComponentFactory(
134 const ::rtl::OUString
& _rImplementationName
,
135 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rxServiceManager
139 /// register a client for the module
140 static void registerClient();
141 /// revoke a client for the module
142 static void revokeClient();
145 /** ensure that the impl class exists
146 @precond m_aMutex is guarded when this method gets called
148 static void ensureImpl();
151 //=========================================================================
152 //= OModuleResourceClient
153 //=========================================================================
154 /** base class for objects which uses any global module-specific ressources
156 class OModuleResourceClient
159 OModuleResourceClient() { OModule::registerClient(); }
160 ~OModuleResourceClient() { OModule::revokeClient(); }
163 //=========================================================================
165 //=========================================================================
166 /** specialized ResId, using the ressource manager provided by the global module
168 class ModuleRes
: public ::ResId
171 ModuleRes(sal_uInt16 _nId
) : ResId(_nId
, *OModule::getResManager()) { }
174 //==========================================================================
175 //= OMultiInstanceAutoRegistration
176 //==========================================================================
177 template <class TYPE
>
178 class OMultiInstanceAutoRegistration
181 /** automatically registeres a multi instance component
182 <p>Assumed that the template argument has the three methods
184 <li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
185 <li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
186 <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
187 Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
190 the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
192 The factory creation function used is <code>::cppu::createSingleFactory</code>.
193 @see OOneInstanceAutoRegistration
195 OMultiInstanceAutoRegistration();
196 ~OMultiInstanceAutoRegistration();
199 template <class TYPE
>
200 OMultiInstanceAutoRegistration
<TYPE
>::OMultiInstanceAutoRegistration()
202 OModule::registerComponent(
203 TYPE::getImplementationName_Static(),
204 TYPE::getSupportedServiceNames_Static(),
206 ::cppu::createSingleFactory
210 template <class TYPE
>
211 OMultiInstanceAutoRegistration
<TYPE
>::~OMultiInstanceAutoRegistration()
213 OModule::revokeComponent(TYPE::getImplementationName_Static());
216 //==========================================================================
217 //= OOneInstanceAutoRegistration
218 //==========================================================================
219 template <class TYPE
>
220 class OOneInstanceAutoRegistration
223 /** automatically registeres a single instance component
224 <p>Assumed that the template argument has the three methods
226 <li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
227 <li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
228 <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
229 Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
232 the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
234 The factory creation function used is <code>::cppu::createOneInstanceFactory</code>.
235 @see OOneInstanceAutoRegistration
237 OOneInstanceAutoRegistration();
238 ~OOneInstanceAutoRegistration();
241 template <class TYPE
>
242 OOneInstanceAutoRegistration
<TYPE
>::OOneInstanceAutoRegistration()
244 OModule::registerComponent(
245 TYPE::getImplementationName_Static(),
246 TYPE::getSupportedServiceNames_Static(),
248 ::cppu::createOneInstanceFactory
252 template <class TYPE
>
253 OOneInstanceAutoRegistration
<TYPE
>::~OOneInstanceAutoRegistration()
255 OModule::revokeComponent(TYPE::getImplementationName_Static());
258 //.........................................................................
259 } // namespace COMPMOD_NAMESPACE
260 //.........................................................................
262 #endif // _EXTENSIONS_COMPONENT_MODULE_HXX_