update credits
[LibreOffice.git] / include / sfx2 / itemwrapper.hxx
blob7143fadf092f4df223d9030d0429ed80bc962695
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #ifndef SFX_ITEMWRAPPER_HXX
21 #define SFX_ITEMWRAPPER_HXX
23 #include "sal/config.h"
24 #include "sfx2/dllapi.h"
25 #include <svl/eitem.hxx>
26 #include <svl/stritem.hxx>
27 #include <svl/intitem.hxx>
28 #include <svl/itemset.hxx>
30 // ============================================================================
32 namespace sfx {
34 // ============================================================================
35 // Helpers
36 // ============================================================================
38 class SFX2_DLLPUBLIC ItemWrapperHelper
40 public:
41 /** Returns the WID of the passed SID in the item set. */
42 static sal_uInt16 GetWhichId( const SfxItemSet& rItemSet, sal_uInt16 nSlot );
44 /** Returns true, if the passed item set supports the SID. */
45 static bool IsKnownItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot );
47 /** Returns an item from an item set, if it is not in "don't know" state.
48 @return Pointer to item, or 0 if it has "don't know" state. */
49 static const SfxPoolItem* GetUniqueItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot );
51 /** Returns the default item from the pool of the passed item set. */
52 static const SfxPoolItem& GetDefaultItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot );
54 /** Removes an item from rDestSet, if it is default in rOldSet. */
55 static void RemoveDefaultItem( SfxItemSet& rDestSet, const SfxItemSet& rOldSet, sal_uInt16 nSlot );
58 // ============================================================================
59 // Item wrappers
60 // ============================================================================
62 /** Base class wrapping a single item.
64 Objects of this class store the SID of an item. Exchanging data with the
65 item is done with the virtual functions GetItemValue() and SetItemValue().
66 Derived classes implement these functions according to the item type they
67 work on.
69 The current tree of base classes/templates and standard item wrappers:
71 SingleItemWrapper< ItemT, ValueT >
73 +- ValueItemWrapper< ItemT, ValueT > [1]
74 | |
75 | +- BoolItemWrapper [1]
76 | +- Int16ItemWrapper [1]
77 | +- UInt16ItemWrapper [1]
78 | +- Int32ItemWrapper [1]
79 | +- UInt32ItemWrapper [1]
80 | +- StringItemWrapper [1]
82 +- IdentItemWrapper< ItemT > [1]
84 Notes:
85 [1] Standard wrappers ready to use.
87 See documentation of class ItemConnectionBase for more details.
89 template< typename ItemT, typename ValueT >
90 class SingleItemWrapper
92 public:
93 typedef ItemT ItemType;
94 typedef ValueT ItemValueType;
95 typedef SingleItemWrapper< ItemT, ValueT > SingleItemWrapperType;
97 inline explicit SingleItemWrapper( sal_uInt16 nSlot ) : mnSlot( nSlot ) {}
99 virtual ~SingleItemWrapper() {}
101 /** Returns the SID this wrapper works on. */
102 inline sal_uInt16 GetSlotId() const { return mnSlot; }
104 /** Returns the item from an item set, if it is not in "don't know" state.
105 @descr Similar to ItemWrapperHelper::GetUniqueItem(), but works always
106 with the own SID and returns the correct item type.
107 @return Pointer to item, or 0 if it has "don't know" state. */
108 const ItemT* GetUniqueItem( const SfxItemSet& rItemSet ) const;
109 /** Returns the default item from the pool of the passed item set.
110 @descr Similar to ItemWrapperHelper::GetDefaultItem(), but works
111 always with the own SID and returns the correct item type. */
112 const ItemT& GetDefaultItem( const SfxItemSet& rItemSet ) const;
114 /** Derived classes return the value of the passed item. */
115 virtual ValueT GetItemValue( const ItemT& rItem ) const = 0;
116 /** Derived classes set the value at the passed item. */
117 virtual void SetItemValue( ItemT& rItem, ValueT aValue ) const = 0;
119 private:
120 sal_uInt16 mnSlot; /// The SID of this item wrapper.
123 // ============================================================================
125 /** An item wrapper usable for most types of items.
127 The item type must support the following functions:
128 - ValueT ItemT::GetValue() const
129 - void ItemT::SetValue( ValueT )
131 The template parameter InternalValueT can be used to specify the internal
132 value type of the item, if it differs from ValueT. This parameter has to be
133 used to prevent compiler warnings.
135 template< typename ItemT, typename ValueT, typename InternalValueT = ValueT >
136 class ValueItemWrapper : public SingleItemWrapper< ItemT, ValueT >
138 public:
139 inline explicit ValueItemWrapper( sal_uInt16 nSlot ) :
140 SingleItemWrapper< ItemT, ValueT >( nSlot ) {}
142 virtual ~ValueItemWrapper() {}
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 ) ); }
150 // ----------------------------------------------------------------------------
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;
159 // ============================================================================
161 /** An item wrapper that uses the item itself as value. */
162 template< typename ItemT >
163 class IdentItemWrapper : public SingleItemWrapper< ItemT, const ItemT& >
165 public:
166 inline explicit IdentItemWrapper( sal_uInt16 nSlot ) :
167 SingleItemWrapper< ItemT, const ItemT& >( nSlot ) {}
169 virtual ~IdentItemWrapper() {}
171 virtual const ItemT& GetItemValue( const ItemT& rItem ) const
172 { return rItem; }
173 virtual void SetItemValue( ItemT& rItem, const ItemT& rValue ) const
174 { rItem = rValue; }
177 // ============================================================================
180 // ============================================================================
181 // *** Implementation of template functions ***
182 // ============================================================================
184 // ============================================================================
185 // Item wrappers
186 // ============================================================================
188 template< typename ItemT, typename ValueT >
189 const ItemT* SingleItemWrapper< ItemT, ValueT >::GetUniqueItem( const SfxItemSet& rItemSet ) const
191 return static_cast< const ItemT* >( ItemWrapperHelper::GetUniqueItem( rItemSet, mnSlot ) );
194 template< typename ItemT, typename ValueT >
195 const ItemT& SingleItemWrapper< ItemT, ValueT >::GetDefaultItem( const SfxItemSet& rItemSet ) const
197 return static_cast< const ItemT& >( ItemWrapperHelper::GetDefaultItem( rItemSet, mnSlot ) );
200 // ============================================================================
202 } // namespace sfx
204 #endif
206 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */