LibreOffice Module comphelper (master) 1
sequenceashashmap.hxx
Go to the documentation of this file.
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_SEQUENCEASHASHMAP_HXX
21#define INCLUDED_COMPHELPER_SEQUENCEASHASHMAP_HXX
22
23#include <unordered_map>
24#include <com/sun/star/uno/Sequence.hxx>
25
27
28namespace com::sun::star::beans { struct NamedValue; }
29namespace com::sun::star::beans { struct PropertyValue; }
30
31namespace comphelper{
32
33
44{
45 OUString maString;
46 sal_Int32 mnHashCode;
47
48 OUStringAndHashCode(OUString s) : maString(s), mnHashCode(maString.hashCode()) {}
49};
51{
52 bool operator()(const OUStringAndHashCode & lhs, const OUStringAndHashCode & rhs) const
53 {
54 return lhs.mnHashCode == rhs.mnHashCode && lhs.maString == rhs.maString;
55 }
56};
58{
59 size_t operator()(const OUStringAndHashCode & i) const
60 {
61 return i.mnHashCode;
62 }
63};
64using SequenceAsHashMapBase = std::unordered_map<OUStringAndHashCode, css::uno::Any, OUStringAndHashCodeHash, OUStringAndHashCodeEqual>;
65
67{
68
69 public:
70
71
75
76
79 SequenceAsHashMap(const css::uno::Any& aSource);
80
81
84 SequenceAsHashMap(const css::uno::Sequence< css::uno::Any >& lSource);
85
86
89 SequenceAsHashMap(const css::uno::Sequence< css::beans::PropertyValue >& lSource);
90
91
94 SequenceAsHashMap(const css::uno::Sequence< css::beans::NamedValue >& lSource);
95
96
114 void operator<<(const css::uno::Any& aSource);
115
116
129 void operator<<(const css::uno::Sequence< css::uno::Any >& lSource);
130
131
138 void operator<<(const css::uno::Sequence< css::beans::PropertyValue >& lSource);
139
140
147 void operator<<(const css::uno::Sequence< css::beans::NamedValue >& lSource);
148
149
156 void operator>>(css::uno::Sequence< css::beans::PropertyValue >& lDestination) const;
157
158
165 void operator>>(css::uno::Sequence< css::beans::NamedValue >& lDestination) const;
166
167
183 css::uno::Any getAsConstAny(bool bAsPropertyValue) const;
184
185
197 css::uno::Sequence< css::beans::NamedValue > getAsConstNamedValueList() const;
198
199
211 css::uno::Sequence< css::beans::PropertyValue > getAsConstPropertyValueList() const;
212
213
236 template< class TValueType >
237 TValueType getUnpackedValueOrDefault(const OUString& sKey ,
238 const TValueType& aDefault) const
239 {
240 auto pIt = m_aMap.find(sKey);
241 if (pIt == m_aMap.end())
242 return aDefault;
243
244 TValueType aValue = TValueType();
245 if (!(pIt->second >>= aValue))
246 return aDefault;
247
248 return aValue;
249 }
250
266 css::uno::Any getValue(const OUString& sKey) const
267 {
268 auto pIt = m_aMap.find(sKey);
269 if (pIt == m_aMap.end())
270 return css::uno::Any();
271
272 return pIt->second;
273 }
274
275
296 template< class TValueType >
297 bool createItemIfMissing(const OUString& sKey ,
298 const TValueType& aValue)
299 {
300 if (m_aMap.find(sKey) == m_aMap.end())
301 {
302 (*this)[sKey] = css::uno::toAny(aValue);
303 return true;
304 }
305
306 return false;
307 }
308
309
325 bool match(const SequenceAsHashMap& rCheck) const;
326
327
338 void update(const SequenceAsHashMap& rSource);
339
340 css::uno::Any& operator[](const OUString& rKey)
341 {
342 return m_aMap[rKey];
343 }
344
345 css::uno::Any& operator[](const OUStringAndHashCode& rKey)
346 {
347 return m_aMap[rKey];
348 }
349
350 using iterator = SequenceAsHashMapBase::iterator;
351 using const_iterator = SequenceAsHashMapBase::const_iterator;
352
353 void clear()
354 {
355 m_aMap.clear();
356 }
357
358 size_t size() const
359 {
360 return m_aMap.size();
361 }
362
363 bool empty() const
364 {
365 return m_aMap.empty();
366 }
367
369 {
370 return m_aMap.begin();
371 }
372
374 {
375 return m_aMap.begin();
376 }
377
379 {
380 return m_aMap.end();
381 }
382
384 {
385 return m_aMap.end();
386 }
387
388 iterator find(const OUString& rKey)
389 {
390 return m_aMap.find(rKey);
391 }
392
393 const_iterator find(const OUString& rKey) const
394 {
395 return m_aMap.find(rKey);
396 }
397
399 {
400 return m_aMap.find(rKey);
401 }
402
404 {
405 return m_aMap.find(rKey);
406 }
407
409 {
410 return m_aMap.erase(it);
411 }
412
413 size_t erase(const OUString& rKey)
414 {
415 return m_aMap.erase(rKey);
416 }
417
418private:
420};
421
422} // namespace comphelper
423
424#endif // INCLUDED_COMPHELPER_SEQUENCEASHASHMAP_HXX
425
426/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
iterator find(const OUStringAndHashCode &rKey)
TValueType getUnpackedValueOrDefault(const OUString &sKey, const TValueType &aDefault) const
check if the specified item exists and return its (unpacked!) value or it returns the specified defau...
const_iterator find(const OUString &rKey) const
iterator find(const OUString &rKey)
bool createItemIfMissing(const OUString &sKey, const TValueType &aValue)
creates a new item with the specified name and value only in case such item name does not already exi...
css::uno::Any getValue(const OUString &sKey) const
check if the specified item exists and return its value or it returns an empty css::uno::Any.
SequenceAsHashMapBase::iterator iterator
css::uno::Any & operator[](const OUString &rKey)
const_iterator find(const OUStringAndHashCode &rKey) const
SequenceAsHashMapBase::const_iterator const_iterator
size_t erase(const OUString &rKey)
css::uno::Any & operator[](const OUStringAndHashCode &rKey)
#define COMPHELPER_DLLPUBLIC
std::unordered_map< OUStringAndHashCode, css::uno::Any, OUStringAndHashCodeHash, OUStringAndHashCodeEqual > SequenceAsHashMapBase
const css::uno::Reference< css::io::XObjectInputStream > & operator>>(const css::uno::Reference< css::io::XObjectInputStream > &_rxInStream, css::awt::FontDescriptor &_rFont)
Definition: basicio.cxx:54
const css::uno::Reference< css::io::XObjectOutputStream > & operator<<(const css::uno::Reference< css::io::XObjectOutputStream > &_rxOutStream, const css::awt::FontDescriptor &_rFont)
Definition: basicio.cxx:29
bool match(const sal_Unicode *pWild, const sal_Unicode *pStr, const sal_Unicode cEscape)
int i
bool operator()(const OUStringAndHashCode &lhs, const OUStringAndHashCode &rhs) const
size_t operator()(const OUStringAndHashCode &i) const
Implements a stl hash map on top of some specialized sequence from type PropertyValue or NamedValue.
#define SAL_WARN_UNUSED
bool update()