Bump version to 21.06.18.1
[LibreOffice.git] / canvas / inc / propertysethelper.hxx
blobe1a37498bb37a196140ef5f068ad613d4656d61d
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #pragma once
22 #include <canvas/canvastools.hxx>
24 #include <functional>
25 #include <vector>
26 #include <memory>
28 #include <canvas/canvastoolsdllapi.h>
30 namespace com::sun::star::beans { class XPropertyChangeListener; }
31 namespace com::sun::star::beans { class XPropertySetInfo; }
32 namespace com::sun::star::beans { class XVetoableChangeListener; }
34 namespace canvas
36 /** Really simplistic XPropertySet helper for properties.
38 This class provides easy access to properties, referenced via
39 ASCII strings. The name/property modification callbacks pairs
40 are passed into this class via a vector. Each time a property
41 is set or queried, the corresponding getter or setter callback
42 is called.
44 Use this class as a delegate for the corresponding
45 XPropertySet methods, and take care of UNO XInterface and lock
46 handling by yourself.
48 The core responsibility of this class is the name/value
49 mapping for property sets.
51 class CANVASTOOLS_DLLPUBLIC PropertySetHelper
53 public:
54 typedef std::function<css::uno::Any ()> GetterType;
55 typedef std::function<void (const css::uno::Any&)> SetterType;
56 struct Callbacks
58 GetterType getter;
59 SetterType setter;
61 typedef tools::ValueMap< Callbacks > MapType;
62 typedef std::vector< MapType::MapEntry > InputMap;
64 class MakeMap : public InputMap
66 public:
67 MakeMap(const char* name,
68 const GetterType& getter,
69 const SetterType& setter)
71 MapType::MapEntry aEntry={name, {getter, setter}};
72 push_back(aEntry);
74 MakeMap(const char* name,
75 const GetterType& getter)
77 MapType::MapEntry aEntry={name, {getter, SetterType()}};
78 push_back(aEntry);
80 MakeMap& operator()(const char* name,
81 const GetterType& getter,
82 const SetterType& setter)
84 MapType::MapEntry aEntry={name, {getter, setter}};
85 push_back(aEntry);
86 return *this;
88 MakeMap& operator()(const char* name,
89 const GetterType& getter)
91 MapType::MapEntry aEntry={name, {getter, SetterType()}};
92 push_back(aEntry);
93 return *this;
97 /** Create helper with zero properties
99 PropertySetHelper();
101 /** Init helper with new name/value map
103 @param rMap
104 Vector of name/function pointers. Each name is offered as
105 a property, and reading/writing to this property is passed
106 on to the given function pointer.
108 void initProperties( const InputMap& rMap );
110 /** Add given properties to helper
112 @param rMap
113 Vector of name/function pointers. Each name is offered as
114 a property, and reading/writing to this property is passed
115 on to the given function pointer. These name/function
116 pairs are added to the already existing ones.
118 void addProperties( const InputMap& rMap );
120 /** Checks whether the given string corresponds to a valid
121 property name.
123 @return true, if the given name maps to a known property.
125 bool isPropertyName( const OUString& aPropertyName ) const;
127 // XPropertySet implementation
128 css::uno::Reference< css::beans::XPropertySetInfo > getPropertySetInfo() const;
129 void setPropertyValue( const OUString& aPropertyName,
130 const css::uno::Any& aValue );
131 css::uno::Any getPropertyValue( const OUString& PropertyName ) const;
132 void addPropertyChangeListener( const OUString& aPropertyName,
133 const css::uno::Reference< css::beans::XPropertyChangeListener >& xListener );
134 void addVetoableChangeListener( const OUString& aPropertyName,
135 const css::uno::Reference< css::beans::XVetoableChangeListener >& xListener );
137 private:
138 std::unique_ptr<MapType> mpMap;
139 InputMap maMapEntries;
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */