1*079eb577SAndrew Rist /**************************************************************
2*079eb577SAndrew Rist  *
3*079eb577SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*079eb577SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*079eb577SAndrew Rist  * distributed with this work for additional information
6*079eb577SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*079eb577SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*079eb577SAndrew Rist  * "License"); you may not use this file except in compliance
9*079eb577SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*079eb577SAndrew Rist  *
11*079eb577SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*079eb577SAndrew Rist  *
13*079eb577SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*079eb577SAndrew Rist  * software distributed under the License is distributed on an
15*079eb577SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*079eb577SAndrew Rist  * KIND, either express or implied.  See the License for the
17*079eb577SAndrew Rist  * specific language governing permissions and limitations
18*079eb577SAndrew Rist  * under the License.
19*079eb577SAndrew Rist  *
20*079eb577SAndrew Rist  *************************************************************/
21cdf0e10cSrcweir 
22cdf0e10cSrcweir #ifndef _CONNECTIVITY_OSUBCOMPONENT_HXX_
23cdf0e10cSrcweir #define _CONNECTIVITY_OSUBCOMPONENT_HXX_
24cdf0e10cSrcweir 
25cdf0e10cSrcweir #ifndef _CPPUHELPER_WEAK_HXX_
26cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
27cdf0e10cSrcweir #endif
28cdf0e10cSrcweir #ifndef _CPPUHELPER_INTERFACECONTAINER_H_
29cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.h>
30cdf0e10cSrcweir #endif
31cdf0e10cSrcweir #ifndef _COM_SUN_STAR_LANG_DISPOSEDEXCEPTION_HPP_
32cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
33cdf0e10cSrcweir #endif
34cdf0e10cSrcweir #ifndef _CPPUHELPER_PROPSHLP_HXX
35cdf0e10cSrcweir #include <cppuhelper/propshlp.hxx>
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir #ifndef _OSL_MUTEX_HXX_
38cdf0e10cSrcweir #include <osl/mutex.hxx>
39cdf0e10cSrcweir #endif
40cdf0e10cSrcweir #ifndef _OSL_DIAGNOSE_H_
41cdf0e10cSrcweir #include <osl/diagnose.h>
42cdf0e10cSrcweir #endif
43cdf0e10cSrcweir 
44cdf0e10cSrcweir namespace cppu {
45cdf0e10cSrcweir 	class IPropertyArrayHelper;
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir namespace com
49cdf0e10cSrcweir {
50cdf0e10cSrcweir 	namespace sun
51cdf0e10cSrcweir 	{
52cdf0e10cSrcweir 		namespace star
53cdf0e10cSrcweir 		{
54cdf0e10cSrcweir 			namespace lang
55cdf0e10cSrcweir 			{
56cdf0e10cSrcweir 				class XComponent;
57cdf0e10cSrcweir 			}
58cdf0e10cSrcweir 		}
59cdf0e10cSrcweir 	}
60cdf0e10cSrcweir }
61cdf0e10cSrcweir namespace connectivity
62cdf0e10cSrcweir {
63cdf0e10cSrcweir 
64cdf0e10cSrcweir 	namespace mysqlc
65cdf0e10cSrcweir 	{
66cdf0e10cSrcweir 		void release(oslInterlockedCount& _refCount,
67cdf0e10cSrcweir 					 ::cppu::OBroadcastHelper& rBHelper,
68cdf0e10cSrcweir 					 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
69cdf0e10cSrcweir 					 ::com::sun::star::lang::XComponent* _pObject);
70cdf0e10cSrcweir 
71cdf0e10cSrcweir 		void checkDisposed(sal_Bool _bThrow) throw (::com::sun::star::lang::DisposedException);
72cdf0e10cSrcweir 		//************************************************************
73cdf0e10cSrcweir 		// OSubComponent
74cdf0e10cSrcweir 		//************************************************************
75cdf0e10cSrcweir 		template <class SELF, class WEAK> class OSubComponent
76cdf0e10cSrcweir 		{
77cdf0e10cSrcweir 		protected:
78cdf0e10cSrcweir 			// the parent must support the tunnel implementation
79cdf0e10cSrcweir 			::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > m_xParent;
80cdf0e10cSrcweir 			SELF*	m_pDerivedImplementation;
81cdf0e10cSrcweir 
82cdf0e10cSrcweir 		public:
OSubComponent(const::com::sun::star::uno::Reference<::com::sun::star::uno::XInterface> & _xParent,SELF * _pDerivedImplementation)83cdf0e10cSrcweir 			OSubComponent(
84cdf0e10cSrcweir 					const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xParent,
85cdf0e10cSrcweir 					SELF* _pDerivedImplementation)
86cdf0e10cSrcweir 				:m_xParent(_xParent)
87cdf0e10cSrcweir 				,m_pDerivedImplementation(_pDerivedImplementation)
88cdf0e10cSrcweir 			{
89cdf0e10cSrcweir 			}
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 		protected:
dispose_ChildImpl()92cdf0e10cSrcweir 			void dispose_ChildImpl()
93cdf0e10cSrcweir 			{
94cdf0e10cSrcweir 				::osl::MutexGuard aGuard(m_pDerivedImplementation->rBHelper.rMutex);
95cdf0e10cSrcweir 				m_xParent = NULL;
96cdf0e10cSrcweir 			}
relase_ChildImpl()97cdf0e10cSrcweir 			void relase_ChildImpl()
98cdf0e10cSrcweir 			{
99cdf0e10cSrcweir 				release(m_pDerivedImplementation->m_refCount,
100cdf0e10cSrcweir 										m_pDerivedImplementation->rBHelper,
101cdf0e10cSrcweir 										m_xParent,
102cdf0e10cSrcweir 										m_pDerivedImplementation);
103cdf0e10cSrcweir 
104cdf0e10cSrcweir 				m_pDerivedImplementation->WEAK::release();
105cdf0e10cSrcweir 			}
106cdf0e10cSrcweir 		};
107cdf0e10cSrcweir 
108cdf0e10cSrcweir 
109cdf0e10cSrcweir 		template <class TYPE>
110cdf0e10cSrcweir 		class OPropertyArrayUsageHelper
111cdf0e10cSrcweir 		{
112cdf0e10cSrcweir 		protected:
113cdf0e10cSrcweir 			static sal_Int32						s_nRefCount;
114cdf0e10cSrcweir 			static ::cppu::IPropertyArrayHelper*	s_pProps;
115cdf0e10cSrcweir 			static ::osl::Mutex						s_aMutex;
116cdf0e10cSrcweir 
117cdf0e10cSrcweir 		public:
118cdf0e10cSrcweir 			OPropertyArrayUsageHelper();
~OPropertyArrayUsageHelper()119cdf0e10cSrcweir 			virtual ~OPropertyArrayUsageHelper()
120cdf0e10cSrcweir 			{	// ARGHHHHHHH ..... would like to implement this in proparrhlp_impl.hxx (as we do with all other methods)
121cdf0e10cSrcweir 				// but SUNPRO 5 compiler (linker) doesn't like this
122cdf0e10cSrcweir 				::osl::MutexGuard aGuard(s_aMutex);
123cdf0e10cSrcweir 				OSL_ENSURE(s_nRefCount > 0, "OPropertyArrayUsageHelper::~OPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
124cdf0e10cSrcweir 				if (!--s_nRefCount)
125cdf0e10cSrcweir 				{
126cdf0e10cSrcweir 					delete s_pProps;
127cdf0e10cSrcweir 					s_pProps = NULL;
128cdf0e10cSrcweir 				}
129cdf0e10cSrcweir 			}
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 			/** call this in the getInfoHelper method of your derived class. The method returns the array helper of the
132cdf0e10cSrcweir 				class, which is created if neccessary.
133cdf0e10cSrcweir 			*/
134cdf0e10cSrcweir 			::cppu::IPropertyArrayHelper*	getArrayHelper();
135cdf0e10cSrcweir 
136cdf0e10cSrcweir 		protected:
137cdf0e10cSrcweir 			/** used to implement the creation of the array helper which is shared amongst all instances of the class.
138cdf0e10cSrcweir 				This method needs to be implemented in derived classes.
139cdf0e10cSrcweir 				<BR>
140cdf0e10cSrcweir 				The method gets called with s_aMutex acquired.
141cdf0e10cSrcweir 				<BR>
142cdf0e10cSrcweir 				as long as IPropertyArrayHelper has no virtual destructor, the implementation of ~OPropertyArrayUsageHelper
143cdf0e10cSrcweir 				assumes that you created an ::cppu::OPropertyArrayHelper when deleting s_pProps.
144cdf0e10cSrcweir 				@return							an pointer to the newly created array helper. Must not be NULL.
145cdf0e10cSrcweir 			*/
146cdf0e10cSrcweir 			virtual ::cppu::IPropertyArrayHelper* createArrayHelper() const = 0;
147cdf0e10cSrcweir 		};
148cdf0e10cSrcweir 
149cdf0e10cSrcweir 		template<class TYPE>
150cdf0e10cSrcweir 		sal_Int32						OPropertyArrayUsageHelper< TYPE >::s_nRefCount	= 0;
151cdf0e10cSrcweir 
152cdf0e10cSrcweir 		template<class TYPE>
153cdf0e10cSrcweir 		::cppu::IPropertyArrayHelper*	OPropertyArrayUsageHelper< TYPE >::s_pProps	= NULL;
154cdf0e10cSrcweir 
155cdf0e10cSrcweir 		template<class TYPE>
156cdf0e10cSrcweir 		::osl::Mutex					OPropertyArrayUsageHelper< TYPE >::s_aMutex;
157cdf0e10cSrcweir 
158cdf0e10cSrcweir 		//------------------------------------------------------------------
159cdf0e10cSrcweir 		template <class TYPE>
OPropertyArrayUsageHelper()160cdf0e10cSrcweir 		OPropertyArrayUsageHelper<TYPE>::OPropertyArrayUsageHelper()
161cdf0e10cSrcweir 		{
162cdf0e10cSrcweir 			::osl::MutexGuard aGuard(s_aMutex);
163cdf0e10cSrcweir 			++s_nRefCount;
164cdf0e10cSrcweir 		}
165cdf0e10cSrcweir 
166cdf0e10cSrcweir 		//------------------------------------------------------------------
167cdf0e10cSrcweir 		template <class TYPE>
getArrayHelper()168cdf0e10cSrcweir 		::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper<TYPE>::getArrayHelper()
169cdf0e10cSrcweir 		{
170cdf0e10cSrcweir 			OSL_ENSURE(s_nRefCount, "OPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
171cdf0e10cSrcweir 			if (!s_pProps) {
172cdf0e10cSrcweir 				::osl::MutexGuard aGuard(s_aMutex);
173cdf0e10cSrcweir 				if (!s_pProps) {
174cdf0e10cSrcweir 					s_pProps = createArrayHelper();
175cdf0e10cSrcweir 					OSL_ENSURE(s_pProps, "OPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
176cdf0e10cSrcweir 				}
177cdf0e10cSrcweir 			}
178cdf0e10cSrcweir 			return s_pProps;
179cdf0e10cSrcweir 		}
180cdf0e10cSrcweir 
181cdf0e10cSrcweir 
182cdf0e10cSrcweir 		class OBase_Mutex
183cdf0e10cSrcweir 		{
184cdf0e10cSrcweir 		public:
185cdf0e10cSrcweir 			::osl::Mutex m_aMutex;
186cdf0e10cSrcweir 		};
187cdf0e10cSrcweir 
188cdf0e10cSrcweir 		namespace internal
189cdf0e10cSrcweir 		{
190cdf0e10cSrcweir 			template <class T>
implCopySequence(const T * _pSource,T * & _pDest,sal_Int32 _nSourceLen)191cdf0e10cSrcweir 			void implCopySequence(const T* _pSource, T*& _pDest, sal_Int32 _nSourceLen)
192cdf0e10cSrcweir 			{
193cdf0e10cSrcweir 				for (sal_Int32 i=0; i<_nSourceLen; ++i, ++_pSource, ++_pDest)
194cdf0e10cSrcweir 					*_pDest = *_pSource;
195cdf0e10cSrcweir 			}
196cdf0e10cSrcweir 		}
197cdf0e10cSrcweir 		//-------------------------------------------------------------------------
198cdf0e10cSrcweir 		/// concat two sequences
199cdf0e10cSrcweir 		template <class T>
concatSequences(const::com::sun::star::uno::Sequence<T> & _rLeft,const::com::sun::star::uno::Sequence<T> & _rRight)200cdf0e10cSrcweir 		::com::sun::star::uno::Sequence<T> concatSequences(const ::com::sun::star::uno::Sequence<T>& _rLeft, const ::com::sun::star::uno::Sequence<T>& _rRight)
201cdf0e10cSrcweir 		{
202cdf0e10cSrcweir 			sal_Int32 nLeft(_rLeft.getLength()), nRight(_rRight.getLength());
203cdf0e10cSrcweir 			const T* pLeft = _rLeft.getConstArray();
204cdf0e10cSrcweir 			const T* pRight = _rRight.getConstArray();
205cdf0e10cSrcweir 
206cdf0e10cSrcweir 			sal_Int32 nReturnLen(nLeft + nRight);
207cdf0e10cSrcweir 			::com::sun::star::uno::Sequence<T> aReturn(nReturnLen);
208cdf0e10cSrcweir 			T* pReturn = aReturn.getArray();
209cdf0e10cSrcweir 
210cdf0e10cSrcweir 			internal::implCopySequence(pLeft, pReturn, nLeft);
211cdf0e10cSrcweir 			internal::implCopySequence(pRight, pReturn, nRight);
212cdf0e10cSrcweir 
213cdf0e10cSrcweir 			return aReturn;
214cdf0e10cSrcweir 		}
215cdf0e10cSrcweir 
216cdf0e10cSrcweir 
217cdf0e10cSrcweir #define DECLARE_SERVICE_INFO()	\
218cdf0e10cSrcweir 	virtual ::rtl::OUString SAL_CALL getImplementationName() throw (::com::sun::star::uno::RuntimeException);	\
219cdf0e10cSrcweir 	virtual sal_Bool SAL_CALL supportsService(const ::rtl::OUString& ServiceName) throw(::com::sun::star::uno::RuntimeException);	\
220cdf0e10cSrcweir 	virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException)	\
221cdf0e10cSrcweir 
222cdf0e10cSrcweir #define IMPLEMENT_SERVICE_INFO(classname, implasciiname, serviceasciiname)	\
223cdf0e10cSrcweir 	::rtl::OUString SAL_CALL classname::getImplementationName() throw (::com::sun::star::uno::RuntimeException)	\
224cdf0e10cSrcweir 	{	\
225cdf0e10cSrcweir 		return ::rtl::OUString::createFromAscii(implasciiname);	\
226cdf0e10cSrcweir 	}	\
227cdf0e10cSrcweir 	::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL classname::getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException)	\
228cdf0e10cSrcweir 	{	\
229cdf0e10cSrcweir 		::com::sun::star::uno::Sequence< ::rtl::OUString > aSupported(1);	\
230cdf0e10cSrcweir 		aSupported[0] = ::rtl::OUString::createFromAscii(serviceasciiname);	\
231cdf0e10cSrcweir 		return aSupported;	\
232cdf0e10cSrcweir 	}	\
233cdf0e10cSrcweir 	sal_Bool SAL_CALL classname::supportsService(const ::rtl::OUString& _rServiceName) throw(::com::sun::star::uno::RuntimeException)	\
234cdf0e10cSrcweir 	{	\
235cdf0e10cSrcweir 		Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());				\
236cdf0e10cSrcweir 		const ::rtl::OUString* pSupported = aSupported.getConstArray();					\
237cdf0e10cSrcweir 		const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();				\
238cdf0e10cSrcweir 		for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)	\
239cdf0e10cSrcweir 			;																			\
240cdf0e10cSrcweir 		return pSupported != pEnd;														\
241cdf0e10cSrcweir 	}	\
242cdf0e10cSrcweir 
243cdf0e10cSrcweir 
244cdf0e10cSrcweir 	}
245cdf0e10cSrcweir }
246cdf0e10cSrcweir #endif // _CONNECTIVITY_OSUBCOMPONENT_HXX_
247cdf0e10cSrcweir 
248