bump product version to 6.3.0.0.beta1
[LibreOffice.git] / svl / source / items / aeitem.cxx
blob37962147e70ce0b9bb70f92f4ddd03b9d91340d7
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>
23 #include <climits>
24 #include <cstddef>
25 #include <vector>
27 struct SfxAllEnumValue_Impl
29 sal_uInt16 nValue;
30 OUString aText;
33 class SfxAllEnumValueArr : public std::vector<SfxAllEnumValue_Impl> {};
36 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which, sal_uInt16 nVal):
37 SfxAllEnumItem_Base(which, nVal)
39 InsertValue( nVal );
42 SfxAllEnumItem::SfxAllEnumItem(sal_uInt16 which):
43 SfxAllEnumItem_Base(which, 0)
47 SfxAllEnumItem::SfxAllEnumItem(const SfxAllEnumItem &rCopy):
48 SfxAllEnumItem_Base(rCopy)
50 if ( rCopy.pValues )
51 pValues.reset( new SfxAllEnumValueArr(*rCopy.pValues) );
54 SfxAllEnumItem::~SfxAllEnumItem()
58 sal_uInt16 SfxAllEnumItem::GetValueCount() const
60 return pValues ? pValues->size() : 0;
63 OUString const & SfxAllEnumItem::GetValueTextByPos( sal_uInt16 nPos ) const
65 assert(pValues && nPos < pValues->size());
66 return (*pValues)[nPos].aText;
69 sal_uInt16 SfxAllEnumItem::GetValueByPos( sal_uInt16 nPos ) const
71 assert(pValues && nPos < pValues->size());
72 return (*pValues)[nPos].nValue;
75 SfxPoolItem* SfxAllEnumItem::Clone( SfxItemPool * ) const
77 return new SfxAllEnumItem(*this);
80 /**
81 * In contrast to @see GetPosByValue(sal_uInt16) const
82 * this internal method returns the position the value would be for non-present values.
84 std::size_t SfxAllEnumItem::GetPosByValue_( sal_uInt16 nVal ) const
86 if ( !pValues )
87 return 0;
89 //FIXME: Optimisation: use binary search or SortArray
90 std::size_t nPos;
91 for ( nPos = 0; nPos < pValues->size(); ++nPos )
92 if ( (*pValues)[nPos].nValue >= nVal )
93 return nPos;
94 return nPos;
97 sal_uInt16 SfxAllEnumItem::GetPosByValue( sal_uInt16 nValue ) const
99 if ( !pValues || pValues->empty() )
100 return nValue;
102 sal_uInt16 nCount = GetValueCount();
103 for (sal_uInt16 i = 0; i < nCount; ++i)
104 if (GetValueByPos(i) == nValue)
105 return i;
106 return USHRT_MAX;
109 void SfxAllEnumItem::InsertValue( sal_uInt16 nValue, const OUString &rValue )
111 SfxAllEnumValue_Impl aVal;
112 aVal.nValue = nValue;
113 aVal.aText = rValue;
114 if ( !pValues )
115 pValues.reset( new SfxAllEnumValueArr );
116 else if ( GetPosByValue( nValue ) != USHRT_MAX )
117 // remove when exists
118 RemoveValue( nValue );
119 // then insert
120 pValues->insert(pValues->begin() + GetPosByValue_(nValue), aVal); // FIXME: Duplicates?
123 void SfxAllEnumItem::InsertValue( sal_uInt16 nValue )
125 SfxAllEnumValue_Impl aVal;
126 aVal.nValue = nValue;
127 aVal.aText = OUString::number(nValue);
128 if ( !pValues )
129 pValues.reset( new SfxAllEnumValueArr );
131 pValues->insert(pValues->begin() + GetPosByValue_(nValue), aVal); // FIXME: Duplicates?
134 void SfxAllEnumItem::RemoveValue( sal_uInt16 nValue )
136 sal_uInt16 nPos = GetPosByValue(nValue);
137 assert(nPos != USHRT_MAX);
138 pValues->erase( pValues->begin() + nPos );
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */