Version 5.4.3.2, tag libreoffice-5.4.3.2
[LibreOffice.git] / include / comphelper / sequenceashashmap.hxx
blob270601af7c67382b695ea5bf5392e0117f7926d8
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_SEQUENCEASHASHMAP_HXX
21 #define INCLUDED_COMPHELPER_SEQUENCEASHASHMAP_HXX
23 #include <algorithm>
24 #include <unordered_map>
25 #include <com/sun/star/uno/Sequence.hxx>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/beans/NamedValue.hpp>
29 #include <comphelper/comphelperdllapi.h>
32 namespace comphelper{
35 /** @short Implements a stl hash map on top of some
36 specialized sequence from type PropertyValue
37 or NamedValue.
39 @descr That provides the possibility to modify
40 such name sequences very easy ...
43 struct SequenceAsHashMapBase : public std::unordered_map<
44 OUString ,
45 css::uno::Any ,
46 OUStringHash >
50 class SAL_WARN_UNUSED COMPHELPER_DLLPUBLIC SequenceAsHashMap : public SequenceAsHashMapBase
53 public:
56 /** @short creates an empty hash map.
58 SequenceAsHashMap();
61 /** @see operator<<(const css::uno::Any&)
63 SequenceAsHashMap(const css::uno::Any& aSource);
66 /** @see operator<<(const css::uno::Sequence< css::uno::Any >&)
68 SequenceAsHashMap(const css::uno::Sequence< css::uno::Any >& lSource);
71 /** @see operator<<(const css::uno::Sequence< css::beans::PropertyValue >&)
73 SequenceAsHashMap(const css::uno::Sequence< css::beans::PropertyValue >& lSource);
76 /** @see operator<<(const css::uno::Sequence< css::beans::NamedValue >&)
78 SequenceAsHashMap(const css::uno::Sequence< css::beans::NamedValue >& lSource);
81 /** @short fill this map from the given
82 Any, which of course must contain
83 a suitable sequence of element types
84 "css.beans.PropertyValue" or "css.beans.NamedValue".
86 @attention If the given Any is an empty one
87 (if it's set to VOID), no exception
88 is thrown. In such case this instance will
89 be created as an empty one too!
91 @param aSource
92 contains the new items for this map.
94 @throw A css::lang::IllegalArgumentException
95 is thrown, if the given Any does not contain a suitable sequence ...
96 but not if it's a VOID Any!
98 void operator<<(const css::uno::Any& aSource);
101 /** @short fill this map from the given
102 sequence, where every Any must contain
103 an item from type "css.beans.PropertyValue"
104 "css.beans.NamedValue".
106 @param lSource
107 contains the new items for this map.
109 @throw A css::lang::IllegalArgumentException
110 is thrown, if the given Any sequence
111 uses wrong types for its items. VOID Any will be ignored!
113 void operator<<(const css::uno::Sequence< css::uno::Any >& lSource);
116 /** @short fill this map from the given
117 PropertyValue sequence.
119 @param lSource
120 contains the new items for this map.
122 void operator<<(const css::uno::Sequence< css::beans::PropertyValue >& lSource);
125 /** @short fill this map from the given
126 NamedValue sequence.
128 @param lSource
129 contains the new items for this map.
131 void operator<<(const css::uno::Sequence< css::beans::NamedValue >& lSource);
134 /** @short converts this map instance to an
135 PropertyValue sequence.
137 @param lDestination
138 target sequence for converting.
140 void operator>>(css::uno::Sequence< css::beans::PropertyValue >& lDestination) const;
143 /** @short converts this map instance to an
144 NamedValue sequence.
146 @param lDestination
147 target sequence for converting.
149 void operator>>(css::uno::Sequence< css::beans::NamedValue >& lDestination) const;
152 /** @short return this map instance as an
153 Any, which can be
154 used in const environments only.
156 @descr It's made const to prevent using of the
157 return value directly as an in/out parameter!
158 usage: myMethod(stlDequeAdapter.getAsAnyList());
160 @param bAsPropertyValue
161 switch between using of PropertyValue or NamedValue as
162 value type.
164 @return A const Any, which
165 contains all items of this map.
167 const css::uno::Any getAsConstAny(bool bAsPropertyValue) const;
170 /** @short return this map instance to as a
171 NamedValue sequence, which can be
172 used in const environments only.
174 @descr It's made const to prevent using of the
175 return value directly as an in/out parameter!
176 usage: myMethod(stlDequeAdapter.getAsNamedValueList());
178 @return A const sequence of type NamedValue, which
179 contains all items of this map.
181 const css::uno::Sequence< css::beans::NamedValue > getAsConstNamedValueList() const;
184 /** @short return this map instance to as a
185 PropertyValue sequence, which can be
186 used in const environments only.
188 @descr It's made const to prevent using of the
189 return value directly as an in/out parameter!
190 usage: myMethod(stlDequeAdapter.getAsPropertyValueList());
192 @return A const sequence of type PropertyValue, which
193 contains all items of this map.
195 const css::uno::Sequence< css::beans::PropertyValue > getAsConstPropertyValueList() const;
198 /** @short check if the specified item exists
199 and return its (unpacked!) value or it returns the
200 specified default value otherwise.
202 @descr If a value should be extracted only in case
203 the requested property exists really (without creating
204 of new items as it the index operator of a
205 hash map does!) this method can be used.
207 @param sKey
208 key name of the item.
210 @param aDefault
211 the default value, which is returned
212 if the specified item could not
213 be found.
215 @return The (unpacked!) value of the specified property or
216 the given default value otherwise.
218 @attention "unpacked" means the Any content of every iterator->second!
220 template< class TValueType >
221 TValueType getUnpackedValueOrDefault(const OUString& sKey ,
222 const TValueType& aDefault) const
224 const_iterator pIt = find(sKey);
225 if (pIt == end())
226 return aDefault;
228 TValueType aValue = TValueType();
229 if (!(pIt->second >>= aValue))
230 return aDefault;
232 return aValue;
235 /** @short check if the specified item exists
236 and return its value or it returns
237 an empty css::uno::Any.
239 @descr If a value should be extracted only in case
240 the requested property exists really (without creating
241 of new items as the index operator of a
242 hash map does!) this method can be used.
244 @param sKey
245 key name of the item.
247 @return The value of the specified property or
248 an empty css::uno::Any.
250 css::uno::Any getValue(const OUString& sKey) const
252 const_iterator pIt = find(sKey);
253 if (pIt == end())
254 return css::uno::Any();
256 return pIt->second;
260 /** @short creates a new item with the specified
261 name and value only in case such item name
262 does not already exist.
264 @descr To check if the property already exists only
265 its name is used for compare. Its value isn't
266 checked!
268 @param sKey
269 key name of the property.
271 @param aValue
272 the new (unpacked!) value.
273 Note: This value will be transformed to an Any
274 internally, because only Any values can be
275 part of a PropertyValue or NamedValue structure.
277 @return TRUE if this property was added as new item;
278 FALSE if it already exists.
280 template< class TValueType >
281 bool createItemIfMissing(const OUString& sKey ,
282 const TValueType& aValue)
284 if (find(sKey) == end())
286 (*this)[sKey] = css::uno::toAny(aValue);
287 return true;
290 return false;
294 /** @short check if all items of given map
295 exists in these called map also.
297 @descr Every item of the given map must exists
298 with same name and value inside these map.
299 But these map can contain additional items
300 which are not part of the search-map.
302 @param rCheck
303 the map containing all items for checking.
305 @return
306 TRUE if all items of Rcheck could be found
307 in these map; FALSE otherwise.
309 bool match(const SequenceAsHashMap& rCheck) const;
312 /** @short merge all values from the given map into
313 this one.
315 @descr Existing items will be overwritten ...
316 missing items will be created new ...
317 but non specified items will stay alive !
319 @param rSource
320 the map containing all items for the update.
322 void update(const SequenceAsHashMap& rSource);
325 } // namespace comphelper
327 #endif // INCLUDED_COMPHELPER_SEQUENCEASHASHMAP_HXX
329 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */