Changeset 4137


Ignore:
Timestamp:
Jan 21, 2022, 3:02:05 PM (20 months ago)
Author:
Peter
Message:

add proxy class to allow returnning a MatrixView? from a function (same solution as in VectorView?). closes #202

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/test/matrix_view.cc

    r4132 r4137  
    198198
    199199
     200utility::MatrixView make_view(utility::Matrix& m)
     201{
     202  return utility::MatrixView(m);
     203}
     204
     205
    200206void test_matrix_view(test::Suite& suite)
    201207{
     
    212218  test_assignment<MatrixView>(suite, m);
    213219  test_iterators<MatrixView>(suite, m);
     220  MatrixView view = make_view(m);
     221}
     222
     223
     224utility::MatrixConstView make_const_view(const utility::Matrix& m)
     225{
     226  return utility::MatrixConstView(m);
    214227}
    215228
     
    227240  test_constructors<MatrixConstView>(suite, m);
    228241  test_iterators<MatrixConstView>(suite, m);
    229 }
     242
     243  MatrixConstView view = make_const_view(m);
     244}
  • trunk/yat/utility/MatrixView.cc

    r4129 r4137  
    134134
    135135
     136  MatrixView::MatrixView(MatrixView::proxy p)
     137    : view_(nullptr)
     138  {
     139    std::swap(view_, p.view_);
     140  }
     141
     142
     143  MatrixView::operator proxy()
     144  {
     145    return proxy(std::move(view_));
     146  }
     147
     148
     149  MatrixView::proxy::proxy(gsl_matrix_view*&& v)
     150    : view_(nullptr)
     151  {
     152    std::swap(view_, v);
     153  }
     154
    136155}}} // of namespace utility, yat and thep
  • trunk/yat/utility/MatrixView.h

    r4129 r4137  
    132132    // x n2 and view(0,0) points to other(k1, k2)
    133133    void copy(gsl_matrix* other, size_t k1, size_t k2, size_t n1, size_t n2);
     134
     135
     136    // See VectorView::proxy for motivation.
     137    struct proxy
     138    {
     139      proxy(void) = delete;
     140      proxy(gsl_matrix_view*&& v);
     141      gsl_matrix_view* view_;
     142    };
     143
     144  public:
     145    /**
     146       \brief create a MatrixView from an internal proxy class.
     147     */
     148    MatrixView(proxy p);
     149
     150    /**
     151       Conversion operator to a private proxy class.
     152     */
     153    operator proxy();
    134154  };
    135155
Note: See TracChangeset for help on using the changeset viewer.