]> git.mxchange.org Git - simgear.git/blob - simgear/scene/util/VectorArrayAdapter.hxx
Let SGCloudLayer handle coverage strings
[simgear.git] / simgear / scene / util / VectorArrayAdapter.hxx
1 /* -*-c++-*-
2  *
3  * Copyright (C) 2007 Tim Moore
4  *
5  * This program is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU General Public License as
7  * published by the Free Software Foundation; either version 2 of the
8  * License, or (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful, but
11  * WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18  * MA 02110-1301, USA.
19  *
20  */
21
22 #ifndef VECTORARRAYADAPTERHXX
23 #define VECTORARRAYADAPTERHXX 1
24
25 // #define SG_CHECK_VECTOR_ACCESS 1
26 namespace simgear
27 {
28 template <typename Vector>
29 class VectorArrayAdapter {
30 public:
31     /*
32      * Adapter that provides 2D array access to the elements of a
33      * std::vector in row major order.
34      * @param v the vector
35      * @param rowStride distance from an element to the corresponding
36      *  element in the next row.
37      * @param baseOffset offset of the first element of the array from the
38      *  beginning of the vector.
39      * @param rowOffset offset of the first element in a row from the
40      *  actual beginning of the row.
41      */
42     VectorArrayAdapter(Vector& v, int rowStride, int baseOffset = 0,
43         int rowOffset = 0):
44         _v(v),  _rowStride(rowStride), _baseOffset(baseOffset),
45         _rowOffset(rowOffset)
46     {
47     }
48 #ifdef SG_CHECK_VECTOR_ACCESS
49     typename Vector::value_type& operator() (int i, int j)
50     {
51         return _v.at(_baseOffset + i * _rowStride + _rowOffset + j);
52     }
53     const typename Vector::value_type& operator() (int i, int j) const
54     {
55         return _v.at(_baseOffset + i * _rowStride + _rowOffset + j);
56     }
57 #else
58     typename Vector::value_type& operator() (int i, int j)
59     {
60         return _v[_baseOffset + i * _rowStride + _rowOffset + j];
61     }
62     const typename Vector::value_type& operator() (int i, int j) const
63     {
64         return _v[_baseOffset + i * _rowStride + _rowOffset + j];
65     }
66 #endif
67 private:
68     Vector& _v;
69     const int _rowStride;
70     const int _baseOffset;
71     const int _rowOffset;
72 };
73 }
74 #endif