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