| 1 |
// Copyright (C) 2007-2015 CEA/DEN, EDF R&D, OPEN CASCADE |
| 2 |
// |
| 3 |
// Copyright (C) 2003-2007 OPEN CASCADE, EADS/CCR, LIP6, CEA/DEN, |
| 4 |
// CEDRAT, EDF R&D, LEG, PRINCIPIA R&D, BUREAU VERITAS |
| 5 |
// |
| 6 |
// This library is free software; you can redistribute it and/or |
| 7 |
// modify it under the terms of the GNU Lesser General Public |
| 8 |
// License as published by the Free Software Foundation; either |
| 9 |
// version 2.1 of the License, or (at your option) any later version. |
| 10 |
// |
| 11 |
// This library is distributed in the hope that it will be useful, |
| 12 |
// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 14 |
// Lesser General Public License for more details. |
| 15 |
// |
| 16 |
// You should have received a copy of the GNU Lesser General Public |
| 17 |
// License along with this library; if not, write to the Free Software |
| 18 |
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 19 |
// |
| 20 |
// See http://www.salome-platform.org/ or email : [email protected] |
| 21 |
// |
| 22 |
#ifndef MED_Vector_HeaderFile |
| 23 |
#define MED_Vector_HeaderFile |
| 24 |
|
| 25 |
#include <vector> |
| 26 |
#include <stdexcept> |
| 27 |
|
| 28 |
//#if defined(_DEBUG_) |
| 29 |
# define MED_TVECTOR_CHECK_RANGE |
| 30 |
//#endif |
| 31 |
|
| 32 |
namespace MED |
| 33 |
{ |
| 34 |
|
| 35 |
//! Main purpose to introduce the class was to customize operator [] |
| 36 |
template<typename _Tp, typename _Alloc = std::allocator<_Tp> > |
| 37 |
class TVector : public std::vector<_Tp, _Alloc> |
| 38 |
{ |
| 39 |
public: |
| 40 |
typedef size_t size_type; |
| 41 |
|
| 42 |
typedef std::vector<_Tp, _Alloc> superclass; |
| 43 |
typedef typename superclass::allocator_type allocator_type; |
| 44 |
|
| 45 |
typedef _Tp value_type; |
| 46 |
typedef value_type& reference; |
| 47 |
typedef const value_type& const_reference; |
| 48 |
|
| 49 |
protected: |
| 50 |
void |
| 51 |
check_range(size_type __n) const |
| 52 |
{ |
| 53 |
if (__n >= this->size()) |
| 54 |
throw std::out_of_range("TVector [] access out of range"); |
| 55 |
} |
| 56 |
|
| 57 |
const_reference |
| 58 |
get_value(size_type __n) const |
| 59 |
{ |
| 60 |
return superclass::operator[](__n); |
| 61 |
} |
| 62 |
|
| 63 |
reference |
| 64 |
get_value(size_type __n) |
| 65 |
{ |
| 66 |
return superclass::operator[](__n); |
| 67 |
} |
| 68 |
|
| 69 |
public: |
| 70 |
explicit |
| 71 |
TVector(const allocator_type& __a = allocator_type()): |
| 72 |
superclass(__a) |
| 73 |
{} |
| 74 |
|
| 75 |
TVector(size_type __n, const value_type& __val, |
| 76 |
const allocator_type& __a = allocator_type()): |
| 77 |
superclass(__n, __val, __a) |
| 78 |
{} |
| 79 |
|
| 80 |
explicit |
| 81 |
TVector(size_type __n): |
| 82 |
superclass(__n) |
| 83 |
{} |
| 84 |
|
| 85 |
TVector(const TVector& __x): |
| 86 |
superclass(__x) |
| 87 |
{} |
| 88 |
|
| 89 |
template<typename _InputIterator> |
| 90 |
TVector(_InputIterator __first, _InputIterator __last, |
| 91 |
const allocator_type& __a = allocator_type()): |
| 92 |
superclass(__first, __last, __a) |
| 93 |
{} |
| 94 |
|
| 95 |
template<typename _Yp, typename _Al> |
| 96 |
TVector(TVector<_Yp, _Al> __y): |
| 97 |
superclass(__y.begin(), __y.end()) |
| 98 |
{} |
| 99 |
|
| 100 |
TVector& |
| 101 |
operator=(const TVector& __x) |
| 102 |
{ |
| 103 |
superclass::operator=(__x); |
| 104 |
return *this; |
| 105 |
} |
| 106 |
|
| 107 |
template<typename _Yp, typename _Al> |
| 108 |
TVector& |
| 109 |
operator=(TVector<_Yp, _Al> __y) |
| 110 |
{ |
| 111 |
this->assign(__y.begin(), __y.end()); |
| 112 |
return *this; |
| 113 |
} |
| 114 |
|
| 115 |
reference |
| 116 |
operator[](size_type __n) |
| 117 |
{ |
| 118 |
#if defined(MED_TVECTOR_CHECK_RANGE) |
| 119 |
check_range(__n); |
| 120 |
#endif |
| 121 |
return get_value(__n); |
| 122 |
} |
| 123 |
|
| 124 |
const_reference |
| 125 |
operator[](size_type __n) const |
| 126 |
{ |
| 127 |
#if defined(MED_TVECTOR_CHECK_RANGE) |
| 128 |
check_range(__n); |
| 129 |
#endif |
| 130 |
return get_value(__n); |
| 131 |
} |
| 132 |
|
| 133 |
reference |
| 134 |
at(size_type __n) |
| 135 |
{ |
| 136 |
check_range(__n); |
| 137 |
return get_value(__n); |
| 138 |
} |
| 139 |
|
| 140 |
const_reference |
| 141 |
at(size_type __n) const |
| 142 |
{ |
| 143 |
check_range(__n); |
| 144 |
return get_value(__n); |
| 145 |
} |
| 146 |
}; |
| 147 |
|
| 148 |
} |
| 149 |
|
| 150 |
#undef MED_TVECTOR_CHECK_RANGE |
| 151 |
|
| 152 |
#endif |