00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024 #ifndef TVMET_COMMA_INITIALIZER_H
00025 #define TVMET_COMMA_INITIALIZER_H
00026
00027 #include <tvmet/CompileTimeError.h>
00028
00029 namespace tvmet {
00030
00031
00063 template<class Obj, std::size_t LEN>
00064 class CommaInitializer
00065 {
00066 CommaInitializer();
00067 CommaInitializer& operator=(const CommaInitializer&);
00068
00069 private:
00074 template<class T, std::size_t N> class Initializer
00075 {
00076 Initializer();
00077 Initializer& operator=(const Initializer&);
00078
00079 public:
00080 typedef T value_type;
00081 typedef T* iterator;
00082
00083 public:
00084 Initializer(iterator iter) : m_iter(iter) { }
00085
00088 Initializer<value_type, N+1> operator,(value_type rhs)
00089 {
00090 TVMET_CT_CONDITION(N < LEN, CommaInitializerList_is_too_long)
00091 *m_iter = rhs;
00092 return Initializer<value_type, N+1>(m_iter + 1);
00093 }
00094
00095 private:
00096 iterator m_iter;
00097 };
00098
00099 public:
00100 typedef typename Obj::value_type value_type;
00101 typedef value_type* iterator;
00102
00103 public:
00104 CommaInitializer(const CommaInitializer& rhs)
00105 : m_object(rhs.m_object),
00106 m_data(rhs.m_data),
00107 m_wipeout_on_destruct(true)
00108 {
00109 rhs.disable();
00110 }
00111
00113 CommaInitializer(Obj& obj, value_type x)
00114 : m_object(obj),
00115 m_data(x),
00116 m_wipeout_on_destruct(true)
00117 { }
00118
00120 ~CommaInitializer() {
00121 if(m_wipeout_on_destruct) m_object.assign_value(m_data);
00122 }
00123
00128 Initializer<value_type, 2> operator,(value_type rhs);
00129
00130 void disable() const { m_wipeout_on_destruct = false; }
00131
00132 private:
00133 Obj& m_object;
00134 value_type m_data;
00135 mutable bool m_wipeout_on_destruct;
00136 };
00137
00138
00139
00140
00141
00142 template<class Obj, std::size_t LEN>
00143 typename CommaInitializer<Obj, LEN>::template Initializer<typename Obj::value_type, 2>
00144 CommaInitializer<Obj, LEN>::operator,(typename Obj::value_type rhs)
00145 {
00146 m_wipeout_on_destruct = false;
00147 iterator iter1 = m_object.data();
00148 *iter1 = m_data;
00149 iterator iter2 = iter1 + 1;
00150 *iter2 = rhs;
00151 return Initializer<value_type, 2>(iter2 + 1);
00152 }
00153
00154
00155
00156 }
00157
00158
00159 #endif // TVMET_COMMA_INITIALIZER_H
00160
00161
00162
00163
00164