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