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