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