19877b273SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
39877b273SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
49877b273SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
59877b273SAndrew Rist  * distributed with this work for additional information
69877b273SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
79877b273SAndrew Rist  * to you under the Apache License, Version 2.0 (the
89877b273SAndrew Rist  * "License"); you may not use this file except in compliance
99877b273SAndrew Rist  * with the License.  You may obtain a copy of the License at
109877b273SAndrew Rist  *
119877b273SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
129877b273SAndrew Rist  *
139877b273SAndrew Rist  * Unless required by applicable law or agreed to in writing,
149877b273SAndrew Rist  * software distributed under the License is distributed on an
159877b273SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
169877b273SAndrew Rist  * KIND, either express or implied.  See the License for the
179877b273SAndrew Rist  * specific language governing permissions and limitations
189877b273SAndrew Rist  * under the License.
199877b273SAndrew Rist  *
209877b273SAndrew Rist  *************************************************************/
219877b273SAndrew Rist 
229877b273SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #ifndef _COMPHELPER_SEQUENCEASVECTOR_HXX_
25cdf0e10cSrcweir #define _COMPHELPER_SEQUENCEASVECTOR_HXX_
26cdf0e10cSrcweir 
27cdf0e10cSrcweir //_______________________________________________
28cdf0e10cSrcweir // includes
29cdf0e10cSrcweir 
30cdf0e10cSrcweir #include <vector>
31cdf0e10cSrcweir #include <algorithm>
32cdf0e10cSrcweir #include <com/sun/star/uno/Sequence.hxx>
33cdf0e10cSrcweir 
34cdf0e10cSrcweir #ifndef _COM_SUN_STAR_BEANS_IllegalTypeException_HPP_
35cdf0e10cSrcweir #include <com/sun/star/beans/IllegalTypeException.hpp>
36cdf0e10cSrcweir #endif
37cdf0e10cSrcweir 
38cdf0e10cSrcweir //_______________________________________________
39cdf0e10cSrcweir // namespace
40cdf0e10cSrcweir 
41cdf0e10cSrcweir namespace comphelper{
42cdf0e10cSrcweir 
43cdf0e10cSrcweir //_______________________________________________
44cdf0e10cSrcweir // definitions
45cdf0e10cSrcweir 
46cdf0e10cSrcweir /** @short  Implements a stl vector on top of any
47cdf0e10cSrcweir             uno sequence.
48cdf0e10cSrcweir 
49cdf0e10cSrcweir     @descr  That provides the possibility to modify
50cdf0e10cSrcweir             sequences very easy ...
51cdf0e10cSrcweir             Of course this can be usefull only, if
52cdf0e10cSrcweir             count of modifications is high, so copying
53cdf0e10cSrcweir             of the sequence make sense!
54cdf0e10cSrcweir  */
55cdf0e10cSrcweir template< class TElementType >
56cdf0e10cSrcweir class SequenceAsVector : public ::std::vector< TElementType >
57cdf0e10cSrcweir {
58cdf0e10cSrcweir     //-------------------------------------------
59cdf0e10cSrcweir     // types
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     public:
62cdf0e10cSrcweir 
63cdf0e10cSrcweir         //---------------------------------------
64cdf0e10cSrcweir         /** @short  When inheriting from a template using typename is generally required when using
65cdf0e10cSrcweir                     types from the base! */
66cdf0e10cSrcweir         typedef typename ::std::vector< TElementType >::const_iterator const_iterator;
67cdf0e10cSrcweir 
68cdf0e10cSrcweir     //-------------------------------------------
69cdf0e10cSrcweir     // interface
70cdf0e10cSrcweir     public:
71cdf0e10cSrcweir 
72cdf0e10cSrcweir         //---------------------------------------
73cdf0e10cSrcweir         /** @short  default ctor, to create an empty list.
74cdf0e10cSrcweir          */
SequenceAsVector()75cdf0e10cSrcweir         SequenceAsVector()
76cdf0e10cSrcweir         {}
77cdf0e10cSrcweir 
78cdf0e10cSrcweir         //---------------------------------------
79cdf0e10cSrcweir         /** @short  default dtor
80cdf0e10cSrcweir          */
~SequenceAsVector()81cdf0e10cSrcweir         ~SequenceAsVector()
82cdf0e10cSrcweir         {}
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         //---------------------------------------
85cdf0e10cSrcweir         /** @short  creates a new vector with the given length.
86cdf0e10cSrcweir 
87cdf0e10cSrcweir             @param  nLength
88cdf0e10cSrcweir                     the number of elements for the new vector.
89cdf0e10cSrcweir          */
SequenceAsVector(sal_Int32 nLength)90cdf0e10cSrcweir         explicit SequenceAsVector(sal_Int32 nLength) :
91cdf0e10cSrcweir             ::std::vector< TElementType >( static_cast< size_t >( nLength ) )
92cdf0e10cSrcweir         {
93cdf0e10cSrcweir         }
94cdf0e10cSrcweir 
95cdf0e10cSrcweir         //---------------------------------------
96cdf0e10cSrcweir         /** @short  creates a new deque from the given uno sequence.
97cdf0e10cSrcweir 
98cdf0e10cSrcweir             @param  lSource
99cdf0e10cSrcweir                     contains the new items for this deque.
100cdf0e10cSrcweir          */
SequenceAsVector(const::com::sun::star::uno::Sequence<TElementType> & lSource)101cdf0e10cSrcweir         SequenceAsVector(const ::com::sun::star::uno::Sequence< TElementType >& lSource)
102cdf0e10cSrcweir         {
103cdf0e10cSrcweir             (*this) << lSource;
104cdf0e10cSrcweir         }
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         //---------------------------------------
107cdf0e10cSrcweir         /** @short      creates a new instance from the given Any, which
108cdf0e10cSrcweir                         of course must contain a valid sequence using the
109cdf0e10cSrcweir                         right element type for every item.
110cdf0e10cSrcweir 
111cdf0e10cSrcweir             @attention  If the given Any is an empty one
112cdf0e10cSrcweir                         (if its set to VOID), no exception
113cdf0e10cSrcweir                         is thrown. In such case this instance will
114cdf0e10cSrcweir                         be created as an empty one too!
115cdf0e10cSrcweir 
116cdf0e10cSrcweir             @param      aSource
117cdf0e10cSrcweir                         this any must contain a suitable sequence. :-)
118cdf0e10cSrcweir 
119cdf0e10cSrcweir             @throw      A <type scope="com::sun::star::beans">IllegalTypeException</type>
120cdf0e10cSrcweir                         if an unsupported element inside this Any
121cdf0e10cSrcweir                         is given. An empty Any reset this instance!
122cdf0e10cSrcweir          */
SequenceAsVector(const::com::sun::star::uno::Any & aSource)123cdf0e10cSrcweir         SequenceAsVector(const ::com::sun::star::uno::Any& aSource)
124cdf0e10cSrcweir         {
125cdf0e10cSrcweir             (*this) << aSource;
126cdf0e10cSrcweir         }
127cdf0e10cSrcweir 
128cdf0e10cSrcweir         //---------------------------------------
129cdf0e10cSrcweir         /** @short  fill this instance from the given uno sequence.
130cdf0e10cSrcweir 
131cdf0e10cSrcweir             @param  lSource
132cdf0e10cSrcweir                     contains the new items for this deque.
133cdf0e10cSrcweir          */
operator <<(const::com::sun::star::uno::Sequence<TElementType> & lSource)134cdf0e10cSrcweir         void operator<<(const ::com::sun::star::uno::Sequence< TElementType >& lSource)
135cdf0e10cSrcweir         {
136cdf0e10cSrcweir             this->clear();
137cdf0e10cSrcweir 
138cdf0e10cSrcweir                   sal_Int32     c       = lSource.getLength();
139cdf0e10cSrcweir             const TElementType* pSource = lSource.getConstArray();
140cdf0e10cSrcweir 
141cdf0e10cSrcweir             for (sal_Int32 i=0; i<c; ++i)
142*7ee1d29cSAriel Constenla-Haile                 this->push_back(pSource[i]);
143cdf0e10cSrcweir         }
144cdf0e10cSrcweir 
145cdf0e10cSrcweir         //---------------------------------------
146cdf0e10cSrcweir         /** @short      fill this instance from the given Any, which
147cdf0e10cSrcweir                         of course must contain a valid sequence using the
148cdf0e10cSrcweir                         right element type for every item.
149cdf0e10cSrcweir 
150cdf0e10cSrcweir             @attention  If the given Any is an empty one
151cdf0e10cSrcweir                         (if its set to VOID), no exception
152cdf0e10cSrcweir                         is thrown. In such case this instance will
153cdf0e10cSrcweir                         be created as an empty one too!
154cdf0e10cSrcweir 
155cdf0e10cSrcweir             @param      aSource
156cdf0e10cSrcweir                         this any must contain a suitable sequence. :-)
157cdf0e10cSrcweir 
158cdf0e10cSrcweir             @throw      A <type scope="com::sun::star::beans">IllegalTypeException</type>
159cdf0e10cSrcweir                         if an unsupported element inside this Any
160cdf0e10cSrcweir                         is given. An empty Any reset this instance!
161cdf0e10cSrcweir          */
operator <<(const::com::sun::star::uno::Any & aSource)162cdf0e10cSrcweir         void operator<<(const ::com::sun::star::uno::Any& aSource)
163cdf0e10cSrcweir         {
164cdf0e10cSrcweir             // An empty Any reset this instance!
165cdf0e10cSrcweir             if (!aSource.hasValue())
166cdf0e10cSrcweir             {
167cdf0e10cSrcweir                 this->clear();
168cdf0e10cSrcweir                 return;
169cdf0e10cSrcweir             }
170cdf0e10cSrcweir 
171cdf0e10cSrcweir             ::com::sun::star::uno::Sequence< TElementType > lSource;
172cdf0e10cSrcweir             if (!(aSource >>= lSource))
173cdf0e10cSrcweir                 throw ::com::sun::star::beans::IllegalTypeException(
174cdf0e10cSrcweir                         ::rtl::OUString::createFromAscii("SequenceAsVector operator<<(Any) was called with an unsupported Any type."),
175cdf0e10cSrcweir                         ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >());
176cdf0e10cSrcweir 
177cdf0e10cSrcweir             (*this) << lSource;
178cdf0e10cSrcweir         }
179cdf0e10cSrcweir 
180cdf0e10cSrcweir         //---------------------------------------
181cdf0e10cSrcweir         /** @short  converts this instance to an uno sequence.
182cdf0e10cSrcweir 
183cdf0e10cSrcweir             @param  lDestination
184cdf0e10cSrcweir                     target sequence for converting.
185cdf0e10cSrcweir          */
operator >>(::com::sun::star::uno::Sequence<TElementType> & lDestination) const186cdf0e10cSrcweir         void operator>>(::com::sun::star::uno::Sequence< TElementType >& lDestination) const
187cdf0e10cSrcweir         {
188cdf0e10cSrcweir             sal_Int32 c = (sal_Int32)this->size();
189cdf0e10cSrcweir             lDestination.realloc(c);
190cdf0e10cSrcweir             TElementType* pDestination = lDestination.getArray();
191cdf0e10cSrcweir 
192cdf0e10cSrcweir             sal_Int32 i = 0;
193cdf0e10cSrcweir             for (typename std::vector<TElementType>::const_iterator pThis  = this->begin();
194cdf0e10cSrcweir                                                                     pThis != this->end()  ;
195cdf0e10cSrcweir                                                                     ++pThis           )
196cdf0e10cSrcweir             {
197cdf0e10cSrcweir                 pDestination[i] = *pThis;
198cdf0e10cSrcweir                 ++i;
199cdf0e10cSrcweir             }
200cdf0e10cSrcweir         }
201cdf0e10cSrcweir 
202cdf0e10cSrcweir         //---------------------------------------
203cdf0e10cSrcweir         /** @short  converts this instance to an uno any
204cdf0e10cSrcweir                     which contains a suitable sequence
205cdf0e10cSrcweir                     of items of this stl struct.
206cdf0e10cSrcweir 
207cdf0e10cSrcweir             @param  aDestination
208cdf0e10cSrcweir                     target any for converting.
209cdf0e10cSrcweir          */
operator >>(::com::sun::star::uno::Any & aDestination) const210cdf0e10cSrcweir         void operator>>(::com::sun::star::uno::Any& aDestination) const
211cdf0e10cSrcweir         {
212cdf0e10cSrcweir             sal_Int32                                       c            = (sal_Int32)this->size();
213cdf0e10cSrcweir             ::com::sun::star::uno::Sequence< TElementType > lDestination(c);
214cdf0e10cSrcweir             TElementType*                                   pDestination = lDestination.getArray();
215cdf0e10cSrcweir 
216cdf0e10cSrcweir             sal_Int32 i = 0;
217cdf0e10cSrcweir             for (typename std::vector<TElementType>::const_iterator pThis  = this->begin();
218cdf0e10cSrcweir                                                                     pThis != this->end()  ;
219cdf0e10cSrcweir                                                                     ++pThis           )
220cdf0e10cSrcweir             {
221cdf0e10cSrcweir                 pDestination[i] = *pThis;
222cdf0e10cSrcweir                 ++i;
223cdf0e10cSrcweir             }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir             aDestination <<= lDestination;
226cdf0e10cSrcweir         }
227cdf0e10cSrcweir 
228cdf0e10cSrcweir         //---------------------------------------
229cdf0e10cSrcweir         /** @short      converts this deque to a suitable uno
230cdf0e10cSrcweir                         sequence which contains all items.
231cdf0e10cSrcweir 
232cdf0e10cSrcweir             @attention  It return a const sequence to prevent
233cdf0e10cSrcweir                         the outside code against using of this
234cdf0e10cSrcweir                         return value as [in/]out parameter for
235cdf0e10cSrcweir                         direct function calls!
236cdf0e10cSrcweir                         Of course it can be casted to non const
237cdf0e10cSrcweir                         ... but then its a problem of the outside
238cdf0e10cSrcweir                         code :-)
239cdf0e10cSrcweir 
240cdf0e10cSrcweir             @return     A (const!) sequence, which contains all items of
241cdf0e10cSrcweir                         this deque.
242cdf0e10cSrcweir          */
getAsConstList() const243cdf0e10cSrcweir         const ::com::sun::star::uno::Sequence< TElementType > getAsConstList() const
244cdf0e10cSrcweir         {
245cdf0e10cSrcweir             ::com::sun::star::uno::Sequence< TElementType > lDestination;
246cdf0e10cSrcweir             (*this) >> lDestination;
247cdf0e10cSrcweir             return lDestination;
248cdf0e10cSrcweir         }
249cdf0e10cSrcweir };
250cdf0e10cSrcweir 
251cdf0e10cSrcweir } // namespace comphelper
252cdf0e10cSrcweir 
253cdf0e10cSrcweir #endif // _COMPHELPER_SEQUENCEASVECTOR_HXX_
254cdf0e10cSrcweir 
255