| 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 <canvas/canvastools.hxx> |
| 23 | |
| 24 | #include <functional> |
| 25 | #include <vector> |
| 26 | #include <memory> |
| 27 | |
| 28 | #include <canvas/canvastoolsdllapi.h> |
| 29 | |
| 30 | namespace com::sun::star::beans { class XPropertyChangeListener; } |
| 31 | namespace com::sun::star::beans { class XPropertySetInfo; } |
| 32 | namespace com::sun::star::beans { class XVetoableChangeListener; } |
| 33 | |
| 34 | namespace canvas |
| 35 | { |
| 36 | /** Really simplistic XPropertySet helper for properties. |
| 37 | |
| 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. |
| 43 | |
| 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. |
| 47 | |
| 48 | The core responsibility of this class is the name/value |
| 49 | mapping for property sets. |
| 50 | */ |
| 51 | class CANVASTOOLS_DLLPUBLIC PropertySetHelper |
| 52 | { |
| 53 | public: |
| 54 | typedef std::function<css::uno::Any ()> GetterType; |
| 55 | typedef std::function<void (const css::uno::Any&)> SetterType; |
| 56 | struct Callbacks |
| 57 | { |
| 58 | GetterType getter; |
| 59 | SetterType setter; |
| 60 | }; |
| 61 | typedef canvastools::ValueMap< Callbacks > MapType; |
| 62 | typedef std::vector< MapType::MapEntry > InputMap; |
| 63 | |
| 64 | class MakeMap : public InputMap |
| 65 | { |
| 66 | public: |
| 67 | MakeMap(const char* name, |
| 68 | const GetterType& getter, |
| 69 | const SetterType& setter) |
| 70 | { |
| 71 | push_back(MapType::MapEntry{name, {getter, setter}}); |
| 72 | } |
| 73 | MakeMap(const char* name, |
| 74 | const GetterType& getter) |
| 75 | { |
| 76 | push_back(MapType::MapEntry{name, {getter, SetterType()}}); |
| 77 | } |
| 78 | MakeMap& operator()(const char* name, |
| 79 | const GetterType& getter, |
| 80 | const SetterType& setter) |
| 81 | { |
| 82 | push_back(MapType::MapEntry{name, {getter, setter}}); |
| 83 | return *this; |
| 84 | } |
| 85 | }; |
| 86 | |
| 87 | /** Create helper with zero properties |
| 88 | */ |
| 89 | PropertySetHelper(); |
| 90 | |
| 91 | /** Init helper with new name/value map |
| 92 | |
| 93 | @param rMap |
| 94 | Vector of name/function pointers. Each name is offered as |
| 95 | a property, and reading/writing to this property is passed |
| 96 | on to the given function pointer. |
| 97 | */ |
| 98 | void initProperties( InputMap&& rMap ); |
| 99 | |
| 100 | /** Add given properties to helper |
| 101 | |
| 102 | @param rMap |
| 103 | Vector of name/function pointers. Each name is offered as |
| 104 | a property, and reading/writing to this property is passed |
| 105 | on to the given function pointer. These name/function |
| 106 | pairs are added to the already existing ones. |
| 107 | */ |
| 108 | void addProperties( const InputMap& rMap ); |
| 109 | |
| 110 | /** Checks whether the given string corresponds to a valid |
| 111 | property name. |
| 112 | |
| 113 | @return true, if the given name maps to a known property. |
| 114 | */ |
| 115 | bool isPropertyName( const OUString& aPropertyName ) const; |
| 116 | |
| 117 | // XPropertySet implementation |
| 118 | css::uno::Reference< css::beans::XPropertySetInfo > getPropertySetInfo() const; |
| 119 | void setPropertyValue( const OUString& aPropertyName, |
| 120 | const css::uno::Any& aValue ); |
| 121 | css::uno::Any getPropertyValue( const OUString& PropertyName ) const; |
| 122 | void addPropertyChangeListener( const OUString& aPropertyName, |
| 123 | const css::uno::Reference< css::beans::XPropertyChangeListener >& xListener ); |
| 124 | void addVetoableChangeListener( const OUString& aPropertyName, |
| 125 | const css::uno::Reference< css::beans::XVetoableChangeListener >& xListener ); |
| 126 | |
| 127 | private: |
| 128 | std::unique_ptr<MapType> mpMap; |
| 129 | InputMap maMapEntries; |
| 130 | }; |
| 131 | } |
| 132 | |
| 133 | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |