GPU-Calc: remove Alloc_Host_Ptr for clmem of NAN vector
[LibreOffice.git] / svl / source / items / aeitem.cxx
blob13f8509de84adbaf75236d86f2bed6516eb4475c
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 #include <rtl/ustring.hxx>
21 #include <svl/aeitem.hxx>
22 #include <vector>
24 // STATIC DATA -----------------------------------------------------------
26 DBG_NAME(SfxAllEnumItem)
28 TYPEINIT1_AUTOFACTORY(SfxAllEnumItem, SfxEnumItem)
30 // -----------------------------------------------------------------------
32 struct SfxAllEnumValue_Impl
34 sal_uInt16 nValue;
35 OUString aText;
38 class SfxAllEnumValueArr : public std::vector<SfxAllEnumValue_Impl*>
40 public:
41 ~SfxAllEnumValueArr()
43 for( const_iterator it = begin(); it != end(); ++it )
44 delete *it;
48 // -----------------------------------------------------------------------
50 SfxAllEnumItem::SfxAllEnumItem() :
51 SfxEnumItem(),
52 pValues( 0 ),
53 pDisabledValues( 0 )
57 // -----------------------------------------------------------------------
59 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which, sal_uInt16 nVal):
60 SfxEnumItem(which, nVal),
61 pValues( 0 ),
62 pDisabledValues( 0 )
64 DBG_CTOR(SfxAllEnumItem, 0);
65 InsertValue( nVal );
68 // -----------------------------------------------------------------------
70 SfxAllEnumItem::SfxAllEnumItem( sal_uInt16 which, SvStream &rStream ):
71 SfxEnumItem(which, rStream),
72 pValues( 0 ),
73 pDisabledValues( 0 )
75 DBG_CTOR(SfxAllEnumItem, 0);
76 InsertValue( GetValue() );
79 // -----------------------------------------------------------------------
82 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which):
83 SfxEnumItem(which, 0),
84 pValues( 0 ),
85 pDisabledValues( 0 )
87 DBG_CTOR(SfxAllEnumItem, 0);
91 // -----------------------------------------------------------------------
93 SfxAllEnumItem::SfxAllEnumItem(const SfxAllEnumItem &rCopy):
94 SfxEnumItem(rCopy),
95 pValues(0),
96 pDisabledValues( 0 )
98 DBG_CTOR(SfxAllEnumItem, 0);
99 if ( !rCopy.pValues )
100 return;
102 pValues = new SfxAllEnumValueArr;
104 for ( sal_uInt16 nPos = 0; nPos < rCopy.pValues->size(); ++nPos )
106 SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
107 pVal->nValue = (*rCopy.pValues)[nPos]->nValue;
108 pVal->aText = (*rCopy.pValues)[nPos]->aText;
109 pValues->insert( pValues->begin() + nPos, pVal );
112 if( rCopy.pDisabledValues )
114 pDisabledValues = new std::vector<sal_uInt16>( *(rCopy.pDisabledValues) );
118 // -----------------------------------------------------------------------
120 SfxAllEnumItem::~SfxAllEnumItem()
122 DBG_DTOR(SfxAllEnumItem, 0);
123 delete pValues;
124 delete pDisabledValues;
127 // -----------------------------------------------------------------------
129 sal_uInt16 SfxAllEnumItem::GetValueCount() const
131 DBG_CHKTHIS(SfxAllEnumItem, 0);
132 return pValues ? pValues->size() : 0;
135 // -----------------------------------------------------------------------
137 OUString SfxAllEnumItem::GetValueTextByPos( sal_uInt16 nPos ) const
139 DBG_CHKTHIS(SfxAllEnumItem, 0);
140 DBG_ASSERT( pValues && nPos < pValues->size(), "enum overflow" );
141 return (*pValues)[nPos]->aText;
144 // -----------------------------------------------------------------------
146 sal_uInt16 SfxAllEnumItem::GetValueByPos( sal_uInt16 nPos ) const
148 DBG_CHKTHIS(SfxAllEnumItem, 0);
149 DBG_ASSERT( pValues && nPos < pValues->size(), "enum overflow" );
150 return (*pValues)[nPos]->nValue;
153 // -----------------------------------------------------------------------
155 SfxPoolItem* SfxAllEnumItem::Clone( SfxItemPool * ) const
157 DBG_CHKTHIS(SfxAllEnumItem, 0);
158 return new SfxAllEnumItem(*this);
161 // -----------------------------------------------------------------------
163 SfxPoolItem* SfxAllEnumItem::Create( SvStream & rStream, sal_uInt16 ) const
165 DBG_CHKTHIS(SfxAllEnumItem, 0);
166 return new SfxAllEnumItem( Which(), rStream );
170 // -----------------------------------------------------------------------
172 sal_uInt16 SfxAllEnumItem::_GetPosByValue( sal_uInt16 nVal ) const
174 /* [Beschreibung]
176 Im Ggs. zu <SfxEnumItemInterface::GetPosByValue(sal_uInt16)const> liefert
177 diese interne Methode bei nicht vorhandenen Values die Position,
178 an der der Wert liegen w"urde.
182 DBG_CHKTHIS(SfxAllEnumItem, 0);
184 if ( !pValues )
185 return 0;
187 //!O: binaere Suche oder SortArray verwenden
188 sal_uInt16 nPos;
189 for ( nPos = 0; nPos < pValues->size(); ++nPos )
190 if ( (*pValues)[nPos]->nValue >= nVal )
191 return nPos;
192 return nPos;
195 // -----------------------------------------------------------------------
197 sal_uInt16 SfxAllEnumItem::GetPosByValue( sal_uInt16 nValue ) const
199 /* [Beschreibung]
201 Liefert im Gegensatz zu <SfxEnumItemInterface::GetPosByValue(sal_uInt16)const>
202 immer nValue zur"uck, solange nicht mindestens ein Wert mit einer der
203 Methoden <SfxAllEnumItem::InsertValue()> eingef"ugt wurde.
207 DBG_CHKTHIS(SfxAllEnumItem, 0);
209 if ( !pValues || pValues->empty() )
210 return nValue;
212 return SfxEnumItem::GetPosByValue( nValue );
215 // -----------------------------------------------------------------------
217 void SfxAllEnumItem::InsertValue( sal_uInt16 nValue, const OUString &rValue )
219 DBG_CHKTHIS(SfxAllEnumItem, 0);
220 SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
221 pVal->nValue = nValue;
222 pVal->aText = rValue;
223 if ( !pValues )
224 pValues = new SfxAllEnumValueArr;
225 else if ( GetPosByValue( nValue ) != USHRT_MAX )
226 // remove when exists
227 RemoveValue( nValue );
228 // then insert
229 pValues->insert( pValues->begin() + _GetPosByValue(nValue), pVal ); //! doppelte?!
232 // -----------------------------------------------------------------------
234 void SfxAllEnumItem::InsertValue( sal_uInt16 nValue )
236 DBG_CHKTHIS(SfxAllEnumItem, 0);
237 SfxAllEnumValue_Impl *pVal = new SfxAllEnumValue_Impl;
238 pVal->nValue = nValue;
239 pVal->aText = OUString::number(nValue);
240 if ( !pValues )
241 pValues = new SfxAllEnumValueArr;
243 pValues->insert( pValues->begin() + _GetPosByValue(nValue), pVal ); //! doppelte?!
246 void SfxAllEnumItem::DisableValue( sal_uInt16 nValue )
248 DBG_CHKTHIS(SfxAllEnumItem, 0);
249 if ( !pDisabledValues )
250 pDisabledValues = new std::vector<sal_uInt16>;
252 pDisabledValues->push_back( nValue );
255 sal_Bool SfxAllEnumItem::IsEnabled( sal_uInt16 nValue ) const
257 if ( pDisabledValues )
259 for ( size_t i=0; i<pDisabledValues->size(); i++ )
260 if ( (*pDisabledValues)[i] == nValue )
261 return sal_False;
264 return sal_True;
267 // -----------------------------------------------------------------------
269 void SfxAllEnumItem::RemoveValue( sal_uInt16 nValue )
271 DBG_CHKTHIS(SfxAllEnumItem, 0);
272 sal_uInt16 nPos = GetPosByValue(nValue);
273 DBG_ASSERT( nPos != USHRT_MAX, "removing value not in enum" );
274 pValues->erase( pValues->begin() + nPos );
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */