1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 #ifndef _COMPHELPER_PROPERTY_ARRAY_HELPER_HXX_
25 #define _COMPHELPER_PROPERTY_ARRAY_HELPER_HXX_
26 
27 #include <comphelper/stl_types.hxx>
28 #include <comphelper/propagg.hxx>
29 #include <cppuhelper/propshlp.hxx>
30 #include <osl/mutex.hxx>
31 #include <osl/diagnose.h>
32 #include <rtl/instance.hxx>
33 
34 namespace cppu {
35 	class IPropertyArrayHelper;
36 }
37 
38 //... namespace comphelper ................................................
39 namespace comphelper
40 {
41 //.........................................................................
42 
43 	namespace staruno	= ::com::sun::star::uno;
44 	namespace starbeans	= ::com::sun::star::beans;
45 
46 
47 //==================================================================
48 
49 template <typename TYPE> struct OPropertyArrayUsageHelperMutex
50 	: public rtl::Static< ::osl::Mutex, OPropertyArrayUsageHelperMutex<TYPE> > {};
51 
52 
53 template <class TYPE>
54 class OPropertyArrayUsageHelper
55 {
56 protected:
57 	static sal_Int32						s_nRefCount;
58 	static ::cppu::IPropertyArrayHelper*	s_pProps;
59 
60 public:
61 	OPropertyArrayUsageHelper();
~OPropertyArrayUsageHelper()62 	virtual ~OPropertyArrayUsageHelper()
63 	{	// ARGHHHHHHH ..... would like to implement this after the class
64         // definition (as we do with all other methods) but SUNPRO 5 compiler
65         // (linker) doesn't like this
66 		::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
67 		OSL_ENSURE(s_nRefCount > 0, "OPropertyArrayUsageHelper::~OPropertyArrayUsageHelper : suspicious call : have a refcount of 0 !");
68 		if (!--s_nRefCount)
69 		{
70 			delete s_pProps;
71 			s_pProps = NULL;
72 		}
73 	}
74 
75 	/** call this in the getInfoHelper method of your derived class. The method returns the array helper of the
76 		class, which is created if neccessary.
77 	*/
78 	::cppu::IPropertyArrayHelper*	getArrayHelper();
79 
80 protected:
81 	/** used to implement the creation of the array helper which is shared amongst all instances of the class.
82 		This method needs to be implemented in derived classes.
83 		<BR>
84 		The method gets called with Mutex acquired.
85 		<BR>
86 		as long as IPropertyArrayHelper has no virtual destructor, the implementation of ~OPropertyArrayUsageHelper
87 		assumes that you created an ::cppu::OPropertyArrayHelper when deleting s_pProps.
88 		@return							an pointer to the newly created array helper. Must not be NULL.
89 	*/
90 	virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const = 0;
91 };
92 
93 //==================================================================
94 /** a OPropertyArrayUsageHelper which will create an OPropertyArrayAggregationHelper
95 */
96 template <class TYPE>
97 class OAggregationArrayUsageHelper: public OPropertyArrayUsageHelper<TYPE>
98 {
99 protected:
100 	/** overwrite this in your derived class. initialize the two sequences with your and your aggregate's
101 		properties.
102 		<BR>
103 		The method gets called with Mutex acquired.
104 		@param		_rProps				out parameter to be filled with the property descriptions of your own class
105 		@param		_rAggregateProps	out parameter to be filled with the properties of your aggregate.
106 	*/
107 	virtual void fillProperties(
108 		::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property >& /* [out] */ _rProps,
109 		::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property >& /* [out] */ _rAggregateProps
110 		) const = 0;
111 
112 	/** creates an OPropertyArrayAggregationHelper filled with properties for which's initialization
113 		fillProperties is called. getInfoService and getFirstAggregateId may be overwritten to determine
114 		the additional parameters of the OPropertyArrayAggregationHelper.
115 	*/
116 	virtual ::cppu::IPropertyArrayHelper* createArrayHelper( ) const;
117 
118 	/** the return value is used for the construction of the OPropertyArrayAggregationHelper.
119 		Beware of the lifetime of the returned object, as it has to exist 'til the last instance
120 		of this class dies.
121 	*/
getInfoService() const122 	virtual IPropertyInfoService* getInfoService() const { return NULL; }
123 
124 	/** the return value is used for the construction of the OPropertyArrayAggregationHelper.
125 	*/
getFirstAggregateId() const126 	virtual sal_Int32 getFirstAggregateId() const { return DEFAULT_AGGREGATE_PROPERTY_ID; }
127 };
128 
129 //------------------------------------------------------------------
130 template<class TYPE>
131 sal_Int32						OPropertyArrayUsageHelper< TYPE >::s_nRefCount	= 0;
132 
133 template<class TYPE>
134 ::cppu::IPropertyArrayHelper*	OPropertyArrayUsageHelper< TYPE >::s_pProps	= NULL;
135 
136 //------------------------------------------------------------------
137 template <class TYPE>
OPropertyArrayUsageHelper()138 OPropertyArrayUsageHelper<TYPE>::OPropertyArrayUsageHelper()
139 {
140 	::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
141 	++s_nRefCount;
142 }
143 
144 //------------------------------------------------------------------
145 template <class TYPE>
getArrayHelper()146 ::cppu::IPropertyArrayHelper* OPropertyArrayUsageHelper<TYPE>::getArrayHelper()
147 {
148 	OSL_ENSURE(s_nRefCount, "OPropertyArrayUsageHelper::getArrayHelper : suspicious call : have a refcount of 0 !");
149 	if (!s_pProps)
150 	{
151 		::osl::MutexGuard aGuard(OPropertyArrayUsageHelperMutex<TYPE>::get());
152 		if (!s_pProps)
153 		{
154 			s_pProps = createArrayHelper();
155 			OSL_ENSURE(s_pProps, "OPropertyArrayUsageHelper::getArrayHelper : createArrayHelper returned nonsense !");
156 		}
157 	}
158 	return s_pProps;
159 }
160 
161 //------------------------------------------------------------------
162 template <class TYPE> inline
createArrayHelper() const163 ::cppu::IPropertyArrayHelper* OAggregationArrayUsageHelper<TYPE>::createArrayHelper() const
164 {
165 	::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property > aProps;
166 	::com::sun::star::uno::Sequence< ::com::sun::star::beans::Property > aAggregateProps;
167 	fillProperties(aProps, aAggregateProps);
168 	OSL_ENSURE(aProps.getLength(), "OAggregationArrayUsageHelper::createArrayHelper : fillProperties returned nonsense !");
169 	return new OPropertyArrayAggregationHelper(aProps, aAggregateProps, getInfoService(), getFirstAggregateId());
170 }
171 
172 //.........................................................................
173 }
174 //... namespace comphelper ................................................
175 
176 #endif // _COMPHELPER_PROPERTY_ARRAY_HELPER_HXX_
177 
178 
179