Eurobot2012_Primary

Dependencies:   mbed Eurobot_2012_Primary

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?

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