TooN 2.1
internal/comma.hh
00001 namespace TooN{
00002 
00003 
00004 
00005 namespace Internal
00006 {
00007     template<int N, int Size, class P, class B> struct VectorFiller
00008     {
00009         Vector<Size, P, B>& v;
00010         VectorFiller<N-1, Size, P, B>* parent;
00011         bool underfill;
00012 
00013         VectorFiller(Vector<Size, P, B>& v_, VectorFiller<N-1, Size, P, B>* p)
00014         :v(v_),parent(p),underfill(N<v.size())
00015         {
00016         }
00017 
00018         VectorFiller<N+1, Size, P, B> operator,(const P& p)
00019         {
00020             Internal::CheckOverFill<N, Size>::check(v.size());
00021             v[N] = p;
00022             return VectorFiller<N+1, Size, P, B>(v, this);
00023         }
00024 
00025         ~VectorFiller()
00026         {
00027             #ifndef TOON_NDEBUG_FILL
00028                 if(underfill)
00029                 {
00030                     #ifdef TOON_TEST_INTERNALS
00031                         throw Internal::Underfill();
00032                     #else
00033                         std::cerr << "TooN: underfilled vector\n";
00034                         std::abort();
00035                     #endif
00036                 }
00037                 else if(parent)
00038                     parent->underfill = 0;
00039             #endif
00040         }
00041     };
00042 
00043     template<int Size, class P, class B> struct VectorStartFill
00044     {
00045         Vector<Size, P, B>& v;
00046         VectorStartFill(Vector<Size, P, B> & v_)
00047         :v(v_){}
00048 
00049         VectorFiller<1, Size, P, B> operator=(const P& p)
00050         {
00051             Internal::CheckOverFill<1, Size>::check(v.size());
00052             v[0] = p;
00053             return VectorFiller<1, Size, P, B>(v, 0);
00054         }
00055     };
00056 
00057 
00058     template<int N, int R, int C, class P, class B> struct MatrixFiller
00059     {
00060         Matrix<R, C, P, B>& m;
00061         MatrixFiller<N-1, R, C, P, B>* parent;
00062         int r, c;
00063         bool underfill;
00064 
00065         MatrixFiller(Matrix<R, C, P, B>& m_, MatrixFiller<N-1, R, C, P, B>*p, int r_, int c_)
00066         :m(m_),parent(p),r(r_),c(c_),underfill(r < m.num_rows())
00067         {}
00068 
00069         MatrixFiller<N+1, R, C, P, B> operator,(const P& p)
00070         {
00071             Internal::CheckMOverFill<N, R, C>::check(m.num_rows() * m.num_cols());
00072             m[r][c] = p;
00073             c++;
00074             if(c == m.num_cols())
00075             {
00076                 c=0;
00077                 r++;
00078             }       
00079 
00080             return MatrixFiller<N+1, R, C, P, B>(m, this, r, c);
00081         }
00082 
00083         ~MatrixFiller()
00084         {
00085             #ifndef TOON_NDEBUG_FILL
00086                 if(underfill)
00087                 {
00088                     #ifdef TOON_TEST_INTERNALS
00089                         throw Internal::Underfill();
00090                     #else
00091                         std::cerr << "TooN: underfilled matrix\n";
00092                         std::abort();
00093                     #endif
00094                 }
00095                 else if(parent)
00096                     parent->underfill = 0;
00097             #endif
00098         }
00099     };
00100 
00101     template<int R, int C, class P, class B> struct MatrixStartFill
00102     {
00103         Matrix<R, C, P, B>& m;
00104         MatrixStartFill(Matrix<R, C, P, B> & m_)
00105         :m(m_){}
00106 
00107         MatrixFiller<1, R, C, P, B> operator=(const P& p)
00108         {
00109             Internal::CheckMOverFill<0, R, C>::check(m.num_rows() * m.num_cols());
00110             m[0][0] = p;
00111             return MatrixFiller<1, R, C, P, B>(m, 0, 0, 1);
00112         }
00113     };
00114 
00115 }
00116 
00117 /**Set up a matrix for filling. Uses the following syntax:
00118 @code
00119     Matrix<2,2> m;
00120     Fill(m) = 1, 2,
00121               3, 4;
00122 @endcode
00123 Overfill is detected at compile time if possible, underfill
00124 is detected at run-time. The checks can not be optimized out
00125 for dynamic matrices, so define \c TOON_NDEBUG_FILL to prevent
00126 the checks from being used.
00127 @param m Matrix to fill
00128 @ingroup gLinAlg
00129 */
00130 template<int R, int C, class Precision, class Base> Internal::MatrixStartFill<R, C, Precision, Base> Fill(Matrix<R, C, Precision, Base>& m)
00131 {
00132     return m;
00133 }
00134 
00135 /**Set up a vector for filling. Uses the following syntax:
00136 @code
00137     Vector<2> v;
00138     Fill(v) = 1, 2;
00139 @endcode
00140 Overfill is detected at compile time if possible, underfill
00141 is detected at run-time. The checks can not be optimized out
00142 for dynamic vectors, so define \c TOON_NDEBUG_FILL to prevent
00143 the checks from being used.
00144 @param v Vector to fill
00145 @ingroup gLinAlg
00146 */
00147 template<int Size, class Precision, class Base> Internal::VectorStartFill<Size, Precision, Base> Fill(Vector<Size, Precision, Base>& v)
00148 {
00149     return v;
00150 }
00151 
00152 }