update dev300-m58
[ooovba.git] / comphelper / source / misc / legacysingletonfactory.cxx
blob98304adffe93830ca45329ec69ab9da7d85f65db
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: legacysingletonfactory.cxx,v $
10 * $Revision: 1.3 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
34 #include "comphelper/legacysingletonfactory.hxx"
36 /** === begin UNO includes === **/
37 #include <com/sun/star/lang/XServiceInfo.hpp>
38 #include <com/sun/star/lang/XInitialization.hpp>
39 /** === end UNO includes === **/
41 #include <cppuhelper/implbase2.hxx>
43 #include <algorithm>
45 //........................................................................
46 namespace comphelper
48 //........................................................................
50 /** === begin UNO using === **/
51 using ::com::sun::star::uno::Reference;
52 using ::com::sun::star::uno::XInterface;
53 using ::com::sun::star::uno::UNO_QUERY;
54 using ::com::sun::star::uno::UNO_QUERY_THROW;
55 using ::com::sun::star::uno::UNO_SET_THROW;
56 using ::com::sun::star::uno::Exception;
57 using ::com::sun::star::uno::RuntimeException;
58 using ::com::sun::star::uno::Any;
59 using ::com::sun::star::uno::makeAny;
60 using ::com::sun::star::lang::XSingleComponentFactory;
61 using ::com::sun::star::uno::Sequence;
62 using ::com::sun::star::uno::XComponentContext;
63 using ::com::sun::star::lang::XServiceInfo;
64 using ::com::sun::star::lang::XInitialization;
65 /** === end UNO using === **/
67 //====================================================================
68 //= LegacySingletonFactory
69 //====================================================================
70 typedef ::cppu::WeakImplHelper2 < XServiceInfo
71 , XSingleComponentFactory
72 > LegacySingletonFactory_Base;
74 class COMPHELPER_DLLPRIVATE LegacySingletonFactory : public LegacySingletonFactory_Base
76 public:
77 LegacySingletonFactory(
78 ::cppu::ComponentFactoryFunc _componentFactoryFunc, const ::rtl::OUString& _rImplementationName,
79 const Sequence< ::rtl::OUString >& _rServiceNames, rtl_ModuleCount* _pModCount
82 // XServiceInfo
83 virtual ::rtl::OUString SAL_CALL getImplementationName( ) throw (RuntimeException);
84 virtual ::sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw (RuntimeException);
85 virtual Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) throw (RuntimeException);
87 // XSingleComponentFactory
88 virtual Reference< XInterface > SAL_CALL createInstanceWithContext( const Reference< XComponentContext >& Context ) throw (Exception, RuntimeException);
89 virtual Reference< XInterface > SAL_CALL createInstanceWithArgumentsAndContext( const Sequence< Any >& Arguments, const Reference< XComponentContext >& Context ) throw (Exception, RuntimeException);
91 protected:
92 ~LegacySingletonFactory();
94 private:
95 /** creates m_xInstance, returns whether it actually was created (<TRUE/>) or existed before (<FALSE/>
97 bool impl_nts_ensureInstance( const Reference< XComponentContext >& _rxContext );
99 private:
100 ::osl::Mutex m_aMutex;
101 ::cppu::ComponentFactoryFunc m_componentFactoryFunc;
102 ::rtl::OUString m_sImplementationName;
103 Sequence< ::rtl::OUString > m_aServiceNames;
104 rtl_ModuleCount* m_pModuleCount;
105 Reference< XInterface > m_xTheInstance;
108 //--------------------------------------------------------------------
109 LegacySingletonFactory::LegacySingletonFactory( ::cppu::ComponentFactoryFunc _componentFactoryFunc, const ::rtl::OUString& _rImplementationName,
110 const Sequence< ::rtl::OUString >& _rServiceNames, rtl_ModuleCount* _pModCount )
111 :m_componentFactoryFunc ( _componentFactoryFunc )
112 ,m_sImplementationName ( _rImplementationName )
113 ,m_aServiceNames ( _rServiceNames )
114 ,m_pModuleCount ( _pModCount )
115 ,m_xTheInstance ( )
117 if ( m_pModuleCount )
118 m_pModuleCount->acquire( m_pModuleCount );
121 //--------------------------------------------------------------------
122 LegacySingletonFactory::~LegacySingletonFactory()
124 if ( m_pModuleCount )
125 m_pModuleCount->release( m_pModuleCount );
128 //--------------------------------------------------------------------
129 ::rtl::OUString SAL_CALL LegacySingletonFactory::getImplementationName( ) throw (RuntimeException)
131 return m_sImplementationName;
134 //--------------------------------------------------------------------
135 ::sal_Bool SAL_CALL LegacySingletonFactory::supportsService( const ::rtl::OUString& _rServiceName ) throw (RuntimeException)
137 Sequence< ::rtl::OUString > aServices( getSupportedServiceNames() );
138 const ::rtl::OUString* pStart = aServices.getConstArray();
139 const ::rtl::OUString* pEnd = aServices.getConstArray() + aServices.getLength();
140 return ::std::find( pStart, pEnd, _rServiceName ) != pEnd;
143 //--------------------------------------------------------------------
144 Sequence< ::rtl::OUString > SAL_CALL LegacySingletonFactory::getSupportedServiceNames( ) throw (RuntimeException)
146 return m_aServiceNames;
149 //--------------------------------------------------------------------
150 bool LegacySingletonFactory::impl_nts_ensureInstance( const Reference< XComponentContext >& _rxContext )
152 if ( m_xTheInstance.is() )
153 return false;
155 m_xTheInstance = (*m_componentFactoryFunc)( _rxContext );
156 if ( !m_xTheInstance.is() )
157 throw RuntimeException();
159 return true; // true -> "was newly created"
162 //--------------------------------------------------------------------
163 Reference< XInterface > SAL_CALL LegacySingletonFactory::createInstanceWithContext( const Reference< XComponentContext >& _rxContext ) throw (Exception, RuntimeException)
165 ::osl::MutexGuard aGuard( m_aMutex );
166 impl_nts_ensureInstance( _rxContext );
168 return m_xTheInstance;
171 //--------------------------------------------------------------------
172 Reference< XInterface > SAL_CALL LegacySingletonFactory::createInstanceWithArgumentsAndContext( const Sequence< Any >& _rArguments, const Reference< XComponentContext >& _rxContext ) throw (Exception, RuntimeException)
174 ::osl::MutexGuard aGuard( m_aMutex );
175 if ( !impl_nts_ensureInstance( _rxContext ) )
176 throw RuntimeException(
177 ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Instance already created before, unable to initialize it." ) ),
178 *this
181 Reference< XInitialization > xInit( m_xTheInstance, UNO_QUERY_THROW );
182 xInit->initialize( _rArguments );
184 return m_xTheInstance;
187 //====================================================================
188 //= createLegacySingletonFactory
189 //====================================================================
190 Reference< XSingleComponentFactory > createLegacySingletonFactory(
191 ::cppu::ComponentFactoryFunc _componentFactoryFunc, const ::rtl::OUString& _rImplementationName,
192 const Sequence< ::rtl::OUString >& _rServiceNames, rtl_ModuleCount* _pModCount )
194 return new LegacySingletonFactory( _componentFactoryFunc, _rImplementationName, _rServiceNames, _pModCount );
198 //........................................................................
199 } // namespace comphelper
200 //........................................................................