1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4*cdf0e10cSrcweir  *
5*cdf0e10cSrcweir  * Copyright 2000, 2010 Oracle and/or its affiliates.
6*cdf0e10cSrcweir  *
7*cdf0e10cSrcweir  * OpenOffice.org - a multi-platform office productivity suite
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  * This file is part of OpenOffice.org.
10*cdf0e10cSrcweir  *
11*cdf0e10cSrcweir  * OpenOffice.org is free software: you can redistribute it and/or modify
12*cdf0e10cSrcweir  * it under the terms of the GNU Lesser General Public License version 3
13*cdf0e10cSrcweir  * only, as published by the Free Software Foundation.
14*cdf0e10cSrcweir  *
15*cdf0e10cSrcweir  * OpenOffice.org is distributed in the hope that it will be useful,
16*cdf0e10cSrcweir  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17*cdf0e10cSrcweir  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18*cdf0e10cSrcweir  * GNU Lesser General Public License version 3 for more details
19*cdf0e10cSrcweir  * (a copy is included in the LICENSE file that accompanied this code).
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  * You should have received a copy of the GNU Lesser General Public License
22*cdf0e10cSrcweir  * version 3 along with OpenOffice.org.  If not, see
23*cdf0e10cSrcweir  * <http://www.openoffice.org/license.html>
24*cdf0e10cSrcweir  * for a copy of the LGPLv3 License.
25*cdf0e10cSrcweir  *
26*cdf0e10cSrcweir  ************************************************************************/
27*cdf0e10cSrcweir 
28*cdf0e10cSrcweir #ifndef _EXTENSIONS_COMPONENT_MODULE_HXX_
29*cdf0e10cSrcweir #define _EXTENSIONS_COMPONENT_MODULE_HXX_
30*cdf0e10cSrcweir 
31*cdf0e10cSrcweir /** you may find this file helpfull if you implement a component (in it's own library) which can't use
32*cdf0e10cSrcweir 	the usual infrastructure.<br/>
33*cdf0e10cSrcweir 	More precise, you find helper classes to ease the use of resources and the registration of services.
34*cdf0e10cSrcweir 	<p>
35*cdf0e10cSrcweir 	You need to define a preprocessor variable COMPMOD_NAMESPACE in order to use this file. Set it to a string
36*cdf0e10cSrcweir 	which should be used as namespace for the classes defined herein.</p>
37*cdf0e10cSrcweir */
38*cdf0e10cSrcweir 
39*cdf0e10cSrcweir #ifndef _OSL_MUTEX_HXX_
40*cdf0e10cSrcweir #include <osl/mutex.hxx>
41*cdf0e10cSrcweir #endif
42*cdf0e10cSrcweir #ifndef _TOOLS_RESID_HXX
43*cdf0e10cSrcweir #include <tools/resid.hxx>
44*cdf0e10cSrcweir #endif
45*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
46*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
47*cdf0e10cSrcweir #endif
48*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_LANG_XSINGLESERVICEFACTORY_HPP_
49*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
50*cdf0e10cSrcweir #endif
51*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_UNO_SEQUENCE_HXX_
52*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
53*cdf0e10cSrcweir #endif
54*cdf0e10cSrcweir #ifndef _COM_SUN_STAR_REGISTRY_XREGISTRYKEY_HPP_
55*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
56*cdf0e10cSrcweir #endif
57*cdf0e10cSrcweir #ifndef _CPPUHELPER_FACTORY_HXX_
58*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
59*cdf0e10cSrcweir #endif
60*cdf0e10cSrcweir #ifndef _RTL_STRING_HXX_
61*cdf0e10cSrcweir #include <rtl/string.hxx>
62*cdf0e10cSrcweir #endif
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir class ResMgr;
65*cdf0e10cSrcweir 
66*cdf0e10cSrcweir //.........................................................................
67*cdf0e10cSrcweir namespace COMPMOD_NAMESPACE
68*cdf0e10cSrcweir {
69*cdf0e10cSrcweir //.........................................................................
70*cdf0e10cSrcweir 
71*cdf0e10cSrcweir typedef ::com::sun::star::uno::Reference< ::com::sun::star::lang::XSingleServiceFactory > (SAL_CALL *FactoryInstantiation)
72*cdf0e10cSrcweir 		(
73*cdf0e10cSrcweir 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rServiceManager,
74*cdf0e10cSrcweir 			const ::rtl::OUString & _rComponentName,
75*cdf0e10cSrcweir 			::cppu::ComponentInstantiation _pCreateFunction,
76*cdf0e10cSrcweir 			const ::com::sun::star::uno::Sequence< ::rtl::OUString > & _rServiceNames,
77*cdf0e10cSrcweir 			rtl_ModuleCount* _pModuleCounter
78*cdf0e10cSrcweir 		);
79*cdf0e10cSrcweir 
80*cdf0e10cSrcweir 	//=========================================================================
81*cdf0e10cSrcweir 	//= OModule
82*cdf0e10cSrcweir 	//=========================================================================
83*cdf0e10cSrcweir 	class OModuleImpl;
84*cdf0e10cSrcweir 	class OModule
85*cdf0e10cSrcweir 	{
86*cdf0e10cSrcweir 		friend class OModuleResourceClient;
87*cdf0e10cSrcweir 
88*cdf0e10cSrcweir 	private:
89*cdf0e10cSrcweir 		OModule();
90*cdf0e10cSrcweir 			// not implemented. OModule is a static class
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 	protected:
93*cdf0e10cSrcweir 		// resource administration
94*cdf0e10cSrcweir 		static ::osl::Mutex		s_aMutex;		/// access safety
95*cdf0e10cSrcweir 		static sal_Int32		s_nClients;		/// number of registered clients
96*cdf0e10cSrcweir 		static OModuleImpl*		s_pImpl;		/// impl class. lives as long as at least one client for the module is registered
97*cdf0e10cSrcweir 		static ::rtl::OString	s_sResPrefix;
98*cdf0e10cSrcweir 
99*cdf0e10cSrcweir 		// auto registration administration
100*cdf0e10cSrcweir 		static	::com::sun::star::uno::Sequence< ::rtl::OUString >*
101*cdf0e10cSrcweir 			s_pImplementationNames;
102*cdf0e10cSrcweir 		static	::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::rtl::OUString > >*
103*cdf0e10cSrcweir 			s_pSupportedServices;
104*cdf0e10cSrcweir 		static	::com::sun::star::uno::Sequence< sal_Int64 >*
105*cdf0e10cSrcweir 			s_pCreationFunctionPointers;
106*cdf0e10cSrcweir 		static	::com::sun::star::uno::Sequence< sal_Int64 >*
107*cdf0e10cSrcweir 			s_pFactoryFunctionPointers;
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 	public:
110*cdf0e10cSrcweir 		// cna be set as long as no resource has been accessed ...
111*cdf0e10cSrcweir 		static void		setResourceFilePrefix(const ::rtl::OString& _rPrefix);
112*cdf0e10cSrcweir 
113*cdf0e10cSrcweir 		/// get the vcl res manager of the module
114*cdf0e10cSrcweir 		static ResMgr*	getResManager();
115*cdf0e10cSrcweir 
116*cdf0e10cSrcweir 		/** register a component implementing a service with the given data.
117*cdf0e10cSrcweir 			@param	_rImplementationName
118*cdf0e10cSrcweir 						the implementation name of the component
119*cdf0e10cSrcweir 			@param	_rServiceNames
120*cdf0e10cSrcweir 						the services the component supports
121*cdf0e10cSrcweir 			@param	_pCreateFunction
122*cdf0e10cSrcweir 						a function for creating an instance of the component
123*cdf0e10cSrcweir 			@param	_pFactoryFunction
124*cdf0e10cSrcweir 						a function for creating a factory for that component
125*cdf0e10cSrcweir 			@see revokeComponent
126*cdf0e10cSrcweir 		*/
127*cdf0e10cSrcweir 		static void registerComponent(
128*cdf0e10cSrcweir 			const ::rtl::OUString& _rImplementationName,
129*cdf0e10cSrcweir 			const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rServiceNames,
130*cdf0e10cSrcweir 			::cppu::ComponentInstantiation _pCreateFunction,
131*cdf0e10cSrcweir 			FactoryInstantiation _pFactoryFunction);
132*cdf0e10cSrcweir 
133*cdf0e10cSrcweir 		/** revoke the registration for the specified component
134*cdf0e10cSrcweir 			@param	_rImplementationName
135*cdf0e10cSrcweir 				the implementation name of the component
136*cdf0e10cSrcweir 		*/
137*cdf0e10cSrcweir 		static void revokeComponent(
138*cdf0e10cSrcweir 			const ::rtl::OUString& _rImplementationName);
139*cdf0e10cSrcweir 
140*cdf0e10cSrcweir 		/** creates a Factory for the component with the given implementation name.
141*cdf0e10cSrcweir 			<p>Usually used from within component_getFactory.<p/>
142*cdf0e10cSrcweir 			@param	_rxServiceManager
143*cdf0e10cSrcweir 						a pointer to an XMultiServiceFactory interface as got in component_getFactory
144*cdf0e10cSrcweir 			@param	_pImplementationName
145*cdf0e10cSrcweir 						the implementation name of the component
146*cdf0e10cSrcweir 			@return
147*cdf0e10cSrcweir 						the XInterface access to a factory for the component
148*cdf0e10cSrcweir 		*/
149*cdf0e10cSrcweir 		static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > getComponentFactory(
150*cdf0e10cSrcweir 			const ::rtl::OUString& _rImplementationName,
151*cdf0e10cSrcweir 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxServiceManager
152*cdf0e10cSrcweir 			);
153*cdf0e10cSrcweir 
154*cdf0e10cSrcweir 	protected:
155*cdf0e10cSrcweir 		/// register a client for the module
156*cdf0e10cSrcweir 		static void	registerClient();
157*cdf0e10cSrcweir 		/// revoke a client for the module
158*cdf0e10cSrcweir 		static void	revokeClient();
159*cdf0e10cSrcweir 
160*cdf0e10cSrcweir 	private:
161*cdf0e10cSrcweir 		/** ensure that the impl class exists
162*cdf0e10cSrcweir 			@precond m_aMutex is guarded when this method gets called
163*cdf0e10cSrcweir 		*/
164*cdf0e10cSrcweir 		static void ensureImpl();
165*cdf0e10cSrcweir 	};
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 	//=========================================================================
168*cdf0e10cSrcweir 	//= OModuleResourceClient
169*cdf0e10cSrcweir 	//=========================================================================
170*cdf0e10cSrcweir 	/** base class for objects which uses any global module-specific ressources
171*cdf0e10cSrcweir 	*/
172*cdf0e10cSrcweir 	class OModuleResourceClient
173*cdf0e10cSrcweir 	{
174*cdf0e10cSrcweir 	public:
175*cdf0e10cSrcweir 		OModuleResourceClient()		{ OModule::registerClient(); }
176*cdf0e10cSrcweir 		~OModuleResourceClient()	{ OModule::revokeClient(); }
177*cdf0e10cSrcweir 	};
178*cdf0e10cSrcweir 
179*cdf0e10cSrcweir 	//=========================================================================
180*cdf0e10cSrcweir 	//= ModuleRes
181*cdf0e10cSrcweir 	//=========================================================================
182*cdf0e10cSrcweir 	/** specialized ResId, using the ressource manager provided by the global module
183*cdf0e10cSrcweir 	*/
184*cdf0e10cSrcweir 	class ModuleRes : public ::ResId
185*cdf0e10cSrcweir 	{
186*cdf0e10cSrcweir 	public:
187*cdf0e10cSrcweir 		ModuleRes(sal_uInt16 _nId) : ResId(_nId, *OModule::getResManager()) { }
188*cdf0e10cSrcweir 	};
189*cdf0e10cSrcweir 
190*cdf0e10cSrcweir 	//==========================================================================
191*cdf0e10cSrcweir 	//= OMultiInstanceAutoRegistration
192*cdf0e10cSrcweir 	//==========================================================================
193*cdf0e10cSrcweir 	template <class TYPE>
194*cdf0e10cSrcweir 	class OMultiInstanceAutoRegistration
195*cdf0e10cSrcweir 	{
196*cdf0e10cSrcweir 	public:
197*cdf0e10cSrcweir 		/** automatically registeres a multi instance component
198*cdf0e10cSrcweir 			<p>Assumed that the template argument has the three methods
199*cdf0e10cSrcweir 				<ul>
200*cdf0e10cSrcweir 					<li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
201*cdf0e10cSrcweir 					<li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
202*cdf0e10cSrcweir 					<li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
203*cdf0e10cSrcweir 						Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
204*cdf0e10cSrcweir 						</li>
205*cdf0e10cSrcweir 				<ul/>
206*cdf0e10cSrcweir 			the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
207*cdf0e10cSrcweir 			<p/>
208*cdf0e10cSrcweir 			The factory creation function used is <code>::cppu::createSingleFactory</code>.
209*cdf0e10cSrcweir 			@see OOneInstanceAutoRegistration
210*cdf0e10cSrcweir 		*/
211*cdf0e10cSrcweir 		OMultiInstanceAutoRegistration();
212*cdf0e10cSrcweir 		~OMultiInstanceAutoRegistration();
213*cdf0e10cSrcweir 	};
214*cdf0e10cSrcweir 
215*cdf0e10cSrcweir 	template <class TYPE>
216*cdf0e10cSrcweir 	OMultiInstanceAutoRegistration<TYPE>::OMultiInstanceAutoRegistration()
217*cdf0e10cSrcweir 	{
218*cdf0e10cSrcweir 		OModule::registerComponent(
219*cdf0e10cSrcweir 			TYPE::getImplementationName_Static(),
220*cdf0e10cSrcweir 			TYPE::getSupportedServiceNames_Static(),
221*cdf0e10cSrcweir 			TYPE::Create,
222*cdf0e10cSrcweir 			::cppu::createSingleFactory
223*cdf0e10cSrcweir 			);
224*cdf0e10cSrcweir 	}
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir 	template <class TYPE>
227*cdf0e10cSrcweir 	OMultiInstanceAutoRegistration<TYPE>::~OMultiInstanceAutoRegistration()
228*cdf0e10cSrcweir 	{
229*cdf0e10cSrcweir 		OModule::revokeComponent(TYPE::getImplementationName_Static());
230*cdf0e10cSrcweir 	}
231*cdf0e10cSrcweir 
232*cdf0e10cSrcweir 	//==========================================================================
233*cdf0e10cSrcweir 	//= OOneInstanceAutoRegistration
234*cdf0e10cSrcweir 	//==========================================================================
235*cdf0e10cSrcweir 	template <class TYPE>
236*cdf0e10cSrcweir 	class OOneInstanceAutoRegistration
237*cdf0e10cSrcweir 	{
238*cdf0e10cSrcweir 	public:
239*cdf0e10cSrcweir 		/** automatically registeres a single instance component
240*cdf0e10cSrcweir 			<p>Assumed that the template argument has the three methods
241*cdf0e10cSrcweir 				<ul>
242*cdf0e10cSrcweir 					<li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
243*cdf0e10cSrcweir 					<li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
244*cdf0e10cSrcweir 					<li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
245*cdf0e10cSrcweir 						Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
246*cdf0e10cSrcweir 						</li>
247*cdf0e10cSrcweir 				<ul/>
248*cdf0e10cSrcweir 			the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
249*cdf0e10cSrcweir 			<p/>
250*cdf0e10cSrcweir 			The factory creation function used is <code>::cppu::createOneInstanceFactory</code>.
251*cdf0e10cSrcweir 			@see OOneInstanceAutoRegistration
252*cdf0e10cSrcweir 		*/
253*cdf0e10cSrcweir 		OOneInstanceAutoRegistration();
254*cdf0e10cSrcweir 		~OOneInstanceAutoRegistration();
255*cdf0e10cSrcweir 	};
256*cdf0e10cSrcweir 
257*cdf0e10cSrcweir 	template <class TYPE>
258*cdf0e10cSrcweir 	OOneInstanceAutoRegistration<TYPE>::OOneInstanceAutoRegistration()
259*cdf0e10cSrcweir 	{
260*cdf0e10cSrcweir 		OModule::registerComponent(
261*cdf0e10cSrcweir 			TYPE::getImplementationName_Static(),
262*cdf0e10cSrcweir 			TYPE::getSupportedServiceNames_Static(),
263*cdf0e10cSrcweir 			TYPE::Create,
264*cdf0e10cSrcweir 			::cppu::createOneInstanceFactory
265*cdf0e10cSrcweir 			);
266*cdf0e10cSrcweir 	}
267*cdf0e10cSrcweir 
268*cdf0e10cSrcweir 	template <class TYPE>
269*cdf0e10cSrcweir 	OOneInstanceAutoRegistration<TYPE>::~OOneInstanceAutoRegistration()
270*cdf0e10cSrcweir 	{
271*cdf0e10cSrcweir 		OModule::revokeComponent(TYPE::getImplementationName_Static());
272*cdf0e10cSrcweir 	}
273*cdf0e10cSrcweir 
274*cdf0e10cSrcweir //.........................................................................
275*cdf0e10cSrcweir }	// namespace COMPMOD_NAMESPACE
276*cdf0e10cSrcweir //.........................................................................
277*cdf0e10cSrcweir 
278*cdf0e10cSrcweir #endif // _EXTENSIONS_COMPONENT_MODULE_HXX_
279*cdf0e10cSrcweir 
280