1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef _EXTENSIONS_COMPONENT_MODULE_HXX_
21 #define _EXTENSIONS_COMPONENT_MODULE_HXX_
23 /** you may find this file helpful if you implement a component (in it's own library) which can't use
24 the usual infrastructure.<br/>
25 More precise, you find helper classes to ease the use of resources and the registration of services.
27 You need to define a preprocessor variable COMPMOD_NAMESPACE in order to use this file. Set it to a string
28 which should be used as namespace for the classes defined herein.</p>
31 #include <osl/mutex.hxx>
32 #include <tools/resid.hxx>
33 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
34 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
35 #include <com/sun/star/uno/Sequence.hxx>
36 #include <com/sun/star/registry/XRegistryKey.hpp>
37 #include <cppuhelper/factory.hxx>
38 #include <rtl/string.hxx>
42 //.........................................................................
43 namespace COMPMOD_NAMESPACE
45 //.........................................................................
47 typedef ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XSingleServiceFactory
> (SAL_CALL
*FactoryInstantiation
)
49 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rServiceManager
,
50 const OUString
& _rComponentName
,
51 ::cppu::ComponentInstantiation _pCreateFunction
,
52 const ::com::sun::star::uno::Sequence
< OUString
> & _rServiceNames
,
56 //=========================================================================
58 //=========================================================================
62 friend class OModuleResourceClient
;
66 // not implemented. OModule is a static class
69 // resource administration
70 static ::osl::Mutex s_aMutex
; /// access safety
71 static sal_Int32 s_nClients
; /// number of registered clients
72 static OModuleImpl
* s_pImpl
; /// impl class. lives as long as at least one client for the module is registered
73 static OString s_sResPrefix
;
75 // auto registration administration
76 static ::com::sun::star::uno::Sequence
< OUString
>*
77 s_pImplementationNames
;
78 static ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Sequence
< OUString
> >*
80 static ::com::sun::star::uno::Sequence
< sal_Int64
>*
81 s_pCreationFunctionPointers
;
82 static ::com::sun::star::uno::Sequence
< sal_Int64
>*
83 s_pFactoryFunctionPointers
;
86 // can be set as long as no resource has been accessed ...
87 static void setResourceFilePrefix(const OString
& _rPrefix
);
89 /// get the vcl res manager of the module
90 static ResMgr
* getResManager();
92 /** register a component implementing a service with the given data.
93 @param _rImplementationName
94 the implementation name of the component
96 the services the component supports
97 @param _pCreateFunction
98 a function for creating an instance of the component
99 @param _pFactoryFunction
100 a function for creating a factory for that component
103 static void registerComponent(
104 const OUString
& _rImplementationName
,
105 const ::com::sun::star::uno::Sequence
< OUString
>& _rServiceNames
,
106 ::cppu::ComponentInstantiation _pCreateFunction
,
107 FactoryInstantiation _pFactoryFunction
);
109 /** revoke the registration for the specified component
110 @param _rImplementationName
111 the implementation name of the component
113 static void revokeComponent(
114 const OUString
& _rImplementationName
);
116 /** creates a Factory for the component with the given implementation name.
117 <p>Usually used from within component_getFactory.<p/>
118 @param _rxServiceManager
119 a pointer to an XMultiServiceFactory interface as got in component_getFactory
120 @param _pImplementationName
121 the implementation name of the component
123 the XInterface access to a factory for the component
125 static ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> getComponentFactory(
126 const OUString
& _rImplementationName
,
127 const ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XMultiServiceFactory
>& _rxServiceManager
131 /// register a client for the module
132 static void registerClient();
133 /// revoke a client for the module
134 static void revokeClient();
137 /** ensure that the impl class exists
138 @precond m_aMutex is guarded when this method gets called
140 static void ensureImpl();
143 //=========================================================================
144 //= OModuleResourceClient
145 //=========================================================================
146 /** base class for objects which uses any global module-specific resources
148 class OModuleResourceClient
151 OModuleResourceClient() { OModule::registerClient(); }
152 ~OModuleResourceClient() { OModule::revokeClient(); }
155 //=========================================================================
157 //=========================================================================
158 /** specialized ResId, using the resource manager provided by the global module
160 class ModuleRes
: public ::ResId
163 ModuleRes(sal_uInt16 _nId
) : ResId(_nId
, *OModule::getResManager()) { }
166 //==========================================================================
167 //= OMultiInstanceAutoRegistration
168 //==========================================================================
169 template <class TYPE
>
170 class OMultiInstanceAutoRegistration
173 /** automatically registeres a multi instance component
174 <p>Assumed that the template argument has the three methods
176 <li><code>static OUString getImplementationName_Static()</code><li/>
177 <li><code>static ::com::sun::star::uno::Sequence< OUString > getSupportedServiceNames_Static()</code><li/>
178 <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
179 Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
182 the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
184 The factory creation function used is <code>::cppu::createSingleFactory</code>.
185 @see OOneInstanceAutoRegistration
187 OMultiInstanceAutoRegistration();
188 ~OMultiInstanceAutoRegistration();
191 template <class TYPE
>
192 OMultiInstanceAutoRegistration
<TYPE
>::OMultiInstanceAutoRegistration()
194 OModule::registerComponent(
195 TYPE::getImplementationName_Static(),
196 TYPE::getSupportedServiceNames_Static(),
198 ::cppu::createSingleFactory
202 template <class TYPE
>
203 OMultiInstanceAutoRegistration
<TYPE
>::~OMultiInstanceAutoRegistration()
205 OModule::revokeComponent(TYPE::getImplementationName_Static());
208 //==========================================================================
209 //= OOneInstanceAutoRegistration
210 //==========================================================================
211 template <class TYPE
>
212 class OOneInstanceAutoRegistration
215 /** automatically registeres a single instance component
216 <p>Assumed that the template argument has the three methods
218 <li><code>static OUString getImplementationName_Static()</code><li/>
219 <li><code>static ::com::sun::star::uno::Sequence< OUString > getSupportedServiceNames_Static()</code><li/>
220 <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
221 Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
224 the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
226 The factory creation function used is <code>::cppu::createOneInstanceFactory</code>.
227 @see OOneInstanceAutoRegistration
229 OOneInstanceAutoRegistration();
230 ~OOneInstanceAutoRegistration();
233 template <class TYPE
>
234 OOneInstanceAutoRegistration
<TYPE
>::OOneInstanceAutoRegistration()
236 OModule::registerComponent(
237 TYPE::getImplementationName_Static(),
238 TYPE::getSupportedServiceNames_Static(),
240 ::cppu::createOneInstanceFactory
244 template <class TYPE
>
245 OOneInstanceAutoRegistration
<TYPE
>::~OOneInstanceAutoRegistration()
247 OModule::revokeComponent(TYPE::getImplementationName_Static());
250 //.........................................................................
251 } // namespace COMPMOD_NAMESPACE
252 //.........................................................................
254 #endif // _EXTENSIONS_COMPONENT_MODULE_HXX_
256 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */