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