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 .
19 #ifndef INCLUDED_COMPHELPER_COMPONENTMODULE_HXX
20 #define INCLUDED_COMPHELPER_COMPONENTMODULE_HXX
22 #include <comphelper/comphelperdllapi.h>
24 #include <com/sun/star/lang/XSingleServiceFactory.hpp>
25 #include <com/sun/star/uno/Sequence.hxx>
27 #include <cppuhelper/factory.hxx>
29 #include <osl/mutex.hxx>
31 #include <rtl/string.hxx>
32 #include <rtl/instance.hxx>
34 //........................................................................
37 //........................................................................
39 /** factory factory declaration
41 typedef ::com::sun::star::uno::Reference
< ::com::sun::star::lang::XSingleComponentFactory
> (SAL_CALL
*FactoryInstantiation
)
43 ::cppu::ComponentFactoryFunc _pFactoryFunc
,
44 OUString
const& _rComponentName
,
45 ::com::sun::star::uno::Sequence
< OUString
> const & _rServiceNames
,
49 //=========================================================================
50 //= ComponentDescription
51 //=========================================================================
52 struct COMPHELPER_DLLPUBLIC ComponentDescription
54 /// the implementation name of the component
55 OUString sImplementationName
;
56 /// the services supported by the component implementation
57 ::com::sun::star::uno::Sequence
< OUString
> aSupportedServices
;
58 /** the name under which the component implementation should be registered as singleton,
59 or empty if the component does not implement a singleton.
61 OUString sSingletonName
;
62 /// the function to create an instance of the component
63 ::cppu::ComponentFactoryFunc pComponentCreationFunc
;
64 /// the function to create a factory for the component (usually <code>::cppu::createSingleComponentFactory</code>)
65 FactoryInstantiation pFactoryCreationFunc
;
67 ComponentDescription()
68 :sImplementationName()
71 ,pComponentCreationFunc( NULL
)
72 ,pFactoryCreationFunc( NULL
)
77 const OUString
& _rImplementationName
,
78 const ::com::sun::star::uno::Sequence
< OUString
>& _rSupportedServices
,
79 const OUString
& _rSingletonName
,
80 ::cppu::ComponentFactoryFunc _pComponentCreationFunc
,
81 FactoryInstantiation _pFactoryCreationFunc
83 :sImplementationName( _rImplementationName
)
84 ,aSupportedServices( _rSupportedServices
)
85 ,sSingletonName( _rSingletonName
)
86 ,pComponentCreationFunc( _pComponentCreationFunc
)
87 ,pFactoryCreationFunc( _pFactoryCreationFunc
)
92 //=========================================================================
94 //=========================================================================
96 class COMPHELPER_DLLPUBLIC OModule
99 oslInterlockedCount m_nClients
; /// number of registered clients
100 OModuleImpl
* m_pImpl
; /// impl class. lives as long as at least one client for the module is registered
103 mutable ::osl::Mutex m_aMutex
; /// access safety
110 /** register a component implementing a service with the given data.
111 @param _rImplementationName
112 the implementation name of the component
113 @param _rServiceNames
114 the services the component supports
115 @param _pCreateFunction
116 a function for creating an instance of the component
117 @param _pFactoryFunction
118 a function for creating a factory for that component
120 void registerImplementation(
121 const OUString
& _rImplementationName
,
122 const ::com::sun::star::uno::Sequence
< OUString
>& _rServiceNames
,
123 ::cppu::ComponentFactoryFunc _pCreateFunction
,
124 FactoryInstantiation _pFactoryFunction
= ::cppu::createSingleComponentFactory
);
126 /** registers a component given by ComponentDescription
128 void registerImplementation( const ComponentDescription
& _rComp
);
130 /** creates a Factory for the component with the given implementation name.
131 <p>Usually used from within component_getFactory.<p/>
132 @param _pImplementationName
133 the implementation name of the component
135 the XInterface access to a factory for the component
137 ::com::sun::star::uno::Reference
< ::com::sun::star::uno::XInterface
> getComponentFactory(
138 const OUString
& _rImplementationName
);
140 /** version of getComponentFactory which directly takes the char argument you got in your component_getFactory call
142 void* getComponentFactory( const sal_Char
* _pImplementationName
);
145 class ClientAccess
{ friend class OModuleClient
; private: ClientAccess() { } };
146 /// register a client for the module
147 void registerClient( ClientAccess
);
148 /// revoke a client for the module
149 void revokeClient( ClientAccess
);
152 /** called when the first client has been registered
154 <member>m_aMutex</member> is locked
156 virtual void onFirstClient();
158 /** called when the last client has been revoked
160 <member>m_aMutex</member> is locked
162 virtual void onLastClient();
165 OModule( const OModule
& ); // never implemented
166 OModule
& operator=( const OModule
& ); // never implemented
169 //=========================================================================
171 //=========================================================================
172 /** base class for objects which uses any global module-specific resources
174 class COMPHELPER_DLLPUBLIC OModuleClient
180 OModuleClient( OModule
& _rModule
) :m_rModule( _rModule
) { m_rModule
.registerClient( OModule::ClientAccess() ); }
181 ~OModuleClient() { m_rModule
.revokeClient( OModule::ClientAccess() ); }
184 //==========================================================================
185 //= OAutoRegistration
186 //==========================================================================
187 template <class TYPE
>
188 class OAutoRegistration
191 /** automatically provides all component information to an OModule instance
192 <p>Assumed that the template argument has the three methods
194 <li><code>static OUString getImplementationName_static()</code><li/>
195 <li><code>static ::com::sun::star::uno::Sequence< OUString > getSupportedServiceNames_static()</code><li/>
196 <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
197 Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
200 the instantiation of this object will automatically register the class via <member>OModule::registerImplementation</member>.
202 The factory creation function used is <code>::cppu::createSingleComponentFactory</code>.
204 OAutoRegistration( OModule
& _rModule
);
207 template <class TYPE
>
208 OAutoRegistration
<TYPE
>::OAutoRegistration( OModule
& _rModule
)
210 _rModule
.registerImplementation(
211 TYPE::getImplementationName_static(),
212 TYPE::getSupportedServiceNames_static(),
217 //==========================================================================
218 //= OSingletonRegistration
219 //==========================================================================
220 template <class TYPE
>
221 class OSingletonRegistration
224 /** automatically provides all component information to an OModule instance,
225 for a singleton component
227 <p>Assumed that the template argument has the three methods
229 <li><code>static OUString getImplementationName_static()</code><li/>
230 <li><code>static ::com::sun::star::uno::Sequence< OUString > getSupportedServiceNames_static()</code><li/>
231 <li><code>static OUString getSingletonName_static()</code></li>
232 <li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
233 Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
236 the instantiation of this object will automatically register the class via <member>OModule::registerImplementation</member>.
239 OSingletonRegistration( OModule
& _rModule
);
242 template <class TYPE
>
243 //--------------------------------------------------------------------------
244 OSingletonRegistration
<TYPE
>::OSingletonRegistration( OModule
& _rModule
)
246 _rModule
.registerImplementation( ComponentDescription(
247 TYPE::getImplementationName_static(),
248 TYPE::getSupportedServiceNames_static(),
249 TYPE::getSingletonName_static(),
251 &::cppu::createSingleComponentFactory
255 //........................................................................
256 } // namespace comphelper
257 //........................................................................
259 #endif // INCLUDED_COMPHELPER_COMPONENTMODULE_HXX
261 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */