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