Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef _COMPHELPER_PROPERTY_HXX_
21 : #define _COMPHELPER_PROPERTY_HXX_
22 :
23 : #include <cppuhelper/proptypehlp.hxx>
24 : #include <comphelper/extract.hxx>
25 : #include <com/sun/star/beans/Property.hpp>
26 : #include <com/sun/star/beans/XPropertySet.hpp>
27 : #include <functional>
28 : #include "comphelper/comphelperdllapi.h"
29 : #include "cppu/unotype.hxx"
30 :
31 : //=========================================================================
32 : //= property helper classes
33 : //=========================================================================
34 :
35 : //... namespace comphelper .......................................................
36 : namespace comphelper
37 : {
38 : //.........................................................................
39 :
40 : namespace starbeans = ::com::sun::star::beans;
41 : namespace staruno = ::com::sun::star::uno;
42 :
43 : /** compare two properties by name
44 : */
45 : //--------------------------------------------------------------------------
46 : // comparing two property instances
47 : struct PropertyCompareByName : public ::std::binary_function< ::com::sun::star::beans::Property, ::com::sun::star::beans::Property, bool >
48 : {
49 7859 : bool operator() (const ::com::sun::star::beans::Property& x, const ::com::sun::star::beans::Property& y) const
50 : {
51 7859 : return x.Name.compareTo(y.Name) < 0;// ? true : false;
52 : }
53 : };
54 :
55 : //--------------------------------------------------------------------------
56 : /** compare two properties by name
57 : */
58 : struct PropertyStringEqualFunctor : ::std::binary_function< ::com::sun::star::beans::Property, ::rtl::OUString, bool >
59 : {
60 : // ................................................................
61 : inline bool operator()( const ::com::sun::star::beans::Property& lhs, const ::rtl::OUString& rhs ) const
62 : {
63 : return lhs.Name.compareTo(rhs) == 0;
64 : }
65 : // ................................................................
66 : inline bool operator()( const ::rtl::OUString& lhs, const ::com::sun::star::beans::Property& rhs ) const
67 : {
68 : return lhs.compareTo(rhs.Name) == 0;
69 : }
70 : };
71 : //--------------------------------------------------------------------------
72 : // comparing two property instances
73 : struct PropertyEqualByName : public ::std::binary_function< ::com::sun::star::beans::Property, ::com::sun::star::beans::Property, bool >
74 : {
75 : bool operator() (const ::com::sun::star::beans::Property& x, const ::com::sun::star::beans::Property& y) const
76 : {
77 : return x.Name.compareTo(y.Name) == 0;
78 : }
79 : };
80 :
81 : //------------------------------------------------------------------
82 : /// remove the property with the given name from the given sequence
83 : COMPHELPER_DLLPUBLIC void RemoveProperty(staruno::Sequence<starbeans::Property>& seqProps, const ::rtl::OUString& _rPropName);
84 :
85 : //------------------------------------------------------------------
86 : /** within the given property sequence, modify attributes of a special property
87 : @param _rProps the sequence of properties to search in
88 : @param _sPropName the name of the property which's attributes should be modified
89 : @param _nAddAttrib the attributes which should be added
90 : @param _nRemoveAttrib the attributes which should be removed
91 : */
92 : COMPHELPER_DLLPUBLIC void ModifyPropertyAttributes(staruno::Sequence<starbeans::Property>& _rProps, const ::rtl::OUString& _sPropName, sal_Int16 _nAddAttrib, sal_Int16 _nRemoveAttrib);
93 :
94 : //------------------------------------------------------------------
95 : /** check if the given set has the given property.
96 : */
97 : COMPHELPER_DLLPUBLIC sal_Bool hasProperty(const rtl::OUString& _rName, const staruno::Reference<starbeans::XPropertySet>& _rxSet);
98 :
99 : //------------------------------------------------------------------
100 : /** copy properties between property sets, in compliance with the property
101 : attributes of the target object
102 : */
103 : COMPHELPER_DLLPUBLIC void copyProperties(const staruno::Reference<starbeans::XPropertySet>& _rxSource,
104 : const staruno::Reference<starbeans::XPropertySet>& _rxDest);
105 :
106 : //==================================================================
107 : //= property conversion helpers
108 : //==================================================================
109 :
110 : /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue
111 : @param _rConvertedValue the conversion result (if successfull)
112 : @param _rOldValue the old value of the property, calculated from _rCurrentValue
113 : @param _rValueToSet the new value which is about to be set
114 : @param _rCurrentValue the current value of the property
115 : @return sal_True, if the value could be converted and has changed
116 : sal_False, if the value could be converted and has not changed
117 : @exception InvalidArgumentException thrown if the value could not be converted to the requested type (which is the template argument)
118 : */
119 : template <typename T>
120 51 : sal_Bool tryPropertyValue(staruno::Any& /*out*/_rConvertedValue, staruno::Any& /*out*/_rOldValue, const staruno::Any& _rValueToSet, const T& _rCurrentValue)
121 : {
122 51 : sal_Bool bModified(sal_False);
123 51 : T aNewValue = T();
124 51 : ::cppu::convertPropertyValue(aNewValue, _rValueToSet);
125 51 : if (aNewValue != _rCurrentValue)
126 : {
127 35 : _rConvertedValue <<= aNewValue;
128 35 : _rOldValue <<= _rCurrentValue;
129 35 : bModified = sal_True;
130 : }
131 51 : return bModified;
132 : }
133 :
134 : /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue for enum values
135 : @param _rConvertedValue the conversion result (if successfull)
136 : @param _rOldValue the old value of the property, calculated from _rCurrentValue
137 : @param _rValueToSet the new value which is about to be set
138 : @param _rCurrentValue the current value of the property
139 : @return sal_True, if the value could be converted and has changed
140 : sal_False, if the value could be converted and has not changed
141 : @exception InvalidArgumentException thrown if the value could not be converted to the requested type (which is the template argument)
142 : */
143 : template <class ENUMTYPE>
144 2 : sal_Bool tryPropertyValueEnum(staruno::Any& /*out*/_rConvertedValue, staruno::Any& /*out*/_rOldValue, const staruno::Any& _rValueToSet, const ENUMTYPE& _rCurrentValue)
145 : {
146 2 : if (cppu::getTypeFavourUnsigned(&_rCurrentValue).getTypeClass()
147 : != staruno::TypeClass_ENUM)
148 0 : return tryPropertyValue(_rConvertedValue, _rOldValue, _rValueToSet, _rCurrentValue);
149 :
150 2 : sal_Bool bModified(sal_False);
151 : ENUMTYPE aNewValue;
152 2 : ::cppu::any2enum(aNewValue, _rValueToSet);
153 : // will throw an exception if not convertible
154 :
155 2 : if (aNewValue != _rCurrentValue)
156 : {
157 0 : _rConvertedValue <<= aNewValue;
158 0 : _rOldValue <<= _rCurrentValue;
159 0 : bModified = sal_True;
160 : }
161 2 : return bModified;
162 : }
163 :
164 : /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue for boolean properties
165 : @param _rConvertedValue the conversion result (if successfull)
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 a boolean type
172 : */
173 7 : inline sal_Bool tryPropertyValue(staruno::Any& /*out*/_rConvertedValue, staruno::Any& /*out*/_rOldValue, const staruno::Any& _rValueToSet, sal_Bool _bCurrentValue)
174 : {
175 7 : sal_Bool bModified(sal_False);
176 7 : sal_Bool bNewValue(sal_False);
177 7 : ::cppu::convertPropertyValue(bNewValue, _rValueToSet);
178 7 : if (bNewValue != _bCurrentValue)
179 : {
180 5 : _rConvertedValue.setValue(&bNewValue, ::getBooleanCppuType());
181 5 : _rOldValue.setValue(&_bCurrentValue, ::getBooleanCppuType());
182 5 : bModified = sal_True;
183 : }
184 7 : return bModified;
185 : }
186 :
187 : /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue
188 : @param _rConvertedValue the conversion result (if successfull)
189 : @param _rOldValue the old value of the property, calculated from _rCurrentValue
190 : @param _rValueToSet the new value which is about to be set
191 : @param _rCurrentValue the current value of the property
192 : @param _rExpectedType the type which the property should have (if not void)
193 : @return sal_True, if the value could be converted and has changed
194 : sal_False, if the value could be converted and has not changed
195 : @exception InvalidArgumentException thrown if the value could not be converted to the requested type (which is the template argument)
196 : */
197 : COMPHELPER_DLLPUBLIC sal_Bool tryPropertyValue(staruno::Any& _rConvertedValue, staruno::Any& _rOldValue, const staruno::Any& _rValueToSet, const staruno::Any& _rCurrentValue, const staruno::Type& _rExpectedType);
198 :
199 : //.........................................................................
200 : }
201 : //... namespace comphelper .......................................................
202 :
203 : #endif // _COMPHELPER_PROPERTY_HXX_
204 :
205 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|