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 CSV_VVECTOR_HXX
21 #define CSV_VVECTOR_HXX
23 #include <cstddef> // for ptrdiff_t
27 #include <cosv/tpl/tpltools.hxx>
42 std::vector
< TYPE
* > &
44 { csv::erase_container_of_heap_ptrs(v
); }
46 /// @precond ->it is a valid iterator within v
48 std::vector
< TYPE
* > &
50 typename
std::vector
< TYPE
* >::iterator
52 { delete *it2erase
; v
.erase(it2erase
); }
54 /// @precond ->v.size() > 0
56 std::vector
< TYPE
* > &
58 { delete v
.back(); v
.pop_back(); }
60 /// @precond ->it is a valid iterator
61 static void ReplacePtr(
62 typename
std::vector
< TYPE
* >::iterator
65 { delete *it
; *it
= pass_new
; }
69 /** One helper class for the ->csv::VirtualVector.
75 static void Destruct(std::vector
< TYPE
* > &)
79 std::vector
< TYPE
* > &
81 typename
std::vector
< TYPE
* >::iterator
83 { v
.erase(it2erase
); }
86 std::vector
< TYPE
* > &
90 /// @precond ->it is a valid iterator
91 static void ReplacePtr(
92 typename
std::vector
< TYPE
* >::iterator
99 } // namespace vvector
104 /** Implements a vector of different implementations of a base
107 Implementation has to be by pointers to get the polymorphic
108 behaviour, however access is by references to the base class.
111 The common base class of vector elements.
114 Has two possible values:
115 vvector::delete_ptrs<XX> Elements have to be on the heap and
116 are deleted when removed (default).
117 vvector::keep_ptrs<XX> Elements are only referenced and not
118 deleted when removed.
121 template <class XX
, class PTRDEL
= vvector::delete_ptrs
<XX
> >
125 typedef VirtualVector
<XX
,PTRDEL
> self
;
126 typedef std::vector
< DYN XX
* > impl_type
;
127 typedef typename
impl_type::size_type size_type
;
128 typedef std::ptrdiff_t difference_type
;
130 class const_iterator
;
135 explicit VirtualVector(
140 const XX
& operator[](
141 size_type i_pos
) const;
147 DYN XX
& i_drElement
);
152 DYN XX
& i_drElement
);
157 DYN XX
& i_drElement
);
164 const_iterator
begin() const;
165 const_iterator
end() const;
166 const XX
& front() const;
167 const XX
& back() const;
177 VirtualVector(const VirtualVector
&);
178 VirtualVector
& operator=(const VirtualVector
&);
181 std::vector
< DYN XX
* >
188 /** Should be usable for all STL algorithms.
189 Implements the Random Access Iterator concept.
191 template <class XX
, class PTRDEL
>
192 class VirtualVector
<XX
,PTRDEL
>::
195 // This derivation provides type information for the STL
196 // It introduces the types "value_type" and "difference_type".
197 : public std::iterator
<std::random_access_iterator_tag
,
201 typedef VirtualVector
<XX
,PTRDEL
> my_container
;
202 typedef typename
my_container::impl_type::const_iterator impl_iterator
;
206 impl_iterator i_implIter
)
207 : itImpl(i_implIter
) {}
210 /////////// STL ITERATOR CONCEPT IMPLEMENTATION //////////////
212 // Default Constructible functions:
216 // Assignable functions:
217 // Assignment and copy constructor use the compiler generated versions.
219 // Equality Comparable functions:
221 const_iterator i_other
) const
222 { return itImpl
== i_other
.itImpl
; }
224 const_iterator i_other
) const
225 { return itImpl
!= i_other
.itImpl
; }
227 // Trivial Iterator functions:
228 const XX
& operator*() const
229 { return *(*itImpl
); }
231 // Input Iterator functions:
232 const_iterator
& operator++()
233 { ++itImpl
; return *this; }
234 const_iterator
operator++(int)
235 { return const_iterator(itImpl
++); }
237 // Bidirectional Iterator functions:
238 const_iterator
& operator--()
239 { --itImpl
; return *this; }
240 const_iterator
operator--(int)
241 { return const_iterator(itImpl
--); }
243 // Less Than Comparable functions:
245 const_iterator i_other
) const
246 { return itImpl
< i_other
.itImpl
; }
248 // Random Access Iterator functions:
249 const_iterator
& operator+=(
250 difference_type i_diff
)
251 { itImpl
+= i_diff
; return *this; }
252 const_iterator
operator+(
253 difference_type i_diff
) const
254 { const_iterator
ret(itImpl
);
255 return ret
+= i_diff
; }
256 const_iterator
& operator-=(
257 difference_type i_diff
)
258 { itImpl
-= i_diff
; return *this; }
259 const_iterator
operator-(
260 difference_type i_diff
) const
261 { const_iterator
ret(itImpl
);
262 return ret
-= i_diff
; }
263 difference_type
operator-(
264 const_iterator i_it
) const
265 { return itImpl
- i_it
.itImpl
; }
266 const XX
& operator[](
267 difference_type i_index
)
268 { return *(*itImpl
[i_index
]); }
270 //////////////////////////////////////////////////////////////////////////
273 friend class VirtualVector
<XX
,PTRDEL
>;
274 impl_iterator
ImplValue() const { return itImpl
; }
277 impl_iterator itImpl
;
283 /** Should be usable for all STL algorithms.
284 Implements the Random Access Iterator concept.
286 template <class XX
, class PTRDEL
>
287 class VirtualVector
<XX
,PTRDEL
>::
290 // This derivation provides type information for the STL
291 // It introduces the types "value_type" and "difference_type".
292 : public std::iterator
<std::random_access_iterator_tag
,
296 typedef VirtualVector
<XX
,PTRDEL
> my_container
;
297 typedef typename
my_container::impl_type::iterator impl_iterator
;
301 impl_iterator i_implIter
)
302 : itImpl(i_implIter
) {}
305 /////////// STL ITERATOR CONCEPT IMPLEMENTATION //////////////
307 // Default Constructible functions:
311 // Assignable functions:
312 // Assignment and copy constructor use the compiler generated versions.
314 // Equality Comparable functions:
316 iterator i_other
) const
317 { return itImpl
== i_other
.itImpl
; }
319 iterator i_other
) const
320 { return itImpl
!= i_other
.itImpl
; }
322 // Trivial Iterator functions:
323 XX
& operator*() const
324 { return *(*itImpl
); }
326 // Input Iterator functions:
327 iterator
& operator++()
328 { ++itImpl
; return *this; }
329 iterator
operator++(int)
330 { return iterator(itImpl
++); }
332 // Bidirectional Iterator functions:
333 iterator
& operator--()
334 { --itImpl
; return *this; }
335 iterator
operator--(int)
336 { return iterator(itImpl
--); }
338 // Less Than Comparable functions:
340 iterator i_other
) const
341 { return itImpl
< i_other
.itImpl
; }
343 // Random Access Iterator functions:
344 iterator
& operator+=(
345 difference_type i_diff
)
346 { itImpl
+= i_diff
; return *this; }
348 difference_type i_diff
) const
349 { iterator
ret(itImpl
);
350 return ret
+= i_diff
; }
351 iterator
& operator-=(
352 difference_type i_diff
)
353 { itImpl
-= i_diff
; return *this; }
355 difference_type i_diff
) const
356 { iterator
ret(itImpl
);
357 return ret
-= i_diff
; }
358 difference_type
operator-(
359 iterator i_it
) const
360 { return itImpl
- i_it
.itImpl
; }
362 difference_type i_index
)
363 { return *(*itImpl
[i_index
]); }
365 //////////////////////////////////////////////////////////////////////////
368 friend class VirtualVector
<XX
,PTRDEL
>;
369 impl_iterator
ImplValue() const { return itImpl
; }
372 impl_iterator itImpl
;
379 template <class XX
, class PTRDEL
>
381 VirtualVector
<XX
,PTRDEL
>::VirtualVector()
386 template <class XX
, class PTRDEL
>
388 VirtualVector
<XX
,PTRDEL
>::VirtualVector(int i_size
)
393 template <class XX
, class PTRDEL
>
395 VirtualVector
<XX
,PTRDEL
>::~VirtualVector()
397 PTRDEL::Destruct(aData
);
400 template <class XX
, class PTRDEL
>
402 VirtualVector
<XX
,PTRDEL
>::operator[]( size_type i_pos
) const
404 return *aData
[i_pos
];
407 template <class XX
, class PTRDEL
>
409 VirtualVector
<XX
,PTRDEL
>::operator[]( size_type i_pos
)
411 return *aData
[i_pos
];
414 template <class XX
, class PTRDEL
>
416 VirtualVector
<XX
,PTRDEL
>::push_back( DYN XX
& i_drElement
)
418 aData
.push_back(&i_drElement
);
421 template <class XX
, class PTRDEL
>
423 VirtualVector
<XX
,PTRDEL
>::pop_back()
425 if (NOT aData
.empty())
426 PTRDEL::PopBack(aData
);
429 template <class XX
, class PTRDEL
>
430 inline typename VirtualVector
<XX
,PTRDEL
>::iterator
431 VirtualVector
<XX
,PTRDEL
>::insert( iterator i_pos
,
432 DYN XX
& i_drElement
)
434 return iterator(aData
.insert(i_pos
.ImplValue(), &i_drElement
));
437 template <class XX
, class PTRDEL
>
439 VirtualVector
<XX
,PTRDEL
>::erase( iterator i_pos
)
441 PTRDEL::Erase(aData
, i_pos
.ImplValue());
444 template <class XX
, class PTRDEL
>
446 VirtualVector
<XX
,PTRDEL
>::replace( iterator i_pos
,
447 DYN XX
& i_drElement
)
449 PTRDEL::ReplacePtr(*i_pos
, &i_drElement
);
452 template <class XX
, class PTRDEL
>
454 VirtualVector
<XX
,PTRDEL
>::reserve( size_type i_size
)
456 aData
.reserve(i_size
);
459 template <class XX
, class PTRDEL
>
461 VirtualVector
<XX
,PTRDEL
>::empty() const
463 return aData
.empty();
466 template <class XX
, class PTRDEL
>
468 VirtualVector
<XX
,PTRDEL
>::size() const
473 template <class XX
, class PTRDEL
>
474 inline typename VirtualVector
<XX
,PTRDEL
>::const_iterator
475 VirtualVector
<XX
,PTRDEL
>::begin() const
477 return const_iterator(aData
.begin());
480 template <class XX
, class PTRDEL
>
481 inline typename VirtualVector
<XX
,PTRDEL
>::const_iterator
482 VirtualVector
<XX
,PTRDEL
>::end() const
484 return const_iterator(aData
.end());
487 template <class XX
, class PTRDEL
>
489 VirtualVector
<XX
,PTRDEL
>::front() const
491 return *aData
.front();
494 template <class XX
, class PTRDEL
>
496 VirtualVector
<XX
,PTRDEL
>::back() const
498 return *aData
.back();
501 template <class XX
, class PTRDEL
>
502 inline typename VirtualVector
<XX
,PTRDEL
>::iterator
503 VirtualVector
<XX
,PTRDEL
>::begin()
505 return iterator(aData
.begin());
508 template <class XX
, class PTRDEL
>
509 inline typename VirtualVector
<XX
,PTRDEL
>::iterator
510 VirtualVector
<XX
,PTRDEL
>::end()
512 return iterator(aData
.end());
515 template <class XX
, class PTRDEL
>
517 VirtualVector
<XX
,PTRDEL
>::front()
519 return *aData
.front();
522 template <class XX
, class PTRDEL
>
524 VirtualVector
<XX
,PTRDEL
>::back()
526 return *aData
.back();
535 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */