LibreOffice Module canvas (master)  1
propertysethelper.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 <string_view>
23 
24 #include <propertysethelper.hxx>
25 #include <com/sun/star/beans/PropertyVetoException.hpp>
26 #include <com/sun/star/beans/UnknownPropertyException.hpp>
27 
28 using namespace ::com::sun::star;
29 
30 namespace canvas
31 {
32  namespace
33  {
34  void throwUnknown( std::u16string_view aPropertyName )
35  {
36  throw beans::UnknownPropertyException(
37  OUString::Concat("PropertySetHelper: property ") +
38  aPropertyName + " not found."
39  );
40  }
41 
42  void throwVeto( std::u16string_view aPropertyName )
43  {
44  throw beans::PropertyVetoException(
45  OUString::Concat("PropertySetHelper: property ") +
46  aPropertyName + " access was vetoed." );
47  }
48 
49  struct EntryComparator
50  {
51  bool operator()( const PropertySetHelper::MapType::MapEntry& rLHS,
52  const PropertySetHelper::MapType::MapEntry& rRHS )
53  {
54  return strcmp( rLHS.maKey,
55  rRHS.maKey ) < 0;
56  }
57  };
58  }
59 
61  {
62  }
63 
65  {
66  mpMap.reset();
67  maMapEntries = std::move(rMap);
68 
69  std::sort( maMapEntries.begin(),
70  maMapEntries.end(),
71  EntryComparator() );
72 
73  if( !maMapEntries.empty() )
74  mpMap.reset( new MapType(maMapEntries.data(),
75  maMapEntries.size(),
76  true) );
77  }
78 
80  {
81  InputMap aMerged( maMapEntries );
82  aMerged.insert( aMerged.end(),
83  rMap.begin(),
84  rMap.end() );
85 
86  initProperties( std::move(aMerged) );
87  }
88 
89  bool PropertySetHelper::isPropertyName( const OUString& aPropertyName ) const
90  {
91  if (!mpMap)
92  return false;
93 
94  Callbacks aDummy;
95  return mpMap->lookup( aPropertyName,
96  aDummy );
97  }
98 
99  uno::Reference< beans::XPropertySetInfo > PropertySetHelper::getPropertySetInfo() const
100  {
101  // we're a stealth property set
102  return uno::Reference< beans::XPropertySetInfo >();
103  }
104 
105  void PropertySetHelper::setPropertyValue( const OUString& aPropertyName,
106  const uno::Any& aValue )
107  {
108  Callbacks aCallbacks;
109  if (!mpMap || !mpMap->lookup(aPropertyName, aCallbacks))
110  {
111  throwUnknown( aPropertyName );
112  }
113 
114  if (!aCallbacks.setter)
115  throwVeto( aPropertyName );
116 
117  aCallbacks.setter(aValue);
118  }
119 
120  uno::Any PropertySetHelper::getPropertyValue( const OUString& aPropertyName ) const
121  {
122  Callbacks aCallbacks;
123  if (!mpMap || !mpMap->lookup(aPropertyName, aCallbacks))
124  {
125  throwUnknown( aPropertyName );
126  }
127 
128  if (aCallbacks.getter)
129  return aCallbacks.getter();
130 
131  // TODO(Q1): subtlety, empty getter method silently returns
132  // the empty any
133  return uno::Any();
134  }
135 
136  void PropertySetHelper::addPropertyChangeListener( const OUString& aPropertyName,
137  const uno::Reference< beans::XPropertyChangeListener >& /*xListener*/ )
138  {
139  // check validity of property, but otherwise ignore the
140  // request
141  if( !isPropertyName( aPropertyName ) )
142  throwUnknown( aPropertyName );
143  }
144 
145  void PropertySetHelper::addVetoableChangeListener( const OUString& aPropertyName,
146  const uno::Reference< beans::XVetoableChangeListener >& /*xListener*/ )
147  {
148  // check validity of property, but otherwise ignore the
149  // request
150  if( !isPropertyName( aPropertyName ) )
151  throwUnknown( aPropertyName );
152  }
153 
154 }
155 
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
std::set< uno_getMappingFunc > aCallbacks
A very simplistic map for ASCII strings and arbitrary value types.
std::unique_ptr< MapType > mpMap
css::uno::Any getPropertyValue(const OUString &PropertyName) const
void setPropertyValue(const OUString &aPropertyName, const css::uno::Any &aValue)
void addVetoableChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XVetoableChangeListener > &xListener)
css::uno::Reference< css::beans::XPropertySetInfo > getPropertySetInfo() const
void addProperties(const InputMap &rMap)
Add given properties to helper.
std::vector< MapType::MapEntry > InputMap
void initProperties(InputMap &&rMap)
Init helper with new name/value map.
PropertySetHelper()
Create helper with zero properties.
void addPropertyChangeListener(const OUString &aPropertyName, const css::uno::Reference< css::beans::XPropertyChangeListener > &xListener)
bool isPropertyName(const OUString &aPropertyName) const
Checks whether the given string corresponds to a valid property name.