xref: /trunk/main/dbaccess/source/ext/adabas/Acomponentmodule.hxx (revision 914d351e5f5b84e4342a86d6ab8d4aca7308b9bd)
12e2212a7SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
32e2212a7SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
42e2212a7SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
52e2212a7SAndrew Rist  * distributed with this work for additional information
62e2212a7SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
72e2212a7SAndrew Rist  * to you under the Apache License, Version 2.0 (the
82e2212a7SAndrew Rist  * "License"); you may not use this file except in compliance
92e2212a7SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
112e2212a7SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
132e2212a7SAndrew Rist  * Unless required by applicable law or agreed to in writing,
142e2212a7SAndrew Rist  * software distributed under the License is distributed on an
152e2212a7SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
162e2212a7SAndrew Rist  * KIND, either express or implied.  See the License for the
172e2212a7SAndrew Rist  * specific language governing permissions and limitations
182e2212a7SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
202e2212a7SAndrew Rist  *************************************************************/
212e2212a7SAndrew Rist 
222e2212a7SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _EXTENSIONS_COMPONENT_MODULE_HXX_
25cdf0e10cSrcweir #define _EXTENSIONS_COMPONENT_MODULE_HXX_
26cdf0e10cSrcweir 
2707a3d7f1SPedro Giffuni /** you may find this file helpful if you implement a component (in it's own library) which can't use
28cdf0e10cSrcweir     the usual infrastructure.<br/>
29cdf0e10cSrcweir     More precise, you find helper classes to ease the use of resources and the registration of services.
30cdf0e10cSrcweir     <p>
31cdf0e10cSrcweir     You need to define a preprocessor variable COMPMOD_NAMESPACE in order to use this file. Set it to a string
32cdf0e10cSrcweir     which should be used as namespace for the classes defined herein.</p>
33cdf0e10cSrcweir */
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #ifndef _OSL_MUTEX_HXX_
36cdf0e10cSrcweir #include <osl/mutex.hxx>
37cdf0e10cSrcweir #endif
38cdf0e10cSrcweir #ifndef _TOOLS_RESID_HXX
39cdf0e10cSrcweir #include <tools/resid.hxx>
40cdf0e10cSrcweir #endif
41cdf0e10cSrcweir #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
42cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
43cdf0e10cSrcweir #endif
44cdf0e10cSrcweir #ifndef _COM_SUN_STAR_LANG_XSINGLESERVICEFACTORY_HPP_
45cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
46cdf0e10cSrcweir #endif
47cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UNO_SEQUENCE_HXX_
48cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
49cdf0e10cSrcweir #endif
50cdf0e10cSrcweir #ifndef _COM_SUN_STAR_REGISTRY_XREGISTRYKEY_HPP_
51cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
52cdf0e10cSrcweir #endif
53cdf0e10cSrcweir #ifndef _CPPUHELPER_FACTORY_HXX_
54cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
55cdf0e10cSrcweir #endif
56cdf0e10cSrcweir #ifndef _RTL_STRING_HXX_
57cdf0e10cSrcweir #include <rtl/string.hxx>
58cdf0e10cSrcweir #endif
59cdf0e10cSrcweir 
60cdf0e10cSrcweir class ResMgr;
61cdf0e10cSrcweir 
62cdf0e10cSrcweir //.........................................................................
63cdf0e10cSrcweir namespace COMPMOD_NAMESPACE
64cdf0e10cSrcweir {
65cdf0e10cSrcweir //.........................................................................
66cdf0e10cSrcweir 
67cdf0e10cSrcweir typedef ::com::sun::star::uno::Reference< ::com::sun::star::lang::XSingleServiceFactory > (SAL_CALL *FactoryInstantiation)
68cdf0e10cSrcweir         (
69cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rServiceManager,
70cdf0e10cSrcweir             const ::rtl::OUString & _rComponentName,
71cdf0e10cSrcweir             ::cppu::ComponentInstantiation _pCreateFunction,
72cdf0e10cSrcweir             const ::com::sun::star::uno::Sequence< ::rtl::OUString > & _rServiceNames,
73cdf0e10cSrcweir             rtl_ModuleCount* _pModuleCounter
74cdf0e10cSrcweir         );
75cdf0e10cSrcweir 
76cdf0e10cSrcweir     //=========================================================================
77cdf0e10cSrcweir     //= OModule
78cdf0e10cSrcweir     //=========================================================================
79cdf0e10cSrcweir     class OModuleImpl;
80cdf0e10cSrcweir     class OModule
81cdf0e10cSrcweir     {
82cdf0e10cSrcweir         friend class OModuleResourceClient;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir     private:
85cdf0e10cSrcweir         OModule();
86cdf0e10cSrcweir             // not implemented. OModule is a static class
87cdf0e10cSrcweir 
88cdf0e10cSrcweir     protected:
89cdf0e10cSrcweir         // resource administration
90cdf0e10cSrcweir         static ::osl::Mutex     s_aMutex;       /// access safety
91cdf0e10cSrcweir         static sal_Int32        s_nClients;     /// number of registered clients
92cdf0e10cSrcweir         static OModuleImpl*     s_pImpl;        /// impl class. lives as long as at least one client for the module is registered
93cdf0e10cSrcweir         static ::rtl::OString   s_sResPrefix;
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         // auto registration administration
96cdf0e10cSrcweir         static  ::com::sun::star::uno::Sequence< ::rtl::OUString >*
97cdf0e10cSrcweir             s_pImplementationNames;
98cdf0e10cSrcweir         static  ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::rtl::OUString > >*
99cdf0e10cSrcweir             s_pSupportedServices;
100cdf0e10cSrcweir         static  ::com::sun::star::uno::Sequence< sal_Int64 >*
101cdf0e10cSrcweir             s_pCreationFunctionPointers;
102cdf0e10cSrcweir         static  ::com::sun::star::uno::Sequence< sal_Int64 >*
103cdf0e10cSrcweir             s_pFactoryFunctionPointers;
104cdf0e10cSrcweir 
105cdf0e10cSrcweir     public:
106faea413bSMatthias Seidel         // can be set as long as no resource has been accessed ...
107cdf0e10cSrcweir         static void     setResourceFilePrefix(const ::rtl::OString& _rPrefix);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir         /// get the vcl res manager of the module
110cdf0e10cSrcweir         static ResMgr*  getResManager();
111cdf0e10cSrcweir 
112cdf0e10cSrcweir         /** register a component implementing a service with the given data.
113cdf0e10cSrcweir             @param  _rImplementationName
114cdf0e10cSrcweir                         the implementation name of the component
115cdf0e10cSrcweir             @param  _rServiceNames
116cdf0e10cSrcweir                         the services the component supports
117cdf0e10cSrcweir             @param  _pCreateFunction
118cdf0e10cSrcweir                         a function for creating an instance of the component
119cdf0e10cSrcweir             @param  _pFactoryFunction
120cdf0e10cSrcweir                         a function for creating a factory for that component
121cdf0e10cSrcweir             @see revokeComponent
122cdf0e10cSrcweir         */
123cdf0e10cSrcweir         static void registerComponent(
124cdf0e10cSrcweir             const ::rtl::OUString& _rImplementationName,
125cdf0e10cSrcweir             const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rServiceNames,
126cdf0e10cSrcweir             ::cppu::ComponentInstantiation _pCreateFunction,
127cdf0e10cSrcweir             FactoryInstantiation _pFactoryFunction);
128cdf0e10cSrcweir 
129cdf0e10cSrcweir         /** revoke the registration for the specified component
130cdf0e10cSrcweir             @param  _rImplementationName
131cdf0e10cSrcweir                 the implementation name of the component
132cdf0e10cSrcweir         */
133cdf0e10cSrcweir         static void revokeComponent(
134cdf0e10cSrcweir             const ::rtl::OUString& _rImplementationName);
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         /** creates a Factory for the component with the given implementation name.
137cdf0e10cSrcweir             <p>Usually used from within component_getFactory.<p/>
138cdf0e10cSrcweir             @param  _rxServiceManager
139cdf0e10cSrcweir                         a pointer to an XMultiServiceFactory interface as got in component_getFactory
140cdf0e10cSrcweir             @param  _pImplementationName
141cdf0e10cSrcweir                         the implementation name of the component
142cdf0e10cSrcweir             @return
143cdf0e10cSrcweir                         the XInterface access to a factory for the component
144cdf0e10cSrcweir         */
145cdf0e10cSrcweir         static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > getComponentFactory(
146cdf0e10cSrcweir             const ::rtl::OUString& _rImplementationName,
147cdf0e10cSrcweir             const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxServiceManager
148cdf0e10cSrcweir             );
149cdf0e10cSrcweir 
150cdf0e10cSrcweir     protected:
151cdf0e10cSrcweir         /// register a client for the module
152cdf0e10cSrcweir         static void registerClient();
153cdf0e10cSrcweir         /// revoke a client for the module
154cdf0e10cSrcweir         static void revokeClient();
155cdf0e10cSrcweir 
156cdf0e10cSrcweir     private:
157cdf0e10cSrcweir         /** ensure that the impl class exists
158cdf0e10cSrcweir             @precond m_aMutex is guarded when this method gets called
159cdf0e10cSrcweir         */
160cdf0e10cSrcweir         static void ensureImpl();
161cdf0e10cSrcweir     };
162cdf0e10cSrcweir 
163cdf0e10cSrcweir     //=========================================================================
164cdf0e10cSrcweir     //= OModuleResourceClient
165cdf0e10cSrcweir     //=========================================================================
166*816026e9SJohn Bampton     /** base class for objects which uses any global module-specific resources
167cdf0e10cSrcweir     */
168cdf0e10cSrcweir     class OModuleResourceClient
169cdf0e10cSrcweir     {
170cdf0e10cSrcweir     public:
OModuleResourceClient()171cdf0e10cSrcweir         OModuleResourceClient()     { OModule::registerClient(); }
~OModuleResourceClient()172cdf0e10cSrcweir         ~OModuleResourceClient()    { OModule::revokeClient(); }
173cdf0e10cSrcweir     };
174cdf0e10cSrcweir 
175cdf0e10cSrcweir     //=========================================================================
176cdf0e10cSrcweir     //= ModuleRes
177cdf0e10cSrcweir     //=========================================================================
17802f6802cSJohn Bampton     /** specialized ResId, using the resource manager provided by the global module
179cdf0e10cSrcweir     */
180cdf0e10cSrcweir     class ModuleRes : public ::ResId
181cdf0e10cSrcweir     {
182cdf0e10cSrcweir     public:
ModuleRes(sal_uInt16 _nId)183cdf0e10cSrcweir         ModuleRes(sal_uInt16 _nId) : ResId(_nId, *OModule::getResManager()) { }
184cdf0e10cSrcweir     };
185cdf0e10cSrcweir 
186cdf0e10cSrcweir     //==========================================================================
187cdf0e10cSrcweir     //= OMultiInstanceAutoRegistration
188cdf0e10cSrcweir     //==========================================================================
189cdf0e10cSrcweir     template <class TYPE>
190cdf0e10cSrcweir     class OMultiInstanceAutoRegistration
191cdf0e10cSrcweir     {
192cdf0e10cSrcweir     public:
193cdf0e10cSrcweir         /** automatically registeres a multi instance component
194cdf0e10cSrcweir             <p>Assumed that the template argument has the three methods
195cdf0e10cSrcweir                 <ul>
196cdf0e10cSrcweir                     <li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
197cdf0e10cSrcweir                     <li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
198cdf0e10cSrcweir                     <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
199cdf0e10cSrcweir                         Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
200cdf0e10cSrcweir                         </li>
201cdf0e10cSrcweir                 <ul/>
202cdf0e10cSrcweir             the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
203cdf0e10cSrcweir             <p/>
204cdf0e10cSrcweir             The factory creation function used is <code>::cppu::createSingleFactory</code>.
205cdf0e10cSrcweir             @see OOneInstanceAutoRegistration
206cdf0e10cSrcweir         */
207cdf0e10cSrcweir         OMultiInstanceAutoRegistration();
208cdf0e10cSrcweir         ~OMultiInstanceAutoRegistration();
209cdf0e10cSrcweir     };
210cdf0e10cSrcweir 
211cdf0e10cSrcweir     template <class TYPE>
OMultiInstanceAutoRegistration()212cdf0e10cSrcweir     OMultiInstanceAutoRegistration<TYPE>::OMultiInstanceAutoRegistration()
213cdf0e10cSrcweir     {
214cdf0e10cSrcweir         OModule::registerComponent(
215cdf0e10cSrcweir             TYPE::getImplementationName_Static(),
216cdf0e10cSrcweir             TYPE::getSupportedServiceNames_Static(),
217cdf0e10cSrcweir             TYPE::Create,
218cdf0e10cSrcweir             ::cppu::createSingleFactory
219cdf0e10cSrcweir             );
220cdf0e10cSrcweir     }
221cdf0e10cSrcweir 
222cdf0e10cSrcweir     template <class TYPE>
~OMultiInstanceAutoRegistration()223cdf0e10cSrcweir     OMultiInstanceAutoRegistration<TYPE>::~OMultiInstanceAutoRegistration()
224cdf0e10cSrcweir     {
225cdf0e10cSrcweir         OModule::revokeComponent(TYPE::getImplementationName_Static());
226cdf0e10cSrcweir     }
227cdf0e10cSrcweir 
228cdf0e10cSrcweir     //==========================================================================
229cdf0e10cSrcweir     //= OOneInstanceAutoRegistration
230cdf0e10cSrcweir     //==========================================================================
231cdf0e10cSrcweir     template <class TYPE>
232cdf0e10cSrcweir     class OOneInstanceAutoRegistration
233cdf0e10cSrcweir     {
234cdf0e10cSrcweir     public:
235cdf0e10cSrcweir         /** automatically registeres a single instance component
236cdf0e10cSrcweir             <p>Assumed that the template argument has the three methods
237cdf0e10cSrcweir                 <ul>
238cdf0e10cSrcweir                     <li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
239cdf0e10cSrcweir                     <li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
240cdf0e10cSrcweir                     <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
241cdf0e10cSrcweir                         Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
242cdf0e10cSrcweir                         </li>
243cdf0e10cSrcweir                 <ul/>
244cdf0e10cSrcweir             the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
245cdf0e10cSrcweir             <p/>
246cdf0e10cSrcweir             The factory creation function used is <code>::cppu::createOneInstanceFactory</code>.
247cdf0e10cSrcweir             @see OOneInstanceAutoRegistration
248cdf0e10cSrcweir         */
249cdf0e10cSrcweir         OOneInstanceAutoRegistration();
250cdf0e10cSrcweir         ~OOneInstanceAutoRegistration();
251cdf0e10cSrcweir     };
252cdf0e10cSrcweir 
253cdf0e10cSrcweir     template <class TYPE>
OOneInstanceAutoRegistration()254cdf0e10cSrcweir     OOneInstanceAutoRegistration<TYPE>::OOneInstanceAutoRegistration()
255cdf0e10cSrcweir     {
256cdf0e10cSrcweir         OModule::registerComponent(
257cdf0e10cSrcweir             TYPE::getImplementationName_Static(),
258cdf0e10cSrcweir             TYPE::getSupportedServiceNames_Static(),
259cdf0e10cSrcweir             TYPE::Create,
260cdf0e10cSrcweir             ::cppu::createOneInstanceFactory
261cdf0e10cSrcweir             );
262cdf0e10cSrcweir     }
263cdf0e10cSrcweir 
264cdf0e10cSrcweir     template <class TYPE>
~OOneInstanceAutoRegistration()265cdf0e10cSrcweir     OOneInstanceAutoRegistration<TYPE>::~OOneInstanceAutoRegistration()
266cdf0e10cSrcweir     {
267cdf0e10cSrcweir         OModule::revokeComponent(TYPE::getImplementationName_Static());
268cdf0e10cSrcweir     }
269cdf0e10cSrcweir 
270cdf0e10cSrcweir //.........................................................................
271cdf0e10cSrcweir }   // namespace COMPMOD_NAMESPACE
272cdf0e10cSrcweir //.........................................................................
273cdf0e10cSrcweir 
274cdf0e10cSrcweir #endif // _EXTENSIONS_COMPONENT_MODULE_HXX_
275