Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / comphelper / property.hxx
blob445f66cb39881a3d2baaecd6e219671738dcd738
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef INCLUDED_COMPHELPER_PROPERTY_HXX
21 #define INCLUDED_COMPHELPER_PROPERTY_HXX
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 <type_traits>
29 #include <comphelper/comphelperdllapi.h>
30 #include <cppu/unotype.hxx>
32 namespace comphelper
35 // comparing two property instances
36 struct PropertyCompareByName : public ::std::binary_function< css::beans::Property, css::beans::Property, bool >
38 bool operator() (const css::beans::Property& x, const css::beans::Property& y) const
40 return x.Name.compareTo(y.Name) < 0;
44 /// remove the property with the given name from the given sequence
45 COMPHELPER_DLLPUBLIC void RemoveProperty(css::uno::Sequence<css::beans::Property>& seqProps, const OUString& _rPropName);
47 /** within the given property sequence, modify attributes of a special property
48 @param _rProps the sequence of properties to search in
49 @param _sPropName the name of the property which's attributes should be modified
50 @param _nAddAttrib the attributes which should be added
51 @param _nRemoveAttrib the attributes which should be removed
53 COMPHELPER_DLLPUBLIC void ModifyPropertyAttributes(css::uno::Sequence<css::beans::Property>& _rProps, const OUString& _sPropName, sal_Int16 _nAddAttrib, sal_Int16 _nRemoveAttrib);
55 /** check if the given set has the given property.
57 COMPHELPER_DLLPUBLIC bool hasProperty(const OUString& _rName, const css::uno::Reference<css::beans::XPropertySet>& _rxSet);
59 /** copy properties between property sets, in compliance with the property
60 attributes of the target object
62 COMPHELPER_DLLPUBLIC void copyProperties(const css::uno::Reference<css::beans::XPropertySet>& _rxSource,
63 const css::uno::Reference<css::beans::XPropertySet>& _rxDest);
65 /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue
66 @param _rConvertedValue the conversion result (if successful)
67 @param _rOldValue the old value of the property, calculated from _rCurrentValue
68 @param _rValueToSet the new value which is about to be set
69 @param _rCurrentValue the current value of the property
70 @return sal_True, if the value could be converted and has changed
71 sal_False, if the value could be converted and has not changed
72 @exception InvalidArgumentException thrown if the value could not be converted to the requested type (which is the template argument)
74 template <typename T>
75 bool tryPropertyValue(css::uno::Any& /*out*/_rConvertedValue, css::uno::Any& /*out*/_rOldValue, const css::uno::Any& _rValueToSet, const T& _rCurrentValue)
77 bool bModified(false);
78 T aNewValue = T();
79 ::cppu::convertPropertyValue(aNewValue, _rValueToSet);
80 if (aNewValue != _rCurrentValue)
82 _rConvertedValue <<= aNewValue;
83 _rOldValue <<= _rCurrentValue;
84 bModified = true;
86 return bModified;
89 /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue for enum values
90 @param _rConvertedValue the conversion result (if successful)
91 @param _rOldValue the old value of the property, calculated from _rCurrentValue
92 @param _rValueToSet the new value which is about to be set
93 @param _rCurrentValue the current value of the property
94 @return sal_True, if the value could be converted and has changed
95 sal_False, if the value could be converted and has not changed
96 @exception InvalidArgumentException thrown if the value could not be converted to the requested type (which is the template argument)
98 template <class ENUMTYPE>
99 typename std::enable_if<std::is_enum<ENUMTYPE>::value, bool>::type
100 tryPropertyValueEnum(css::uno::Any& /*out*/_rConvertedValue, css::uno::Any& /*out*/_rOldValue, const css::uno::Any& _rValueToSet, const ENUMTYPE& _rCurrentValue)
102 bool bModified(false);
103 ENUMTYPE aNewValue;
104 ::cppu::any2enum(aNewValue, _rValueToSet);
105 // will throw an exception if not convertible
107 if (aNewValue != _rCurrentValue)
109 _rConvertedValue <<= aNewValue;
110 _rOldValue <<= _rCurrentValue;
111 bModified = true;
113 return bModified;
116 /** helper for implementing ::cppu::OPropertySetHelper::convertFastPropertyValue
117 @param _rConvertedValue the conversion result (if successful)
118 @param _rOldValue the old value of the property, calculated from _rCurrentValue
119 @param _rValueToSet the new value which is about to be set
120 @param _rCurrentValue the current value of the property
121 @param _rExpectedType the type which the property should have (if not void)
122 @return sal_True, if the value could be converted and has changed
123 sal_False, if the value could be converted and has not changed
124 @exception InvalidArgumentException thrown if the value could not be converted to the requested type (which is the template argument)
126 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);
130 #endif // INCLUDED_COMPHELPER_PROPERTY_HXX
132 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */