Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / include / sfx2 / itemwrapper.hxx
blob64fbf6d2282474512dedfb53635451243b1cabfe
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 INCLUDED_SFX2_ITEMWRAPPER_HXX
21 #define INCLUDED_SFX2_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>
31 namespace sfx {
34 // Helpers
37 class SFX2_DLLPUBLIC ItemWrapperHelper
39 public:
40 /** Returns the WID of the passed SID in the item set. */
41 static sal_uInt16 GetWhichId( const SfxItemSet& rItemSet, sal_uInt16 nSlot );
43 /** Returns true, if the passed item set supports the SID. */
44 static bool IsKnownItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot );
46 /** Returns an item from an item set, if it is not in "don't know" state.
47 @return Pointer to item, or 0 if it has "don't know" state. */
48 static const SfxPoolItem* GetUniqueItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot );
50 /** Returns the default item from the pool of the passed item set. */
51 static const SfxPoolItem& GetDefaultItem( const SfxItemSet& rItemSet, sal_uInt16 nSlot );
53 /** Removes an item from rDestSet, if it is default in rOldSet. */
54 static void RemoveDefaultItem( SfxItemSet& rDestSet, const SfxItemSet& rOldSet, sal_uInt16 nSlot );
58 // Item wrappers
61 /** Base class wrapping a single item.
63 Objects of this class store the SID of an item. Exchanging data with the
64 item is done with the virtual functions GetItemValue() and SetItemValue().
65 Derived classes implement these functions according to the item type they
66 work on.
68 The current tree of base classes/templates and standard item wrappers:
70 SingleItemWrapper< ItemT, ValueT >
72 +- ValueItemWrapper< ItemT, ValueT > [1]
73 | |
74 | +- BoolItemWrapper [1]
75 | +- UInt16ItemWrapper [1]
76 | +- Int32ItemWrapper [1]
78 +- IdentItemWrapper< ItemT > [1]
80 Notes:
81 [1] Standard wrappers ready to use.
83 See documentation of class ItemConnectionBase for more details.
85 template< typename ItemT, typename ValueT >
86 class SingleItemWrapper
88 public:
89 typedef ItemT ItemType;
90 typedef ValueT ItemValueType;
91 typedef SingleItemWrapper< ItemT, ValueT > SingleItemWrapperType;
93 inline explicit SingleItemWrapper( sal_uInt16 nSlot ) : mnSlot( nSlot ) {}
95 virtual ~SingleItemWrapper() {}
97 /** Returns the SID this wrapper works on. */
98 inline sal_uInt16 GetSlotId() const { return mnSlot; }
100 /** Returns the item from an item set, if it is not in "don't know" state.
101 @descr Similar to ItemWrapperHelper::GetUniqueItem(), but works always
102 with the own SID and returns the correct item type.
103 @return Pointer to item, or 0 if it has "don't know" state. */
104 const ItemT* GetUniqueItem( const SfxItemSet& rItemSet ) const;
105 /** Returns the default item from the pool of the passed item set.
106 @descr Similar to ItemWrapperHelper::GetDefaultItem(), but works
107 always with the own SID and returns the correct item type. */
108 const ItemT& GetDefaultItem( const SfxItemSet& rItemSet ) const;
110 /** Derived classes return the value of the passed item. */
111 virtual ValueT GetItemValue( const ItemT& rItem ) const = 0;
112 /** Derived classes set the value at the passed item. */
113 virtual void SetItemValue( ItemT& rItem, ValueT aValue ) const = 0;
115 private:
116 sal_uInt16 mnSlot; /// The SID of this item wrapper.
120 /** An item wrapper usable for most types of items.
122 The item type must support the following functions:
123 - ValueT ItemT::GetValue() const
124 - void ItemT::SetValue( ValueT )
126 The template parameter InternalValueT can be used to specify the internal
127 value type of the item, if it differs from ValueT. This parameter has to be
128 used to prevent compiler warnings.
130 template< typename ItemT, typename ValueT, typename InternalValueT = ValueT >
131 class ValueItemWrapper : public SingleItemWrapper< ItemT, ValueT >
133 public:
134 inline explicit ValueItemWrapper( sal_uInt16 nSlot ) :
135 SingleItemWrapper< ItemT, ValueT >( nSlot ) {}
137 virtual ~ValueItemWrapper() {}
139 virtual ValueT GetItemValue( const ItemT& rItem ) const SAL_OVERRIDE
140 { return static_cast< ValueT >( rItem.GetValue() ); }
141 virtual void SetItemValue( ItemT& rItem, ValueT aValue ) const SAL_OVERRIDE
142 { rItem.SetValue( static_cast< InternalValueT >( aValue ) ); }
146 typedef ValueItemWrapper< SfxBoolItem, sal_Bool > BoolItemWrapper;
147 typedef ValueItemWrapper< SfxUInt16Item, sal_uInt16 > UInt16ItemWrapper;
148 typedef ValueItemWrapper< SfxInt32Item, sal_Int32 > Int32ItemWrapper;
151 /** An item wrapper that uses the item itself as value. */
152 template< typename ItemT >
153 class IdentItemWrapper : public SingleItemWrapper< ItemT, const ItemT& >
155 public:
156 inline explicit IdentItemWrapper( sal_uInt16 nSlot ) :
157 SingleItemWrapper< ItemT, const ItemT& >( nSlot ) {}
159 virtual ~IdentItemWrapper() {}
161 virtual const ItemT& GetItemValue( const ItemT& rItem ) const SAL_OVERRIDE
162 { return rItem; }
163 virtual void SetItemValue( ItemT& rItem, const ItemT& rValue ) const SAL_OVERRIDE
164 { rItem = rValue; }
168 // *** Implementation of template functions ***
171 // Item wrappers
174 template< typename ItemT, typename ValueT >
175 const ItemT* SingleItemWrapper< ItemT, ValueT >::GetUniqueItem( const SfxItemSet& rItemSet ) const
177 return static_cast< const ItemT* >( ItemWrapperHelper::GetUniqueItem( rItemSet, mnSlot ) );
180 template< typename ItemT, typename ValueT >
181 const ItemT& SingleItemWrapper< ItemT, ValueT >::GetDefaultItem( const SfxItemSet& rItemSet ) const
183 return static_cast< const ItemT& >( ItemWrapperHelper::GetDefaultItem( rItemSet, mnSlot ) );
187 } // namespace sfx
189 #endif
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */