Carray.h 8.46 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*! \file Carray.h
 * \brief classes to interface raw C arrays (prototypes)
 * 
 * ----------------------------------------------------------------------------
 * 
 * $Id$
 * \author Thomas Forbriger
 * \date 14/05/2011
 * 
 * classes to interface raw C arrays (prototypes)
 * 
 * Copyright (c) 2011 by Thomas Forbriger (BFO Schiltach) 
 *
 * ----
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version. 
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
 * ----
 *
 * 
 * REVISIONS and CHANGES 
 *  - 14/05/2011   V1.0   Thomas Forbriger
 * 
 * ============================================================================
 */

// include guard
#ifndef AFF_CARRAY_H_VERSION

#define AFF_CARRAY_H_VERSION \
  "AFF_CARRAY_H   V1.0   "
#define AFF_CARRAY_H_CVSID \
  "$Id$"

thomas.forbriger's avatar
thomas.forbriger committed
45
46
47
#include<aff/array.h>
#include<aff/lib/checkedcast.h>

48
49
namespace aff {

thomas.forbriger's avatar
thomas.forbriger committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
  /*! \brief Interface class to raw memory (C style array)
   *
   */
  template<class T>
    class ConstCArray
    {
      public:
        /*! \name Various types
         *
         * In particular due to our concept of const-correctness we need
         * several typedefs to declare types derived from the element type of
         * the array.
         *
         * \sa \ref sec_design_interface_typedef
         * \sa \ref sec_design_const
         */
        //@{
        //! Type of array to be interfaced
        typedef aff::ConstArray<T> Tarray;
        //! Type of representation
        typedef Tarray::Trepresentation Trepresentation;
        //! Type of shape
        typedef Tarray::Tshape Tshape;
        //! we use this for one of the access operators
        typedef Tshape::TSizeVec TSizeVec;
        //! Element type
        typedef Tarray::Tvalue Tvalue;
        //! Type of pointer to element
        typedef Tarray::Tpointer Tpointer;
        //! Type of reference to element
        typedef Tarray::Treference Treference;
        //! const element type
        typedef Tarray::Tconst_value Tconst_value;
        //! Type of pointer to const element
        typedef Tarray::Tconst_pointer Tconst_pointer;
        //! Type of reference to const element
        typedef Tarray::Tconst_reference Tconst_reference;
        //! Type of this array
        typedef ConstCArray<T> Tcontainer;
        //! Type of the array of const values
        typedef Tcontainer Tcontainer_of_const;
        //! Short for Tcontainer_of_const
        typedef Tcontainer Tcoc;
        //@}

        /*-----------------------------------------------------------------*/
        
        /*! \name Constructors
         *
         * No copy constructors or copy operators are provided since this is
         * provided as an interface class only.
         */
        //@{
        //! construct from shape and representation
        ConstCArray(const Tarray& array)
          : Mrepresentation(array.representation())
        {
        }
        //@}

        /*------------------------------------------------------------------*/

        /*! \name Shape access
         */
        //@{
        //! size of dimension \par i
        const Tsize& size(const Tsubscript& i) const
        { return (Msize[i]); }
        //! stride of dimension \par i
        const Tsize& stride(const Tsubscript& i) const
        { return Mstride[i]; }
        //! size of dimensions
        const TSizeVec& size() const
        { return (Msize); }
        //! strides of dimensions
        const TSizeVec& stride() const
        { return Mstride; }
        //@}

        /*-----------------------------------------------------------------*/
        
        /*! \name Memory access
         *
         */
        //@{
        //! return pointer to first element in Fortran layout
        Tconst_pointer pointer() const 
        { return(&Mrepresentation[this->offset()]); }
        /*! \brief return type-casted pointer to first element in Fortran
         * layout
         *
         * The cast checks for const-correctness and type-size. But you have
         * to ensure that there is a meaningful relation between both types
         * involved.
         *
         * \sa aff::util::SizeCheckedCast
         */
        template<class TT>
          TT* castedpointer() const
          { return(SizeCheckedCast<Tconst_value,TT>::cast(this->pointer())); }
        //@}

      protected:
        //! pass offset to derived class
        const TSizeVec::Tvalue& offset() const { return Moffset; }
          
      private:
        //! representation member
        Trepresentation Mrepresentation;
        //! sizes of dimensions
        TSizeVec Msize;
        //! strides of dimensions
        TSizeVec Mstride;
        //! offset to first index
        TSizeVec::Tvalue Moffset;

    }; // class ConstCArray

  /*======================================================================*/

  /*! \brief Interface class to raw memory (C style array)
   *
   * See aff::ConstCArray for the basic concepts used here.
   *
   * \sa aff::ConstCArray
   */
  template<class T>
    class CArray:
      public ConstCArray<T>
    {
      public:
        /*! \name Various types
         *
         * In particular due to our concept of const-correctness we need
         * several typedefs to declare types derived from the element type of
         * the array.
         *
         * \sa \ref sec_design_interface_typedef
         * \sa \ref sec_design_const
         */
        //@{
        //! Type of array to be interfaced
        typedef aff::Array<T> Tarray;
        //! Base class
        typedef aff::ConstCArray<T> Tbase;
        //! Type of representation
        typedef Tarray::Trepresentation Trepresentation;
        //! Type of shape
        typedef Tarray::Tshape Tshape;
        //! we use this for one of the access operators
        typedef Tshape::TSizeVec TSizeVec;
        //! Element type
        typedef Tarray::Tvalue Tvalue;
        //! Type of pointer to element
        typedef Tarray::Tpointer Tpointer;
        //! Type of reference to element
        typedef Tarray::Treference Treference;
        //! const element type
        typedef Tarray::Tconst_value Tconst_value;
        //! Type of pointer to const element
        typedef Tarray::Tconst_pointer Tconst_pointer;
        //! Type of reference to const element
        typedef Tarray::Tconst_reference Tconst_reference;
        //! Type of this array
        typedef CArray<T> Tcontainer;
        //! Type of the array of const values
        typedef Tbase Tcontainer_of_const;
        //! Short for Tcontainer_of_const
        typedef Tcontainer Tcoc;
        //@}

        /*-----------------------------------------------------------------*/
        
        /*! \name Constructors
         *
         * No copy constructors or copy operators are provided since this is
         * provided as an interface class only.
         */
        //@{
        //! construct from shape and representation
        Array(const Tarray& array)
          : Tbase(array), Mrepresentation(array.representation()) 
        { }
        //@}

        /*-----------------------------------------------------------------*/
        
        /*! \name Memory access
         *
         */
        //@{
        //! return pointer to first element in Fortran layout
        Tpointer pointer() const 
        { return(&Mrepresentation[this->offset()]); }
        /*! \brief return type-casted pointer to first element in Fortran
         * layout
         *
         * The cast checks for const-correctness and type-size. But you have
         * to ensure that there is a meaningful relation between both types
         * involved.
         *
         * \sa aff::util::SizeCheckedCast
         */
        template<class TT>
          TT* castedpointer() const
          { return(SizeCheckedCast<Tvalue,TT>::cast(this->pointer())); }
        //@}

      private:
        //! my (mutable) data representation
        Trepresentation Mrepresentation;

    }; // class CArray

264
265
266
267
268
} // namespace aff

#endif // AFF_CARRAY_H_VERSION (includeguard)

/* ----- END OF Carray.h ----- */