fortranshape.h 5.11 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.6 2006-03-28 16:03:02 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
40
41
42
43
 * 
 * ============================================================================
 */

// include guard
#ifndef AFF_FORTRANSHAPE_H_VERSION

#define AFF_FORTRANSHAPE_H_VERSION \
44
  "AFF_FORTRANSHAPE_H   V1.1"
thomas.forbriger's avatar
thomas.forbriger committed
45
#define AFF_FORTRANSHAPE_H_CVSID \
thomas.forbriger's avatar
thomas.forbriger committed
46
  "$Id: fortranshape.h,v 1.6 2006-03-28 16:03:02 tforb Exp $"
thomas.forbriger's avatar
thomas.forbriger committed
47
48

#include<aff/array.h>
49
#include<aff/lib/checkedcast.h>
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

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); }
81
82
83
84
85
86
      //! 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
    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
 */
105
template<class C>
thomas.forbriger's avatar
thomas.forbriger committed
106
107
108
class FortranArray: private aff::util::FortranShape {
  public:
    //! type of corresponding array
109
    typedef C Tarray;
thomas.forbriger's avatar
thomas.forbriger committed
110
111
112
113
114
115
116
117
    //! shape class
    typedef typename Tarray::Tshape Tshape;
    //! representation class
    typedef typename Tarray::Trepresentation Trepresentation;
    //! base class
    typedef aff::util::FortranShape Tbase;
    //! pointer to array base in memory
    typedef typename Tarray::Tpointer Tpointer;
118
119
    //! pointer to array base in memory
    typedef typename Tarray::Tvalue Tvalue;
thomas.forbriger's avatar
thomas.forbriger committed
120
    //! create
121
    FortranArray(Tarray array, const bool& BaseOne=true):
thomas.forbriger's avatar
thomas.forbriger committed
122
123
124
125
126
127
128
129
130
131
132
      Tbase(array.shape(), BaseOne)
      {
        Trepresentation repr=array.representation();
        Mpointer=&repr[this->Tbase::offset()];
      }
    //! access declarations
    //@{
    Tbase::first;
    Tbase::last;
    Tbase::dimlast;
    //@}
133
    //! return pointer to first element in Fortran layout
thomas.forbriger's avatar
thomas.forbriger committed
134
    Tpointer pointer() const { return(Mpointer); }
135
136
137
138
139
140
141
142
143
144
    /*! \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(Mpointer)); }
thomas.forbriger's avatar
thomas.forbriger committed
145
146
147
148
149
150
151
152
153
154
  private:
    //! pointer to memory
    Tpointer Mpointer;
}; // class FortranArray

} // namespace aff

#endif // AFF_FORTRANSHAPE_H_VERSION (includeguard)

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