Salome HOME
CoTech decision: move MEDWrapper from MED to SMESH
[modules/smesh.git] / src / MEDWrapper / Base / MED_Vector.hxx
1 // Copyright (C) 2007-2013  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.
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 : webmaster.salome@opencascade.com
21 //
22 //
23 #ifndef MED_Vector_HeaderFile
24 #define MED_Vector_HeaderFile
25
26 #include <vector>
27 #include <stdexcept>
28
29 //#if defined(_DEBUG_)
30 #  define MED_TVECTOR_CHECK_RANGE
31 //#endif
32
33 namespace MED
34 {
35
36   //! Main purpose to introduce the class was to customize operator [] 
37   template<typename _Tp, typename _Alloc = std::allocator<_Tp> >
38   class TVector : public std::vector<_Tp, _Alloc>
39   {
40   public:
41     typedef size_t size_type;
42
43     typedef std::vector<_Tp, _Alloc> superclass;
44     typedef typename superclass::allocator_type allocator_type;
45
46     typedef _Tp value_type;
47     typedef value_type& reference;
48     typedef const value_type& const_reference;
49
50   protected:
51     void
52     check_range(size_type __n) const
53     {
54       if (__n >= this->size())
55         throw std::out_of_range("TVector [] access out of range");
56     }
57
58     const_reference
59     get_value(size_type __n) const
60     {
61       return superclass::operator[](__n);
62     }
63
64     reference
65     get_value(size_type __n)
66     {
67       return superclass::operator[](__n);
68     }
69
70   public:
71     explicit
72     TVector(const allocator_type& __a = allocator_type()): 
73       superclass(__a) 
74     {}
75     
76     TVector(size_type __n, const value_type& __val,
77             const allocator_type& __a = allocator_type()):
78       superclass(__n, __val, __a)
79     {}
80     
81     explicit
82     TVector(size_type __n):
83       superclass(__n)
84     {}
85
86     TVector(const TVector& __x):
87       superclass(__x)
88     {}
89
90     template<typename _InputIterator>
91     TVector(_InputIterator __first, _InputIterator __last,
92             const allocator_type& __a = allocator_type()):
93       superclass(__first, __last, __a)
94     {}
95
96     template<typename _Yp, typename _Al>
97     TVector(TVector<_Yp, _Al> __y):
98       superclass(__y.begin(), __y.end())
99     {}
100
101     TVector&
102     operator=(const TVector& __x)
103     {
104       superclass::operator=(__x);
105       return *this;
106     }
107
108     template<typename _Yp, typename _Al>
109     TVector&
110     operator=(TVector<_Yp, _Al> __y)
111     {
112       this->assign(__y.begin(), __y.end());
113       return *this;
114     }
115
116     reference
117     operator[](size_type __n)
118     {
119 #if defined(MED_TVECTOR_CHECK_RANGE)
120       check_range(__n);
121 #endif
122       return get_value(__n);
123     }
124
125     const_reference
126     operator[](size_type __n) const
127     {
128 #if defined(MED_TVECTOR_CHECK_RANGE)
129       check_range(__n);
130 #endif
131       return get_value(__n);
132     }
133
134     reference
135     at(size_type __n)
136     {
137       check_range(__n);
138       return get_value(__n);
139     }
140
141     const_reference
142     at(size_type __n) const
143     {
144       check_range(__n);
145       return get_value(__n);
146     }
147   };
148
149 }
150
151 #undef MED_TVECTOR_CHECK_RANGE
152
153 #endif