Honeycomb  0.1
Component-Model Framework
Builder.h
Go to the documentation of this file.
1 // Honeycomb, Copyright (C) 2015 NewGamePlus Inc. Distributed under the Boost Software License v1.0.
2 #pragma once
3 
5 
6 namespace honey { namespace matrix
7 {
8 
10 template<class Matrix>
11 class Builder
12 {
13 public:
14  Builder(Matrix& m) : m(m), row(0), col(0), height(0) {}
15  Builder(Builder&& rhs) : m(rhs.m), row(rhs.row), col(rhs.col), height(rhs.height) { rhs.height = 0; }
16 
18  {
19  if (height == 0) return; //Do nothing if no elements are set
20  assert(row + height == m.rows() && col == m.cols(), sout()
21  << "Assigned too few matrix elements in comma initializer.\n"
22  << "Matrix size: (" << m.rows() << ", " << m.cols() << ")\n"
23  << "Cursor index: (" << row << ", " << col << ")\n"
24  << "Current row height: " << height);
25  }
26 
28  template<class T>
29  Builder& operator,(const Builder<T>& rhs) { return operator,(rhs.eval()); }
30 
33  {
34  preAppend(1, 1);
35  m(row, col++) = rhs;
36  return *this;
37  }
38 
40  template<class T>
42  {
43  typedef MatrixBase<T> Rhs;
44  preAppend(rhs.rows(), rhs.cols());
45  if (Rhs::s_size != matrix::dynamic)
46  m.block<Rhs::s_rows, Rhs::s_cols>(row, col) = rhs;
47  else
48  m.block(row, col, rhs.rows(), rhs.cols()) = rhs;
49  col += rhs.cols();
50  return *this;
51  }
52 
54  Matrix& eval() const { return m; }
56  operator Matrix&() const { return eval(); }
57 
58 private:
59  void preAppend(sdt rows, sdt cols)
60  {
61  mt_unused(cols);
62  if (col == m.cols())
63  {
64  //Begin new row
65  row += height;
66  height = 0;
67  col = 0;
68  }
69  assert(row + rows <= m.rows() && col + cols <= m.cols(), sout()
70  << "Block assignment in comma initializer out of matrix bounds.\n"
71  << "Matrix size: (" << m.rows() << ", " << m.cols() << ")\n"
72  << "Cursor index: (" << row << ", " << col << ")\n"
73  << "Block size: (" << rows << ", " << cols << ")");
74  //Ensure any previously set blocks can't be overwritten
75  if (height < rows) height = rows;
76  }
77 
78  Matrix& m;
79  sdt row;
80  sdt col;
81  sdt height;
82 };
83 
84 } }
85 
(m x n)-dimensional matrix
Definition: Matrix.h:24
static const sdt dynamic
Definition: Traits.h:23
Builder & operator,(const MatrixBase< T > &rhs)
Append matrix.
Definition: Builder.h:41
Matrix comma initializer.
Definition: Builder.h:11
#define mt_unused(Param)
Remove the unused parameter warning.
Definition: Meta.h:20
matrix::Block< MatrixS, Rows, Cols > block(sdt row, sdt col, sdt rows=-1, sdt cols=-1)
Get block at offset (row,col) with size (Rows, Cols). If Rows or Cols is fixed then rows or cols may ...
Definition: Base.h:252
ptrdiff_t sdt
Size difference type, shorthand for ptrdiff_t.
Definition: Core.h:92
Matrix & eval() const
Get initialized matrix.
Definition: Builder.h:54
Builder & operator,(typename Matrix::Real rhs)
Append scalar.
Definition: Builder.h:32
ostringstream sout()
Shorthand to create ostringstream.
Definition: Stream.h:15
#define assert(...)
Forwards to assert_#args. See assert_1(), assert_2().
Definition: Debug.h:24
float Real
Real number type. See Real_ for real number operations and constants.
Definition: Real.h:21
~Builder()
Definition: Builder.h:17
Builder(Builder &&rhs)
Definition: Builder.h:15
Builder & operator,(const Builder< T > &rhs)
Append builder.
Definition: Builder.h:29
Builder(Matrix &m)
Definition: Builder.h:14
Matrix base class.
Definition: Base.h:17
Global Honeycomb namespace.