Update git submodules
[LibreOffice.git] / odk / examples / DevelopersGuide / Database / DriverSkeleton / OSubComponent.hxx
blobd1113ad8efe1e33371362f0d845e32168216730c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*************************************************************************
4 * The Contents of this file are made available subject to the terms of
5 * the BSD license.
7 * Copyright 2000, 2010 Oracle and/or its affiliates.
8 * All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. Neither the name of Sun Microsystems, Inc. nor the names of its
19 * contributors may be used to endorse or promote products derived
20 * from this software without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
29 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
30 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR
31 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 * USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *************************************************************************/
36 #ifndef INCLUDED_EXAMPLES_DATABASE_DRIVERSKELETON_OSUBCOMPONENT_HXX
37 #define INCLUDED_EXAMPLES_DATABASE_DRIVERSKELETON_OSUBCOMPONENT_HXX
39 #include <com/sun/star/lang/DisposedException.hpp>
40 #include <cppuhelper/interfacecontainer.h>
41 #include <cppuhelper/propshlp.hxx>
42 #include <cppuhelper/supportsservice.hxx>
43 #include <cppuhelper/weak.hxx>
44 #include <osl/mutex.hxx>
45 #include <osl/diagnose.h>
47 namespace cppu {
48 class IPropertyArrayHelper;
51 namespace com
53 namespace sun
55 namespace star
57 namespace lang
59 class XComponent;
65 namespace connectivity
68 namespace skeleton
70 void release(oslInterlockedCount& _refCount,
71 ::cppu::OBroadcastHelper& rBHelper,
72 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
73 ::com::sun::star::lang::XComponent* _pObject);
75 void checkDisposed(sal_Bool _bThrow);
77 template <class SELF, class WEAK> class OSubComponent
79 protected:
80 // the parent must support the tunnel implementation
81 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > m_xParent;
82 SELF* m_pDerivedImplementation;
84 public:
85 OSubComponent(
86 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xParent,
87 SELF* _pDerivedImplementation)
88 :m_xParent(_xParent)
89 ,m_pDerivedImplementation(_pDerivedImplementation)
93 protected:
94 void dispose_ChildImpl()
96 ::osl::MutexGuard aGuard( m_pDerivedImplementation->rBHelper.rMutex );
97 m_xParent = NULL;
99 void release_ChildImpl()
101 release(m_pDerivedImplementation->m_refCount,
102 m_pDerivedImplementation->rBHelper,
103 m_xParent,
104 m_pDerivedImplementation);
106 m_pDerivedImplementation->WEAK::release();
110 template <class TYPE>
111 class OPropertyArrayUsageHelper
113 protected:
114 static sal_Int32 s_nRefCount;
115 static ::cppu::IPropertyArrayHelper* s_pProps;
116 static ::osl::Mutex s_aMutex;
118 public:
119 OPropertyArrayUsageHelper();
120 virtual ~OPropertyArrayUsageHelper();
122 /** call this in the getInfoHelper method of your derived class. The method returns the array helper of the
123 class, which is created if necessary.
125 ::cppu::IPropertyArrayHelper* getArrayHelper();
127 protected:
128 /** used to implement the creation of the array helper which is shared amongst all instances of the class.
129 This method needs to be implemented in derived classes.
130 <BR>
131 The method gets called with s_aMutex acquired.
132 @return a pointer to the newly created array helper. Must not be NULL.
134 virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const = 0;
137 template<class TYPE>
138 sal_Int32 OPropertyArrayUsageHelper< TYPE >::s_nRefCount = 0;
140 template<class TYPE>
141 ::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper< TYPE >::s_pProps = NULL;
143 template<class TYPE>
144 ::osl::Mutex OPropertyArrayUsageHelper< TYPE >::s_aMutex;
146 template <class TYPE>
147 OPropertyArrayUsageHelper<TYPE>::OPropertyArrayUsageHelper()
149 ::osl::MutexGuard aGuard(s_aMutex);
150 ++s_nRefCount;
153 template <class TYPE>
154 OPropertyArrayUsageHelper<TYPE>::~OPropertyArrayUsageHelper()
156 ::osl::MutexGuard aGuard(s_aMutex);
157 OSL_ENSURE(s_nRefCount > 0, "OPropertyArrayUsageHelper::~OPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
158 if (!--s_nRefCount)
160 delete s_pProps;
161 s_pProps = NULL;
165 template <class TYPE>
166 ::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper<TYPE>::getArrayHelper()
168 OSL_ENSURE(s_nRefCount, "OPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
169 if (!s_pProps)
171 ::osl::MutexGuard aGuard(s_aMutex);
172 if (!s_pProps)
174 s_pProps = createArrayHelper();
175 OSL_ENSURE(s_pProps, "OPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
178 return s_pProps;
181 class OBase_Mutex
183 public:
184 ::osl::Mutex m_aMutex;
187 namespace internal
189 template <class T>
190 void implCopySequence(const T* _pSource, T*& _pDest, sal_Int32 _nSourceLen)
192 for (sal_Int32 i=0; i<_nSourceLen; ++i, ++_pSource, ++_pDest)
193 *_pDest = *_pSource;
197 /// concat two sequences
198 template <class T>
199 ::com::sun::star::uno::Sequence<T> concatSequences(const ::com::sun::star::uno::Sequence<T>& _rLeft, const ::com::sun::star::uno::Sequence<T>& _rRight)
201 sal_Int32 nLeft(_rLeft.getLength()), nRight(_rRight.getLength());
202 const T* pLeft = _rLeft.getConstArray();
203 const T* pRight = _rRight.getConstArray();
205 sal_Int32 nReturnLen(nLeft + nRight);
206 ::com::sun::star::uno::Sequence<T> aReturn(nReturnLen);
207 T* pReturn = aReturn.getArray();
209 internal::implCopySequence(pLeft, pReturn, nLeft);
210 internal::implCopySequence(pRight, pReturn, nRight);
212 return aReturn;
216 #define DECLARE_SERVICE_INFO() \
217 virtual ::rtl::OUString SAL_CALL getImplementationName( ); \
218 virtual sal_Bool SAL_CALL supportsService( const ::rtl::OUString& ServiceName ) ; \
219 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames( ) \
221 #define IMPLEMENT_SERVICE_INFO(classname, implasciiname, serviceasciiname) \
222 ::rtl::OUString SAL_CALL classname::getImplementationName( ) \
224 return ::rtl::OUString::createFromAscii(implasciiname); \
226 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL classname::getSupportedServiceNames( ) \
228 ::com::sun::star::uno::Sequence< ::rtl::OUString > aSupported(1); \
229 aSupported[0] = ::rtl::OUString::createFromAscii(serviceasciiname); \
230 return aSupported; \
232 sal_Bool SAL_CALL classname::supportsService( const ::rtl::OUString& _rServiceName ) \
234 return cppu::supportsService(this, _rServiceName); \
240 #endif // INCLUDED_EXAMPLES_DATABASE_DRIVERSKELETON_OSUBCOMPONENT_HXX
242 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */