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 INCLUDED_COMPHELPER_PROPERTY_HXX
21 : #define INCLUDED_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 : /** compare two properties by name
40 : */
41 :
42 : // comparing two property instances
43 : struct PropertyCompareByName : public ::std::binary_function< ::com::sun::star::beans::Property, ::com::sun::star::beans::Property, bool >
44 : {
45 2346213 : bool operator() (const ::com::sun::star::beans::Property& x, const ::com::sun::star::beans::Property& y) const
46 : {
47 2346213 : return x.Name.compareTo(y.Name) < 0;
48 : }
49 : };
50 :
51 :
52 : /** compare two properties by name
53 : */
54 : struct PropertyStringEqualFunctor : ::std::binary_function< ::com::sun::star::beans::Property, OUString, bool >
55 : {
56 :
57 : inline bool operator()( const ::com::sun::star::beans::Property& lhs, const OUString& rhs ) const
58 : {
59 : return lhs.Name == rhs ;
60 : }
61 :
62 : inline bool operator()( const OUString& lhs, const ::com::sun::star::beans::Property& rhs ) const
63 : {
64 : return lhs == rhs.Name ;
65 : }
66 : };
67 :
68 : // comparing two property instances
69 : struct PropertyEqualByName : public ::std::binary_function< ::com::sun::star::beans::Property, ::com::sun::star::beans::Property, bool >
70 : {
71 : bool operator() (const ::com::sun::star::beans::Property& x, const ::com::sun::star::beans::Property& y) const
72 : {
73 : return x.Name == y.Name ;
74 : }
75 : };
76 :
77 :
78 : /// remove the property with the given name from the given sequence
79 : COMPHELPER_DLLPUBLIC void RemoveProperty(css::uno::Sequence<css::beans::Property>& seqProps, const OUString& _rPropName);
80 :
81 :
82 : /** within the given property sequence, modify attributes of a special property
83 : @param _rProps the sequence of properties to search in
84 : @param _sPropName the name of the property which's attributes should be modified
85 : @param _nAddAttrib the attributes which should be added
86 : @param _nRemoveAttrib the attributes which should be removed
87 : */
88 : COMPHELPER_DLLPUBLIC void ModifyPropertyAttributes(css::uno::Sequence<css::beans::Property>& _rProps, const OUString& _sPropName, sal_Int16 _nAddAttrib, sal_Int16 _nRemoveAttrib);
89 :
90 :
91 : /** check if the given set has the given property.
92 : */
93 : COMPHELPER_DLLPUBLIC bool hasProperty(const OUString& _rName, const css::uno::Reference<css::beans::XPropertySet>& _rxSet);
94 :
95 :
96 : /** copy properties between property sets, in compliance with the property
97 : attributes of the target object
98 : */
99 : COMPHELPER_DLLPUBLIC void copyProperties(const css::uno::Reference<css::beans::XPropertySet>& _rxSource,
100 : const css::uno::Reference<css::beans::XPropertySet>& _rxDest);
101 :
102 :
103 : //= property conversion helpers
104 :
105 :
106 : /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue
107 : @param _rConvertedValue the conversion result (if successful)
108 : @param _rOldValue the old value of the property, calculated from _rCurrentValue
109 : @param _rValueToSet the new value which is about to be set
110 : @param _rCurrentValue the current value of the property
111 : @return sal_True, if the value could be converted and has changed
112 : sal_False, if the value could be converted and has not changed
113 : @exception InvalidArgumentException thrown if the value could not be converted to the requested type (which is the template argument)
114 : */
115 : template <typename T>
116 14301 : bool tryPropertyValue(css::uno::Any& /*out*/_rConvertedValue, css::uno::Any& /*out*/_rOldValue, const css::uno::Any& _rValueToSet, const T& _rCurrentValue)
117 : {
118 14301 : bool bModified(false);
119 14301 : T aNewValue = T();
120 14301 : ::cppu::convertPropertyValue(aNewValue, _rValueToSet);
121 14297 : if (aNewValue != _rCurrentValue)
122 : {
123 4573 : _rConvertedValue <<= aNewValue;
124 4573 : _rOldValue <<= _rCurrentValue;
125 4573 : bModified = true;
126 : }
127 14297 : return bModified;
128 : }
129 :
130 : /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue for enum values
131 : @param _rConvertedValue the conversion result (if successful)
132 : @param _rOldValue the old value of the property, calculated from _rCurrentValue
133 : @param _rValueToSet the new value which is about to be set
134 : @param _rCurrentValue the current value of the property
135 : @return sal_True, if the value could be converted and has changed
136 : sal_False, if the value could be converted and has not changed
137 : @exception InvalidArgumentException thrown if the value could not be converted to the requested type (which is the template argument)
138 : */
139 : template <class ENUMTYPE>
140 316 : bool tryPropertyValueEnum(css::uno::Any& /*out*/_rConvertedValue, css::uno::Any& /*out*/_rOldValue, const css::uno::Any& _rValueToSet, const ENUMTYPE& _rCurrentValue)
141 : {
142 632 : if (cppu::getTypeFavourUnsigned(&_rCurrentValue).getTypeClass()
143 316 : != css::uno::TypeClass_ENUM)
144 0 : return tryPropertyValue(_rConvertedValue, _rOldValue, _rValueToSet, _rCurrentValue);
145 :
146 316 : bool bModified(false);
147 : ENUMTYPE aNewValue;
148 316 : ::cppu::any2enum(aNewValue, _rValueToSet);
149 : // will throw an exception if not convertible
150 :
151 316 : if (aNewValue != _rCurrentValue)
152 : {
153 76 : _rConvertedValue <<= aNewValue;
154 76 : _rOldValue <<= _rCurrentValue;
155 76 : bModified = true;
156 : }
157 316 : return bModified;
158 : }
159 :
160 : /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue for boolean properties
161 : @param _rConvertedValue the conversion result (if successful)
162 : @param _rOldValue the old value of the property, calculated from _rCurrentValue
163 : @param _rValueToSet the new value which is about to be set
164 : @param _rCurrentValue the current value of the property
165 : @return sal_True, if the value could be converted and has changed
166 : sal_False, if the value could be converted and has not changed
167 : @exception InvalidArgumentException thrown if the value could not be converted to a boolean type
168 : */
169 3981 : inline bool tryPropertyValue(css::uno::Any& /*out*/_rConvertedValue, css::uno::Any& /*out*/_rOldValue, const css::uno::Any& _rValueToSet, bool _bCurrentValue)
170 : {
171 3981 : bool bModified(false);
172 3981 : sal_Bool bNewValue(sal_False);
173 3981 : ::cppu::convertPropertyValue(bNewValue, _rValueToSet);
174 3981 : if (bool(bNewValue) != _bCurrentValue)
175 : {
176 923 : _rConvertedValue.setValue(&bNewValue, ::getBooleanCppuType());
177 923 : _rOldValue.setValue(&_bCurrentValue, ::getBooleanCppuType());
178 923 : bModified = true;
179 : }
180 3981 : return bModified;
181 : }
182 :
183 : /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue
184 : @param _rConvertedValue the conversion result (if successful)
185 : @param _rOldValue the old value of the property, calculated from _rCurrentValue
186 : @param _rValueToSet the new value which is about to be set
187 : @param _rCurrentValue the current value of the property
188 : @param _rExpectedType the type which the property should have (if not void)
189 : @return sal_True, if the value could be converted and has changed
190 : sal_False, if the value could be converted and has not changed
191 : @exception InvalidArgumentException thrown if the value could not be converted to the requested type (which is the template argument)
192 : */
193 : COMPHELPER_DLLPUBLIC bool tryPropertyValue(css::uno::Any& _rConvertedValue, css::uno::Any& _rOldValue, const css::uno::Any& _rValueToSet, const css::uno::Any& _rCurrentValue, const css::uno::Type& _rExpectedType);
194 :
195 :
196 : }
197 : //... namespace comphelper .......................................................
198 :
199 : #endif // INCLUDED_COMPHELPER_PROPERTY_HXX
200 :
201 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|