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