1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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_
25 #include <com/sun/star/uno/Sequence.hxx>
27 #include <com/sun/star/beans/IllegalTypeException.hpp>
33 /** @short Implements a stl vector on top of any
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 //-------------------------------------------
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 //-------------------------------------------
64 //---------------------------------------
65 /** @short default ctor, to create an empty list.
70 //---------------------------------------
71 /** @short default dtor
76 //---------------------------------------
77 /** @short creates a new vector with the given length.
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.
91 contains the new items for this deque.
93 SequenceAsVector(const ::com::sun::star::uno::Sequence
< TElementType
>& 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!
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
)
120 //---------------------------------------
121 /** @short fill this instance from the given uno sequence.
124 contains the new items for this deque.
126 void operator<<(const ::com::sun::star::uno::Sequence
< TElementType
>& lSource
)
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!
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())
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
>());
172 //---------------------------------------
173 /** @short converts this instance to an uno sequence.
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();
185 for (typename
std::vector
<TElementType
>::const_iterator pThis
= this->begin();
186 pThis
!= this->end() ;
189 pDestination
[i
] = *pThis
;
194 //---------------------------------------
195 /** @short converts this instance to an uno any
196 which contains a suitable sequence
197 of items of this stl struct.
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();
209 for (typename
std::vector
<TElementType
>::const_iterator pThis
= this->begin();
210 pThis
!= this->end() ;
213 pDestination
[i
] = *pThis
;
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
232 @return A (const!) sequence, which contains all items of
235 const ::com::sun::star::uno::Sequence
< TElementType
> getAsConstList() const
237 ::com::sun::star::uno::Sequence
< TElementType
> lDestination
;
238 (*this) >> lDestination
;
243 } // namespace comphelper
245 #endif // _COMPHELPER_SEQUENCEASVECTOR_HXX_
247 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */