TooN
comma.hh
1 //Copyright (C) Edward Rosten 2009
2 
3 //All rights reserved.
4 //
5 //Redistribution and use in source and binary forms, with or without
6 //modification, are permitted provided that the following conditions
7 //are met:
8 //1. Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 //2. Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 //THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND OTHER CONTRIBUTORS ``AS IS''
15 //AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 //IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 //ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR OTHER CONTRIBUTORS BE
18 //LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
19 //CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
20 //SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 //INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
22 //CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
23 //ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24 //POSSIBILITY OF SUCH DAMAGE.
25 
26 namespace TooN{
27 
28 
29 
30 namespace Internal
31 {
32  template<int N, int Size, class P, class B> struct VectorFiller
33  {
35  VectorFiller<N-1, Size, P, B>* parent;
36  bool underfill;
37 
39  :v(v_),parent(p),underfill(N<v.size())
40  {
41  }
42 
43  VectorFiller<N+1, Size, P, B> operator,(const P& p)
44  {
46  v[N] = p;
47  return VectorFiller<N+1, Size, P, B>(v, this);
48  }
49 
50  ~VectorFiller()
51  {
52  #ifndef TOON_NDEBUG_FILL
53  if(underfill)
54  {
55  #ifdef TOON_TEST_INTERNALS
56  throw Internal::Underfill();
57  #else
58  std::cerr << "TooN: underfilled vector\n";
59  std::abort();
60  #endif
61  }
62  else if(parent)
63  parent->underfill = 0;
64  #endif
65  }
66  };
67 
68  template<int Size, class P, class B> struct VectorStartFill
69  {
72  :v(v_){}
73 
74  VectorFiller<1, Size, P, B> operator=(const P& p)
75  {
77  v[0] = p;
78  return VectorFiller<1, Size, P, B>(v, 0);
79  }
80  };
81 
82 
83  template<int N, int R, int C, class P, class B> struct MatrixFiller
84  {
86  MatrixFiller<N-1, R, C, P, B>* parent;
87  int r, c;
88  bool underfill;
89 
91  :m(m_),parent(p),r(r_),c(c_),underfill(r < m.num_rows())
92  {}
93 
94  MatrixFiller<N+1, R, C, P, B> operator,(const P& p)
95  {
96  Internal::CheckMOverFill<N, R, C>::check(m.num_rows() * m.num_cols());
97  m[r][c] = p;
98  c++;
99  if(c == m.num_cols())
100  {
101  c=0;
102  r++;
103  }
104 
105  return MatrixFiller<N+1, R, C, P, B>(m, this, r, c);
106  }
107 
108  ~MatrixFiller()
109  {
110  #ifndef TOON_NDEBUG_FILL
111  if(underfill)
112  {
113  #ifdef TOON_TEST_INTERNALS
114  throw Internal::Underfill();
115  #else
116  std::cerr << "TooN: underfilled matrix\n";
117  std::abort();
118  #endif
119  }
120  else if(parent)
121  parent->underfill = 0;
122  #endif
123  }
124  };
125 
126  template<int R, int C, class P, class B> struct MatrixStartFill
127  {
130  :m(m_){}
131 
132  MatrixFiller<1, R, C, P, B> operator=(const P& p)
133  {
134  Internal::CheckMOverFill<0, R, C>::check(m.num_rows() * m.num_cols());
135  m[0][0] = p;
136  return MatrixFiller<1, R, C, P, B>(m, 0, 0, 1);
137  }
138  };
139 
140 }
141 
155 template<int R, int C, class Precision, class Base> Internal::MatrixStartFill<R, C, Precision, Base> Fill(Matrix<R, C, Precision, Base>& m)
156 {
157  return m;
158 }
159 
173 {
174  return v;
175 }
176 
177 }
Definition: comma.hh:126
Definition: overfill_error.hh:65
void Fill(Vector< Size, Precision, Base > &v, const Precision &p)
Definition: helpers.h:79
Definition: comma.hh:32
Pretty generic SFINAE introspection generator.
Definition: vec_test.cc:21
Definition: overfill_error.hh:32
Definition: comma.hh:83
Definition: comma.hh:68