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