update credits
[LibreOffice.git] / include / comphelper / sequenceasvector.hxx
blob305eba9245ff06a31c30090ebc6941c149ea6bce
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 _COMPHELPER_SEQUENCEASVECTOR_HXX_
21 #define _COMPHELPER_SEQUENCEASVECTOR_HXX_
23 #include <vector>
24 #include <algorithm>
25 #include <com/sun/star/uno/Sequence.hxx>
27 #include <com/sun/star/beans/IllegalTypeException.hpp>
30 namespace comphelper{
33 /** @short Implements a stl vector on top of any
34 uno sequence.
36 @descr That provides the possibility to modify
37 sequences very easy ...
38 Of course this can be useful only, if
39 count of modifications is high, so copying
40 of the sequence make sense!
42 template< class TElementType >
43 class SequenceAsVector : public ::std::vector< TElementType >
45 //-------------------------------------------
46 // types
48 public:
50 //---------------------------------------
51 /** @short When inheriting from a template using typename is generally required when using
52 types from the base! */
53 typedef typename ::std::vector< TElementType >::const_iterator const_iterator;
55 //---------------------------------------
56 /** @short When inheriting from a template using typename is generally required when using
57 types from the base! */
58 typedef typename ::std::vector< TElementType >::iterator iterator;
60 //-------------------------------------------
61 // interface
62 public:
64 //---------------------------------------
65 /** @short default ctor, to create an empty list.
67 SequenceAsVector()
70 //---------------------------------------
71 /** @short default dtor
73 ~SequenceAsVector()
76 //---------------------------------------
77 /** @short creates a new vector with the given length.
79 @param nLength
80 the number of elements for the new vector.
82 explicit SequenceAsVector(sal_Int32 nLength) :
83 ::std::vector< TElementType >( static_cast< size_t >( nLength ) )
87 //---------------------------------------
88 /** @short creates a new deque from the given uno sequence.
90 @param lSource
91 contains the new items for this deque.
93 SequenceAsVector(const ::com::sun::star::uno::Sequence< TElementType >& lSource)
95 (*this) << lSource;
98 //---------------------------------------
99 /** @short creates a new instance from the given Any, which
100 of course must contain a valid sequence using the
101 right element type for every item.
103 @attention If the given Any is an empty one
104 (if its set to VOID), no exception
105 is thrown. In such case this instance will
106 be created as an empty one too!
108 @param aSource
109 this any must contain a suitable sequence. :-)
111 @throw A <type scope="com::sun::star::beans">IllegalTypeException</type>
112 if an unsupported element inside this Any
113 is given. An empty Any reset this instance!
115 SequenceAsVector(const ::com::sun::star::uno::Any& aSource)
117 (*this) << aSource;
120 //---------------------------------------
121 /** @short fill this instance from the given uno sequence.
123 @param lSource
124 contains the new items for this deque.
126 void operator<<(const ::com::sun::star::uno::Sequence< TElementType >& lSource)
128 this->clear();
130 sal_Int32 c = lSource.getLength();
131 const TElementType* pSource = lSource.getConstArray();
133 for (sal_Int32 i=0; i<c; ++i)
134 this->push_back(pSource[i]);
137 //---------------------------------------
138 /** @short fill this instance from the given Any, which
139 of course must contain a valid sequence using the
140 right element type for every item.
142 @attention If the given Any is an empty one
143 (if its set to VOID), no exception
144 is thrown. In such case this instance will
145 be created as an empty one too!
147 @param aSource
148 this any must contain a suitable sequence. :-)
150 @throw A <type scope="com::sun::star::beans">IllegalTypeException</type>
151 if an unsupported element inside this Any
152 is given. An empty Any reset this instance!
154 void operator<<(const ::com::sun::star::uno::Any& aSource)
156 // An empty Any reset this instance!
157 if (!aSource.hasValue())
159 this->clear();
160 return;
163 ::com::sun::star::uno::Sequence< TElementType > lSource;
164 if (!(aSource >>= lSource))
165 throw ::com::sun::star::beans::IllegalTypeException(
166 OUString("SequenceAsVector operator<<(Any) was called with an unsupported Any type."),
167 ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >());
169 (*this) << lSource;
172 //---------------------------------------
173 /** @short converts this instance to an uno sequence.
175 @param lDestination
176 target sequence for converting.
178 void operator>>(::com::sun::star::uno::Sequence< TElementType >& lDestination) const
180 sal_Int32 c = (sal_Int32)this->size();
181 lDestination.realloc(c);
182 TElementType* pDestination = lDestination.getArray();
184 sal_Int32 i = 0;
185 for (typename std::vector<TElementType>::const_iterator pThis = this->begin();
186 pThis != this->end() ;
187 ++pThis )
189 pDestination[i] = *pThis;
190 ++i;
194 //---------------------------------------
195 /** @short converts this instance to an uno any
196 which contains a suitable sequence
197 of items of this stl struct.
199 @param aDestination
200 target any for converting.
202 void operator>>(::com::sun::star::uno::Any& aDestination) const
204 sal_Int32 c = (sal_Int32)this->size();
205 ::com::sun::star::uno::Sequence< TElementType > lDestination(c);
206 TElementType* pDestination = lDestination.getArray();
208 sal_Int32 i = 0;
209 for (typename std::vector<TElementType>::const_iterator pThis = this->begin();
210 pThis != this->end() ;
211 ++pThis )
213 pDestination[i] = *pThis;
214 ++i;
217 aDestination <<= lDestination;
220 //---------------------------------------
221 /** @short converts this deque to a suitable uno
222 sequence which contains all items.
224 @attention It return a const sequence to prevent
225 the outside code against using of this
226 return value as [in/]out parameter for
227 direct function calls!
228 Of course it can be casted to non const
229 ... but then its a problem of the outside
230 code :-)
232 @return A (const!) sequence, which contains all items of
233 this deque.
235 const ::com::sun::star::uno::Sequence< TElementType > getAsConstList() const
237 ::com::sun::star::uno::Sequence< TElementType > lDestination;
238 (*this) >> lDestination;
239 return lDestination;
243 } // namespace comphelper
245 #endif // _COMPHELPER_SEQUENCEASVECTOR_HXX_
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */