1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
6 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * OpenOffice.org - a multi-platform office productivity suite
10 * This file is part of OpenOffice.org.
12 * OpenOffice.org is free software: you can redistribute it and/or modify
13 * it under the terms of the GNU Lesser General Public License version 3
14 * only, as published by the Free Software Foundation.
16 * OpenOffice.org is distributed in the hope that it will be useful,
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 * GNU Lesser General Public License version 3 for more details
20 * (a copy is included in the LICENSE file that accompanied this code).
22 * You should have received a copy of the GNU Lesser General Public License
23 * version 3 along with OpenOffice.org. If not, see
24 * <http://www.openoffice.org/license.html>
25 * for a copy of the LGPLv3 License.
27 ************************************************************************/
29 #ifndef _EXTENSIONS_COMPONENT_MODULE_HXX_
30 #define _EXTENSIONS_COMPONENT_MODULE_HXX_
32 /** you may find this file helpfull if you implement a component (in it's own library) which can't use
33 the usual infrastructure.<br/>
34 More precise, you find helper classes to ease the use of resources and the registration of services.
36 You need to define a preprocessor variable COMPMOD_NAMESPACE in order to use this file. Set it to a string
37 which should be used as namespace for the classes defined herein.</p>
40 #include <osl/mutex.hxx>
41 #include <tools/resid.hxx>
42 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
44 #include <com/sun/star/uno/Sequence.hxx>
45 #include <com/sun/star/registry/XRegistryKey.hpp>
46 #include <cppuhelper/factory.hxx>
47 #include <rtl/string.hxx>
51 //.........................................................................
52 namespace COMPMOD_NAMESPACE
54 //.........................................................................
56 typedef ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XSingleServiceFactory
> (SAL_CALL
*FactoryInstantiation
)
58 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rServiceManager
,
59 const ::rtl::OUString
& _rComponentName
,
60 ::cppu::ComponentInstantiation _pCreateFunction
,
61 const ::com::sun::star::uno::Sequence
< ::rtl::OUString
> & _rServiceNames
,
62 rtl_ModuleCount
* _pModuleCounter
65 //=========================================================================
67 //=========================================================================
71 friend class OModuleResourceClient
;
75 // not implemented. OModule is a static class
78 // resource administration
79 static ::osl::Mutex s_aMutex
; /// access safety
80 static sal_Int32 s_nClients
; /// number of registered clients
81 static OModuleImpl
* s_pImpl
; /// impl class. lives as long as at least one client for the module is registered
82 static ::rtl::OString s_sResPrefix
;
84 // auto registration administration
85 static ::com::sun::star::uno::Sequence
< ::rtl::OUString
>*
86 s_pImplementationNames
;
87 static ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< ::rtl::OUString
> >*
89 static ::com::sun::star::uno::Sequence
< sal_Int64
>*
90 s_pCreationFunctionPointers
;
91 static ::com::sun::star::uno::Sequence
< sal_Int64
>*
92 s_pFactoryFunctionPointers
;
95 // cna be set as long as no resource has been accessed ...
96 static void setResourceFilePrefix(const ::rtl::OString
& _rPrefix
);
98 /// get the vcl res manager of the module
99 static ResMgr
* getResManager();
101 /** register a component implementing a service with the given data.
102 @param _rImplementationName
103 the implementation name of the component
104 @param _rServiceNames
105 the services the component supports
106 @param _pCreateFunction
107 a function for creating an instance of the component
108 @param _pFactoryFunction
109 a function for creating a factory for that component
112 static void registerComponent(
113 const ::rtl::OUString
& _rImplementationName
,
114 const ::com::sun::star::uno::Sequence
< ::rtl::OUString
>& _rServiceNames
,
115 ::cppu::ComponentInstantiation _pCreateFunction
,
116 FactoryInstantiation _pFactoryFunction
);
118 /** revoke the registration for the specified component
119 @param _rImplementationName
120 the implementation name of the component
122 static void revokeComponent(
123 const ::rtl::OUString
& _rImplementationName
);
125 /** creates a Factory for the component with the given implementation name.
126 <p>Usually used from within component_getFactory.<p/>
127 @param _rxServiceManager
128 a pointer to an XMultiServiceFactory interface as got in component_getFactory
129 @param _pImplementationName
130 the implementation name of the component
132 the XInterface access to a factory for the component
134 static ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> getComponentFactory(
135 const ::rtl::OUString
& _rImplementationName
,
136 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rxServiceManager
140 /// register a client for the module
141 static void registerClient();
142 /// revoke a client for the module
143 static void revokeClient();
146 /** ensure that the impl class exists
147 @precond m_aMutex is guarded when this method gets called
149 static void ensureImpl();
152 //=========================================================================
153 //= OModuleResourceClient
154 //=========================================================================
155 /** base class for objects which uses any global module-specific ressources
157 class OModuleResourceClient
160 OModuleResourceClient() { OModule::registerClient(); }
161 ~OModuleResourceClient() { OModule::revokeClient(); }
164 //=========================================================================
166 //=========================================================================
167 /** specialized ResId, using the ressource manager provided by the global module
169 class ModuleRes
: public ::ResId
172 ModuleRes(sal_uInt16 _nId
) : ResId(_nId
, *OModule::getResManager()) { }
175 //==========================================================================
176 //= OMultiInstanceAutoRegistration
177 //==========================================================================
178 template <class TYPE
>
179 class OMultiInstanceAutoRegistration
182 /** automatically registeres a multi instance component
183 <p>Assumed that the template argument has the three methods
185 <li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
186 <li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
187 <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
188 Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
191 the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
193 The factory creation function used is <code>::cppu::createSingleFactory</code>.
194 @see OOneInstanceAutoRegistration
196 OMultiInstanceAutoRegistration();
197 ~OMultiInstanceAutoRegistration();
200 template <class TYPE
>
201 OMultiInstanceAutoRegistration
<TYPE
>::OMultiInstanceAutoRegistration()
203 OModule::registerComponent(
204 TYPE::getImplementationName_Static(),
205 TYPE::getSupportedServiceNames_Static(),
207 ::cppu::createSingleFactory
211 template <class TYPE
>
212 OMultiInstanceAutoRegistration
<TYPE
>::~OMultiInstanceAutoRegistration()
214 OModule::revokeComponent(TYPE::getImplementationName_Static());
217 //==========================================================================
218 //= OOneInstanceAutoRegistration
219 //==========================================================================
220 template <class TYPE
>
221 class OOneInstanceAutoRegistration
224 /** automatically registeres a single instance component
225 <p>Assumed that the template argument has the three methods
227 <li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
228 <li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
229 <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
230 Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
233 the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
235 The factory creation function used is <code>::cppu::createOneInstanceFactory</code>.
236 @see OOneInstanceAutoRegistration
238 OOneInstanceAutoRegistration();
239 ~OOneInstanceAutoRegistration();
242 template <class TYPE
>
243 OOneInstanceAutoRegistration
<TYPE
>::OOneInstanceAutoRegistration()
245 OModule::registerComponent(
246 TYPE::getImplementationName_Static(),
247 TYPE::getSupportedServiceNames_Static(),
249 ::cppu::createOneInstanceFactory
253 template <class TYPE
>
254 OOneInstanceAutoRegistration
<TYPE
>::~OOneInstanceAutoRegistration()
256 OModule::revokeComponent(TYPE::getImplementationName_Static());
259 //.........................................................................
260 } // namespace COMPMOD_NAMESPACE
261 //.........................................................................
263 #endif // _EXTENSIONS_COMPONENT_MODULE_HXX_
265 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */