LibreOffice Module xmloff (master)  1
MultiPropertySetHandler.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_XMLOFF_SOURCE_CHART_MULTIPROPERTYSETHANDLER_HXX
21 #define INCLUDED_XMLOFF_SOURCE_CHART_MULTIPROPERTYSETHANDLER_HXX
22 
23 #include <map>
24 #include <memory>
25 #include <string_view>
26 
27 #include <rtl/ustring.hxx>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/beans/XMultiPropertySet.hpp>
30 
47 {
48 public:
52  explicit PropertyWrapperBase (const OUString & rName)
53  : msName (rName)
54  {}
56  {}
57 
61  virtual void SetValue (const css::uno::Any & rValue) = 0;
62 
63  const OUString msName;
64 };
65 
69 template<class T> class PropertyWrapper : public PropertyWrapperBase
70 {
71 public:
74  PropertyWrapper (const OUString & rName, T & rValue)
75  : PropertyWrapperBase (rName),
76  mrValue (rValue)
77  {}
78 
82  virtual void SetValue (const css::uno::Any & rValue) override
83  {
84  rValue >>= mrValue;
85  }
86 
87 private:
89  T & mrValue;
90 };
91 
95 {
96 public:
98  bool operator() (const OUString & a, std::u16string_view b) const
99  {
100  return (a.compareTo (b) < 0);
101  }
102 };
103 
120 {
121 public:
128  explicit MultiPropertySetHandler (css::uno::Reference<
129  css::uno::XInterface> const & xObject);
137  template<class T> void Add (const OUString & sName, T& rValue)
138  {
139  aPropertyList[sName] = std::make_unique<PropertyWrapper<T>>(sName, rValue);
140  }
141 
149  inline bool GetProperties();
150 
151 private:
158  inline bool MultiGet (const css::uno::Sequence<
159  OUString> & rNameList);
160 
167  inline bool SingleGet (const css::uno::Sequence<
168  OUString> & rNameList);
169 
174  ::std::map< OUString, std::unique_ptr<PropertyWrapperBase>, OUStringComparison> aPropertyList;
175 
177  css::uno::Reference< css::uno::XInterface> mxObject;
178 };
179 
181  css::uno::XInterface> const & xObject)
182  : mxObject (xObject)
183 {
184 }
185 
187 {
188  css::uno::Sequence< OUString> aNameList (aPropertyList.size());
189  int i = 0;
190  for (const auto& rProperty : aPropertyList)
191  aNameList[i++] = rProperty.second->msName;
192  if ( ! MultiGet(aNameList))
193  if ( ! SingleGet(aNameList))
194  return false;
195  return true;
196 }
197 
198 bool MultiPropertySetHandler::MultiGet (const css::uno::Sequence<
199  OUString> & rNameList)
200 {
201  css::uno::Reference< css::beans::XMultiPropertySet> xMultiSet (
202  mxObject, css::uno::UNO_QUERY);
203  if (xMultiSet.is())
204  try
205  {
206  int i = 0;
207  css::uno::Sequence< css::uno::Any> aValueList =
208  xMultiSet->getPropertyValues (rNameList);
209  for (auto& rProperty : aPropertyList)
210  rProperty.second->SetValue (aValueList[i++]);
211  }
212  catch (const css::beans::UnknownPropertyException&)
213  {
214  return false;
215  }
216  else
217  return false;
218 
219  return true;
220 }
221 
222 bool MultiPropertySetHandler::SingleGet (const css::uno::Sequence<
223  OUString> & rNameList)
224 {
225  css::uno::Reference< css::beans::XPropertySet> xSingleSet (
226  mxObject, css::uno::UNO_QUERY);
227  if (xSingleSet.is())
228  try
229  {
230  int i = 0;
231  for (auto& rProperty : aPropertyList)
232  rProperty.second->SetValue (xSingleSet->getPropertyValue (rNameList[i++]));
233  }
234  catch (const css::beans::UnknownPropertyException&)
235  {
236  return false;
237  }
238  else
239  return false;
240 
241  return true;
242 }
243 
244 #endif
245 
246 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
bool SingleGet(const css::uno::Sequence< OUString > &rNameList)
Try to use the XPropertySet interface to get the property values.
MultiPropertySetHandler(css::uno::Reference< css::uno::XInterface > const &xObject)
Create a handler of the property set of the given object.
virtual void SetValue(const css::uno::Any &rValue) override
descr Set the given value inside an Any to the variable referenced by the data member.
css::uno::Reference< css::uno::XInterface > mxObject
The object from which to get the property values.
PropertyWrapperBase(const OUString &rName)
Create a class instance and store the given name.
::std::map< OUString, std::unique_ptr< PropertyWrapperBase >, OUStringComparison > aPropertyList
STL map that maps from property names to polymorphic instances of PropertyWrapper.
PropertyWrapper(const OUString &rName, T &rValue)
Create a wrapper for a property of type T.
MultiPropertySetHandler handles the two slightly different interfaces XPropertySet and XMultiProperty...
css::uno::Any const & rValue
Definition: ImageStyle.hxx:38
int i
virtual void SetValue(const css::uno::Any &rValue)=0
Abstract interface of a method for setting a variables value to that of the property.
bool operator()(const OUString &a, std::u16string_view b) const
Compare two strings. Returns true if the first is before the second.
void Add(const OUString &sName, T &rValue)
Add a property to handle.
This class lets you get the values from an object that either supports the interface XPropertySet or ...
T & mrValue
Reference to a variable. Its value can be modified by a call to SetValue.
bool GetProperties()
Try to get the values for all properties added with the Add method.
For every property type there will be one instantiation of this template class with its own and type ...
bool MultiGet(const css::uno::Sequence< OUString > &rNameList)
Try to use the XMultiPropertySet interface to get the property values.
Function object for comparing two OUStrings.