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 #include <osl/mutex.hxx>
40*cdf0e10cSrcweir #include <tools/resid.hxx>
41*cdf0e10cSrcweir #include <com/sun/star/lang/XMultiServiceFactory.hpp>
42*cdf0e10cSrcweir #include <com/sun/star/lang/XSingleServiceFactory.hpp>
43*cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
44*cdf0e10cSrcweir #include <com/sun/star/registry/XRegistryKey.hpp>
45*cdf0e10cSrcweir #include <cppuhelper/factory.hxx>
46*cdf0e10cSrcweir #include <rtl/string.hxx>
47*cdf0e10cSrcweir 
48*cdf0e10cSrcweir class ResMgr;
49*cdf0e10cSrcweir 
50*cdf0e10cSrcweir //.........................................................................
51*cdf0e10cSrcweir namespace COMPMOD_NAMESPACE
52*cdf0e10cSrcweir {
53*cdf0e10cSrcweir //.........................................................................
54*cdf0e10cSrcweir 
55*cdf0e10cSrcweir typedef ::com::sun::star::uno::Reference< ::com::sun::star::lang::XSingleServiceFactory > (SAL_CALL *FactoryInstantiation)
56*cdf0e10cSrcweir 		(
57*cdf0e10cSrcweir 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rServiceManager,
58*cdf0e10cSrcweir 			const ::rtl::OUString & _rComponentName,
59*cdf0e10cSrcweir 			::cppu::ComponentInstantiation _pCreateFunction,
60*cdf0e10cSrcweir 			const ::com::sun::star::uno::Sequence< ::rtl::OUString > & _rServiceNames,
61*cdf0e10cSrcweir 			rtl_ModuleCount* _pModuleCounter
62*cdf0e10cSrcweir 		);
63*cdf0e10cSrcweir 
64*cdf0e10cSrcweir 	//=========================================================================
65*cdf0e10cSrcweir 	//= OModule
66*cdf0e10cSrcweir 	//=========================================================================
67*cdf0e10cSrcweir 	class OModuleImpl;
68*cdf0e10cSrcweir 	class OModule
69*cdf0e10cSrcweir 	{
70*cdf0e10cSrcweir 		friend class OModuleResourceClient;
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 	private:
73*cdf0e10cSrcweir 		OModule();
74*cdf0e10cSrcweir 			// not implemented. OModule is a static class
75*cdf0e10cSrcweir 
76*cdf0e10cSrcweir 	protected:
77*cdf0e10cSrcweir 		// resource administration
78*cdf0e10cSrcweir 		static ::osl::Mutex		s_aMutex;		/// access safety
79*cdf0e10cSrcweir 		static sal_Int32		s_nClients;		/// number of registered clients
80*cdf0e10cSrcweir 		static OModuleImpl*		s_pImpl;		/// impl class. lives as long as at least one client for the module is registered
81*cdf0e10cSrcweir 		static ::rtl::OString	s_sResPrefix;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 		// auto registration administration
84*cdf0e10cSrcweir 		static	::com::sun::star::uno::Sequence< ::rtl::OUString >*
85*cdf0e10cSrcweir 			s_pImplementationNames;
86*cdf0e10cSrcweir 		static	::com::sun::star::uno::Sequence< ::com::sun::star::uno::Sequence< ::rtl::OUString > >*
87*cdf0e10cSrcweir 			s_pSupportedServices;
88*cdf0e10cSrcweir 		static	::com::sun::star::uno::Sequence< sal_Int64 >*
89*cdf0e10cSrcweir 			s_pCreationFunctionPointers;
90*cdf0e10cSrcweir 		static	::com::sun::star::uno::Sequence< sal_Int64 >*
91*cdf0e10cSrcweir 			s_pFactoryFunctionPointers;
92*cdf0e10cSrcweir 
93*cdf0e10cSrcweir 	public:
94*cdf0e10cSrcweir 		// cna be set as long as no resource has been accessed ...
95*cdf0e10cSrcweir 		static void		setResourceFilePrefix(const ::rtl::OString& _rPrefix);
96*cdf0e10cSrcweir 
97*cdf0e10cSrcweir 		/// get the vcl res manager of the module
98*cdf0e10cSrcweir 		static ResMgr*	getResManager();
99*cdf0e10cSrcweir 
100*cdf0e10cSrcweir 		/** register a component implementing a service with the given data.
101*cdf0e10cSrcweir 			@param	_rImplementationName
102*cdf0e10cSrcweir 						the implementation name of the component
103*cdf0e10cSrcweir 			@param	_rServiceNames
104*cdf0e10cSrcweir 						the services the component supports
105*cdf0e10cSrcweir 			@param	_pCreateFunction
106*cdf0e10cSrcweir 						a function for creating an instance of the component
107*cdf0e10cSrcweir 			@param	_pFactoryFunction
108*cdf0e10cSrcweir 						a function for creating a factory for that component
109*cdf0e10cSrcweir 			@see revokeComponent
110*cdf0e10cSrcweir 		*/
111*cdf0e10cSrcweir 		static void registerComponent(
112*cdf0e10cSrcweir 			const ::rtl::OUString& _rImplementationName,
113*cdf0e10cSrcweir 			const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rServiceNames,
114*cdf0e10cSrcweir 			::cppu::ComponentInstantiation _pCreateFunction,
115*cdf0e10cSrcweir 			FactoryInstantiation _pFactoryFunction);
116*cdf0e10cSrcweir 
117*cdf0e10cSrcweir 		/** revoke the registration for the specified component
118*cdf0e10cSrcweir 			@param	_rImplementationName
119*cdf0e10cSrcweir 				the implementation name of the component
120*cdf0e10cSrcweir 		*/
121*cdf0e10cSrcweir 		static void revokeComponent(
122*cdf0e10cSrcweir 			const ::rtl::OUString& _rImplementationName);
123*cdf0e10cSrcweir 
124*cdf0e10cSrcweir 		/** creates a Factory for the component with the given implementation name.
125*cdf0e10cSrcweir 			<p>Usually used from within component_getFactory.<p/>
126*cdf0e10cSrcweir 			@param	_rxServiceManager
127*cdf0e10cSrcweir 						a pointer to an XMultiServiceFactory interface as got in component_getFactory
128*cdf0e10cSrcweir 			@param	_pImplementationName
129*cdf0e10cSrcweir 						the implementation name of the component
130*cdf0e10cSrcweir 			@return
131*cdf0e10cSrcweir 						the XInterface access to a factory for the component
132*cdf0e10cSrcweir 		*/
133*cdf0e10cSrcweir 		static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > getComponentFactory(
134*cdf0e10cSrcweir 			const ::rtl::OUString& _rImplementationName,
135*cdf0e10cSrcweir 			const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >& _rxServiceManager
136*cdf0e10cSrcweir 			);
137*cdf0e10cSrcweir 
138*cdf0e10cSrcweir 	protected:
139*cdf0e10cSrcweir 		/// register a client for the module
140*cdf0e10cSrcweir 		static void	registerClient();
141*cdf0e10cSrcweir 		/// revoke a client for the module
142*cdf0e10cSrcweir 		static void	revokeClient();
143*cdf0e10cSrcweir 
144*cdf0e10cSrcweir 	private:
145*cdf0e10cSrcweir 		/** ensure that the impl class exists
146*cdf0e10cSrcweir 			@precond m_aMutex is guarded when this method gets called
147*cdf0e10cSrcweir 		*/
148*cdf0e10cSrcweir 		static void ensureImpl();
149*cdf0e10cSrcweir 	};
150*cdf0e10cSrcweir 
151*cdf0e10cSrcweir 	//=========================================================================
152*cdf0e10cSrcweir 	//= OModuleResourceClient
153*cdf0e10cSrcweir 	//=========================================================================
154*cdf0e10cSrcweir 	/** base class for objects which uses any global module-specific ressources
155*cdf0e10cSrcweir 	*/
156*cdf0e10cSrcweir 	class OModuleResourceClient
157*cdf0e10cSrcweir 	{
158*cdf0e10cSrcweir 	public:
159*cdf0e10cSrcweir 		OModuleResourceClient()		{ OModule::registerClient(); }
160*cdf0e10cSrcweir 		~OModuleResourceClient()	{ OModule::revokeClient(); }
161*cdf0e10cSrcweir 	};
162*cdf0e10cSrcweir 
163*cdf0e10cSrcweir 	//=========================================================================
164*cdf0e10cSrcweir 	//= ModuleRes
165*cdf0e10cSrcweir 	//=========================================================================
166*cdf0e10cSrcweir 	/** specialized ResId, using the ressource manager provided by the global module
167*cdf0e10cSrcweir 	*/
168*cdf0e10cSrcweir 	class ModuleRes : public ::ResId
169*cdf0e10cSrcweir 	{
170*cdf0e10cSrcweir 	public:
171*cdf0e10cSrcweir 		ModuleRes(sal_uInt16 _nId) : ResId(_nId, *OModule::getResManager()) { }
172*cdf0e10cSrcweir 	};
173*cdf0e10cSrcweir 
174*cdf0e10cSrcweir 	//==========================================================================
175*cdf0e10cSrcweir 	//= OMultiInstanceAutoRegistration
176*cdf0e10cSrcweir 	//==========================================================================
177*cdf0e10cSrcweir 	template <class TYPE>
178*cdf0e10cSrcweir 	class OMultiInstanceAutoRegistration
179*cdf0e10cSrcweir 	{
180*cdf0e10cSrcweir 	public:
181*cdf0e10cSrcweir 		/** automatically registeres a multi instance component
182*cdf0e10cSrcweir 			<p>Assumed that the template argument has the three methods
183*cdf0e10cSrcweir 				<ul>
184*cdf0e10cSrcweir 					<li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
185*cdf0e10cSrcweir 					<li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
186*cdf0e10cSrcweir 					<li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
187*cdf0e10cSrcweir 						Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
188*cdf0e10cSrcweir 						</li>
189*cdf0e10cSrcweir 				<ul/>
190*cdf0e10cSrcweir 			the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
191*cdf0e10cSrcweir 			<p/>
192*cdf0e10cSrcweir 			The factory creation function used is <code>::cppu::createSingleFactory</code>.
193*cdf0e10cSrcweir 			@see OOneInstanceAutoRegistration
194*cdf0e10cSrcweir 		*/
195*cdf0e10cSrcweir 		OMultiInstanceAutoRegistration();
196*cdf0e10cSrcweir 		~OMultiInstanceAutoRegistration();
197*cdf0e10cSrcweir 	};
198*cdf0e10cSrcweir 
199*cdf0e10cSrcweir 	template <class TYPE>
200*cdf0e10cSrcweir 	OMultiInstanceAutoRegistration<TYPE>::OMultiInstanceAutoRegistration()
201*cdf0e10cSrcweir 	{
202*cdf0e10cSrcweir 		OModule::registerComponent(
203*cdf0e10cSrcweir 			TYPE::getImplementationName_Static(),
204*cdf0e10cSrcweir 			TYPE::getSupportedServiceNames_Static(),
205*cdf0e10cSrcweir 			TYPE::Create,
206*cdf0e10cSrcweir 			::cppu::createSingleFactory
207*cdf0e10cSrcweir 			);
208*cdf0e10cSrcweir 	}
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 	template <class TYPE>
211*cdf0e10cSrcweir 	OMultiInstanceAutoRegistration<TYPE>::~OMultiInstanceAutoRegistration()
212*cdf0e10cSrcweir 	{
213*cdf0e10cSrcweir 		OModule::revokeComponent(TYPE::getImplementationName_Static());
214*cdf0e10cSrcweir 	}
215*cdf0e10cSrcweir 
216*cdf0e10cSrcweir 	//==========================================================================
217*cdf0e10cSrcweir 	//= OOneInstanceAutoRegistration
218*cdf0e10cSrcweir 	//==========================================================================
219*cdf0e10cSrcweir 	template <class TYPE>
220*cdf0e10cSrcweir 	class OOneInstanceAutoRegistration
221*cdf0e10cSrcweir 	{
222*cdf0e10cSrcweir 	public:
223*cdf0e10cSrcweir 		/** automatically registeres a single instance component
224*cdf0e10cSrcweir 			<p>Assumed that the template argument has the three methods
225*cdf0e10cSrcweir 				<ul>
226*cdf0e10cSrcweir 					<li><code>static ::rtl::OUString getImplementationName_Static()</code><li/>
227*cdf0e10cSrcweir 					<li><code>static ::com::sun::star::uno::Sequence< ::rtl::OUString > getSupportedServiceNames_Static()</code><li/>
228*cdf0e10cSrcweir 					<li><code>static ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >
229*cdf0e10cSrcweir 						Create(const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XMultiServiceFactory >&)</code>
230*cdf0e10cSrcweir 						</li>
231*cdf0e10cSrcweir 				<ul/>
232*cdf0e10cSrcweir 			the instantiation of this object will automatically register the class via <method>OModule::registerComponent</method>.
233*cdf0e10cSrcweir 			<p/>
234*cdf0e10cSrcweir 			The factory creation function used is <code>::cppu::createOneInstanceFactory</code>.
235*cdf0e10cSrcweir 			@see OOneInstanceAutoRegistration
236*cdf0e10cSrcweir 		*/
237*cdf0e10cSrcweir 		OOneInstanceAutoRegistration();
238*cdf0e10cSrcweir 		~OOneInstanceAutoRegistration();
239*cdf0e10cSrcweir 	};
240*cdf0e10cSrcweir 
241*cdf0e10cSrcweir 	template <class TYPE>
242*cdf0e10cSrcweir 	OOneInstanceAutoRegistration<TYPE>::OOneInstanceAutoRegistration()
243*cdf0e10cSrcweir 	{
244*cdf0e10cSrcweir 		OModule::registerComponent(
245*cdf0e10cSrcweir 			TYPE::getImplementationName_Static(),
246*cdf0e10cSrcweir 			TYPE::getSupportedServiceNames_Static(),
247*cdf0e10cSrcweir 			TYPE::Create,
248*cdf0e10cSrcweir 			::cppu::createOneInstanceFactory
249*cdf0e10cSrcweir 			);
250*cdf0e10cSrcweir 	}
251*cdf0e10cSrcweir 
252*cdf0e10cSrcweir 	template <class TYPE>
253*cdf0e10cSrcweir 	OOneInstanceAutoRegistration<TYPE>::~OOneInstanceAutoRegistration()
254*cdf0e10cSrcweir 	{
255*cdf0e10cSrcweir 		OModule::revokeComponent(TYPE::getImplementationName_Static());
256*cdf0e10cSrcweir 	}
257*cdf0e10cSrcweir 
258*cdf0e10cSrcweir //.........................................................................
259*cdf0e10cSrcweir }	// namespace COMPMOD_NAMESPACE
260*cdf0e10cSrcweir //.........................................................................
261*cdf0e10cSrcweir 
262*cdf0e10cSrcweir #endif // _EXTENSIONS_COMPONENT_MODULE_HXX_
263*cdf0e10cSrcweir 
264