Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / mysqlc / source / mysqlc_subcomponent.hxx
blob85a5dc7d772101b3d3a37a6e0d894c5f1603ed59
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #ifndef _CONNECTIVITY_OSUBCOMPONENT_HXX_
21 #define _CONNECTIVITY_OSUBCOMPONENT_HXX_
23 #include <cppuhelper/weak.hxx>
24 #include <cppuhelper/interfacecontainer.h>
25 #include <com/sun/star/lang/DisposedException.hpp>
26 #include <cppuhelper/propshlp.hxx>
27 #include <osl/mutex.hxx>
28 #include <osl/diagnose.h>
30 namespace cppu {
31 class IPropertyArrayHelper;
34 namespace com
36 namespace sun
38 namespace star
40 namespace lang
42 class XComponent;
47 namespace connectivity
50 namespace mysqlc
52 void release(oslInterlockedCount& _refCount,
53 ::cppu::OBroadcastHelper& rBHelper,
54 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xInterface,
55 ::com::sun::star::lang::XComponent* _pObject);
57 void checkDisposed(sal_Bool _bThrow) throw (::com::sun::star::lang::DisposedException);
58 //************************************************************
59 // OSubComponent
60 //************************************************************
61 template <class SELF, class WEAK> class OSubComponent
63 protected:
64 // the parent must support the tunnel implementation
65 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface > m_xParent;
66 SELF* m_pDerivedImplementation;
68 public:
69 OSubComponent(
70 const ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >& _xParent,
71 SELF* _pDerivedImplementation)
72 :m_xParent(_xParent)
73 ,m_pDerivedImplementation(_pDerivedImplementation)
77 protected:
78 void dispose_ChildImpl()
80 ::osl::MutexGuard aGuard(m_pDerivedImplementation->rBHelper.rMutex);
81 m_xParent = NULL;
83 void relase_ChildImpl()
85 release(m_pDerivedImplementation->m_refCount,
86 m_pDerivedImplementation->rBHelper,
87 m_xParent,
88 m_pDerivedImplementation);
90 m_pDerivedImplementation->WEAK::release();
95 template <class TYPE>
96 class OPropertyArrayUsageHelper
98 protected:
99 static sal_Int32 s_nRefCount;
100 static ::cppu::IPropertyArrayHelper* s_pProps;
101 static ::osl::Mutex s_aMutex;
103 public:
104 OPropertyArrayUsageHelper();
105 virtual ~OPropertyArrayUsageHelper()
106 { // ARGHHHHHHH ..... would like to implement this in proparrhlp_impl.hxx (as we do with all other methods)
107 // but SUNPRO 5 compiler (linker) doesn't like this
108 ::osl::MutexGuard aGuard(s_aMutex);
109 OSL_ENSURE(s_nRefCount > 0, "OPropertyArrayUsageHelper::~OPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
110 if (!--s_nRefCount)
112 delete s_pProps;
113 s_pProps = NULL;
117 /** call this in the getInfoHelper method of your derived class. The method returns the array helper of the
118 class, which is created if neccessary.
120 ::cppu::IPropertyArrayHelper* getArrayHelper();
122 protected:
123 /** used to implement the creation of the array helper which is shared amongst all instances of the class.
124 This method needs to be implemented in derived classes.
125 <BR>
126 The method gets called with s_aMutex acquired.
127 <BR>
128 as long as IPropertyArrayHelper has no virtual destructor, the implementation of ~OPropertyArrayUsageHelper
129 assumes that you created an ::cppu::OPropertyArrayHelper when deleting s_pProps.
130 @return an pointer to the newly created array helper. Must not be NULL.
132 virtual ::cppu::IPropertyArrayHelper* createArrayHelper() const = 0;
135 template<class TYPE>
136 sal_Int32 OPropertyArrayUsageHelper< TYPE >::s_nRefCount = 0;
138 template<class TYPE>
139 ::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper< TYPE >::s_pProps = NULL;
141 template<class TYPE>
142 ::osl::Mutex OPropertyArrayUsageHelper< TYPE >::s_aMutex;
144 //------------------------------------------------------------------
145 template <class TYPE>
146 OPropertyArrayUsageHelper<TYPE>::OPropertyArrayUsageHelper()
148 ::osl::MutexGuard aGuard(s_aMutex);
149 ++s_nRefCount;
152 //------------------------------------------------------------------
153 template <class TYPE>
154 ::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper<TYPE>::getArrayHelper()
156 OSL_ENSURE(s_nRefCount, "OPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
157 if (!s_pProps) {
158 ::osl::MutexGuard aGuard(s_aMutex);
159 if (!s_pProps) {
160 s_pProps = createArrayHelper();
161 OSL_ENSURE(s_pProps, "OPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
164 return s_pProps;
168 class OBase_Mutex
170 public:
171 ::osl::Mutex m_aMutex;
174 namespace internal
176 template <class T>
177 void implCopySequence(const T* _pSource, T*& _pDest, sal_Int32 _nSourceLen)
179 for (sal_Int32 i=0; i<_nSourceLen; ++i, ++_pSource, ++_pDest)
180 *_pDest = *_pSource;
183 //-------------------------------------------------------------------------
184 /// concat two sequences
185 template <class T>
186 ::com::sun::star::uno::Sequence<T> concatSequences(const ::com::sun::star::uno::Sequence<T>& _rLeft, const ::com::sun::star::uno::Sequence<T>& _rRight)
188 sal_Int32 nLeft(_rLeft.getLength()), nRight(_rRight.getLength());
189 const T* pLeft = _rLeft.getConstArray();
190 const T* pRight = _rRight.getConstArray();
192 sal_Int32 nReturnLen(nLeft + nRight);
193 ::com::sun::star::uno::Sequence<T> aReturn(nReturnLen);
194 T* pReturn = aReturn.getArray();
196 internal::implCopySequence(pLeft, pReturn, nLeft);
197 internal::implCopySequence(pRight, pReturn, nRight);
199 return aReturn;
203 #define DECLARE_SERVICE_INFO() \
204 virtual ::rtl::OUString SAL_CALL getImplementationName() throw (::com::sun::star::uno::RuntimeException); \
205 virtual sal_Bool SAL_CALL supportsService(const ::rtl::OUString& ServiceName) throw(::com::sun::star::uno::RuntimeException); \
206 virtual ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException) \
208 #define IMPLEMENT_SERVICE_INFO(classname, implasciiname, serviceasciiname) \
209 ::rtl::OUString SAL_CALL classname::getImplementationName() throw (::com::sun::star::uno::RuntimeException) \
211 return ::rtl::OUString::createFromAscii(implasciiname); \
213 ::com::sun::star::uno::Sequence< ::rtl::OUString > SAL_CALL classname::getSupportedServiceNames() throw(::com::sun::star::uno::RuntimeException) \
215 ::com::sun::star::uno::Sequence< ::rtl::OUString > aSupported(1); \
216 aSupported[0] = ::rtl::OUString::createFromAscii(serviceasciiname); \
217 return aSupported; \
219 sal_Bool SAL_CALL classname::supportsService(const ::rtl::OUString& _rServiceName) throw(::com::sun::star::uno::RuntimeException) \
221 Sequence< ::rtl::OUString > aSupported(getSupportedServiceNames()); \
222 const ::rtl::OUString* pSupported = aSupported.getConstArray(); \
223 const ::rtl::OUString* pEnd = pSupported + aSupported.getLength(); \
224 for (;pSupported != pEnd && !pSupported->equals(_rServiceName); ++pSupported) \
226 return pSupported != pEnd; \
232 #endif // _CONNECTIVITY_OSUBCOMPONENT_HXX_
234 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */