1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*dde7d3faSAndrew Rist  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*dde7d3faSAndrew Rist  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19*dde7d3faSAndrew Rist  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir #include <comphelper/componentmodule.hxx>
27cdf0e10cSrcweir 
28cdf0e10cSrcweir /** === begin UNO includes === **/
29cdf0e10cSrcweir /** === end UNO includes === **/
30cdf0e10cSrcweir #include <comphelper/sequence.hxx>
31cdf0e10cSrcweir #include <osl/diagnose.h>
32cdf0e10cSrcweir 
33cdf0e10cSrcweir #include <vector>
34cdf0e10cSrcweir 
35cdf0e10cSrcweir //........................................................................
36cdf0e10cSrcweir namespace comphelper
37cdf0e10cSrcweir {
38cdf0e10cSrcweir //........................................................................
39cdf0e10cSrcweir 
40cdf0e10cSrcweir     using namespace ::cppu;
41cdf0e10cSrcweir     /** === being UNO using === **/
42cdf0e10cSrcweir     using ::com::sun::star::uno::Sequence;
43cdf0e10cSrcweir     using ::com::sun::star::uno::RuntimeException;
44cdf0e10cSrcweir     using ::com::sun::star::uno::Reference;
45cdf0e10cSrcweir     using ::com::sun::star::lang::XMultiServiceFactory;
46cdf0e10cSrcweir     using ::com::sun::star::registry::XRegistryKey;
47cdf0e10cSrcweir     using ::com::sun::star::uno::Exception;
48cdf0e10cSrcweir     using ::com::sun::star::uno::XInterface;
49cdf0e10cSrcweir     /** === end UNO using === **/
50cdf0e10cSrcweir 
51cdf0e10cSrcweir     typedef ::std::vector< ComponentDescription >   ComponentDescriptions;
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 	//=========================================================================
54cdf0e10cSrcweir 	//= OModuleImpl
55cdf0e10cSrcweir 	//=========================================================================
56cdf0e10cSrcweir 	/** implementation for <type>OModule</type>. not threadsafe, has to be guarded by it's owner
57cdf0e10cSrcweir 	*/
58cdf0e10cSrcweir 	class OModuleImpl
59cdf0e10cSrcweir 	{
60cdf0e10cSrcweir     public:
61cdf0e10cSrcweir         ComponentDescriptions                           m_aRegisteredComponents;
62cdf0e10cSrcweir 
63cdf0e10cSrcweir 		OModuleImpl();
64cdf0e10cSrcweir 		~OModuleImpl();
65cdf0e10cSrcweir 	};
66cdf0e10cSrcweir 
67cdf0e10cSrcweir 	//-------------------------------------------------------------------------
OModuleImpl()68cdf0e10cSrcweir 	OModuleImpl::OModuleImpl()
69cdf0e10cSrcweir 	{
70cdf0e10cSrcweir 	}
71cdf0e10cSrcweir 
72cdf0e10cSrcweir 	//-------------------------------------------------------------------------
~OModuleImpl()73cdf0e10cSrcweir 	OModuleImpl::~OModuleImpl()
74cdf0e10cSrcweir 	{
75cdf0e10cSrcweir 	}
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 	//=========================================================================
78cdf0e10cSrcweir 	//= OModule
79cdf0e10cSrcweir 	//=========================================================================
80cdf0e10cSrcweir 	//-------------------------------------------------------------------------
OModule()81cdf0e10cSrcweir     OModule::OModule()
82cdf0e10cSrcweir         :m_nClients( 0 )
83cdf0e10cSrcweir 	    ,m_pImpl( new OModuleImpl )
84cdf0e10cSrcweir     {
85cdf0e10cSrcweir     }
86cdf0e10cSrcweir 
~OModule()87cdf0e10cSrcweir     OModule::~OModule() {}
88cdf0e10cSrcweir 
89cdf0e10cSrcweir 	//-------------------------------------------------------------------------
registerClient(OModule::ClientAccess)90cdf0e10cSrcweir     void OModule::registerClient( OModule::ClientAccess )
91cdf0e10cSrcweir 	{
92cdf0e10cSrcweir 		::osl::MutexGuard aGuard(m_aMutex);
93cdf0e10cSrcweir         if ( 1 == osl_incrementInterlockedCount( &m_nClients ) )
94cdf0e10cSrcweir             onFirstClient();
95cdf0e10cSrcweir 	}
96cdf0e10cSrcweir 
97cdf0e10cSrcweir 	//-------------------------------------------------------------------------
revokeClient(OModule::ClientAccess)98cdf0e10cSrcweir 	void OModule::revokeClient( OModule::ClientAccess )
99cdf0e10cSrcweir 	{
100cdf0e10cSrcweir 		::osl::MutexGuard aGuard(m_aMutex);
101cdf0e10cSrcweir         if ( 0 == osl_decrementInterlockedCount( &m_nClients ) )
102cdf0e10cSrcweir             onLastClient();
103cdf0e10cSrcweir 	}
104cdf0e10cSrcweir 
105cdf0e10cSrcweir 	//--------------------------------------------------------------------------
onFirstClient()106cdf0e10cSrcweir     void OModule::onFirstClient()
107cdf0e10cSrcweir     {
108cdf0e10cSrcweir     }
109cdf0e10cSrcweir 
110cdf0e10cSrcweir 	//--------------------------------------------------------------------------
onLastClient()111cdf0e10cSrcweir     void OModule::onLastClient()
112cdf0e10cSrcweir     {
113cdf0e10cSrcweir     }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir     //--------------------------------------------------------------------------
registerImplementation(const ComponentDescription & _rComp)116cdf0e10cSrcweir     void OModule::registerImplementation( const ComponentDescription& _rComp )
117cdf0e10cSrcweir     {
118cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
119cdf0e10cSrcweir         if ( !m_pImpl )
120cdf0e10cSrcweir             throw RuntimeException();
121cdf0e10cSrcweir 
122cdf0e10cSrcweir         m_pImpl->m_aRegisteredComponents.push_back( _rComp );
123cdf0e10cSrcweir     }
124cdf0e10cSrcweir 
125cdf0e10cSrcweir 	//--------------------------------------------------------------------------
registerImplementation(const::rtl::OUString & _rImplementationName,const::com::sun::star::uno::Sequence<::rtl::OUString> & _rServiceNames,::cppu::ComponentFactoryFunc _pCreateFunction,FactoryInstantiation _pFactoryFunction)126cdf0e10cSrcweir     void OModule::registerImplementation( const ::rtl::OUString& _rImplementationName, const ::com::sun::star::uno::Sequence< ::rtl::OUString >& _rServiceNames,
127cdf0e10cSrcweir         ::cppu::ComponentFactoryFunc _pCreateFunction, FactoryInstantiation _pFactoryFunction )
128cdf0e10cSrcweir 	{
129cdf0e10cSrcweir         ComponentDescription aComponent( _rImplementationName, _rServiceNames, ::rtl::OUString(), _pCreateFunction, _pFactoryFunction );
130cdf0e10cSrcweir         registerImplementation( aComponent );
131cdf0e10cSrcweir 	}
132cdf0e10cSrcweir 
133cdf0e10cSrcweir 	//--------------------------------------------------------------------------
getComponentFactory(const sal_Char * _pImplementationName,void * _pServiceManager,void *)134cdf0e10cSrcweir 	void* OModule::getComponentFactory( const sal_Char* _pImplementationName, void* _pServiceManager, void* /*_pRegistryKey*/ )
135cdf0e10cSrcweir 	{
136cdf0e10cSrcweir         Reference< XInterface > xFactory( getComponentFactory(
137cdf0e10cSrcweir             ::rtl::OUString::createFromAscii( _pImplementationName ),
138cdf0e10cSrcweir             Reference< XMultiServiceFactory >( static_cast< XMultiServiceFactory* >( _pServiceManager ) )
139cdf0e10cSrcweir         ) );
140cdf0e10cSrcweir         return xFactory.get();
141cdf0e10cSrcweir     }
142cdf0e10cSrcweir 
143cdf0e10cSrcweir 	//--------------------------------------------------------------------------
getComponentFactory(const::rtl::OUString & _rImplementationName,const Reference<XMultiServiceFactory> &)144cdf0e10cSrcweir 	Reference< XInterface > OModule::getComponentFactory( const ::rtl::OUString& _rImplementationName,
145cdf0e10cSrcweir         const Reference< XMultiServiceFactory >& /* _rxServiceManager */ )
146cdf0e10cSrcweir 	{
147cdf0e10cSrcweir 		Reference< XInterface > xReturn;
148cdf0e10cSrcweir 
149cdf0e10cSrcweir         for (   ComponentDescriptions::const_iterator component = m_pImpl->m_aRegisteredComponents.begin();
150cdf0e10cSrcweir                 component != m_pImpl->m_aRegisteredComponents.end();
151cdf0e10cSrcweir                 ++component
152cdf0e10cSrcweir             )
153cdf0e10cSrcweir 		{
154cdf0e10cSrcweir 			if ( component->sImplementationName == _rImplementationName )
155cdf0e10cSrcweir 			{
156cdf0e10cSrcweir 				xReturn = component->pFactoryCreationFunc(
157cdf0e10cSrcweir                     component->pComponentCreationFunc,
158cdf0e10cSrcweir                     component->sImplementationName,
159cdf0e10cSrcweir                     component->aSupportedServices,
160cdf0e10cSrcweir                     NULL
161cdf0e10cSrcweir                 );
162cdf0e10cSrcweir 				if ( xReturn.is() )
163cdf0e10cSrcweir 				{
164cdf0e10cSrcweir 					xReturn->acquire();
165cdf0e10cSrcweir 					return xReturn.get();
166cdf0e10cSrcweir 				}
167cdf0e10cSrcweir 			}
168cdf0e10cSrcweir 		}
169cdf0e10cSrcweir 
170cdf0e10cSrcweir 		return NULL;
171cdf0e10cSrcweir 	}
172cdf0e10cSrcweir 
173cdf0e10cSrcweir //........................................................................
174cdf0e10cSrcweir } // namespace comphelper
175cdf0e10cSrcweir //........................................................................
176