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