From d22eea2fd6130b431bc11694410775b993c5ab7a Mon Sep 17 00:00:00 2001 From: Dan Goodliffe Date: Thu, 17 Jun 2021 19:38:26 +0100 Subject: Make RecordSet foreach ready --- lib/dbRecordSet.cpp | 39 +++++++++++++++++++++++++++++++++++++++ lib/dbRecordSet.h | 8 ++++++++ 2 files changed, 47 insertions(+) diff --git a/lib/dbRecordSet.cpp b/lib/dbRecordSet.cpp index 3754130..d5e55f0 100644 --- a/lib/dbRecordSet.cpp +++ b/lib/dbRecordSet.cpp @@ -1,4 +1,5 @@ #include "dbRecordSet.h" +#include "helpers.h" namespace MyGrate { RowView::RowView(const RecordSet * rs, std::size_t rw) : recordSet {rs}, row {rw} { } @@ -9,6 +10,32 @@ namespace MyGrate { return recordSet->at(row, col); } + bool + RowView::operator==(const RowView & that) const + { + verify(this->recordSet == that.recordSet, "Cannot iterator between RecordSets"); + return this->row == that.row; + } + + RowView & + RowView::operator++() + { + row++; + return *this; + } + + const RowView & + RowView::operator*() const + { + return *this; + } + + std::size_t + RowView::currentRow() const + { + return row; + } + RowView RecordSet::operator[](std::size_t row) const { @@ -20,4 +47,16 @@ namespace MyGrate { { return at(0, 0); } + + RowView + RecordSet::begin() const + { + return {this, 0}; + } + + RowView + RecordSet::end() const + { + return {this, rows()}; + } } diff --git a/lib/dbRecordSet.h b/lib/dbRecordSet.h index e430e58..a9da882 100644 --- a/lib/dbRecordSet.h +++ b/lib/dbRecordSet.h @@ -25,6 +25,11 @@ namespace MyGrate { return std::make_unique((*this)[I + O]...); } + bool operator==(const RowView &) const; + RowView & operator++(); + const RowView & operator*() const; + std::size_t currentRow() const; + private: const RecordSet * recordSet; std::size_t row; @@ -39,6 +44,9 @@ namespace MyGrate { virtual DbValue at(std::size_t, std::size_t) const = 0; RowView operator[](std::size_t row) const; DbValue operator*() const; + + RowView begin() const; + RowView end() const; }; using RecordSetPtr = std::unique_ptr; } -- cgit v1.2.3