checkedcast.h 7.25 KB
Newer Older
1
2
3
4
5
/*! \file checkedcast.h
 * \brief size checked pointer cast (prototypes)
 * 
 * ----------------------------------------------------------------------------
 * 
6
 * $Id$
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
 * \author Thomas Forbriger
 * \date 03/01/2003
 * 
 * size checked pointer cast (prototypes)
 *
 * ----
 * 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
 * ----
 *
 * \sa aff::util::SizeCheckedCast
 * 
 * Copyright (c) 2003 by Thomas Forbriger (IMG Frankfurt) 
 * 
 * REVISIONS and CHANGES 
 *  - 03/01/2003   V1.0   Thomas Forbriger
 * 
 * ============================================================================
 */

// include guard
#ifndef AFF_CHECKEDCAST_H_VERSION

#define AFF_CHECKEDCAST_H_VERSION \
  "AFF_CHECKEDCAST_H   V1.0"
#define AFF_CHECKEDCAST_H_CVSID \
44
  "$Id$"
45
46
47
48
49
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

namespace aff {
namespace util {

  /*! \brief utility for compile-time checked cast
   *
   * Cast a pointer from T* to TT* and check for same size of values of type T
   * and TT and for const-correctness of the conversion. Although
   * reinterpret_cast should check for conts-correctness, we implement a check
   * here too (just for safety - I'm so paranoid, you know).
   *
   * \sa aff::util::SizeCheckedCast::ConstCheck
   * \sa aff::util::SizeCheckedCast::SizeCheck
   *
   * When exchaning data with Fortran code we have to pass pointers to arrays.
   * The f2c.h uses a structure (e.g.) for values of type doublereal that have
   * the same memory layout like complex<double> (first a double for the real
   * part than a double for the imaginary part) but there is no trivial
   * conversion between pointers of these types. Hence we have to use
   * reinterpret_cast, which is totally unchecked. With this class we
   * implement a cast that checks for matching type-size and const-correctness
   * at compile-time. The involved code-overhead is easily optimized away by
   * the compiler.
   *
   * There could be solutions to this problem that involve less code. This
   * however is a solution that produces well understandable error messages
   * with g++.
   *
   * Invoking static functions of subclasses in the cast-function may seem
   * unnecessary. Indeed, there is a solution of that problem, that relies on
   * typedefs to be resolved, which works well with g++. Since I don't know
   * about the template instatiation characteristics of other compilers, I
   * prefer to force the compiler to instantiate all involved template
   * specializations to check whether they support the cast.
   *
   * \sa aff::FortranArray
   * \sa tests/f77test.cc
   *
   * \param T the input pointer is of type T*
   * \param TT the output pointer is of type TT*
   */
  template<class T, class TT>
    class SizeCheckedCast {

      public:
        //! return a type-casted (and type-checked) pointer
        static TT* cast(T* p) {
          return(reinterpret_cast<TT*>(
                   SizeCheck<sizeof(T),sizeof(TT)>::have_same_size(
                     ConstCheck<T,TT>::respects_constness(p)))); }

      private:
        /*! \brief checks for const-correctness of the cast
         *
         * Only those specializations of this class offer a function
         * respects_constness, that represent a const-correct conversion.
         *
         * The general template if for two non-const types - which is a
         * const-correct conversion
         *
         * \param Tfrom source type (non-const)
         * \param Tto destination type (non-const)
         *
         * \sa aff::util::SizeCheckedCast::ConstCheck<const Tfrom, const Tto>
         * \sa aff::util::SizeCheckedCast::ConstCheck<Tfrom, const Tto>
         * \sa aff::util::SizeCheckedCast::ConstCheck<const Tfrom, Tto>
         * \sa aff::util::SizeCheckedCast
         */
        template<class Tfrom, class Tto> class ConstCheck {
          public: static T* respects_constness(T* v) { return v; }
        }; // class ConstCheck

        /*! \brief specialization for const-checked cast
         *
         * \param Tfrom source type (non-const)
         * \param Tto destination type (const)
         *
         * This conversion is const-correct.
         * \sa aff::util::SizeCheckedCast::ConstCheck
         * \sa aff::util::SizeCheckedCast
         */
        template<class Tfrom, class Tto> 
          class ConstCheck<Tfrom, const Tto> {
          public: static T* respects_constness(T* v) { return v; }
        }; // class ConstCheck (specialization)

        /*! \brief specialization for const-checked cast
         *
         * \param Tfrom source type (const)
         * \param Tto destination type (const)
         *
         * This conversion is const-correct.
         * \sa aff::util::SizeCheckedCast::ConstCheck
         * \sa aff::util::SizeCheckedCast
         */
        template<class Tfrom, class Tto> 
          class ConstCheck<const Tfrom, const Tto> {
          public: static T* respects_constness(T* v) { return v; }
        }; // class ConstCheck (specialization)

        /*! \brief specialization for const-checked cast
         *
         * \param Tfrom source type (const)
         * \param Tto destination type (non-const)
         *
         * This conversion is \b not const-correct.
         * \sa aff::util::SizeCheckedCast::ConstCheck
         * \sa aff::util::SizeCheckedCast
         */
        template<class Tfrom, class Tto> 
          class ConstCheck<const Tfrom, Tto> 
          { }; // class ConstCheck (specialization)

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

        /*! \brief checks for matching type-size
         *
         * The general template (non-matching sizes) does not offer 
         * a function have_same_size. This function is only offered by the
         * specialization that is invoked by conversions for types of same
         * size.
         *
         * \param N size of source type
         * \param M size of destination type (N!=M)
         *
         * \sa aff::util::SizeCheckedCast::SizeCheck<N,N>
         * \sa aff::util::SizeCheckedCast
         */ 
        template<int N, int M> class SizeCheck 
        { }; // class SizeCheck (specialization)

        /*! \brief specialization for type-size checked cast
         *
         * This specialization is invoked if both types have the same size 
         * \p N. It thus offers the function have_same_size.
         *
         * \sa aff::util::SizeCheckedCast::SizeCheck
         * \sa aff::util::SizeCheckedCast
         */
        template<int N> class SizeCheck<N,N> { 
          public: static T* have_same_size(T* v) { return v; }
        }; // class SizeCheck

    }; // class SizeCheckedCast

} // namespace util
} // namespace aff

#endif // AFF_CHECKEDCAST_H_VERSION (includeguard)

/* ----- END OF checkedcast.h ----- */