1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include <rtl/ustring.hxx>
22 #include <com/sun/star/uno/Sequence.hxx>
25 namespace com::sun::star
29 class XMultiPropertySet
;
37 class XPropertySetInfo
;
42 * The MultiPropertySetHelper performs the following functions:
44 * Given a list of property names (as char** or OUString*), it can
45 * query an XMultiPropertySet (or XPropertySet) which of these properties
46 * it supports (method hasProperties(...)). The properties *MUST* be
47 * sorted alphabetically.
49 * Then, the X(Multi)PropertySet can be queried for values, and only
50 * the supported properties are queried. (method getValues(...)) The
51 * values are stored in the helper itself.
53 * Finally, each property can be queried for existence
54 * (method hasProperty(...)) or its value (method (getValue(...))).
56 * After some initial preparation (hasProperties, getValues) the
57 * MultiPropertySetHelper can be used similarly to an
58 * XPropertySet in that you can query the values in the places where you
59 * need them. However, if an XMultiPropertySet is supplied, the queries
60 * are more efficient, often significantly so.
62 class MultiPropertySetHelper
64 /// names of all properties
65 std::unique_ptr
<OUString
[]> pPropertyNames
;
67 /// length of pPropertyNames array
70 /// the sequence of property names that the current (multi)
71 /// property set implementation supports
72 css::uno::Sequence
<OUString
> aPropertySequence
;
74 /// an array of indices that maps from pPropertyNames indices to
75 /// aPropertySequence indices
76 std::unique_ptr
<sal_Int16
[]> pSequenceIndex
;
78 /// the last set of values retrieved by getValues
79 css::uno::Sequence
<css::uno::Any
> aValues
;
81 /// result of aValues.getConstArray()
82 const css::uno::Any
* pValues
;
85 css::uno::Any aEmptyAny
;
88 MultiPropertySetHelper(const char** pNames
);
90 ~MultiPropertySetHelper();
93 * Call hasPropertiesByName for the provided XPropertySetInfo and build
94 * list of allowed properties.
96 void hasProperties(const css::uno::Reference
<css::beans::XPropertySetInfo
>&);
99 * Return whether hasProperties was called
100 * (i.e. if we are ready to call getValues)
102 bool checkedProperties();
105 * Get values from the XMultiPropertySet.
107 * May only be called after hasProperties() was called for the
108 * appropriate XPropertySetInfo.
110 void getValues(const css::uno::Reference
<css::beans::XMultiPropertySet
>&);
113 * Get values from the XPropertySet. This can be much slower than
114 * getValues( const Reference<XMultiPropertySet& ) and hence
117 * May only be called after hasProperties() was called for the
118 * appropriate XPropertySetInfo.
120 void getValues(const css::uno::Reference
<css::beans::XPropertySet
>&);
123 * Get a value from the values array.
125 * May only be called after getValues() was called.
127 inline const css::uno::Any
& getValue(sal_Int16 nIndex
);
130 * Find out if this property is supported.
132 * May only be called after hasProperties() was called.
134 inline bool hasProperty(sal_Int16 nIndex
);
137 * Get a value from the XPropertySet on demand.
139 * If neither getValues nor getValueOnDemand has been called already
140 * after the last call to resetValues, the values are retrieved
141 * using getValues. Otherwise the value already retrieved is returned.
142 * In case XMultiPropertySet is supported by the XPropertySet and
143 * bTryMult is set, the XMultiPropertySet is used to get the values.
146 const css::uno::Any
& getValue(sal_Int16 nIndex
,
147 const css::uno::Reference
<css::beans::XPropertySet
>&,
148 bool bTryMulti
= false);
151 * Get a value from the XMultiPropertySet on demand.
153 * If neither getValues nor getValueOnDemand has been called already
154 * after the last call to resetValues, the values are retrieved
155 * using getValues. Otherwise the value already retrieved is returned.
156 * In case XMultiPropertySet is supported by the XPropertySet,
157 * XMultiPropertySet is used to get the values.
160 const css::uno::Any
& getValue(sal_Int16 nIndex
,
161 const css::uno::Reference
<css::beans::XMultiPropertySet
>&);
163 void resetValues() { pValues
= nullptr; }
166 // inline implementations of the often-called methods getValue and hasProperty:
168 const css::uno::Any
& MultiPropertySetHelper::getValue(sal_Int16 nValueNo
)
170 assert(pValues
&& "called getValue() without calling getValues()");
171 assert(pSequenceIndex
&& "called getValue() without calling hasProperties()");
172 assert(nValueNo
< nLength
);
174 sal_Int16 nIndex
= pSequenceIndex
[nValueNo
];
175 return (nIndex
!= -1) ? pValues
[nIndex
] : aEmptyAny
;
178 bool MultiPropertySetHelper::hasProperty(sal_Int16 nValueNo
)
180 assert(pSequenceIndex
&& "called hasProperty() without calling hasProperties()");
181 assert(nValueNo
< nLength
);
183 return pSequenceIndex
[nValueNo
] != -1;
186 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */