From ea5668c5e3e72800c8e97a413f306da451061529 Mon Sep 17 00:00:00 2001 From: Ichthyostega Date: Tue, 5 Jan 2010 02:53:20 +0100 Subject: [PATCH] WIP define expected usage of IterSouce --- src/lib/iter-source.hpp | 73 ++++++++++++++++ tests/lib/iter-source-test.cpp | 147 +++++++++++++++++++++++++++++++++ 2 files changed, 220 insertions(+) create mode 100644 src/lib/iter-source.hpp create mode 100644 tests/lib/iter-source-test.cpp diff --git a/src/lib/iter-source.hpp b/src/lib/iter-source.hpp new file mode 100644 index 000000000..9ef4b93d9 --- /dev/null +++ b/src/lib/iter-source.hpp @@ -0,0 +1,73 @@ +/* + ITER-SOURCE.hpp - an interface to build an opaque iterator-based data source + + Copyright (C) Lumiera.org + 2010, Hermann Vosseler + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +*/ + +/** @file iter-source.hpp + ** Extension module to build an opaque data source, accessible as + ** Lumiera Forward Iterator. It is based on combining an IterAdapter + ** with classical polymorphism; here, the data source, which is addressed + ** by IderAdapter through the "iteration control API", is abstracted behind + ** an interface (with virtual functions). Together this allows to build + ** a simple data source type, without needing to disclose details of + ** the implementation. + ** + ** @see iter-adapter.hpp + ** @see itertool.hpp + ** @see iter-source-test.cpp + ** + */ + + +#ifndef LIB_ITER_SOURCE_H +#define LIB_ITER_SOURCE_H + + +#include "lib/iter-adapter.hpp" +//#include "lib/bool-checkable.hpp" + + + + +namespace lib { + + + namespace { + } + + + /** + * Iteration source interface to abstract a data source, + * which then can be accessed through IterAdapter as a frontend, + * allowing to pull individual elements until exhaustion. + * + * @see PlacementIndex::Table#_eachEntry_4check usage example + * @see iter-source-test.cpp + */ + template + class IterSource + : public lib::BoolCheckable > + { + }; + + + +} // namespace lib +#endif diff --git a/tests/lib/iter-source-test.cpp b/tests/lib/iter-source-test.cpp new file mode 100644 index 000000000..8ba128603 --- /dev/null +++ b/tests/lib/iter-source-test.cpp @@ -0,0 +1,147 @@ +/* + IterSource(Test) - how to build an opaque iterator-based data source + + Copyright (C) Lumiera.org + 2010, Hermann Vosseler + + This program is free software; you can redistribute it and/or + modify it under the terms of the GNU General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. + + You should have received a copy of the GNU General Public License + along with this program; if not, write to the Free Software + Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. + +* *****************************************************/ + + + +#include "lib/test/run.hpp" +#include "lib/util.hpp" + +#include "lib/iter-source.hpp" + +#include +#include +#include + + + +namespace lib { +namespace test{ + + using ::Test; + using boost::lexical_cast; + using util::isnil; + using std::list; + using std::cout; + using std::endl; + + + + namespace { // Subject of test + + + uint NUM_ELMS = 10; + + /** + * Explicit implementation of the IterSource interface (test dummy) + */ + class TestSource + : public IterSource + { + }; + + + /** test dummy, simply wrapping an STL container + * and exposing a range as Lumiera Forward Iterator + */ + struct WrappedList + { + list data_; + + WrappedList(uint num) + { + while (num) + data_.push_back(num--); + } + + typedef list::iterator sourceIter; + typedef RangeIter iterator; + + typedef list::const_iterator const_sourceIter; + typedef RangeIter const_iterator; + + iterator begin() { return iterator(data_.begin(),data_.end()); } + iterator end() { return iterator(); } + const_iterator begin() const { return const_iterator(data_.begin(),data_.end()); } + const_iterator end() const { return const_iterator(); } + + }; + + } // (END) impl test dummy containers + + + + + + + + /****************************************************************** + * @test create some (opaque) data sources, + * and then pull the data out by iteration. + * Demonstrates simple usage of the IterSource inteface + * + */ + class IterSource_test : public Test + { + + typedef IterSource::iterator IntIter; + typedef IterSource::iterator StrIter; + + virtual void + run (Arg arg) + { + if (0 < arg.size()) NUM_ELMS = lexical_cast (arg[0]); + + // build the test data sources + WrappedList customList(NUM_ELMS); + TestSource dedicatedSource(NUM_ELMS); + + IntIter iii (eachEntry (customList)); + StrIter cii (IterSource::build(dedicatedSource)); + + ASSERT (!isnil (iii)); + ASSERT (!isnil (cii)); + + pullOut (iii); + pullOut (cii); + + ASSERT (!iii); + ASSERT (!cii); + } + + + + template + void + pullOut (IT const& ii) + { + for (IT iter(ii) ; iter; ++iter ) + cout << "::" << *iter; + cout << endl; + } + + }; + + LAUNCHER (IterSource_test, "unit common"); + + +}} // namespace lib::test +