xref: /aoo4110/main/sfx2/inc/sfx2/itemwrapper.hxx (revision b1cdbd2c)
1*b1cdbd2cSJim Jagielski /**************************************************************
2*b1cdbd2cSJim Jagielski  *
3*b1cdbd2cSJim Jagielski  * Licensed to the Apache Software Foundation (ASF) under one
4*b1cdbd2cSJim Jagielski  * or more contributor license agreements.  See the NOTICE file
5*b1cdbd2cSJim Jagielski  * distributed with this work for additional information
6*b1cdbd2cSJim Jagielski  * regarding copyright ownership.  The ASF licenses this file
7*b1cdbd2cSJim Jagielski  * to you under the Apache License, Version 2.0 (the
8*b1cdbd2cSJim Jagielski  * "License"); you may not use this file except in compliance
9*b1cdbd2cSJim Jagielski  * with the License.  You may obtain a copy of the License at
10*b1cdbd2cSJim Jagielski  *
11*b1cdbd2cSJim Jagielski  *   http://www.apache.org/licenses/LICENSE-2.0
12*b1cdbd2cSJim Jagielski  *
13*b1cdbd2cSJim Jagielski  * Unless required by applicable law or agreed to in writing,
14*b1cdbd2cSJim Jagielski  * software distributed under the License is distributed on an
15*b1cdbd2cSJim Jagielski  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*b1cdbd2cSJim Jagielski  * KIND, either express or implied.  See the License for the
17*b1cdbd2cSJim Jagielski  * specific language governing permissions and limitations
18*b1cdbd2cSJim Jagielski  * under the License.
19*b1cdbd2cSJim Jagielski  *
20*b1cdbd2cSJim Jagielski  *************************************************************/
21*b1cdbd2cSJim Jagielski 
22*b1cdbd2cSJim Jagielski 
23*b1cdbd2cSJim Jagielski 
24*b1cdbd2cSJim Jagielski #ifndef SFX_ITEMWRAPPER_HXX
25*b1cdbd2cSJim Jagielski #define SFX_ITEMWRAPPER_HXX
26*b1cdbd2cSJim Jagielski 
27*b1cdbd2cSJim Jagielski #include "sal/config.h"
28*b1cdbd2cSJim Jagielski #include "sfx2/dllapi.h"
29*b1cdbd2cSJim Jagielski #include <svl/eitem.hxx>
30*b1cdbd2cSJim Jagielski #include <svl/stritem.hxx>
31*b1cdbd2cSJim Jagielski #include <svl/intitem.hxx>
32*b1cdbd2cSJim Jagielski #include <svl/itemset.hxx>
33*b1cdbd2cSJim Jagielski 
34*b1cdbd2cSJim Jagielski // ============================================================================
35*b1cdbd2cSJim Jagielski 
36*b1cdbd2cSJim Jagielski namespace sfx {
37*b1cdbd2cSJim Jagielski 
38*b1cdbd2cSJim Jagielski // ============================================================================
39*b1cdbd2cSJim Jagielski // Helpers
40*b1cdbd2cSJim Jagielski // ============================================================================
41*b1cdbd2cSJim Jagielski 
42*b1cdbd2cSJim Jagielski class SFX2_DLLPUBLIC ItemWrapperHelper
43*b1cdbd2cSJim Jagielski {
44*b1cdbd2cSJim Jagielski public:
45*b1cdbd2cSJim Jagielski     /** Returns the WID of the passed SID in the item set. */
46*b1cdbd2cSJim Jagielski     static sal_uInt16       GetWhichId( const SfxItemSet& rItemSet, sal_uInt16 nSlot );
47*b1cdbd2cSJim Jagielski 
48*b1cdbd2cSJim Jagielski     /** Returns true, if the passed item set supports the SID. */
49*b1cdbd2cSJim Jagielski     static bool         IsKnownItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot );
50*b1cdbd2cSJim Jagielski 
51*b1cdbd2cSJim Jagielski     /** Returns an item from an item set, if it is not in "don't know" state.
52*b1cdbd2cSJim Jagielski         @return  Pointer to item, or 0 if it has "don't know" state. */
53*b1cdbd2cSJim Jagielski     static const SfxPoolItem* GetUniqueItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot );
54*b1cdbd2cSJim Jagielski 
55*b1cdbd2cSJim Jagielski     /** Returns the default item from the pool of the passed item set. */
56*b1cdbd2cSJim Jagielski     static const SfxPoolItem& GetDefaultItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot );
57*b1cdbd2cSJim Jagielski 
58*b1cdbd2cSJim Jagielski     /** Removes an item from rDestSet, if it is default in rOldSet. */
59*b1cdbd2cSJim Jagielski     static void         RemoveDefaultItem( SfxItemSet& rDestSet, const SfxItemSet& rOldSet, sal_uInt16 nSlot );
60*b1cdbd2cSJim Jagielski };
61*b1cdbd2cSJim Jagielski 
62*b1cdbd2cSJim Jagielski // ============================================================================
63*b1cdbd2cSJim Jagielski // Item wrappers
64*b1cdbd2cSJim Jagielski // ============================================================================
65*b1cdbd2cSJim Jagielski 
66*b1cdbd2cSJim Jagielski /** Base class wrapping a single item.
67*b1cdbd2cSJim Jagielski 
68*b1cdbd2cSJim Jagielski     Objects of this class store the SID of an item. Exchanging data with the
69*b1cdbd2cSJim Jagielski     item is done with the virtual functions GetItemValue() and SetItemValue().
70*b1cdbd2cSJim Jagielski     Derived classes implement these functions according to the item type they
71*b1cdbd2cSJim Jagielski     work on.
72*b1cdbd2cSJim Jagielski 
73*b1cdbd2cSJim Jagielski     The current tree of base classes/templates and standard item wrappers:
74*b1cdbd2cSJim Jagielski 
75*b1cdbd2cSJim Jagielski     SingleItemWrapper< ItemT, ValueT >
76*b1cdbd2cSJim Jagielski      |
77*b1cdbd2cSJim Jagielski      +- ValueItemWrapper< ItemT, ValueT >   [1]
78*b1cdbd2cSJim Jagielski      |   |
79*b1cdbd2cSJim Jagielski      |   +- BoolItemWrapper   [1]
80*b1cdbd2cSJim Jagielski      |   +- Int16ItemWrapper   [1]
81*b1cdbd2cSJim Jagielski      |   +- UInt16ItemWrapper   [1]
82*b1cdbd2cSJim Jagielski      |   +- Int32ItemWrapper   [1]
83*b1cdbd2cSJim Jagielski      |   +- UInt32ItemWrapper   [1]
84*b1cdbd2cSJim Jagielski      |   +- StringItemWrapper   [1]
85*b1cdbd2cSJim Jagielski      |
86*b1cdbd2cSJim Jagielski      +- IdentItemWrapper< ItemT >   [1]
87*b1cdbd2cSJim Jagielski 
88*b1cdbd2cSJim Jagielski     Notes:
89*b1cdbd2cSJim Jagielski     [1] Standard wrappers ready to use.
90*b1cdbd2cSJim Jagielski 
91*b1cdbd2cSJim Jagielski     See documentation of class ItemConnectionBase for more details.
92*b1cdbd2cSJim Jagielski  */
93*b1cdbd2cSJim Jagielski template< typename ItemT, typename ValueT >
94*b1cdbd2cSJim Jagielski class SingleItemWrapper
95*b1cdbd2cSJim Jagielski {
96*b1cdbd2cSJim Jagielski public:
97*b1cdbd2cSJim Jagielski     typedef ItemT                               ItemType;
98*b1cdbd2cSJim Jagielski     typedef ValueT                              ItemValueType;
99*b1cdbd2cSJim Jagielski     typedef SingleItemWrapper< ItemT, ValueT >  SingleItemWrapperType;
100*b1cdbd2cSJim Jagielski 
SingleItemWrapper(sal_uInt16 nSlot)101*b1cdbd2cSJim Jagielski     inline explicit     SingleItemWrapper( sal_uInt16 nSlot ) : mnSlot( nSlot ) {}
102*b1cdbd2cSJim Jagielski 
103*b1cdbd2cSJim Jagielski     /** Returns the SID this wrapper works on. */
GetSlotId() const104*b1cdbd2cSJim Jagielski     inline sal_uInt16       GetSlotId() const { return mnSlot; }
105*b1cdbd2cSJim Jagielski 
106*b1cdbd2cSJim Jagielski     /** Returns the item from an item set, if it is not in "don't know" state.
107*b1cdbd2cSJim Jagielski         @descr  Similar to ItemWrapperHelper::GetUniqueItem(), but works always
108*b1cdbd2cSJim Jagielski         with the own SID and returns the correct item type.
109*b1cdbd2cSJim Jagielski         @return Pointer to item, or 0 if it has "don't know" state. */
110*b1cdbd2cSJim Jagielski     const ItemT*        GetUniqueItem( const SfxItemSet& rItemSet ) const;
111*b1cdbd2cSJim Jagielski     /** Returns the default item from the pool of the passed item set.
112*b1cdbd2cSJim Jagielski         @descr  Similar to ItemWrapperHelper::GetDefaultItem(), but works
113*b1cdbd2cSJim Jagielski         always with the own SID and returns the correct item type. */
114*b1cdbd2cSJim Jagielski     const ItemT&        GetDefaultItem( const SfxItemSet& rItemSet ) const;
115*b1cdbd2cSJim Jagielski 
116*b1cdbd2cSJim Jagielski     /** Derived classes return the value of the passed item. */
117*b1cdbd2cSJim Jagielski     virtual ValueT      GetItemValue( const ItemT& rItem ) const = 0;
118*b1cdbd2cSJim Jagielski     /** Derived classes set the value at the passed item. */
119*b1cdbd2cSJim Jagielski     virtual void        SetItemValue( ItemT& rItem, ValueT aValue ) const = 0;
120*b1cdbd2cSJim Jagielski 
121*b1cdbd2cSJim Jagielski private:
122*b1cdbd2cSJim Jagielski     sal_uInt16              mnSlot;     /// The SID of this item wrapper.
123*b1cdbd2cSJim Jagielski };
124*b1cdbd2cSJim Jagielski 
125*b1cdbd2cSJim Jagielski // ============================================================================
126*b1cdbd2cSJim Jagielski 
127*b1cdbd2cSJim Jagielski /** An item wrapper usable for most types of items.
128*b1cdbd2cSJim Jagielski 
129*b1cdbd2cSJim Jagielski     The item type must support the following functions:
130*b1cdbd2cSJim Jagielski     - ValueT ItemT::GetValue() const
131*b1cdbd2cSJim Jagielski     - void ItemT::SetValue( ValueT )
132*b1cdbd2cSJim Jagielski 
133*b1cdbd2cSJim Jagielski     The template parameter InternalValueT can be used to specify the internal
134*b1cdbd2cSJim Jagielski     value type of the item, if it differs from ValueT. This parameter has to be
135*b1cdbd2cSJim Jagielski     used to prevent compiler warnings.
136*b1cdbd2cSJim Jagielski  */
137*b1cdbd2cSJim Jagielski template< typename ItemT, typename ValueT, typename InternalValueT = ValueT >
138*b1cdbd2cSJim Jagielski class ValueItemWrapper : public SingleItemWrapper< ItemT, ValueT >
139*b1cdbd2cSJim Jagielski {
140*b1cdbd2cSJim Jagielski public:
ValueItemWrapper(sal_uInt16 nSlot)141*b1cdbd2cSJim Jagielski     inline explicit     ValueItemWrapper( sal_uInt16 nSlot ) :
142*b1cdbd2cSJim Jagielski                             SingleItemWrapper< ItemT, ValueT >( nSlot ) {}
143*b1cdbd2cSJim Jagielski 
GetItemValue(const ItemT & rItem) const144*b1cdbd2cSJim Jagielski     virtual ValueT      GetItemValue( const ItemT& rItem ) const
145*b1cdbd2cSJim Jagielski                             { return static_cast< ValueT >( rItem.GetValue() ); }
SetItemValue(ItemT & rItem,ValueT aValue) const146*b1cdbd2cSJim Jagielski     virtual void        SetItemValue( ItemT& rItem, ValueT aValue ) const
147*b1cdbd2cSJim Jagielski                             { rItem.SetValue( static_cast< InternalValueT >( aValue ) ); }
148*b1cdbd2cSJim Jagielski };
149*b1cdbd2cSJim Jagielski 
150*b1cdbd2cSJim Jagielski // ----------------------------------------------------------------------------
151*b1cdbd2cSJim Jagielski 
152*b1cdbd2cSJim Jagielski typedef ValueItemWrapper< SfxBoolItem,   sal_Bool >          BoolItemWrapper;
153*b1cdbd2cSJim Jagielski typedef ValueItemWrapper< SfxInt16Item,  sal_Int16 >         Int16ItemWrapper;
154*b1cdbd2cSJim Jagielski typedef ValueItemWrapper< SfxUInt16Item, sal_uInt16 >        UInt16ItemWrapper;
155*b1cdbd2cSJim Jagielski typedef ValueItemWrapper< SfxInt32Item,  sal_Int32 >         Int32ItemWrapper;
156*b1cdbd2cSJim Jagielski typedef ValueItemWrapper< SfxUInt32Item, sal_uInt32 >        UInt32ItemWrapper;
157*b1cdbd2cSJim Jagielski typedef ValueItemWrapper< SfxStringItem, const String& > StringItemWrapper;
158*b1cdbd2cSJim Jagielski 
159*b1cdbd2cSJim Jagielski // ============================================================================
160*b1cdbd2cSJim Jagielski 
161*b1cdbd2cSJim Jagielski /** An item wrapper that uses the item itself as value. */
162*b1cdbd2cSJim Jagielski template< typename ItemT >
163*b1cdbd2cSJim Jagielski class IdentItemWrapper : public SingleItemWrapper< ItemT, const ItemT& >
164*b1cdbd2cSJim Jagielski {
165*b1cdbd2cSJim Jagielski public:
IdentItemWrapper(sal_uInt16 nSlot)166*b1cdbd2cSJim Jagielski     inline explicit     IdentItemWrapper( sal_uInt16 nSlot ) :
167*b1cdbd2cSJim Jagielski                             SingleItemWrapper< ItemT, const ItemT& >( nSlot ) {}
168*b1cdbd2cSJim Jagielski 
GetItemValue(const ItemT & rItem) const169*b1cdbd2cSJim Jagielski     virtual const ItemT& GetItemValue( const ItemT& rItem ) const
170*b1cdbd2cSJim Jagielski                             { return rItem; }
SetItemValue(ItemT & rItem,const ItemT & rValue) const171*b1cdbd2cSJim Jagielski     virtual void        SetItemValue( ItemT& rItem, const ItemT& rValue ) const
172*b1cdbd2cSJim Jagielski                             { rItem = rValue; }
173*b1cdbd2cSJim Jagielski };
174*b1cdbd2cSJim Jagielski 
175*b1cdbd2cSJim Jagielski // ============================================================================
176*b1cdbd2cSJim Jagielski 
177*b1cdbd2cSJim Jagielski 
178*b1cdbd2cSJim Jagielski // ============================================================================
179*b1cdbd2cSJim Jagielski //               ***  Implementation of template functions  ***
180*b1cdbd2cSJim Jagielski // ============================================================================
181*b1cdbd2cSJim Jagielski 
182*b1cdbd2cSJim Jagielski // ============================================================================
183*b1cdbd2cSJim Jagielski // Item wrappers
184*b1cdbd2cSJim Jagielski // ============================================================================
185*b1cdbd2cSJim Jagielski 
186*b1cdbd2cSJim Jagielski template< typename ItemT, typename ValueT >
GetUniqueItem(const SfxItemSet & rItemSet) const187*b1cdbd2cSJim Jagielski const ItemT* SingleItemWrapper< ItemT, ValueT >::GetUniqueItem( const SfxItemSet& rItemSet ) const
188*b1cdbd2cSJim Jagielski {
189*b1cdbd2cSJim Jagielski     return static_cast< const ItemT* >( ItemWrapperHelper::GetUniqueItem( rItemSet, mnSlot ) );
190*b1cdbd2cSJim Jagielski }
191*b1cdbd2cSJim Jagielski 
192*b1cdbd2cSJim Jagielski template< typename ItemT, typename ValueT >
GetDefaultItem(const SfxItemSet & rItemSet) const193*b1cdbd2cSJim Jagielski const ItemT& SingleItemWrapper< ItemT, ValueT >::GetDefaultItem( const SfxItemSet& rItemSet ) const
194*b1cdbd2cSJim Jagielski {
195*b1cdbd2cSJim Jagielski     return static_cast< const ItemT& >( ItemWrapperHelper::GetDefaultItem( rItemSet, mnSlot ) );
196*b1cdbd2cSJim Jagielski }
197*b1cdbd2cSJim Jagielski 
198*b1cdbd2cSJim Jagielski // ============================================================================
199*b1cdbd2cSJim Jagielski 
200*b1cdbd2cSJim Jagielski } // namespace sfx
201*b1cdbd2cSJim Jagielski 
202*b1cdbd2cSJim Jagielski #endif
203*b1cdbd2cSJim Jagielski 
204