Eurobot2012_Primary
Dependencies: mbed Eurobot_2012_Primary
tvmet/TypePromotion.h@26:0995f61cb7b8, 2012-10-17 (annotated)
- Committer:
- narshu
- Date:
- Wed Oct 17 22:22:47 2012 +0000
- Revision:
- 26:0995f61cb7b8
- Parent:
- 25:143b19c1fb05
Eurobot 2012 Primary;
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
narshu | 25:143b19c1fb05 | 1 | /* |
narshu | 25:143b19c1fb05 | 2 | * Tiny Vector Matrix Library |
narshu | 25:143b19c1fb05 | 3 | * Dense Vector Matrix Libary of Tiny size using Expression Templates |
narshu | 25:143b19c1fb05 | 4 | * |
narshu | 25:143b19c1fb05 | 5 | * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net> |
narshu | 25:143b19c1fb05 | 6 | * |
narshu | 25:143b19c1fb05 | 7 | * This library is free software; you can redistribute it and/or |
narshu | 25:143b19c1fb05 | 8 | * modify it under the terms of the GNU Lesser General Public |
narshu | 25:143b19c1fb05 | 9 | * License as published by the Free Software Foundation; either |
narshu | 25:143b19c1fb05 | 10 | * version 2.1 of the License, or (at your option) any later version. |
narshu | 25:143b19c1fb05 | 11 | * |
narshu | 25:143b19c1fb05 | 12 | * This library is distributed in the hope that it will be useful, |
narshu | 25:143b19c1fb05 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
narshu | 25:143b19c1fb05 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
narshu | 25:143b19c1fb05 | 15 | * Lesser General Public License for more details. |
narshu | 25:143b19c1fb05 | 16 | * |
narshu | 25:143b19c1fb05 | 17 | * You should have received a copy of the GNU Lesser General Public |
narshu | 25:143b19c1fb05 | 18 | * License along with this library; if not, write to the Free Software |
narshu | 25:143b19c1fb05 | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
narshu | 25:143b19c1fb05 | 20 | * |
narshu | 25:143b19c1fb05 | 21 | * $Id: TypePromotion.h,v 1.10 2007-06-23 15:58:58 opetzold Exp $ |
narshu | 25:143b19c1fb05 | 22 | */ |
narshu | 25:143b19c1fb05 | 23 | |
narshu | 25:143b19c1fb05 | 24 | #ifndef TVMET_TYPE_PROMOTION_H |
narshu | 25:143b19c1fb05 | 25 | #define TVMET_TYPE_PROMOTION_H |
narshu | 25:143b19c1fb05 | 26 | |
narshu | 25:143b19c1fb05 | 27 | namespace tvmet { |
narshu | 25:143b19c1fb05 | 28 | |
narshu | 25:143b19c1fb05 | 29 | |
narshu | 25:143b19c1fb05 | 30 | /** |
narshu | 25:143b19c1fb05 | 31 | * \class PrecisionTraits TypePromotion.h "tvmet/TypePromotion.h" |
narshu | 25:143b19c1fb05 | 32 | * \brief Declaring ranks of types to avoid specializing |
narshu | 25:143b19c1fb05 | 33 | * |
narshu | 25:143b19c1fb05 | 34 | * All possible promoted types. For example, bool=1, int=2, float=3, double=4, |
narshu | 25:143b19c1fb05 | 35 | * etc. We can use a traits class to map from a type such as float onto its |
narshu | 25:143b19c1fb05 | 36 | * "precision rank". We will promote to whichever type has a higher |
narshu | 25:143b19c1fb05 | 37 | * "precision rank". f there is no "precision rank" for a type, we'll |
narshu | 25:143b19c1fb05 | 38 | * promote to whichever type requires more storage space |
narshu | 25:143b19c1fb05 | 39 | * (and hopefully more precision). |
narshu | 25:143b19c1fb05 | 40 | */ |
narshu | 25:143b19c1fb05 | 41 | template<class T> |
narshu | 25:143b19c1fb05 | 42 | struct PrecisionTraits { |
narshu | 25:143b19c1fb05 | 43 | enum { |
narshu | 25:143b19c1fb05 | 44 | rank = 0, /**< the rank of type. */ |
narshu | 25:143b19c1fb05 | 45 | known = 0 /**< true, if the rank is specialized = known. */ |
narshu | 25:143b19c1fb05 | 46 | }; |
narshu | 25:143b19c1fb05 | 47 | }; |
narshu | 25:143b19c1fb05 | 48 | |
narshu | 25:143b19c1fb05 | 49 | |
narshu | 25:143b19c1fb05 | 50 | #define TVMET_PRECISION(T,R) \ |
narshu | 25:143b19c1fb05 | 51 | template<> \ |
narshu | 25:143b19c1fb05 | 52 | struct PrecisionTraits< T > { \ |
narshu | 25:143b19c1fb05 | 53 | enum { \ |
narshu | 25:143b19c1fb05 | 54 | rank = R, \ |
narshu | 25:143b19c1fb05 | 55 | known = 1 \ |
narshu | 25:143b19c1fb05 | 56 | }; \ |
narshu | 25:143b19c1fb05 | 57 | }; |
narshu | 25:143b19c1fb05 | 58 | |
narshu | 25:143b19c1fb05 | 59 | |
narshu | 25:143b19c1fb05 | 60 | /* |
narshu | 25:143b19c1fb05 | 61 | * pod types |
narshu | 25:143b19c1fb05 | 62 | */ |
narshu | 25:143b19c1fb05 | 63 | TVMET_PRECISION(int, 100) |
narshu | 25:143b19c1fb05 | 64 | TVMET_PRECISION(unsigned int, 200) |
narshu | 25:143b19c1fb05 | 65 | TVMET_PRECISION(long, 300) |
narshu | 25:143b19c1fb05 | 66 | TVMET_PRECISION(unsigned long, 400) |
narshu | 25:143b19c1fb05 | 67 | |
narshu | 25:143b19c1fb05 | 68 | #if defined(TVMET_HAVE_LONG_LONG) |
narshu | 25:143b19c1fb05 | 69 | TVMET_PRECISION(long long, 500) |
narshu | 25:143b19c1fb05 | 70 | TVMET_PRECISION(unsigned long long, 600) |
narshu | 25:143b19c1fb05 | 71 | #endif // defined(TVMET_HAVE_LONG_LONG) |
narshu | 25:143b19c1fb05 | 72 | |
narshu | 25:143b19c1fb05 | 73 | TVMET_PRECISION(float, 700) |
narshu | 25:143b19c1fb05 | 74 | TVMET_PRECISION(double, 800) |
narshu | 25:143b19c1fb05 | 75 | |
narshu | 25:143b19c1fb05 | 76 | #if defined(TVMET_HAVE_LONG_DOUBLE) |
narshu | 25:143b19c1fb05 | 77 | TVMET_PRECISION(long double, 900) |
narshu | 25:143b19c1fb05 | 78 | #endif // defined(TVMET_HAVE_LONG_DOUBLE) |
narshu | 25:143b19c1fb05 | 79 | |
narshu | 25:143b19c1fb05 | 80 | |
narshu | 25:143b19c1fb05 | 81 | /* |
narshu | 25:143b19c1fb05 | 82 | * complex types |
narshu | 25:143b19c1fb05 | 83 | */ |
narshu | 25:143b19c1fb05 | 84 | #if defined(TVMET_HAVE_COMPLEX) |
narshu | 25:143b19c1fb05 | 85 | TVMET_PRECISION(std::complex<int>, 1000) |
narshu | 25:143b19c1fb05 | 86 | TVMET_PRECISION(std::complex<unsigned int>, 1100) |
narshu | 25:143b19c1fb05 | 87 | TVMET_PRECISION(std::complex<long>, 1200) |
narshu | 25:143b19c1fb05 | 88 | TVMET_PRECISION(std::complex<unsigned long>, 1300) |
narshu | 25:143b19c1fb05 | 89 | |
narshu | 25:143b19c1fb05 | 90 | #if defined(TVMET_HAVE_LONG_LONG) |
narshu | 25:143b19c1fb05 | 91 | TVMET_PRECISION(std::complex<long long>, 1400) |
narshu | 25:143b19c1fb05 | 92 | TVMET_PRECISION(std::complex<unsigned long long>, 1500) |
narshu | 25:143b19c1fb05 | 93 | #endif // defined(TVMET_HAVE_LONG_LONG) |
narshu | 25:143b19c1fb05 | 94 | |
narshu | 25:143b19c1fb05 | 95 | TVMET_PRECISION(std::complex<float>, 1600) |
narshu | 25:143b19c1fb05 | 96 | TVMET_PRECISION(std::complex<double>, 1700) |
narshu | 25:143b19c1fb05 | 97 | |
narshu | 25:143b19c1fb05 | 98 | #if defined(TVMET_HAVE_LONG_DOUBLE) |
narshu | 25:143b19c1fb05 | 99 | TVMET_PRECISION(std::complex<long double>, 1800) |
narshu | 25:143b19c1fb05 | 100 | #endif // defined(TVMET_HAVE_LONG_DOUBLE) |
narshu | 25:143b19c1fb05 | 101 | |
narshu | 25:143b19c1fb05 | 102 | #endif // defined(TVMET_HAVE_COMPLEX) |
narshu | 25:143b19c1fb05 | 103 | |
narshu | 25:143b19c1fb05 | 104 | |
narshu | 25:143b19c1fb05 | 105 | /** \class PrecisionTraits<int> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 106 | /** \class PrecisionTraits<unsigned int> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 107 | /** \class PrecisionTraits<long> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 108 | /** \class PrecisionTraits<unsigned long> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 109 | /** \class PrecisionTraits<long long> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 110 | /** \class PrecisionTraits<unsigned long long> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 111 | /** \class PrecisionTraits<float> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 112 | /** \class PrecisionTraits<double> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 113 | /** \class PrecisionTraits<long double> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 114 | /** \class PrecisionTraits< std::complex<int> > TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 115 | /** \class PrecisionTraits< std::complex<unsigned int> > TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 116 | /** \class PrecisionTraits< std::complex<long> > TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 117 | /** \class PrecisionTraits< std::complex<unsigned long> > TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 118 | /** \class PrecisionTraits< std::complex<long long> > TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 119 | /** \class PrecisionTraits< std::complex<unsigned long long> > TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 120 | /** \class PrecisionTraits< std::complex<float> > TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 121 | /** \class PrecisionTraits< std::complex<double> > TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 122 | /** \class PrecisionTraits< std::complex<long double> > TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 123 | |
narshu | 25:143b19c1fb05 | 124 | #undef TVMET_PRECISION |
narshu | 25:143b19c1fb05 | 125 | |
narshu | 25:143b19c1fb05 | 126 | |
narshu | 25:143b19c1fb05 | 127 | /** |
narshu | 25:143b19c1fb05 | 128 | * \class AutopromoteTraits TypePromotion.h "tvmet/TypePromotion.h" |
narshu | 25:143b19c1fb05 | 129 | * \brief The promoted types traits. |
narshu | 25:143b19c1fb05 | 130 | */ |
narshu | 25:143b19c1fb05 | 131 | template<class T> |
narshu | 25:143b19c1fb05 | 132 | struct AutopromoteTraits { |
narshu | 25:143b19c1fb05 | 133 | typedef T value_type; |
narshu | 25:143b19c1fb05 | 134 | }; |
narshu | 25:143b19c1fb05 | 135 | |
narshu | 25:143b19c1fb05 | 136 | |
narshu | 25:143b19c1fb05 | 137 | /* |
narshu | 25:143b19c1fb05 | 138 | * Defines a macro for specializing/defining |
narshu | 25:143b19c1fb05 | 139 | * the promotion traits. bool, char, unsigned char, short int, etc. will |
narshu | 25:143b19c1fb05 | 140 | * be autopromote to int, as in C and C++. |
narshu | 25:143b19c1fb05 | 141 | */ |
narshu | 25:143b19c1fb05 | 142 | #define TVMET_AUTOPROMOTE(T1,T2) \ |
narshu | 25:143b19c1fb05 | 143 | template<> \ |
narshu | 25:143b19c1fb05 | 144 | struct AutopromoteTraits<T1> { \ |
narshu | 25:143b19c1fb05 | 145 | typedef T2 value_type; \ |
narshu | 25:143b19c1fb05 | 146 | }; |
narshu | 25:143b19c1fb05 | 147 | |
narshu | 25:143b19c1fb05 | 148 | TVMET_AUTOPROMOTE(bool, int) |
narshu | 25:143b19c1fb05 | 149 | TVMET_AUTOPROMOTE(char, int) |
narshu | 25:143b19c1fb05 | 150 | TVMET_AUTOPROMOTE(unsigned char, int) |
narshu | 25:143b19c1fb05 | 151 | TVMET_AUTOPROMOTE(short int, int) |
narshu | 25:143b19c1fb05 | 152 | TVMET_AUTOPROMOTE(short unsigned int, unsigned int) |
narshu | 25:143b19c1fb05 | 153 | |
narshu | 25:143b19c1fb05 | 154 | /** \class AutopromoteTraits<bool> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 155 | /** \class AutopromoteTraits<char> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 156 | /** \class AutopromoteTraits<unsigned char> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 157 | /** \class AutopromoteTraits<short int> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 158 | /** \class AutopromoteTraits<short unsigned int> TypePromotion.h "tvmet/TypePromotion.h" */ |
narshu | 25:143b19c1fb05 | 159 | |
narshu | 25:143b19c1fb05 | 160 | #undef TVMET_AUTOPROMOTE |
narshu | 25:143b19c1fb05 | 161 | |
narshu | 25:143b19c1fb05 | 162 | |
narshu | 25:143b19c1fb05 | 163 | /** |
narshu | 25:143b19c1fb05 | 164 | * \class promoteTo TypePromotion.h "tvmet/TypePromotion.h" |
narshu | 25:143b19c1fb05 | 165 | * \brief Promote to T1. |
narshu | 25:143b19c1fb05 | 166 | */ |
narshu | 25:143b19c1fb05 | 167 | template<class T1, class T2, int promoteToT1> |
narshu | 25:143b19c1fb05 | 168 | struct promoteTo { |
narshu | 25:143b19c1fb05 | 169 | typedef T1 value_type; |
narshu | 25:143b19c1fb05 | 170 | }; |
narshu | 25:143b19c1fb05 | 171 | |
narshu | 25:143b19c1fb05 | 172 | |
narshu | 25:143b19c1fb05 | 173 | /** |
narshu | 25:143b19c1fb05 | 174 | * \class promoteTo<T1,T2,0> TypePromotion.h "tvmet/TypePromotion.h" |
narshu | 25:143b19c1fb05 | 175 | * \brief Promote to T2 |
narshu | 25:143b19c1fb05 | 176 | */ |
narshu | 25:143b19c1fb05 | 177 | template<class T1, class T2> |
narshu | 25:143b19c1fb05 | 178 | struct promoteTo<T1,T2,0> { |
narshu | 25:143b19c1fb05 | 179 | typedef T2 value_type; |
narshu | 25:143b19c1fb05 | 180 | }; |
narshu | 25:143b19c1fb05 | 181 | |
narshu | 25:143b19c1fb05 | 182 | |
narshu | 25:143b19c1fb05 | 183 | /** |
narshu | 25:143b19c1fb05 | 184 | * \class PromoteTraits TypePromotion.h "tvmet/TypePromotion.h" |
narshu | 25:143b19c1fb05 | 185 | * \brief Promote type traits |
narshu | 25:143b19c1fb05 | 186 | */ |
narshu | 25:143b19c1fb05 | 187 | template<class T1org, class T2org> |
narshu | 25:143b19c1fb05 | 188 | class PromoteTraits { |
narshu | 25:143b19c1fb05 | 189 | // Handle promotion of small integers to int/unsigned int |
narshu | 25:143b19c1fb05 | 190 | typedef typename AutopromoteTraits<T1org>::value_type T1; |
narshu | 25:143b19c1fb05 | 191 | typedef typename AutopromoteTraits<T2org>::value_type T2; |
narshu | 25:143b19c1fb05 | 192 | |
narshu | 25:143b19c1fb05 | 193 | enum { |
narshu | 25:143b19c1fb05 | 194 | // True if T1 is higher ranked |
narshu | 25:143b19c1fb05 | 195 | T1IsBetter = int(PrecisionTraits<T1>::rank) > int(PrecisionTraits<T2>::rank), |
narshu | 25:143b19c1fb05 | 196 | |
narshu | 25:143b19c1fb05 | 197 | // True if we know ranks for both T1 and T2 |
narshu | 25:143b19c1fb05 | 198 | knowBothRanks = PrecisionTraits<T1>::known && PrecisionTraits<T2>::known, |
narshu | 25:143b19c1fb05 | 199 | |
narshu | 25:143b19c1fb05 | 200 | // True if we know T1 but not T2 |
narshu | 25:143b19c1fb05 | 201 | knowT1butNotT2 = PrecisionTraits<T1>::known && !(PrecisionTraits<T2>::known), |
narshu | 25:143b19c1fb05 | 202 | |
narshu | 25:143b19c1fb05 | 203 | // True if we know T2 but not T1 |
narshu | 25:143b19c1fb05 | 204 | knowT2butNotT1 = PrecisionTraits<T2>::known && !(PrecisionTraits<T1>::known), |
narshu | 25:143b19c1fb05 | 205 | |
narshu | 25:143b19c1fb05 | 206 | // True if T1 is bigger than T2 |
narshu | 25:143b19c1fb05 | 207 | T1IsLarger = sizeof(T1) >= sizeof(T2), |
narshu | 25:143b19c1fb05 | 208 | |
narshu | 25:143b19c1fb05 | 209 | // We know T1 but not T2: true |
narshu | 25:143b19c1fb05 | 210 | // We know T2 but not T1: false |
narshu | 25:143b19c1fb05 | 211 | // Otherwise, if T1 is bigger than T2: true |
narshu | 25:143b19c1fb05 | 212 | defaultPromotion = knowT1butNotT2 ? false : (knowT2butNotT1 ? true : T1IsLarger), |
narshu | 25:143b19c1fb05 | 213 | |
narshu | 25:143b19c1fb05 | 214 | // If we have both ranks, then use them. |
narshu | 25:143b19c1fb05 | 215 | // If we have only one rank, then use the unknown type. |
narshu | 25:143b19c1fb05 | 216 | // If we have neither rank, then promote to the larger type. |
narshu | 25:143b19c1fb05 | 217 | promoteToT1 = (knowBothRanks ? T1IsBetter : defaultPromotion) ? 1 : 0 |
narshu | 25:143b19c1fb05 | 218 | }; |
narshu | 25:143b19c1fb05 | 219 | |
narshu | 25:143b19c1fb05 | 220 | public: |
narshu | 25:143b19c1fb05 | 221 | typedef typename promoteTo<T1,T2,promoteToT1>::value_type value_type; |
narshu | 25:143b19c1fb05 | 222 | }; |
narshu | 25:143b19c1fb05 | 223 | |
narshu | 25:143b19c1fb05 | 224 | |
narshu | 25:143b19c1fb05 | 225 | } // namespace tvmet |
narshu | 25:143b19c1fb05 | 226 | |
narshu | 25:143b19c1fb05 | 227 | #endif // TVMET_TYPE_PROMOTION_H |
narshu | 25:143b19c1fb05 | 228 | |
narshu | 25:143b19c1fb05 | 229 | // Local Variables: |
narshu | 25:143b19c1fb05 | 230 | // mode:C++ |
narshu | 25:143b19c1fb05 | 231 | // tab-width:8 |
narshu | 25:143b19c1fb05 | 232 | // End: |