Line data Source code
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_CANVAS_PROPERTYSETHELPER_HXX
21 : #define INCLUDED_CANVAS_PROPERTYSETHELPER_HXX
22 :
23 : #include <com/sun/star/beans/XPropertySetInfo.hpp>
24 : #include <com/sun/star/beans/XPropertySet.hpp>
25 : #include <canvas/canvastools.hxx>
26 :
27 : #include <boost/function.hpp>
28 : #include <vector>
29 : #include <memory>
30 :
31 : #include <canvas/canvastoolsdllapi.h>
32 :
33 : namespace canvas
34 : {
35 : /** Really simplistic XPropertySet helper for properties.
36 :
37 : This class provides easy access to properties, referenced via
38 : ASCII strings. The name/property modification callbacks pairs
39 : are passed into this class via a vector. Each time a property
40 : is set or queried, the corresponding getter or setter callback
41 : is called.
42 :
43 : Use this class as a delegate for the corresponding
44 : XPropertySet methods, and take care of UNO XInterface and lock
45 : handling by yourself.
46 :
47 : The core responsibility of this class is the name/value
48 : mapping for property sets.
49 : */
50 3 : class CANVASTOOLS_DLLPUBLIC PropertySetHelper
51 : {
52 : public:
53 : typedef boost::function0< ::com::sun::star::uno::Any > GetterType;
54 : typedef boost::function1<void, const ::com::sun::star::uno::Any&> SetterType;
55 114 : struct Callbacks
56 : {
57 : GetterType getter;
58 : SetterType setter;
59 : };
60 : typedef tools::ValueMap< Callbacks > MapType;
61 : typedef std::vector< MapType::MapEntry > InputMap;
62 :
63 3 : class MakeMap : public InputMap
64 : {
65 : public:
66 0 : MakeMap(const char* name,
67 : const GetterType& getter,
68 : const SetterType& setter)
69 0 : {
70 0 : MapType::MapEntry aEntry={name, {getter, setter}};
71 0 : this->push_back(aEntry);
72 0 : }
73 3 : MakeMap(const char* name,
74 : const GetterType& getter)
75 3 : {
76 3 : MapType::MapEntry aEntry={name, {getter, SetterType()}};
77 3 : this->push_back(aEntry);
78 3 : }
79 3 : MakeMap& operator()(const char* name,
80 : const GetterType& getter,
81 : const SetterType& setter)
82 : {
83 3 : MapType::MapEntry aEntry={name, {getter, setter}};
84 3 : this->push_back(aEntry);
85 3 : return *this;
86 : }
87 6 : MakeMap& operator()(const char* name,
88 : const GetterType& getter)
89 : {
90 6 : MapType::MapEntry aEntry={name, {getter, SetterType()}};
91 6 : this->push_back(aEntry);
92 6 : return *this;
93 : }
94 : };
95 :
96 : /** Create helper with zero properties
97 : */
98 : PropertySetHelper();
99 :
100 : /** Init helper with new name/value map
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.
106 : */
107 : void initProperties( const InputMap& rMap );
108 :
109 : /** Add given properties to helper
110 :
111 : @param rMap
112 : Vector of name/function pointers. Each name is offered as
113 : a property, and reading/writing to this property is passed
114 : on to the given function pointer. These name/function
115 : pairs are added to the already existing ones.
116 : */
117 : void addProperties( const InputMap& rMap );
118 :
119 : /** Checks whether the given string corresponds to a valid
120 : property name.
121 :
122 : @return true, if the given name maps to a known property.
123 : */
124 : bool isPropertyName( const OUString& aPropertyName ) const;
125 :
126 : /** Request the currently active map
127 : */
128 0 : const InputMap& getPropertyMap() const { return maMapEntries; }
129 :
130 : // XPropertySet implementation
131 : ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertySetInfo > getPropertySetInfo() const;
132 : void setPropertyValue( const OUString& aPropertyName,
133 : const ::com::sun::star::uno::Any& aValue );
134 : ::com::sun::star::uno::Any getPropertyValue( const OUString& PropertyName ) const;
135 : void addPropertyChangeListener( const OUString& aPropertyName,
136 : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XPropertyChangeListener >& xListener );
137 : void addVetoableChangeListener( const OUString& aPropertyName,
138 : const ::com::sun::star::uno::Reference< ::com::sun::star::beans::XVetoableChangeListener >& xListener );
139 :
140 : private:
141 : std::unique_ptr<MapType> mpMap;
142 : InputMap maMapEntries;
143 : };
144 : }
145 :
146 : #endif /* INCLUDED_CANVAS_PROPERTYSETHELPER_HXX */
147 :
148 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|