fortranshape.h 5.53 KB
Newer Older
thomas.forbriger's avatar
thomas.forbriger committed
1
2
3
4
5
/*! \file fortranshape.h
 * \brief prepare information to pass to Fortran subroutines (prototypes)
 * 
 * ----------------------------------------------------------------------------
 * 
thomas.forbriger's avatar
thomas.forbriger committed
6
 * $Id: fortranshape.h,v 1.7 2007/12/10 16:42:43 tforb Exp $
thomas.forbriger's avatar
thomas.forbriger committed
7
8
9
10
 * \author Thomas Forbriger
 * \date 23/12/2002
 * 
 * prepare information to pass to Fortran subroutines (prototypes)
thomas.forbriger's avatar
thomas.forbriger committed
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
 *
 * ----
 * 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
 * ----
thomas.forbriger's avatar
thomas.forbriger committed
27
28
29
30
31
 * 
 * Copyright (c) 2002 by Thomas Forbriger (IMG Frankfurt) 
 * 
 * REVISIONS and CHANGES 
 *  - 23/12/2002   V1.0   Thomas Forbriger
32
33
 *  - 03/01/2003   V1.1   (thof)
 *                        - now offers a casted pointer
34
35
 *                        - FortranArray now takes container type as template
 *                          argument
thomas.forbriger's avatar
thomas.forbriger committed
36
37
38
39
 *  - 10/12/2007   V1.2   (thof)
 *                        - we need to use the types of the representation
 *                          class to distinguish correctly for const
 *                          correctness
thomas.forbriger's avatar
thomas.forbriger committed
40
41
42
43
44
45
46
47
 * 
 * ============================================================================
 */

// include guard
#ifndef AFF_FORTRANSHAPE_H_VERSION

#define AFF_FORTRANSHAPE_H_VERSION \
thomas.forbriger's avatar
thomas.forbriger committed
48
  "AFF_FORTRANSHAPE_H   V1.2"
thomas.forbriger's avatar
thomas.forbriger committed
49
#define AFF_FORTRANSHAPE_H_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
50
  "$Id: fortranshape.h,v 1.7 2007/12/10 16:42:43 tforb Exp $"
thomas.forbriger's avatar
thomas.forbriger committed
51
52

#include<aff/array.h>
53
#include<aff/lib/checkedcast.h>
thomas.forbriger's avatar
thomas.forbriger committed
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

namespace aff {

namespace util {

  /*! \brief find appropriate leading dimensions
   *
   * This class is used to calculate appropriate leading dimensions from a
   * aff::Strided shape. These values are needed to pass an array to a Fortran
   * 77 subroutine.
   *
   * \sa aff::FortranArray
   */
  class FortranShape {
    public:
      //! This is coded for Strided shapes
      typedef aff::Strided Tshape;
      //! Vector of index values
      typedef Tshape::TIndexVec TIndexVec;
      //! Vector of size values
      typedef Tshape::TSizeVec TSizeVec;
      //! construct from shape
      explicit FortranShape(const Tshape& shape, const bool& BaseOne=true);
      //! first index in dimension \p i
      const Tsubscript& first(const Tdim& i) const { return(Mfirst[i]); }
      //! last index in dimension \p i
      const Tsubscript& last(const Tdim& i) const { return(Mlast[i]); }
      //! last index as dimensioned in dimension \p i
      const Tsubscript& dimlast(const Tdim& i) const { return(Mdimlast[i]); }
      //! offset to access representation
      const Tsubscript& offset() const { return(Moffset); }
85
86
87
88
89
90
      //! full first vector
      const TIndexVec& first() const { return(Mfirst); }
      //! full last vector
      const TIndexVec& last() const { return(Mlast); }
      //! full dimlast vector
      const TIndexVec& dimlast() const { return(Mdimlast); }
thomas.forbriger's avatar
thomas.forbriger committed
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
    private:
      //! first index
      TIndexVec Mfirst;
      //! last index
      TIndexVec Mlast;
      //! last index as dimensioned in memory
      TIndexVec Mdimlast;
      //! offset of first element in representation
      Tsubscript Moffset;
  }; // class FortranShape
} // namespace util

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

/*! \brief Class to provide Fortran interface values
 *
 * \sa aff::util::FortranShape
 */
109
template<class C>
thomas.forbriger's avatar
thomas.forbriger committed
110
111
112
class FortranArray: private aff::util::FortranShape {
  public:
    //! type of corresponding array
113
    typedef C Tarray;
thomas.forbriger's avatar
thomas.forbriger committed
114
115
116
117
    //! shape class
    typedef typename Tarray::Tshape Tshape;
    //! representation class
    typedef typename Tarray::Trepresentation Trepresentation;
thomas.forbriger's avatar
thomas.forbriger committed
118
119
    //! pointer to value used in representation class
    typedef typename Trepresentation::Tpointer Treppointer;
thomas.forbriger's avatar
thomas.forbriger committed
120
121
122
123
    //! base class
    typedef aff::util::FortranShape Tbase;
    //! pointer to array base in memory
    typedef typename Tarray::Tpointer Tpointer;
thomas.forbriger's avatar
thomas.forbriger committed
124
    //! value type in array class
125
    typedef typename Tarray::Tvalue Tvalue;
thomas.forbriger's avatar
thomas.forbriger committed
126
127
    //! value type in representation class
    typedef typename Trepresentation::Tvalue Trepvalue;
thomas.forbriger's avatar
thomas.forbriger committed
128
    //! create
129
    FortranArray(Tarray array, const bool& BaseOne=true):
thomas.forbriger's avatar
thomas.forbriger committed
130
131
132
133
134
135
136
137
138
139
140
      Tbase(array.shape(), BaseOne)
      {
        Trepresentation repr=array.representation();
        Mpointer=&repr[this->Tbase::offset()];
      }
    //! access declarations
    //@{
    Tbase::first;
    Tbase::last;
    Tbase::dimlast;
    //@}
141
    //! return pointer to first element in Fortran layout
thomas.forbriger's avatar
thomas.forbriger committed
142
    Treppointer pointer() const { return(Mpointer); }
143
144
145
146
147
148
149
150
151
    /*! \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
thomas.forbriger's avatar
thomas.forbriger committed
152
      { return(SizeCheckedCast<Trepvalue,TT>::cast(Mpointer)); }
thomas.forbriger's avatar
thomas.forbriger committed
153
154
  private:
    //! pointer to memory
thomas.forbriger's avatar
thomas.forbriger committed
155
    Treppointer Mpointer;
thomas.forbriger's avatar
thomas.forbriger committed
156
157
158
159
160
161
162
}; // class FortranArray

} // namespace aff

#endif // AFF_FORTRANSHAPE_H_VERSION (includeguard)

/* ----- END OF fortranshape.h ----- */