xref: /AOO41X/main/odk/examples/DevelopersGuide/Database/DriverSkeleton/OSubComponent.hxx (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir /*************************************************************************
2*cdf0e10cSrcweir  *
3*cdf0e10cSrcweir  *  The Contents of this file are made available subject to the terms of
4*cdf0e10cSrcweir  *  the BSD license.
5*cdf0e10cSrcweir  *
6*cdf0e10cSrcweir  *  Copyright 2000, 2010 Oracle and/or its affiliates.
7*cdf0e10cSrcweir  *  All rights reserved.
8*cdf0e10cSrcweir  *
9*cdf0e10cSrcweir  *  Redistribution and use in source and binary forms, with or without
10*cdf0e10cSrcweir  *  modification, are permitted provided that the following conditions
11*cdf0e10cSrcweir  *  are met:
12*cdf0e10cSrcweir  *  1. Redistributions of source code must retain the above copyright
13*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer.
14*cdf0e10cSrcweir  *  2. Redistributions in binary form must reproduce the above copyright
15*cdf0e10cSrcweir  *     notice, this list of conditions and the following disclaimer in the
16*cdf0e10cSrcweir  *     documentation and/or other materials provided with the distribution.
17*cdf0e10cSrcweir  *  3. Neither the name of Sun Microsystems, Inc. nor the names of its
18*cdf0e10cSrcweir  *     contributors may be used to endorse or promote products derived
19*cdf0e10cSrcweir  *     from this software without specific prior written permission.
20*cdf0e10cSrcweir  *
21*cdf0e10cSrcweir  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22*cdf0e10cSrcweir  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23*cdf0e10cSrcweir  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24*cdf0e10cSrcweir  *  FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25*cdf0e10cSrcweir  *  COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26*cdf0e10cSrcweir  *  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
27*cdf0e10cSrcweir  *  BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
28*cdf0e10cSrcweir  *  OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
29*cdf0e10cSrcweir  *  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
30*cdf0e10cSrcweir  *  TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
31*cdf0e10cSrcweir  *  USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32*cdf0e10cSrcweir  *
33*cdf0e10cSrcweir  *************************************************************************/
34*cdf0e10cSrcweir 
35*cdf0e10cSrcweir #ifndef _CONNECTIVITY_OSUBCOMPONENT_HXX_
36*cdf0e10cSrcweir #define _CONNECTIVITY_OSUBCOMPONENT_HXX_
37*cdf0e10cSrcweir 
38*cdf0e10cSrcweir #include <cppuhelper/weak.hxx>
39*cdf0e10cSrcweir #include <cppuhelper/interfacecontainer.h>
40*cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp>
41*cdf0e10cSrcweir #include <cppuhelper/propshlp.hxx>
42*cdf0e10cSrcweir #include <osl/mutex.hxx>
43*cdf0e10cSrcweir #include <osl/diagnose.h>
44*cdf0e10cSrcweir 
45*cdf0e10cSrcweir namespace cppu {
46*cdf0e10cSrcweir 	class IPropertyArrayHelper;
47*cdf0e10cSrcweir }
48*cdf0e10cSrcweir 
49*cdf0e10cSrcweir namespace com
50*cdf0e10cSrcweir {
51*cdf0e10cSrcweir 	namespace sun
52*cdf0e10cSrcweir 	{
53*cdf0e10cSrcweir 		namespace star
54*cdf0e10cSrcweir 		{
55*cdf0e10cSrcweir 			namespace lang
56*cdf0e10cSrcweir 			{
57*cdf0e10cSrcweir 				class XComponent;
58*cdf0e10cSrcweir 			}
59*cdf0e10cSrcweir 		}
60*cdf0e10cSrcweir 	}
61*cdf0e10cSrcweir }
62*cdf0e10cSrcweir namespace connectivity
63*cdf0e10cSrcweir {
64*cdf0e10cSrcweir 
65*cdf0e10cSrcweir 	namespace skeleton
66*cdf0e10cSrcweir 	{
67*cdf0e10cSrcweir 		void release(oslInterlockedCount& _refCount,
68*cdf0e10cSrcweir 					 ::cppu::OBroadcastHelper& rBHelper,
69*cdf0e10cSrcweir 					 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
70*cdf0e10cSrcweir 					 ::com::sun::star::lang::XComponent* _pObject);
71*cdf0e10cSrcweir 
72*cdf0e10cSrcweir 		void checkDisposed(sal_Bool _bThrow) throw ( ::com::sun::star::lang::DisposedException );
73*cdf0e10cSrcweir 		//************************************************************
74*cdf0e10cSrcweir 		// OSubComponent
75*cdf0e10cSrcweir 		//************************************************************
76*cdf0e10cSrcweir 		template <class SELF, class WEAK> class OSubComponent
77*cdf0e10cSrcweir 		{
78*cdf0e10cSrcweir 		protected:
79*cdf0e10cSrcweir 			// the parent must support the tunnel implementation
80*cdf0e10cSrcweir 			::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > m_xParent;
81*cdf0e10cSrcweir 			SELF*	m_pDerivedImplementation;
82*cdf0e10cSrcweir 
83*cdf0e10cSrcweir 		public:
84*cdf0e10cSrcweir 			OSubComponent(
85*cdf0e10cSrcweir 					const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xParent,
86*cdf0e10cSrcweir 					SELF* _pDerivedImplementation)
87*cdf0e10cSrcweir 				:m_xParent(_xParent)
88*cdf0e10cSrcweir 				,m_pDerivedImplementation(_pDerivedImplementation)
89*cdf0e10cSrcweir 			{
90*cdf0e10cSrcweir 			}
91*cdf0e10cSrcweir 
92*cdf0e10cSrcweir 		protected:
93*cdf0e10cSrcweir 			void dispose_ChildImpl()
94*cdf0e10cSrcweir 			{
95*cdf0e10cSrcweir 				::osl::MutexGuard aGuard( m_pDerivedImplementation->rBHelper.rMutex );
96*cdf0e10cSrcweir 				m_xParent = NULL;
97*cdf0e10cSrcweir 			}
98*cdf0e10cSrcweir 			void relase_ChildImpl()
99*cdf0e10cSrcweir 			{
100*cdf0e10cSrcweir 				release(m_pDerivedImplementation->m_refCount,
101*cdf0e10cSrcweir 										m_pDerivedImplementation->rBHelper,
102*cdf0e10cSrcweir 										m_xParent,
103*cdf0e10cSrcweir 										m_pDerivedImplementation);
104*cdf0e10cSrcweir 
105*cdf0e10cSrcweir 				m_pDerivedImplementation->WEAK::release();
106*cdf0e10cSrcweir 			}
107*cdf0e10cSrcweir 		};
108*cdf0e10cSrcweir 
109*cdf0e10cSrcweir 
110*cdf0e10cSrcweir 		template <class TYPE>
111*cdf0e10cSrcweir 		class OPropertyArrayUsageHelper
112*cdf0e10cSrcweir 		{
113*cdf0e10cSrcweir 		protected:
114*cdf0e10cSrcweir 			static sal_Int32						s_nRefCount;
115*cdf0e10cSrcweir 			static ::cppu::IPropertyArrayHelper*	s_pProps;
116*cdf0e10cSrcweir 			static ::osl::Mutex						s_aMutex;
117*cdf0e10cSrcweir 
118*cdf0e10cSrcweir 		public:
119*cdf0e10cSrcweir 			OPropertyArrayUsageHelper();
120*cdf0e10cSrcweir 			virtual ~OPropertyArrayUsageHelper()
121*cdf0e10cSrcweir 			{	// ARGHHHHHHH ..... would like to implement this in proparrhlp_impl.hxx (as we do with all other methods)
122*cdf0e10cSrcweir 				// but SUNPRO 5 compiler (linker) doesn't like this
123*cdf0e10cSrcweir 				::osl::MutexGuard aGuard(s_aMutex);
124*cdf0e10cSrcweir 				OSL_ENSURE(s_nRefCount > 0, "OPropertyArrayUsageHelper::~OPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
125*cdf0e10cSrcweir 				if (!--s_nRefCount)
126*cdf0e10cSrcweir 				{
127*cdf0e10cSrcweir 					delete s_pProps;
128*cdf0e10cSrcweir 					s_pProps = NULL;
129*cdf0e10cSrcweir 				}
130*cdf0e10cSrcweir 			}
131*cdf0e10cSrcweir 
132*cdf0e10cSrcweir 			/** call this in the getInfoHelper method of your derived class. The method returns the array helper of the
133*cdf0e10cSrcweir 				class, which is created if neccessary.
134*cdf0e10cSrcweir 			*/
135*cdf0e10cSrcweir 			::cppu::IPropertyArrayHelper*	getArrayHelper();
136*cdf0e10cSrcweir 
137*cdf0e10cSrcweir 		protected:
138*cdf0e10cSrcweir 			/** used to implement the creation of the array helper which is shared amongst all instances of the class.
139*cdf0e10cSrcweir 				This method needs to be implemented in derived classes.
140*cdf0e10cSrcweir 				<BR>
141*cdf0e10cSrcweir 				The method gets called with s_aMutex acquired.
142*cdf0e10cSrcweir 				<BR>
143*cdf0e10cSrcweir 				as long as IPropertyArrayHelper has no virtual destructor, the implementation of ~OPropertyArrayUsageHelper
144*cdf0e10cSrcweir 				assumes that you created an ::cppu::OPropertyArrayHelper when deleting s_pProps.
145*cdf0e10cSrcweir 				@return							an pointer to the newly created array helper. Must not be NULL.
146*cdf0e10cSrcweir 			*/
147*cdf0e10cSrcweir 			virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const = 0;
148*cdf0e10cSrcweir 		};
149*cdf0e10cSrcweir 
150*cdf0e10cSrcweir 		template<class TYPE>
151*cdf0e10cSrcweir 		sal_Int32						OPropertyArrayUsageHelper< TYPE >::s_nRefCount	= 0;
152*cdf0e10cSrcweir 
153*cdf0e10cSrcweir 		template<class TYPE>
154*cdf0e10cSrcweir 		::cppu::IPropertyArrayHelper*	OPropertyArrayUsageHelper< TYPE >::s_pProps	= NULL;
155*cdf0e10cSrcweir 
156*cdf0e10cSrcweir 		template<class TYPE>
157*cdf0e10cSrcweir 		::osl::Mutex					OPropertyArrayUsageHelper< TYPE >::s_aMutex;
158*cdf0e10cSrcweir 
159*cdf0e10cSrcweir 		//------------------------------------------------------------------
160*cdf0e10cSrcweir 		template <class TYPE>
161*cdf0e10cSrcweir 		OPropertyArrayUsageHelper<TYPE>::OPropertyArrayUsageHelper()
162*cdf0e10cSrcweir 		{
163*cdf0e10cSrcweir 			::osl::MutexGuard aGuard(s_aMutex);
164*cdf0e10cSrcweir 			++s_nRefCount;
165*cdf0e10cSrcweir 		}
166*cdf0e10cSrcweir 
167*cdf0e10cSrcweir 		//------------------------------------------------------------------
168*cdf0e10cSrcweir 		template <class TYPE>
169*cdf0e10cSrcweir 		::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper<TYPE>::getArrayHelper()
170*cdf0e10cSrcweir 		{
171*cdf0e10cSrcweir 			OSL_ENSURE(s_nRefCount, "OPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
172*cdf0e10cSrcweir 			if (!s_pProps)
173*cdf0e10cSrcweir 			{
174*cdf0e10cSrcweir 				::osl::MutexGuard aGuard(s_aMutex);
175*cdf0e10cSrcweir 				if (!s_pProps)
176*cdf0e10cSrcweir 				{
177*cdf0e10cSrcweir 					s_pProps = createArrayHelper();
178*cdf0e10cSrcweir 					OSL_ENSURE(s_pProps, "OPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
179*cdf0e10cSrcweir 				}
180*cdf0e10cSrcweir 			}
181*cdf0e10cSrcweir 			return s_pProps;
182*cdf0e10cSrcweir 		}
183*cdf0e10cSrcweir 
184*cdf0e10cSrcweir 
185*cdf0e10cSrcweir 
186*cdf0e10cSrcweir 		class OBase_Mutex
187*cdf0e10cSrcweir 		{
188*cdf0e10cSrcweir 		public:
189*cdf0e10cSrcweir 			::osl::Mutex m_aMutex;
190*cdf0e10cSrcweir 		};
191*cdf0e10cSrcweir 
192*cdf0e10cSrcweir 		namespace internal
193*cdf0e10cSrcweir 		{
194*cdf0e10cSrcweir 			template <class T>
195*cdf0e10cSrcweir 			void implCopySequence(const T* _pSource, T*& _pDest, sal_Int32 _nSourceLen)
196*cdf0e10cSrcweir 			{
197*cdf0e10cSrcweir 				for (sal_Int32 i=0; i<_nSourceLen; ++i, ++_pSource, ++_pDest)
198*cdf0e10cSrcweir 					*_pDest = *_pSource;
199*cdf0e10cSrcweir 			}
200*cdf0e10cSrcweir 		}
201*cdf0e10cSrcweir 		//-------------------------------------------------------------------------
202*cdf0e10cSrcweir 		/// concat two sequences
203*cdf0e10cSrcweir 		template <class T>
204*cdf0e10cSrcweir 		::com::sun::star::uno::Sequence<T> concatSequences(const ::com::sun::star::uno::Sequence<T>& _rLeft, const ::com::sun::star::uno::Sequence<T>& _rRight)
205*cdf0e10cSrcweir 		{
206*cdf0e10cSrcweir 			sal_Int32 nLeft(_rLeft.getLength()), nRight(_rRight.getLength());
207*cdf0e10cSrcweir 			const T* pLeft = _rLeft.getConstArray();
208*cdf0e10cSrcweir 			const T* pRight = _rRight.getConstArray();
209*cdf0e10cSrcweir 
210*cdf0e10cSrcweir 			sal_Int32 nReturnLen(nLeft + nRight);
211*cdf0e10cSrcweir 			::com::sun::star::uno::Sequence<T> aReturn(nReturnLen);
212*cdf0e10cSrcweir 			T* pReturn = aReturn.getArray();
213*cdf0e10cSrcweir 
214*cdf0e10cSrcweir 			internal::implCopySequence(pLeft, pReturn, nLeft);
215*cdf0e10cSrcweir 			internal::implCopySequence(pRight, pReturn, nRight);
216*cdf0e10cSrcweir 
217*cdf0e10cSrcweir 			return aReturn;
218*cdf0e10cSrcweir 		}
219*cdf0e10cSrcweir 
220*cdf0e10cSrcweir 
221*cdf0e10cSrcweir #define DECLARE_SERVICE_INFO()	\
222*cdf0e10cSrcweir 	virtual ::rtl::OUString SAL_CALL getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException);	\
223*cdf0e10cSrcweir     virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) throw(::com::sun::star::uno::RuntimeException);	\
224*cdf0e10cSrcweir     virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException)	\
225*cdf0e10cSrcweir 
226*cdf0e10cSrcweir #define IMPLEMENT_SERVICE_INFO(classname, implasciiname, serviceasciiname)	\
227*cdf0e10cSrcweir 	::rtl::OUString SAL_CALL classname::getImplementationName(  ) throw (::com::sun::star::uno::RuntimeException)	\
228*cdf0e10cSrcweir 	{	\
229*cdf0e10cSrcweir 		return ::rtl::OUString::createFromAscii(implasciiname);	\
230*cdf0e10cSrcweir 	}	\
231*cdf0e10cSrcweir     ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL classname::getSupportedServiceNames(  ) throw(::com::sun::star::uno::RuntimeException)	\
232*cdf0e10cSrcweir 	{	\
233*cdf0e10cSrcweir 		::com::sun::star::uno::Sequence< ::rtl::OUString > aSupported(1);	\
234*cdf0e10cSrcweir 		aSupported[0] = ::rtl::OUString::createFromAscii(serviceasciiname);	\
235*cdf0e10cSrcweir 		return aSupported;	\
236*cdf0e10cSrcweir 	}	\
237*cdf0e10cSrcweir     sal_Bool SAL_CALL classname::supportsService( const ::rtl::OUString& _rServiceName ) throw(::com::sun::star::uno::RuntimeException)	\
238*cdf0e10cSrcweir 	{	\
239*cdf0e10cSrcweir 		Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames());				\
240*cdf0e10cSrcweir 		const ::rtl::OUString* pSupported = aSupported.getConstArray();					\
241*cdf0e10cSrcweir 		const ::rtl::OUString* pEnd = pSupported + aSupported.getLength();				\
242*cdf0e10cSrcweir 		for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported)	\
243*cdf0e10cSrcweir 			;																			\
244*cdf0e10cSrcweir 																						\
245*cdf0e10cSrcweir 		return pSupported != pEnd;														\
246*cdf0e10cSrcweir 	}	\
247*cdf0e10cSrcweir 
248*cdf0e10cSrcweir 
249*cdf0e10cSrcweir 	}
250*cdf0e10cSrcweir }
251*cdf0e10cSrcweir #endif // _CONNECTIVITY_OSUBCOMPONENT_HXX_
252*cdf0e10cSrcweir 
253