Update ooo320-m1
[ooovba.git] / comphelper / inc / comphelper / proparrhlp.hxx
blob9691d6146edd98eca0af5ccffa77de6c241e5ab2
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: proparrhlp.hxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #ifndef _COMPHELPER_PROPERTY_ARRAY_HELPER_HXX_
32 #define _COMPHELPER_PROPERTY_ARRAY_HELPER_HXX_
34 #include <comphelper/stl_types.hxx>
35 #include <comphelper/propagg.hxx>
36 #include <cppuhelper/propshlp.hxx>
37 #include <osl/mutex.hxx>
38 #include <osl/diagnose.h>
39 #include <rtl/instance.hxx>
41 namespace cppu {
42 class IPropertyArrayHelper;
45 //... namespace comphelper ................................................
46 namespace comphelper
48 //.........................................................................
50 namespace staruno = ::com::sun::star::uno;
51 namespace starbeans = ::com::sun::star::beans;
54 //==================================================================
56 template <typename TYPE> struct OPropertyArrayUsageHelperMutex
57 : public rtl::Static< ::osl::Mutex, OPropertyArrayUsageHelperMutex<TYPE> > {};
60 template <class TYPE>
61 class OPropertyArrayUsageHelper
63 protected:
64 static sal_Int32 s_nRefCount;
65 static ::cppu::IPropertyArrayHelper* s_pProps;
67 public:
68 OPropertyArrayUsageHelper();
69 virtual ~OPropertyArrayUsageHelper()
70 { // ARGHHHHHHH ..... would like to implement this after the class
71 // definition (as we do with all other methods) but SUNPRO 5 compiler
72 // (linker) doesn't like this
73 ::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
74 OSL_ENSURE(s_nRefCount > 0, "OPropertyArrayUsageHelper::~OPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
75 if (!--s_nRefCount)
77 delete s_pProps;
78 s_pProps = NULL;
82 /** call this in the getInfoHelper method of your derived class. The method returns the array helper of the
83 class, which is created if neccessary.
85 ::cppu::IPropertyArrayHelper* getArrayHelper();
87 protected:
88 /** used to implement the creation of the array helper which is shared amongst all instances of the class.
89 This method needs to be implemented in derived classes.
90 <BR>
91 The method gets called with Mutex acquired.
92 <BR>
93 as long as IPropertyArrayHelper has no virtual destructor, the implementation of ~OPropertyArrayUsageHelper
94 assumes that you created an ::cppu::OPropertyArrayHelper when deleting s_pProps.
95 @return an pointer to the newly created array helper. Must not be NULL.
97 virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const = 0;
100 //==================================================================
101 /** a OPropertyArrayUsageHelper which will create an OPropertyArrayAggregationHelper
103 template <class TYPE>
104 class OAggregationArrayUsageHelper: public OPropertyArrayUsageHelper<TYPE>
106 protected:
107 /** overwrite this in your derived class. initialize the two sequences with your and your aggregate's
108 properties.
109 <BR>
110 The method gets called with Mutex acquired.
111 @param _rProps out parameter to be filled with the property descriptions of your own class
112 @param _rAggregateProps out parameter to be filled with the properties of your aggregate.
114 virtual void fillProperties(
115 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property >& /* [out] */ _rProps,
116 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property >& /* [out] */ _rAggregateProps
117 ) const = 0;
119 /** creates an OPropertyArrayAggregationHelper filled with properties for which's initialization
120 fillProperties is called. getInfoService and getFirstAggregateId may be overwritten to determine
121 the additional parameters of the OPropertyArrayAggregationHelper.
123 virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const;
125 /** the return value is used for the construction of the OPropertyArrayAggregationHelper.
126 Beware of the lifetime of the returned object, as it has to exist 'til the last instance
127 of this class dies.
129 virtual IPropertyInfoService* getInfoService() const { return NULL; }
131 /** the return value is used for the construction of the OPropertyArrayAggregationHelper.
133 virtual sal_Int32 getFirstAggregateId() const { return DEFAULT_AGGREGATE_PROPERTY_ID; }
136 //------------------------------------------------------------------
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 //------------------------------------------------------------------
144 template <class TYPE>
145 OPropertyArrayUsageHelper<TYPE>::OPropertyArrayUsageHelper()
147 ::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
148 ++s_nRefCount;
151 //------------------------------------------------------------------
152 template <class TYPE>
153 ::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper<TYPE>::getArrayHelper()
155 OSL_ENSURE(s_nRefCount, "OPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
156 if (!s_pProps)
158 ::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
159 if (!s_pProps)
161 s_pProps = createArrayHelper();
162 OSL_ENSURE(s_pProps, "OPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
165 return s_pProps;
168 //------------------------------------------------------------------
169 template <class TYPE> inline
170 ::cppu::IPropertyArrayHelper* OAggregationArrayUsageHelper<TYPE>::createArrayHelper() const
172 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property > aProps;
173 ::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property > aAggregateProps;
174 fillProperties(aProps, aAggregateProps);
175 OSL_ENSURE(aProps.getLength(), "OAggregationArrayUsageHelper::createArrayHelper : fillProperties returned nonsense !");
176 return new OPropertyArrayAggregationHelper(aProps, aAggregateProps, getInfoService(), getFirstAggregateId());
179 //.........................................................................
181 //... namespace comphelper ................................................
183 #endif // _COMPHELPER_PROPERTY_ARRAY_HELPER_HXX_