1 /************************************************************************* 2 * 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * Copyright 2000, 2010 Oracle and/or its affiliates. 6 * 7 * OpenOffice.org - a multi-platform office productivity suite 8 * 9 * This file is part of OpenOffice.org. 10 * 11 * OpenOffice.org is free software: you can redistribute it and/or modify 12 * it under the terms of the GNU Lesser General Public License version 3 13 * only, as published by the Free Software Foundation. 14 * 15 * OpenOffice.org is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU Lesser General Public License version 3 for more details 19 * (a copy is included in the LICENSE file that accompanied this code). 20 * 21 * You should have received a copy of the GNU Lesser General Public License 22 * version 3 along with OpenOffice.org. If not, see 23 * <http://www.openoffice.org/license.html> 24 * for a copy of the LGPLv3 License. 25 * 26 ************************************************************************/ 27 28 #ifndef SFX_ITEMWRAPPER_HXX 29 #define SFX_ITEMWRAPPER_HXX 30 31 #include "sal/config.h" 32 #include "sfx2/dllapi.h" 33 #include <svl/eitem.hxx> 34 #include <svl/stritem.hxx> 35 #include <svl/intitem.hxx> 36 #include <svl/itemset.hxx> 37 38 // ============================================================================ 39 40 namespace sfx { 41 42 // ============================================================================ 43 // Helpers 44 // ============================================================================ 45 46 class SFX2_DLLPUBLIC ItemWrapperHelper 47 { 48 public: 49 /** Returns the WID of the passed SID in the item set. */ 50 static sal_uInt16 GetWhichId( const SfxItemSet& rItemSet, sal_uInt16 nSlot ); 51 52 /** Returns true, if the passed item set supports the SID. */ 53 static bool IsKnownItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot ); 54 55 /** Returns an item from an item set, if it is not in "don't know" state. 56 @return Pointer to item, or 0 if it has "don't know" state. */ 57 static const SfxPoolItem* GetUniqueItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot ); 58 59 /** Returns the default item from the pool of the passed item set. */ 60 static const SfxPoolItem& GetDefaultItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot ); 61 62 /** Removes an item from rDestSet, if it is default in rOldSet. */ 63 static void RemoveDefaultItem( SfxItemSet& rDestSet, const SfxItemSet& rOldSet, sal_uInt16 nSlot ); 64 }; 65 66 // ============================================================================ 67 // Item wrappers 68 // ============================================================================ 69 70 /** Base class wrapping a single item. 71 72 Objects of this class store the SID of an item. Exchanging data with the 73 item is done with the virtual functions GetItemValue() and SetItemValue(). 74 Derived classes implement these functions according to the item type they 75 work on. 76 77 The current tree of base classes/templates and standard item wrappers: 78 79 SingleItemWrapper< ItemT, ValueT > 80 | 81 +- ValueItemWrapper< ItemT, ValueT > [1] 82 | | 83 | +- BoolItemWrapper [1] 84 | +- Int16ItemWrapper [1] 85 | +- UInt16ItemWrapper [1] 86 | +- Int32ItemWrapper [1] 87 | +- UInt32ItemWrapper [1] 88 | +- StringItemWrapper [1] 89 | 90 +- IdentItemWrapper< ItemT > [1] 91 92 Notes: 93 [1] Standard wrappers ready to use. 94 95 See documentation of class ItemConnectionBase for more details. 96 */ 97 template< typename ItemT, typename ValueT > 98 class SingleItemWrapper 99 { 100 public: 101 typedef ItemT ItemType; 102 typedef ValueT ItemValueType; 103 typedef SingleItemWrapper< ItemT, ValueT > SingleItemWrapperType; 104 105 inline explicit SingleItemWrapper( sal_uInt16 nSlot ) : mnSlot( nSlot ) {} 106 107 /** Returns the SID this wrapper works on. */ 108 inline sal_uInt16 GetSlotId() const { return mnSlot; } 109 110 /** Returns the item from an item set, if it is not in "don't know" state. 111 @descr Similar to ItemWrapperHelper::GetUniqueItem(), but works always 112 with the own SID and returns the correct item type. 113 @return Pointer to item, or 0 if it has "don't know" state. */ 114 const ItemT* GetUniqueItem( const SfxItemSet& rItemSet ) const; 115 /** Returns the default item from the pool of the passed item set. 116 @descr Similar to ItemWrapperHelper::GetDefaultItem(), but works 117 always with the own SID and returns the correct item type. */ 118 const ItemT& GetDefaultItem( const SfxItemSet& rItemSet ) const; 119 120 /** Derived classes return the value of the passed item. */ 121 virtual ValueT GetItemValue( const ItemT& rItem ) const = 0; 122 /** Derived classes set the value at the passed item. */ 123 virtual void SetItemValue( ItemT& rItem, ValueT aValue ) const = 0; 124 125 private: 126 sal_uInt16 mnSlot; /// The SID of this item wrapper. 127 }; 128 129 // ============================================================================ 130 131 /** An item wrapper usable for most types of items. 132 133 The item type must support the following functions: 134 - ValueT ItemT::GetValue() const 135 - void ItemT::SetValue( ValueT ) 136 137 The template parameter InternalValueT can be used to specify the internal 138 value type of the item, if it differs from ValueT. This parameter has to be 139 used to prevent compiler warnings. 140 */ 141 template< typename ItemT, typename ValueT, typename InternalValueT = ValueT > 142 class ValueItemWrapper : public SingleItemWrapper< ItemT, ValueT > 143 { 144 public: 145 inline explicit ValueItemWrapper( sal_uInt16 nSlot ) : 146 SingleItemWrapper< ItemT, ValueT >( nSlot ) {} 147 148 virtual ValueT GetItemValue( const ItemT& rItem ) const 149 { return static_cast< ValueT >( rItem.GetValue() ); } 150 virtual void SetItemValue( ItemT& rItem, ValueT aValue ) const 151 { rItem.SetValue( static_cast< InternalValueT >( aValue ) ); } 152 }; 153 154 // ---------------------------------------------------------------------------- 155 156 typedef ValueItemWrapper< SfxBoolItem, sal_Bool > BoolItemWrapper; 157 typedef ValueItemWrapper< SfxInt16Item, sal_Int16 > Int16ItemWrapper; 158 typedef ValueItemWrapper< SfxUInt16Item, sal_uInt16 > UInt16ItemWrapper; 159 typedef ValueItemWrapper< SfxInt32Item, sal_Int32 > Int32ItemWrapper; 160 typedef ValueItemWrapper< SfxUInt32Item, sal_uInt32 > UInt32ItemWrapper; 161 typedef ValueItemWrapper< SfxStringItem, const String& > StringItemWrapper; 162 163 // ============================================================================ 164 165 /** An item wrapper that uses the item itself as value. */ 166 template< typename ItemT > 167 class IdentItemWrapper : public SingleItemWrapper< ItemT, const ItemT& > 168 { 169 public: 170 inline explicit IdentItemWrapper( sal_uInt16 nSlot ) : 171 SingleItemWrapper< ItemT, const ItemT& >( nSlot ) {} 172 173 virtual const ItemT& GetItemValue( const ItemT& rItem ) const 174 { return rItem; } 175 virtual void SetItemValue( ItemT& rItem, const ItemT& rValue ) const 176 { rItem = rValue; } 177 }; 178 179 // ============================================================================ 180 181 182 // ============================================================================ 183 // *** Implementation of template functions *** 184 // ============================================================================ 185 186 // ============================================================================ 187 // Item wrappers 188 // ============================================================================ 189 190 template< typename ItemT, typename ValueT > 191 const ItemT* SingleItemWrapper< ItemT, ValueT >::GetUniqueItem( const SfxItemSet& rItemSet ) const 192 { 193 return static_cast< const ItemT* >( ItemWrapperHelper::GetUniqueItem( rItemSet, mnSlot ) ); 194 } 195 196 template< typename ItemT, typename ValueT > 197 const ItemT& SingleItemWrapper< ItemT, ValueT >::GetDefaultItem( const SfxItemSet& rItemSet ) const 198 { 199 return static_cast< const ItemT& >( ItemWrapperHelper::GetDefaultItem( rItemSet, mnSlot ) ); 200 } 201 202 // ============================================================================ 203 204 } // namespace sfx 205 206 #endif 207 208