Oskar Weigl
/
Eurobot2013
We are going to win! wohoo
tvmet/CommaInitializer.h@9:08552997b544, 2012-11-14 (annotated)
- Committer:
- madcowswe
- Date:
- Wed Nov 14 17:15:53 2012 +0000
- Revision:
- 9:08552997b544
- Parent:
- 1:6799c07fe510
Added an important comment
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sv | 1:6799c07fe510 | 1 | /* |
sv | 1:6799c07fe510 | 2 | * Tiny Vector Matrix Library |
sv | 1:6799c07fe510 | 3 | * Dense Vector Matrix Libary of Tiny size using Expression Templates |
sv | 1:6799c07fe510 | 4 | * |
sv | 1:6799c07fe510 | 5 | * Copyright (C) 2001 - 2007 Olaf Petzold <opetzold@users.sourceforge.net> |
sv | 1:6799c07fe510 | 6 | * |
sv | 1:6799c07fe510 | 7 | * This library is free software; you can redistribute it and/or |
sv | 1:6799c07fe510 | 8 | * modify it under the terms of the GNU Lesser General Public |
sv | 1:6799c07fe510 | 9 | * License as published by the Free Software Foundation; either |
sv | 1:6799c07fe510 | 10 | * version 2.1 of the License, or (at your option) any later version. |
sv | 1:6799c07fe510 | 11 | * |
sv | 1:6799c07fe510 | 12 | * This library is distributed in the hope that it will be useful, |
sv | 1:6799c07fe510 | 13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
sv | 1:6799c07fe510 | 14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
sv | 1:6799c07fe510 | 15 | * Lesser General Public License for more details. |
sv | 1:6799c07fe510 | 16 | * |
sv | 1:6799c07fe510 | 17 | * You should have received a copy of the GNU Lesser General Public |
sv | 1:6799c07fe510 | 18 | * License along with this library; if not, write to the Free Software |
sv | 1:6799c07fe510 | 19 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
sv | 1:6799c07fe510 | 20 | * |
sv | 1:6799c07fe510 | 21 | * $Id: CommaInitializer.h,v 1.18 2007-06-23 15:58:58 opetzold Exp $ |
sv | 1:6799c07fe510 | 22 | */ |
sv | 1:6799c07fe510 | 23 | |
sv | 1:6799c07fe510 | 24 | #ifndef TVMET_COMMA_INITIALIZER_H |
sv | 1:6799c07fe510 | 25 | #define TVMET_COMMA_INITIALIZER_H |
sv | 1:6799c07fe510 | 26 | |
sv | 1:6799c07fe510 | 27 | #include <tvmet/CompileTimeError.h> |
sv | 1:6799c07fe510 | 28 | |
sv | 1:6799c07fe510 | 29 | namespace tvmet { |
sv | 1:6799c07fe510 | 30 | |
sv | 1:6799c07fe510 | 31 | |
sv | 1:6799c07fe510 | 32 | /** |
sv | 1:6799c07fe510 | 33 | * \class CommaInitializer CommaInitializer.h "tvmet/CommaInitializer.h" |
sv | 1:6799c07fe510 | 34 | * \brief Initialize classes using a comma separated lists. |
sv | 1:6799c07fe510 | 35 | * |
sv | 1:6799c07fe510 | 36 | * The comma operator is called when it appears next to an object of |
sv | 1:6799c07fe510 | 37 | * the type the comma is defined for. However, "operator," is not called |
sv | 1:6799c07fe510 | 38 | * for function argument lists, only for objects that are out in the open, |
sv | 1:6799c07fe510 | 39 | * separated by commas (Thinking C++ |
sv | 1:6799c07fe510 | 40 | * <a href=http://www.ida.liu.se/~TDDA14/online/v1ticpp/Chapter12.html> |
sv | 1:6799c07fe510 | 41 | * Ch.12: Operator comma</a>). |
sv | 1:6799c07fe510 | 42 | * |
sv | 1:6799c07fe510 | 43 | * This implementation uses the same technique as described in Todd Veldhuizen |
sv | 1:6799c07fe510 | 44 | * Techniques for Scientific C++ |
sv | 1:6799c07fe510 | 45 | * <a href=http://extreme.indiana.edu/~tveldhui/papers/techniques/techniques01.html#l43> |
sv | 1:6799c07fe510 | 46 | * chapter 1.11 Comma overloading</a>. |
sv | 1:6799c07fe510 | 47 | * |
sv | 1:6799c07fe510 | 48 | * The initializer list is avaible after instanciation of the object, |
sv | 1:6799c07fe510 | 49 | * therefore use it like: |
sv | 1:6799c07fe510 | 50 | * \code |
sv | 1:6799c07fe510 | 51 | * vector3d t; |
sv | 1:6799c07fe510 | 52 | * t = 1.0, 2.0, 3.0; |
sv | 1:6799c07fe510 | 53 | * \endcode |
sv | 1:6799c07fe510 | 54 | * It's evaluated to (((t = 1.0), 2.0), 3.0) |
sv | 1:6799c07fe510 | 55 | * |
sv | 1:6799c07fe510 | 56 | * For matrizes the initilization is done row wise. |
sv | 1:6799c07fe510 | 57 | * |
sv | 1:6799c07fe510 | 58 | * If the comma separted list of values longer then the size of the vector |
sv | 1:6799c07fe510 | 59 | * or matrix a compile time error will occour. Otherwise the pending values |
sv | 1:6799c07fe510 | 60 | * will be written random into the memory. |
sv | 1:6799c07fe510 | 61 | * |
sv | 1:6799c07fe510 | 62 | */ |
sv | 1:6799c07fe510 | 63 | template<class Obj, std::size_t LEN> |
sv | 1:6799c07fe510 | 64 | class CommaInitializer |
sv | 1:6799c07fe510 | 65 | { |
sv | 1:6799c07fe510 | 66 | CommaInitializer(); |
sv | 1:6799c07fe510 | 67 | CommaInitializer& operator=(const CommaInitializer&); |
sv | 1:6799c07fe510 | 68 | |
sv | 1:6799c07fe510 | 69 | private: |
sv | 1:6799c07fe510 | 70 | /** |
sv | 1:6799c07fe510 | 71 | * \class Initializer |
sv | 1:6799c07fe510 | 72 | * \brief Helper fo recursive overloaded comma operator. |
sv | 1:6799c07fe510 | 73 | */ |
sv | 1:6799c07fe510 | 74 | template<class T, std::size_t N> class Initializer |
sv | 1:6799c07fe510 | 75 | { |
sv | 1:6799c07fe510 | 76 | Initializer(); |
sv | 1:6799c07fe510 | 77 | Initializer& operator=(const Initializer&); |
sv | 1:6799c07fe510 | 78 | |
sv | 1:6799c07fe510 | 79 | public: |
sv | 1:6799c07fe510 | 80 | typedef T value_type; |
sv | 1:6799c07fe510 | 81 | typedef T* iterator; |
sv | 1:6799c07fe510 | 82 | |
sv | 1:6799c07fe510 | 83 | public: |
sv | 1:6799c07fe510 | 84 | Initializer(iterator iter) : m_iter(iter) { } |
sv | 1:6799c07fe510 | 85 | |
sv | 1:6799c07fe510 | 86 | /** Overloads the comma operator for recursive assign values from comma |
sv | 1:6799c07fe510 | 87 | separated list. */ |
sv | 1:6799c07fe510 | 88 | Initializer<value_type, N+1> operator,(value_type rhs) |
sv | 1:6799c07fe510 | 89 | { |
sv | 1:6799c07fe510 | 90 | TVMET_CT_CONDITION(N < LEN, CommaInitializerList_is_too_long) |
sv | 1:6799c07fe510 | 91 | *m_iter = rhs; |
sv | 1:6799c07fe510 | 92 | return Initializer<value_type, N+1>(m_iter + 1); |
sv | 1:6799c07fe510 | 93 | } |
sv | 1:6799c07fe510 | 94 | |
sv | 1:6799c07fe510 | 95 | private: |
sv | 1:6799c07fe510 | 96 | iterator m_iter; |
sv | 1:6799c07fe510 | 97 | }; |
sv | 1:6799c07fe510 | 98 | |
sv | 1:6799c07fe510 | 99 | public: |
sv | 1:6799c07fe510 | 100 | typedef typename Obj::value_type value_type; |
sv | 1:6799c07fe510 | 101 | typedef value_type* iterator; |
sv | 1:6799c07fe510 | 102 | |
sv | 1:6799c07fe510 | 103 | public: |
sv | 1:6799c07fe510 | 104 | CommaInitializer(const CommaInitializer& rhs) |
sv | 1:6799c07fe510 | 105 | : m_object(rhs.m_object), |
sv | 1:6799c07fe510 | 106 | m_data(rhs.m_data), |
sv | 1:6799c07fe510 | 107 | m_wipeout_on_destruct(true) |
sv | 1:6799c07fe510 | 108 | { |
sv | 1:6799c07fe510 | 109 | rhs.disable(); |
sv | 1:6799c07fe510 | 110 | } |
sv | 1:6799c07fe510 | 111 | |
sv | 1:6799c07fe510 | 112 | /** Constructor used by Vector or Matrix operator(value_type rhs) */ |
sv | 1:6799c07fe510 | 113 | CommaInitializer(Obj& obj, value_type x) |
sv | 1:6799c07fe510 | 114 | : m_object(obj), |
sv | 1:6799c07fe510 | 115 | m_data(x), |
sv | 1:6799c07fe510 | 116 | m_wipeout_on_destruct(true) |
sv | 1:6799c07fe510 | 117 | { } |
sv | 1:6799c07fe510 | 118 | |
sv | 1:6799c07fe510 | 119 | /** Destructs and assigns the comma separated value. */ |
sv | 1:6799c07fe510 | 120 | ~CommaInitializer() { |
sv | 1:6799c07fe510 | 121 | if(m_wipeout_on_destruct) m_object.assign_value(m_data); |
sv | 1:6799c07fe510 | 122 | } |
sv | 1:6799c07fe510 | 123 | |
sv | 1:6799c07fe510 | 124 | /** Overloaded comma operator, called only once for the first occoured comma. This |
sv | 1:6799c07fe510 | 125 | means the first value is assigned by %operator=() and the 2nd value after the |
sv | 1:6799c07fe510 | 126 | comma. Therfore we call the %Initializer::operator,() for the list starting |
sv | 1:6799c07fe510 | 127 | after the 2nd. */ |
sv | 1:6799c07fe510 | 128 | Initializer<value_type, 2> operator,(value_type rhs); |
sv | 1:6799c07fe510 | 129 | |
sv | 1:6799c07fe510 | 130 | void disable() const { m_wipeout_on_destruct = false; } |
sv | 1:6799c07fe510 | 131 | |
sv | 1:6799c07fe510 | 132 | private: |
sv | 1:6799c07fe510 | 133 | Obj& m_object; |
sv | 1:6799c07fe510 | 134 | value_type m_data; |
sv | 1:6799c07fe510 | 135 | mutable bool m_wipeout_on_destruct; |
sv | 1:6799c07fe510 | 136 | }; |
sv | 1:6799c07fe510 | 137 | |
sv | 1:6799c07fe510 | 138 | |
sv | 1:6799c07fe510 | 139 | /* |
sv | 1:6799c07fe510 | 140 | * Implementation |
sv | 1:6799c07fe510 | 141 | */ |
sv | 1:6799c07fe510 | 142 | template<class Obj, std::size_t LEN> |
sv | 1:6799c07fe510 | 143 | typename CommaInitializer<Obj, LEN>::template Initializer<typename Obj::value_type, 2> |
sv | 1:6799c07fe510 | 144 | CommaInitializer<Obj, LEN>::operator,(typename Obj::value_type rhs) |
sv | 1:6799c07fe510 | 145 | { |
sv | 1:6799c07fe510 | 146 | m_wipeout_on_destruct = false; |
sv | 1:6799c07fe510 | 147 | iterator iter1 = m_object.data(); |
sv | 1:6799c07fe510 | 148 | *iter1 = m_data; |
sv | 1:6799c07fe510 | 149 | iterator iter2 = iter1 + 1; |
sv | 1:6799c07fe510 | 150 | *iter2 = rhs; |
sv | 1:6799c07fe510 | 151 | return Initializer<value_type, 2>(iter2 + 1); |
sv | 1:6799c07fe510 | 152 | } |
sv | 1:6799c07fe510 | 153 | |
sv | 1:6799c07fe510 | 154 | |
sv | 1:6799c07fe510 | 155 | |
sv | 1:6799c07fe510 | 156 | } // namespace tvmet |
sv | 1:6799c07fe510 | 157 | |
sv | 1:6799c07fe510 | 158 | |
sv | 1:6799c07fe510 | 159 | #endif // TVMET_COMMA_INITIALIZER_H |
sv | 1:6799c07fe510 | 160 | |
sv | 1:6799c07fe510 | 161 | // Local Variables: |
sv | 1:6799c07fe510 | 162 | // mode:C++ |
sv | 1:6799c07fe510 | 163 | // tab-width:8 |
sv | 1:6799c07fe510 | 164 | // End: |