cid#1636690 Dereference after null check
[LibreOffice.git] / svl / source / items / slstitm.cxx
blob1141784397b1538d843ad558d6d09128969ce0ac
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 .
21 #include <svl/slstitm.hxx>
22 #include <svl/poolitem.hxx>
23 #include <com/sun/star/uno/Any.hxx>
24 #include <com/sun/star/uno/Sequence.hxx>
25 #include <comphelper/sequence.hxx>
26 #include <osl/diagnose.h>
27 #include <rtl/ustrbuf.hxx>
28 #include <tools/lineend.hxx>
30 SfxPoolItem* SfxStringListItem::CreateDefault() { return new SfxStringListItem; }
32 SfxStringListItem::SfxStringListItem() :
33 SfxPoolItem( 0, SfxItemType::SfxStringListItemType )
38 SfxStringListItem::SfxStringListItem( sal_uInt16 which, const std::vector<OUString>* pList ) :
39 SfxPoolItem( which, SfxItemType::SfxStringListItemType )
41 // FIXME: Putting an empty list does not work
42 // Therefore the query after the count is commented out
43 if( pList /*!!! && pList->Count() */ )
45 mpList = std::make_shared<std::vector<OUString>>(*pList);
50 SfxStringListItem::~SfxStringListItem()
55 std::vector<OUString>& SfxStringListItem::GetList()
57 if( !mpList )
58 mpList = std::make_shared<std::vector<OUString>>();
59 return *mpList;
62 const std::vector<OUString>& SfxStringListItem::GetList () const
64 return const_cast< SfxStringListItem * >(this)->GetList();
68 bool SfxStringListItem::operator==( const SfxPoolItem& rItem ) const
70 assert(SfxPoolItem::operator==(rItem));
72 const SfxStringListItem& rSSLItem = static_cast<const SfxStringListItem&>(rItem);
74 return mpList == rSSLItem.mpList;
78 bool SfxStringListItem::GetPresentation
80 SfxItemPresentation /*ePresentation*/,
81 MapUnit /*eCoreMetric*/,
82 MapUnit /*ePresentationMetric*/,
83 OUString& rText,
84 const IntlWrapper&
85 ) const
87 rText = "(List)";
88 return false;
91 SfxStringListItem* SfxStringListItem::Clone( SfxItemPool *) const
93 return new SfxStringListItem( *this );
96 void SfxStringListItem::SetString( const OUString& rStr )
98 mpList = std::make_shared<std::vector<OUString>>();
100 OUString aStr(convertLineEnd(rStr, LINEEND_CR));
101 // put last string only if not empty
102 for (sal_Int32 nStart = 0; nStart >= 0 && nStart < aStr.getLength();)
103 mpList->push_back(aStr.getToken(0, '\r', nStart));
107 OUString SfxStringListItem::GetString()
109 OUStringBuffer aStr;
110 if ( mpList )
112 for (auto iter = mpList->begin(), end = mpList->end(); iter != end;)
114 aStr.append(*iter);
115 ++iter;
117 if (iter == end)
118 break;
120 aStr.append(SAL_NEWLINE_STRING);
123 return aStr.makeStringAndClear();
127 void SfxStringListItem::SetStringList( const css::uno::Sequence< OUString >& rList )
129 mpList = std::make_shared<std::vector<OUString>>(
130 comphelper::sequenceToContainer<std::vector<OUString>>(rList));
133 void SfxStringListItem::GetStringList( css::uno::Sequence< OUString >& rList ) const
135 size_t nCount = mpList->size();
137 rList.realloc( nCount );
138 auto pList = rList.getArray();
139 for( size_t i=0; i < nCount; i++ )
140 pList[i] = (*mpList)[i];
143 // virtual
144 bool SfxStringListItem::PutValue( const css::uno::Any& rVal, sal_uInt8 )
146 css::uno::Sequence< OUString > aValue;
147 if ( rVal >>= aValue )
149 SetStringList( aValue );
150 return true;
153 OSL_FAIL( "SfxStringListItem::PutValue - Wrong type!" );
154 return false;
157 // virtual
158 bool SfxStringListItem::QueryValue( css::uno::Any& rVal, sal_uInt8 ) const
160 css::uno::Sequence< OUString > aStringList;
161 GetStringList( aStringList );
162 rVal <<= aStringList;
163 return true;
167 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */