LibreOffice Module comphelper (master)  1
sequenceashashmap.cxx
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 #include <sal/config.h>
21 
22 #include <com/sun/star/beans/NamedValue.hpp>
23 #include <com/sun/star/beans/PropertyValue.hpp>
24 #include <com/sun/star/lang/IllegalArgumentException.hpp>
26 
27 
28 namespace comphelper{
29 
31 {
32 }
33 
34 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Any& aSource)
35 {
36  (*this) << aSource;
37 }
38 
39 
40 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::uno::Any >& lSource)
41 {
42  (*this) << lSource;
43 }
44 
45 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::beans::PropertyValue >& lSource)
46 {
47  (*this) << lSource;
48 }
49 
50 SequenceAsHashMap::SequenceAsHashMap(const css::uno::Sequence< css::beans::NamedValue >& lSource)
51 {
52  (*this) << lSource;
53 }
54 
55 void SequenceAsHashMap::operator<<(const css::uno::Any& aSource)
56 {
57  // An empty Any reset this instance!
58  if (!aSource.hasValue())
59  {
60  clear();
61  return;
62  }
63 
64  css::uno::Sequence< css::beans::NamedValue > lN;
65  if (aSource >>= lN)
66  {
67  (*this) << lN;
68  return;
69  }
70 
71  css::uno::Sequence< css::beans::PropertyValue > lP;
72  if (aSource >>= lP)
73  {
74  (*this) << lP;
75  return;
76  }
77 
78  throw css::lang::IllegalArgumentException(
79  "Any contains wrong type.", css::uno::Reference<css::uno::XInterface>(),
80  -1);
81 }
82 
83 
84 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::uno::Any >& lSource)
85 {
86  sal_Int32 c = lSource.getLength();
87  sal_Int32 i = 0;
88 
89  for (i=0; i<c; ++i)
90  {
91  css::beans::PropertyValue lP;
92  if (lSource[i] >>= lP)
93  {
94  if (
95  (lP.Name.isEmpty()) ||
96  (!lP.Value.hasValue())
97  )
98  throw css::lang::IllegalArgumentException(
99  "PropertyValue struct contains no useful information.",
100  css::uno::Reference<css::uno::XInterface>(), -1);
101  (*this)[lP.Name] = lP.Value;
102  continue;
103  }
104 
105  css::beans::NamedValue lN;
106  if (lSource[i] >>= lN)
107  {
108  if (
109  (lN.Name.isEmpty()) ||
110  (!lN.Value.hasValue())
111  )
112  throw css::lang::IllegalArgumentException(
113  "NamedValue struct contains no useful information.",
114  css::uno::Reference<css::uno::XInterface>(), -1);
115  (*this)[lN.Name] = lN.Value;
116  continue;
117  }
118 
119  // ignore VOID Any ... but reject wrong filled ones!
120  if (lSource[i].hasValue())
121  throw css::lang::IllegalArgumentException(
122  "Any contains wrong type.",
123  css::uno::Reference<css::uno::XInterface>(), -1);
124  }
125 }
126 
127 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::beans::PropertyValue >& lSource)
128 {
129  clear();
130 
131  sal_Int32 c = lSource.getLength();
132  const css::beans::PropertyValue* pSource = lSource.getConstArray();
133 
134  for (sal_Int32 i=0; i<c; ++i)
135  (*this)[pSource[i].Name] = pSource[i].Value;
136 }
137 
138 void SequenceAsHashMap::operator<<(const css::uno::Sequence< css::beans::NamedValue >& lSource)
139 {
140  clear();
141 
142  sal_Int32 c = lSource.getLength();
143  const css::beans::NamedValue* pSource = lSource.getConstArray();
144 
145  for (sal_Int32 i=0; i<c; ++i)
146  (*this)[pSource[i].Name] = pSource[i].Value;
147 }
148 
149 void SequenceAsHashMap::operator>>(css::uno::Sequence< css::beans::PropertyValue >& lDestination) const
150 {
151  sal_Int32 c = static_cast<sal_Int32>(size());
152  lDestination.realloc(c);
153  css::beans::PropertyValue* pDestination = lDestination.getArray();
154 
155  sal_Int32 i = 0;
156  for (const_iterator pThis = begin();
157  pThis != end() ;
158  ++pThis )
159  {
160  pDestination[i].Name = pThis->first ;
161  pDestination[i].Value = pThis->second;
162  ++i;
163  }
164 }
165 
166 void SequenceAsHashMap::operator>>(css::uno::Sequence< css::beans::NamedValue >& lDestination) const
167 {
168  sal_Int32 c = static_cast<sal_Int32>(size());
169  lDestination.realloc(c);
170  css::beans::NamedValue* pDestination = lDestination.getArray();
171 
172  sal_Int32 i = 0;
173  for (const_iterator pThis = begin();
174  pThis != end() ;
175  ++pThis )
176  {
177  pDestination[i].Name = pThis->first ;
178  pDestination[i].Value = pThis->second;
179  ++i;
180  }
181 }
182 
183 css::uno::Any SequenceAsHashMap::getAsConstAny(bool bAsPropertyValueList) const
184 {
185  css::uno::Any aDestination;
186  if (bAsPropertyValueList)
187  aDestination <<= getAsConstPropertyValueList();
188  else
189  aDestination <<= getAsConstNamedValueList();
190  return aDestination;
191 }
192 
193 css::uno::Sequence< css::beans::NamedValue > SequenceAsHashMap::getAsConstNamedValueList() const
194 {
195  css::uno::Sequence< css::beans::NamedValue > lReturn;
196  (*this) >> lReturn;
197  return lReturn;
198 }
199 
200 css::uno::Sequence< css::beans::PropertyValue > SequenceAsHashMap::getAsConstPropertyValueList() const
201 {
202  css::uno::Sequence< css::beans::PropertyValue > lReturn;
203  (*this) >> lReturn;
204  return lReturn;
205 }
206 
208 {
209  for (auto const& elem : rCheck)
210  {
211  const OUString& sCheckName = elem.first;
212  const css::uno::Any& aCheckValue = elem.second;
213  const_iterator pFound = find(sCheckName);
214 
215  if (pFound == end())
216  return false;
217 
218  const css::uno::Any& aFoundValue = pFound->second;
219  if (aFoundValue != aCheckValue)
220  return false;
221  }
222 
223  return true;
224 }
225 
227 {
228  for (auto const& elem : rUpdate)
229  {
230  const OUString& sName = elem.first;
231  const css::uno::Any& aValue = elem.second;
232 
233  (*this)[sName] = aValue;
234  }
235 }
236 
237 } // namespace comphelper
238 
239 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
SequenceAsHashMapBase::const_iterator const_iterator
void operator<<(const css::uno::Any &aSource)
fill this map from the given Any, which of course must contain a suitable sequence of element types "...
css::uno::Sequence< css::beans::PropertyValue > getAsConstPropertyValueList() const
return this map instance to as a PropertyValue sequence, which can be used in const environments only...
css::uno::Sequence< css::beans::NamedValue > getAsConstNamedValueList() const
return this map instance to as a NamedValue sequence, which can be used in const environments only...
const char * sName
void update(const SequenceAsHashMap &rSource)
merge all values from the given map into this one.
int i
css::uno::Any getAsConstAny(bool bAsPropertyValue) const
return this map instance as an Any, which can be used in const environments only. ...
iterator find(const OUString &rKey)
SequenceAsHashMap()
creates an empty hash map.
bool match(const SequenceAsHashMap &rCheck) const
check if all items of given map exists in these called map also.
void operator>>(css::uno::Sequence< css::beans::PropertyValue > &lDestination) const
converts this map instance to an PropertyValue sequence.