From 4eb94786232aee936cd6371824764705c9359538 Mon Sep 17 00:00:00 2001 From: Christian Mollekopf Date: Wed, 17 Jun 2015 00:57:24 +0200 Subject: An almost generic query implementation. With equality filter on arbitrary properties as a bonus. --- common/resultset.h | 60 +++++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 55 insertions(+), 5 deletions(-) (limited to 'common') diff --git a/common/resultset.h b/common/resultset.h index f9b6cea..33f3c68 100644 --- a/common/resultset.h +++ b/common/resultset.h @@ -19,6 +19,8 @@ #pragma once #include +#include +#include "domain/applicationdomaintype.h" /* * An iterator to a result set. @@ -27,6 +29,22 @@ */ class ResultSet { public: + + + ResultSet(const std::function)> &generator) + : mValueGenerator(generator), + mIt(nullptr) + { + + } + + ResultSet(const std::function &generator) + : mGenerator(generator), + mIt(nullptr) + { + + } + ResultSet(const QVector &resultSet) : mResultSet(resultSet), mIt(nullptr) @@ -36,17 +54,46 @@ class ResultSet { bool next() { - if (!mIt) { - mIt = mResultSet.constBegin(); + if (mGenerator) { + mCurrentValue = mGenerator(); } else { - mIt++; + if (!mIt) { + mIt = mResultSet.constBegin(); + } else { + mIt++; + } + return mIt != mResultSet.constEnd(); } - return mIt != mResultSet.constEnd(); + } + + bool next(std::function callback) + { + Q_ASSERT(mValueGenerator); + return mValueGenerator(callback); + } + + bool next(std::function callback) + { + if (mGenerator) { + mCurrentValue = mGenerator(); + } else { + if (!mIt) { + mIt = mResultSet.constBegin(); + } else { + mIt++; + } + return mIt != mResultSet.constEnd(); + } + } QByteArray id() { - return *mIt; + if (mIt) { + return *mIt; + } else { + return mCurrentValue; + } } bool isEmpty() @@ -57,5 +104,8 @@ class ResultSet { private: QVector mResultSet; QVector::ConstIterator mIt; + QByteArray mCurrentValue; + std::function mGenerator; + std::function)> mValueGenerator; }; -- cgit v1.2.3