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