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 .
20 #ifndef INCLUDED_COMPHELPER_SEQUENCEASHASHMAP_HXX
21 #define INCLUDED_COMPHELPER_SEQUENCEASHASHMAP_HXX
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>
35 /** @short Implements a stl hash map on top of some
36 specialized sequence from type PropertyValue
39 @descr That provides the possibility to modify
40 such name sequences very easy ...
43 struct SequenceAsHashMapBase
: public std::unordered_map
<
45 ::com::sun::star::uno::Any
,
47 ::std::equal_to
< OUString
> >
51 class COMPHELPER_DLLPUBLIC SequenceAsHashMap
: public SequenceAsHashMapBase
57 /** @short creates an empty hash map.
62 /** @see operator<<(const ::com::sun::star::uno::Any&)
64 SequenceAsHashMap(const ::com::sun::star::uno::Any
& aSource
);
67 /** @see operator<<(const ::com::sun::star::uno::Sequence< ::com::sun::star::uno::Any >&)
69 SequenceAsHashMap(const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& lSource
);
72 /** @see operator<<(const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::PropertyValue >&)
74 SequenceAsHashMap(const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>& lSource
);
77 /** @see operator<<(const ::com::sun::star::uno::Sequence< ::com::sun::star::beans::NamedValue >&)
79 SequenceAsHashMap(const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
>& lSource
);
82 /** @short not really used but maybe useful :-)
87 /** @short fill this map from the given
88 Any, which of course must contain
89 a suitable sequence of element types
90 "css.beans.PropertyValue" or "css.beans.NamedValue".
92 @attention If the given Any is an empty one
93 (if it's set to VOID), no exception
94 is thrown. In such case this instance will
95 be created as an empty one too!
98 contains the new items for this map.
100 @throw A com::sun::star::lang::IllegalArgumentException
101 is thrown, if the given Any does not contain a suitable sequence ...
102 but not if it's a VOID Any!
104 void operator<<(const ::com::sun::star::uno::Any
& aSource
);
107 /** @short fill this map from the given
108 sequence, where every Any must contain
109 an item from type "css.beans.PropertyValue"
110 "css.beans.NamedValue".
113 contains the new items for this map.
115 @throw A com::sun::star::lang::IllegalArgumentException
116 is thrown, if the given Any sequence
117 uses wrong types for its items. VOID Any will be ignored!
119 void operator<<(const ::com::sun::star::uno::Sequence
< ::com::sun::star::uno::Any
>& lSource
);
122 /** @short fill this map from the given
123 PropertyValue sequence.
126 contains the new items for this map.
128 void operator<<(const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>& lSource
);
131 /** @short fill this map from the given
135 contains the new items for this map.
137 void operator<<(const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
>& lSource
);
140 /** @short converts this map instance to an
141 PropertyValue sequence.
144 target sequence for converting.
146 void operator>>(::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
>& lDestination
) const;
149 /** @short converts this map instance to an
153 target sequence for converting.
155 void operator>>(::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
>& lDestination
) const;
158 /** @short return this map instance as an
160 used in const environments only.
162 @descr It's made const to prevent using of the
163 return value directly as an in/out parameter!
164 usage: myMethod(stlDequeAdapter.getAsAnyList());
166 @param bAsPropertyValue
167 switch between using of PropertyValue or NamedValue as
170 @return A const Any, which
171 contains all items of this map.
173 const ::com::sun::star::uno::Any
getAsConstAny(bool bAsPropertyValue
) const;
176 /** @short return this map instance to as a
177 NamedValue sequence, which can be
178 used in const environments only.
180 @descr It's made const to prevent using of the
181 return value directly as an in/out parameter!
182 usage: myMethod(stlDequeAdapter.getAsNamedValueList());
184 @return A const sequence of type NamedValue, which
185 contains all items of this map.
187 const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::NamedValue
> getAsConstNamedValueList() const;
190 /** @short return this map instance to as a
191 PropertyValue sequence, which can be
192 used in const environments only.
194 @descr It's made const to prevent using of the
195 return value directly as an in/out parameter!
196 usage: myMethod(stlDequeAdapter.getAsPropertyValueList());
198 @return A const sequence of type PropertyValue, which
199 contains all items of this map.
201 const ::com::sun::star::uno::Sequence
< ::com::sun::star::beans::PropertyValue
> getAsConstPropertyValueList() const;
204 /** @short check if the specified item exists
205 and return its (unpacked!) value or it returns the
206 specified default value otherwise.
208 @descr If a value should be extracted only in case
209 the requested property exists really (without creating
210 of new items as it the index operator of a
211 hash map does!) this method can be used.
214 key name of the item.
217 the default value, which is returned
218 if the specified item could not
221 @return The (unpacked!) value of the specified property or
222 the given default value otherwise.
224 @attention "unpacked" means the Any content of every iterator->second!
226 template< class TValueType
>
227 TValueType
getUnpackedValueOrDefault(const OUString
& sKey
,
228 const TValueType
& aDefault
) const
230 const_iterator pIt
= find(sKey
);
234 TValueType aValue
= TValueType();
235 if (!(pIt
->second
>>= aValue
))
242 /** @short creates a new item with the specified
243 name and value only in case such item name
244 does not already exist.
246 @descr To check if the property already exists only
247 its name is used for compare. Its value isn't
251 key name of the property.
254 the new (unpacked!) value.
255 Note: This value will be transformed to an Any
256 internally, because only Any values can be
257 part of a PropertyValue or NamedValue structure.
259 @return TRUE if this property was added as new item;
260 FALSE if it already exists.
262 template< class TValueType
>
263 bool createItemIfMissing(const OUString
& sKey
,
264 const TValueType
& aValue
)
266 if (find(sKey
) == end())
268 (*this)[sKey
] = ::com::sun::star::uno::toAny(aValue
);
276 /** @short check if all items of given map
277 exists in these called map also.
279 @descr Every item of the given map must exists
280 with same name and value inside these map.
281 But these map can contain additional items
282 which are not part of the search-map.
285 the map containing all items for checking.
288 TRUE if all items of Rcheck could be found
289 in these map; FALSE otherwise.
291 bool match(const SequenceAsHashMap
& rCheck
) const;
294 /** @short merge all values from the given map into
297 @descr Existing items will be overwritten ...
298 missing items will be created new ...
299 but non specified items will stay alive !
302 the map containing all items for the update.
304 void update(const SequenceAsHashMap
& rSource
);
307 } // namespace comphelper
309 #endif // INCLUDED_COMPHELPER_SEQUENCEASHASHMAP_HXX
311 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */