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