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_HXX_
25 #define _COMPHELPER_PROPERTY_HXX_
26 
27 #include <cppuhelper/proptypehlp.hxx>
28 #include <comphelper/extract.hxx>
29 #include <com/sun/star/beans/Property.hpp>
30 #include <com/sun/star/beans/XPropertySet.hpp>
31 #include <functional>
32 #include "comphelper/comphelperdllapi.h"
33 #include "cppu/unotype.hxx"
34 
35 //=========================================================================
36 //= property helper classes
37 //=========================================================================
38 
39 //... namespace comphelper .......................................................
40 namespace comphelper
41 {
42 //.........................................................................
43 
44 	namespace starbeans	= ::com::sun::star::beans;
45 	namespace staruno	= ::com::sun::star::uno;
46 
47 /** compare two properties by name
48 */
49 	struct PropertyStringLessFunctor : ::std::binary_function< ::com::sun::star::beans::Property, ::rtl::OUString, bool >
50 	{
51 		// ................................................................
operator ()comphelper::PropertyStringLessFunctor52 		inline bool operator()( const ::com::sun::star::beans::Property& lhs, const ::rtl::OUString& rhs ) const
53 		{
54 			return lhs.Name.compareTo(rhs) < 0;
55 		}
56 		// ................................................................
operator ()comphelper::PropertyStringLessFunctor57 		inline bool operator()( const ::rtl::OUString& lhs, const ::com::sun::star::beans::Property& rhs ) const
58 		{
59 			return lhs.compareTo(rhs.Name) < 0;
60 		}
61 	};
62 	//--------------------------------------------------------------------------
63 	// comparing two property instances
64 	struct PropertyCompareByName : public ::std::binary_function< ::com::sun::star::beans::Property, ::com::sun::star::beans::Property, bool >
65 	{
operator ()comphelper::PropertyCompareByName66 		bool operator() (const ::com::sun::star::beans::Property& x, const ::com::sun::star::beans::Property& y) const
67 		{
68 			return x.Name.compareTo(y.Name) < 0;// ? true : false;
69 		}
70 	};
71 
72 	//--------------------------------------------------------------------------
73     /** compare two properties by name
74      */
75 	struct PropertyStringEqualFunctor : ::std::binary_function< ::com::sun::star::beans::Property, ::rtl::OUString, bool >
76 	{
77 		// ................................................................
operator ()comphelper::PropertyStringEqualFunctor78 		inline bool operator()( const ::com::sun::star::beans::Property& lhs, const ::rtl::OUString& rhs ) const
79 		{
80 			return lhs.Name.compareTo(rhs) == 0;
81 		}
82 		// ................................................................
operator ()comphelper::PropertyStringEqualFunctor83 		inline bool operator()( const ::rtl::OUString& lhs, const ::com::sun::star::beans::Property& rhs ) const
84 		{
85 			return lhs.compareTo(rhs.Name) == 0;
86 		}
87 	};
88 	//--------------------------------------------------------------------------
89 	// comparing two property instances
90 	struct PropertyEqualByName : public ::std::binary_function< ::com::sun::star::beans::Property, ::com::sun::star::beans::Property, bool >
91 	{
operator ()comphelper::PropertyEqualByName92 		bool operator() (const ::com::sun::star::beans::Property& x, const ::com::sun::star::beans::Property& y) const
93 		{
94 			return x.Name.compareTo(y.Name) == 0;
95 		}
96 	};
97 
98 //------------------------------------------------------------------
99 /** find property with given name
100 
101     @param o_rProp
102     Output parameter receiving the property, if found
103 
104     @param i_rPropName
105     Name of the property to find
106 
107     @return false, if property was not found
108  */
109 COMPHELPER_DLLPUBLIC bool findProperty(starbeans::Property& o_rProp, staruno::Sequence<starbeans::Property>& i_seqProps, const ::rtl::OUString& i_rPropName);
110 
111 //------------------------------------------------------------------
112 /// remove the property with the given name from the given sequence
113 COMPHELPER_DLLPUBLIC void RemoveProperty(staruno::Sequence<starbeans::Property>& seqProps, const ::rtl::OUString& _rPropName);
114 
115 //------------------------------------------------------------------
116 /** within the given property sequence, modify attributes of a special property
117 	@param	_rProps			the sequence of properties to search in
118 	@param	_sPropName		the name of the property which's attributes should be modified
119 	@param	_nAddAttrib		the attributes which should be added
120 	@param	_nRemoveAttrib	the attributes which should be removed
121 */
122 COMPHELPER_DLLPUBLIC void ModifyPropertyAttributes(staruno::Sequence<starbeans::Property>& _rProps, const ::rtl::OUString& _sPropName, sal_Int16 _nAddAttrib, sal_Int16 _nRemoveAttrib);
123 
124 //------------------------------------------------------------------
125 /**	check if the given set has the given property.
126 */
127 COMPHELPER_DLLPUBLIC sal_Bool hasProperty(const rtl::OUString& _rName, const staruno::Reference<starbeans::XPropertySet>& _rxSet);
128 
129 //------------------------------------------------------------------
130 /**	copy properties between property sets, in compliance with the property
131 	attributes of the target object
132 */
133 COMPHELPER_DLLPUBLIC void copyProperties(const staruno::Reference<starbeans::XPropertySet>& _rxSource,
134 					const staruno::Reference<starbeans::XPropertySet>& _rxDest);
135 
136 //==================================================================
137 //= property conversion helpers
138 //==================================================================
139 
140 /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue
141 	@param			_rConvertedValue	the conversion result (if successful)
142 	@param			_rOldValue			the old value of the property, calculated from _rCurrentValue
143 	@param			_rValueToSet		the new value which is about to be set
144 	@param			_rCurrentValue		the current value of the property
145 	@return			sal_True, if the value could be converted and has changed
146 					sal_False, if the value could be converted and has not changed
147 	@exception		InvalidArgumentException thrown if the value could not be converted to the requested type (which is the template argument)
148 */
149 template <typename T>
tryPropertyValue(staruno::Any & _rConvertedValue,staruno::Any & _rOldValue,const staruno::Any & _rValueToSet,const T & _rCurrentValue)150 sal_Bool tryPropertyValue(staruno::Any& /*out*/_rConvertedValue, staruno::Any& /*out*/_rOldValue, const staruno::Any& _rValueToSet, const T& _rCurrentValue)
151 {
152 	sal_Bool bModified(sal_False);
153 	T aNewValue = T();
154 	::cppu::convertPropertyValue(aNewValue, _rValueToSet);
155 	if (aNewValue != _rCurrentValue)
156 	{
157 		_rConvertedValue <<= aNewValue;
158 		_rOldValue <<= _rCurrentValue;
159 		bModified = sal_True;
160 	}
161 	return bModified;
162 }
163 
164 /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue for enum values
165 	@param			_rConvertedValue	the conversion result (if successful)
166 	@param			_rOldValue			the old value of the property, calculated from _rCurrentValue
167 	@param			_rValueToSet		the new value which is about to be set
168 	@param			_rCurrentValue		the current value of the property
169 	@return			sal_True, if the value could be converted and has changed
170 					sal_False, if the value could be converted and has not changed
171 	@exception		InvalidArgumentException thrown if the value could not be converted to the requested type (which is the template argument)
172 */
173 template <class ENUMTYPE>
tryPropertyValueEnum(staruno::Any & _rConvertedValue,staruno::Any & _rOldValue,const staruno::Any & _rValueToSet,const ENUMTYPE & _rCurrentValue)174 sal_Bool tryPropertyValueEnum(staruno::Any& /*out*/_rConvertedValue, staruno::Any& /*out*/_rOldValue, const staruno::Any& _rValueToSet, const ENUMTYPE& _rCurrentValue)
175 {
176     if (cppu::getTypeFavourUnsigned(&_rCurrentValue).getTypeClass()
177         != staruno::TypeClass_ENUM)
178 		return tryPropertyValue(_rConvertedValue, _rOldValue, _rValueToSet, _rCurrentValue);
179 
180 	sal_Bool bModified(sal_False);
181 	ENUMTYPE aNewValue;
182 	::cppu::any2enum(aNewValue, _rValueToSet);
183 		// will throw an exception if not convertible
184 
185 	if (aNewValue != _rCurrentValue)
186 	{
187 		_rConvertedValue <<= aNewValue;
188 		_rOldValue <<= _rCurrentValue;
189 		bModified = sal_True;
190 	}
191 	return bModified;
192 }
193 
194 /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue for boolean properties
195 	@param			_rConvertedValue	the conversion result (if successful)
196 	@param			_rOldValue			the old value of the property, calculated from _rCurrentValue
197 	@param			_rValueToSet		the new value which is about to be set
198 	@param			_rCurrentValue		the current value of the property
199 	@return			sal_True, if the value could be converted and has changed
200 					sal_False, if the value could be converted and has not changed
201 	@exception		InvalidArgumentException thrown if the value could not be converted to a boolean type
202 */
tryPropertyValue(staruno::Any & _rConvertedValue,staruno::Any & _rOldValue,const staruno::Any & _rValueToSet,sal_Bool _bCurrentValue)203 inline sal_Bool tryPropertyValue(staruno::Any& /*out*/_rConvertedValue, staruno::Any& /*out*/_rOldValue, const staruno::Any& _rValueToSet, sal_Bool _bCurrentValue)
204 {
205 	sal_Bool bModified(sal_False);
206 	sal_Bool bNewValue(sal_False);
207 	::cppu::convertPropertyValue(bNewValue, _rValueToSet);
208 	if (bNewValue != _bCurrentValue)
209 	{
210 		_rConvertedValue.setValue(&bNewValue, ::getBooleanCppuType());
211 		_rOldValue.setValue(&_bCurrentValue, ::getBooleanCppuType());
212 		bModified = sal_True;
213 	}
214 	return bModified;
215 }
216 
217 /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue
218 	@param			_rConvertedValue	the conversion result (if successful)
219 	@param			_rOldValue			the old value of the property, calculated from _rCurrentValue
220 	@param			_rValueToSet		the new value which is about to be set
221 	@param			_rCurrentValue		the current value of the property
222 	@param			_rExpectedType		the type which the property should have (if not void)
223 	@return			sal_True, if the value could be converted and has changed
224 					sal_False, if the value could be converted and has not changed
225 	@exception		InvalidArgumentException thrown if the value could not be converted to the requested type (which is the template argument)
226 */
227 COMPHELPER_DLLPUBLIC sal_Bool tryPropertyValue(staruno::Any& _rConvertedValue, staruno::Any& _rOldValue, const staruno::Any& _rValueToSet, const staruno::Any& _rCurrentValue, const staruno::Type& _rExpectedType);
228 
229 //.........................................................................
230 }
231 //... namespace comphelper .......................................................
232 
233 #endif // _COMPHELPER_PROPERTY_HXX_
234 
235