Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #ifndef INCLUDED_COMPHELPER_SEQUENCEASVECTOR_HXX
21 : #define INCLUDED_COMPHELPER_SEQUENCEASVECTOR_HXX
22 :
23 : #include <vector>
24 : #include <algorithm>
25 : #include <com/sun/star/uno/Sequence.hxx>
26 :
27 : #include <com/sun/star/beans/IllegalTypeException.hpp>
28 :
29 :
30 : namespace comphelper{
31 :
32 :
33 : /** @short Implements a stl vector on top of any
34 : uno sequence.
35 :
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!
41 : */
42 : template< class TElementType >
43 498113 : class SequenceAsVector : public ::std::vector< TElementType >
44 : {
45 :
46 : // types
47 :
48 : public:
49 :
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;
54 :
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;
59 :
60 :
61 : // interface
62 : public:
63 :
64 :
65 : /** @short default ctor, to create an empty list.
66 : */
67 4025997 : SequenceAsVector()
68 4025997 : {}
69 :
70 :
71 : /** @short default dtor
72 : */
73 5690134 : ~SequenceAsVector()
74 5690134 : {}
75 :
76 :
77 : /** @short creates a new vector with the given length.
78 :
79 : @param nLength
80 : the number of elements for the new vector.
81 : */
82 2 : explicit SequenceAsVector(sal_Int32 nLength) :
83 2 : ::std::vector< TElementType >( static_cast< size_t >( nLength ) )
84 : {
85 2 : }
86 :
87 :
88 : /** @short creates a new deque from the given uno sequence.
89 :
90 : @param lSource
91 : contains the new items for this deque.
92 : */
93 884636 : SequenceAsVector(const ::com::sun::star::uno::Sequence< TElementType >& lSource)
94 884636 : {
95 884636 : (*this) << lSource;
96 884636 : }
97 :
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.
102 :
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!
107 :
108 : @param aSource
109 : this any must contain a suitable sequence. :-)
110 :
111 : @throw A com::sun::star::beans::IllegalTypeException
112 : if an unsupported element inside this Any
113 : is given. An empty Any reset this instance!
114 : */
115 524208 : SequenceAsVector(const ::com::sun::star::uno::Any& aSource)
116 524208 : {
117 524208 : (*this) << aSource;
118 524208 : }
119 :
120 :
121 : /** @short fill this instance from the given uno sequence.
122 :
123 : @param lSource
124 : contains the new items for this deque.
125 : */
126 1418935 : void operator<<(const ::com::sun::star::uno::Sequence< TElementType >& lSource)
127 : {
128 1418935 : this->clear();
129 :
130 1418935 : sal_Int32 c = lSource.getLength();
131 1418935 : const TElementType* pSource = lSource.getConstArray();
132 :
133 18296054 : for (sal_Int32 i=0; i<c; ++i)
134 16877119 : this->push_back(pSource[i]);
135 1418935 : }
136 :
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.
141 :
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!
146 :
147 : @param aSource
148 : this any must contain a suitable sequence. :-)
149 :
150 : @throw A com::sun::star::beans::IllegalTypeException
151 : if an unsupported element inside this Any
152 : is given. An empty Any reset this instance!
153 : */
154 531576 : void operator<<(const ::com::sun::star::uno::Any& aSource)
155 : {
156 : // An empty Any reset this instance!
157 531576 : if (!aSource.hasValue())
158 : {
159 6794 : this->clear();
160 538370 : return;
161 : }
162 :
163 524782 : ::com::sun::star::uno::Sequence< TElementType > lSource;
164 524782 : if (!(aSource >>= lSource))
165 0 : throw ::com::sun::star::beans::IllegalTypeException(
166 : OUString("SequenceAsVector operator<<(Any) was called with an unsupported Any type."),
167 0 : ::com::sun::star::uno::Reference< ::com::sun::star::uno::XInterface >());
168 :
169 524782 : (*this) << lSource;
170 : }
171 :
172 :
173 : /** @short converts this instance to an uno sequence.
174 :
175 : @param lDestination
176 : target sequence for converting.
177 : */
178 1094361 : void operator>>(::com::sun::star::uno::Sequence< TElementType >& lDestination) const
179 : {
180 1094361 : sal_Int32 c = static_cast<sal_Int32>(this->size());
181 1094361 : lDestination.realloc(c);
182 1094361 : TElementType* pDestination = lDestination.getArray();
183 :
184 1094361 : sal_Int32 i = 0;
185 24441570 : for (typename std::vector<TElementType>::const_iterator pThis = this->begin();
186 16294380 : pThis != this->end() ;
187 : ++pThis )
188 : {
189 7052829 : pDestination[i] = *pThis;
190 7052829 : ++i;
191 : }
192 1094361 : }
193 :
194 :
195 : /** @short converts this instance to an uno any
196 : which contains a suitable sequence
197 : of items of this stl struct.
198 :
199 : @param aDestination
200 : target any for converting.
201 : */
202 : void operator>>(::com::sun::star::uno::Any& aDestination) const
203 : {
204 : sal_Int32 c = static_cast<sal_Int32>(this->size());
205 : ::com::sun::star::uno::Sequence< TElementType > lDestination(c);
206 : TElementType* pDestination = lDestination.getArray();
207 :
208 : sal_Int32 i = 0;
209 : for (typename std::vector<TElementType>::const_iterator pThis = this->begin();
210 : pThis != this->end() ;
211 : ++pThis )
212 : {
213 : pDestination[i] = *pThis;
214 : ++i;
215 : }
216 :
217 : aDestination <<= lDestination;
218 : }
219 :
220 :
221 : /** @short converts this deque to a suitable uno
222 : sequence which contains all items.
223 :
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 it's a problem of the outside
230 : code :-)
231 :
232 : @return A (const!) sequence, which contains all items of
233 : this deque.
234 : */
235 995661 : const ::com::sun::star::uno::Sequence< TElementType > getAsConstList() const
236 : {
237 995661 : ::com::sun::star::uno::Sequence< TElementType > lDestination;
238 995661 : (*this) >> lDestination;
239 995661 : return lDestination;
240 : }
241 : };
242 :
243 : } // namespace comphelper
244 :
245 : #endif // INCLUDED_COMPHELPER_SEQUENCEASVECTOR_HXX
246 :
247 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|