This is some awesome robot code
Dependencies: mbed-rtos mbed QEI
Fork of ICRSEurobot13 by
tvmet/CommaInitializer.h@87:272a7129b04b, 2013-04-16 (annotated)
- Committer:
- madcowswe
- Date:
- Tue Apr 16 12:24:25 2013 +0000
- Revision:
- 87:272a7129b04b
- Parent:
- 15:9c5aaeda36dc
Red 57
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: CommaInitializer.h,v 1.18 2007-06-23 15:58:58 opetzold Exp $ |
madcowswe | 15:9c5aaeda36dc | 22 | */ |
madcowswe | 15:9c5aaeda36dc | 23 | |
madcowswe | 15:9c5aaeda36dc | 24 | #ifndef TVMET_COMMA_INITIALIZER_H |
madcowswe | 15:9c5aaeda36dc | 25 | #define TVMET_COMMA_INITIALIZER_H |
madcowswe | 15:9c5aaeda36dc | 26 | |
madcowswe | 15:9c5aaeda36dc | 27 | #include <tvmet/CompileTimeError.h> |
madcowswe | 15:9c5aaeda36dc | 28 | |
madcowswe | 15:9c5aaeda36dc | 29 | namespace tvmet { |
madcowswe | 15:9c5aaeda36dc | 30 | |
madcowswe | 15:9c5aaeda36dc | 31 | |
madcowswe | 15:9c5aaeda36dc | 32 | /** |
madcowswe | 15:9c5aaeda36dc | 33 | * \class CommaInitializer CommaInitializer.h "tvmet/CommaInitializer.h" |
madcowswe | 15:9c5aaeda36dc | 34 | * \brief Initialize classes using a comma separated lists. |
madcowswe | 15:9c5aaeda36dc | 35 | * |
madcowswe | 15:9c5aaeda36dc | 36 | * The comma operator is called when it appears next to an object of |
madcowswe | 15:9c5aaeda36dc | 37 | * the type the comma is defined for. However, "operator," is not called |
madcowswe | 15:9c5aaeda36dc | 38 | * for function argument lists, only for objects that are out in the open, |
madcowswe | 15:9c5aaeda36dc | 39 | * separated by commas (Thinking C++ |
madcowswe | 15:9c5aaeda36dc | 40 | * <a href=http://www.ida.liu.se/~TDDA14/online/v1ticpp/Chapter12.html> |
madcowswe | 15:9c5aaeda36dc | 41 | * Ch.12: Operator comma</a>). |
madcowswe | 15:9c5aaeda36dc | 42 | * |
madcowswe | 15:9c5aaeda36dc | 43 | * This implementation uses the same technique as described in Todd Veldhuizen |
madcowswe | 15:9c5aaeda36dc | 44 | * Techniques for Scientific C++ |
madcowswe | 15:9c5aaeda36dc | 45 | * <a href=http://extreme.indiana.edu/~tveldhui/papers/techniques/techniques01.html#l43> |
madcowswe | 15:9c5aaeda36dc | 46 | * chapter 1.11 Comma overloading</a>. |
madcowswe | 15:9c5aaeda36dc | 47 | * |
madcowswe | 15:9c5aaeda36dc | 48 | * The initializer list is avaible after instanciation of the object, |
madcowswe | 15:9c5aaeda36dc | 49 | * therefore use it like: |
madcowswe | 15:9c5aaeda36dc | 50 | * \code |
madcowswe | 15:9c5aaeda36dc | 51 | * vector3d t; |
madcowswe | 15:9c5aaeda36dc | 52 | * t = 1.0, 2.0, 3.0; |
madcowswe | 15:9c5aaeda36dc | 53 | * \endcode |
madcowswe | 15:9c5aaeda36dc | 54 | * It's evaluated to (((t = 1.0), 2.0), 3.0) |
madcowswe | 15:9c5aaeda36dc | 55 | * |
madcowswe | 15:9c5aaeda36dc | 56 | * For matrizes the initilization is done row wise. |
madcowswe | 15:9c5aaeda36dc | 57 | * |
madcowswe | 15:9c5aaeda36dc | 58 | * If the comma separted list of values longer then the size of the vector |
madcowswe | 15:9c5aaeda36dc | 59 | * or matrix a compile time error will occour. Otherwise the pending values |
madcowswe | 15:9c5aaeda36dc | 60 | * will be written random into the memory. |
madcowswe | 15:9c5aaeda36dc | 61 | * |
madcowswe | 15:9c5aaeda36dc | 62 | */ |
madcowswe | 15:9c5aaeda36dc | 63 | template<class Obj, std::size_t LEN> |
madcowswe | 15:9c5aaeda36dc | 64 | class CommaInitializer |
madcowswe | 15:9c5aaeda36dc | 65 | { |
madcowswe | 15:9c5aaeda36dc | 66 | CommaInitializer(); |
madcowswe | 15:9c5aaeda36dc | 67 | CommaInitializer& operator=(const CommaInitializer&); |
madcowswe | 15:9c5aaeda36dc | 68 | |
madcowswe | 15:9c5aaeda36dc | 69 | private: |
madcowswe | 15:9c5aaeda36dc | 70 | /** |
madcowswe | 15:9c5aaeda36dc | 71 | * \class Initializer |
madcowswe | 15:9c5aaeda36dc | 72 | * \brief Helper fo recursive overloaded comma operator. |
madcowswe | 15:9c5aaeda36dc | 73 | */ |
madcowswe | 15:9c5aaeda36dc | 74 | template<class T, std::size_t N> class Initializer |
madcowswe | 15:9c5aaeda36dc | 75 | { |
madcowswe | 15:9c5aaeda36dc | 76 | Initializer(); |
madcowswe | 15:9c5aaeda36dc | 77 | Initializer& operator=(const Initializer&); |
madcowswe | 15:9c5aaeda36dc | 78 | |
madcowswe | 15:9c5aaeda36dc | 79 | public: |
madcowswe | 15:9c5aaeda36dc | 80 | typedef T value_type; |
madcowswe | 15:9c5aaeda36dc | 81 | typedef T* iterator; |
madcowswe | 15:9c5aaeda36dc | 82 | |
madcowswe | 15:9c5aaeda36dc | 83 | public: |
madcowswe | 15:9c5aaeda36dc | 84 | Initializer(iterator iter) : m_iter(iter) { } |
madcowswe | 15:9c5aaeda36dc | 85 | |
madcowswe | 15:9c5aaeda36dc | 86 | /** Overloads the comma operator for recursive assign values from comma |
madcowswe | 15:9c5aaeda36dc | 87 | separated list. */ |
madcowswe | 15:9c5aaeda36dc | 88 | Initializer<value_type, N+1> operator,(value_type rhs) |
madcowswe | 15:9c5aaeda36dc | 89 | { |
madcowswe | 15:9c5aaeda36dc | 90 | TVMET_CT_CONDITION(N < LEN, CommaInitializerList_is_too_long) |
madcowswe | 15:9c5aaeda36dc | 91 | *m_iter = rhs; |
madcowswe | 15:9c5aaeda36dc | 92 | return Initializer<value_type, N+1>(m_iter + 1); |
madcowswe | 15:9c5aaeda36dc | 93 | } |
madcowswe | 15:9c5aaeda36dc | 94 | |
madcowswe | 15:9c5aaeda36dc | 95 | private: |
madcowswe | 15:9c5aaeda36dc | 96 | iterator m_iter; |
madcowswe | 15:9c5aaeda36dc | 97 | }; |
madcowswe | 15:9c5aaeda36dc | 98 | |
madcowswe | 15:9c5aaeda36dc | 99 | public: |
madcowswe | 15:9c5aaeda36dc | 100 | typedef typename Obj::value_type value_type; |
madcowswe | 15:9c5aaeda36dc | 101 | typedef value_type* iterator; |
madcowswe | 15:9c5aaeda36dc | 102 | |
madcowswe | 15:9c5aaeda36dc | 103 | public: |
madcowswe | 15:9c5aaeda36dc | 104 | CommaInitializer(const CommaInitializer& rhs) |
madcowswe | 15:9c5aaeda36dc | 105 | : m_object(rhs.m_object), |
madcowswe | 15:9c5aaeda36dc | 106 | m_data(rhs.m_data), |
madcowswe | 15:9c5aaeda36dc | 107 | m_wipeout_on_destruct(true) |
madcowswe | 15:9c5aaeda36dc | 108 | { |
madcowswe | 15:9c5aaeda36dc | 109 | rhs.disable(); |
madcowswe | 15:9c5aaeda36dc | 110 | } |
madcowswe | 15:9c5aaeda36dc | 111 | |
madcowswe | 15:9c5aaeda36dc | 112 | /** Constructor used by Vector or Matrix operator(value_type rhs) */ |
madcowswe | 15:9c5aaeda36dc | 113 | CommaInitializer(Obj& obj, value_type x) |
madcowswe | 15:9c5aaeda36dc | 114 | : m_object(obj), |
madcowswe | 15:9c5aaeda36dc | 115 | m_data(x), |
madcowswe | 15:9c5aaeda36dc | 116 | m_wipeout_on_destruct(true) |
madcowswe | 15:9c5aaeda36dc | 117 | { } |
madcowswe | 15:9c5aaeda36dc | 118 | |
madcowswe | 15:9c5aaeda36dc | 119 | /** Destructs and assigns the comma separated value. */ |
madcowswe | 15:9c5aaeda36dc | 120 | ~CommaInitializer() { |
madcowswe | 15:9c5aaeda36dc | 121 | if(m_wipeout_on_destruct) m_object.assign_value(m_data); |
madcowswe | 15:9c5aaeda36dc | 122 | } |
madcowswe | 15:9c5aaeda36dc | 123 | |
madcowswe | 15:9c5aaeda36dc | 124 | /** Overloaded comma operator, called only once for the first occoured comma. This |
madcowswe | 15:9c5aaeda36dc | 125 | means the first value is assigned by %operator=() and the 2nd value after the |
madcowswe | 15:9c5aaeda36dc | 126 | comma. Therfore we call the %Initializer::operator,() for the list starting |
madcowswe | 15:9c5aaeda36dc | 127 | after the 2nd. */ |
madcowswe | 15:9c5aaeda36dc | 128 | Initializer<value_type, 2> operator,(value_type rhs); |
madcowswe | 15:9c5aaeda36dc | 129 | |
madcowswe | 15:9c5aaeda36dc | 130 | void disable() const { m_wipeout_on_destruct = false; } |
madcowswe | 15:9c5aaeda36dc | 131 | |
madcowswe | 15:9c5aaeda36dc | 132 | private: |
madcowswe | 15:9c5aaeda36dc | 133 | Obj& m_object; |
madcowswe | 15:9c5aaeda36dc | 134 | value_type m_data; |
madcowswe | 15:9c5aaeda36dc | 135 | mutable bool m_wipeout_on_destruct; |
madcowswe | 15:9c5aaeda36dc | 136 | }; |
madcowswe | 15:9c5aaeda36dc | 137 | |
madcowswe | 15:9c5aaeda36dc | 138 | |
madcowswe | 15:9c5aaeda36dc | 139 | /* |
madcowswe | 15:9c5aaeda36dc | 140 | * Implementation |
madcowswe | 15:9c5aaeda36dc | 141 | */ |
madcowswe | 15:9c5aaeda36dc | 142 | template<class Obj, std::size_t LEN> |
madcowswe | 15:9c5aaeda36dc | 143 | typename CommaInitializer<Obj, LEN>::template Initializer<typename Obj::value_type, 2> |
madcowswe | 15:9c5aaeda36dc | 144 | CommaInitializer<Obj, LEN>::operator,(typename Obj::value_type rhs) |
madcowswe | 15:9c5aaeda36dc | 145 | { |
madcowswe | 15:9c5aaeda36dc | 146 | m_wipeout_on_destruct = false; |
madcowswe | 15:9c5aaeda36dc | 147 | iterator iter1 = m_object.data(); |
madcowswe | 15:9c5aaeda36dc | 148 | *iter1 = m_data; |
madcowswe | 15:9c5aaeda36dc | 149 | iterator iter2 = iter1 + 1; |
madcowswe | 15:9c5aaeda36dc | 150 | *iter2 = rhs; |
madcowswe | 15:9c5aaeda36dc | 151 | return Initializer<value_type, 2>(iter2 + 1); |
madcowswe | 15:9c5aaeda36dc | 152 | } |
madcowswe | 15:9c5aaeda36dc | 153 | |
madcowswe | 15:9c5aaeda36dc | 154 | |
madcowswe | 15:9c5aaeda36dc | 155 | |
madcowswe | 15:9c5aaeda36dc | 156 | } // namespace tvmet |
madcowswe | 15:9c5aaeda36dc | 157 | |
madcowswe | 15:9c5aaeda36dc | 158 | |
madcowswe | 15:9c5aaeda36dc | 159 | #endif // TVMET_COMMA_INITIALIZER_H |
madcowswe | 15:9c5aaeda36dc | 160 | |
madcowswe | 15:9c5aaeda36dc | 161 | // Local Variables: |
madcowswe | 15:9c5aaeda36dc | 162 | // mode:C++ |
madcowswe | 15:9c5aaeda36dc | 163 | // tab-width:8 |
madcowswe | 15:9c5aaeda36dc | 164 | // End: |