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