WIP define expected usage of IterSouce

This commit is contained in:
Fischlurch 2010-01-05 02:53:20 +01:00
parent 73613c1e7d
commit ea5668c5e3
2 changed files with 220 additions and 0 deletions

73
src/lib/iter-source.hpp Normal file
View file

@ -0,0 +1,73 @@
/*
ITER-SOURCE.hpp - an interface to build an opaque iterator-based data source
Copyright (C) Lumiera.org
2010, Hermann Vosseler <Ichthyostega@web.de>
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
** <i>Lumiera Forward Iterator</i>. 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 TY>
class IterSource
: public lib::BoolCheckable<IterSource<TY> >
{
};
} // namespace lib
#endif

View file

@ -0,0 +1,147 @@
/*
IterSource(Test) - how to build an opaque iterator-based data source
Copyright (C) Lumiera.org
2010, Hermann Vosseler <Ichthyostega@web.de>
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 <boost/lexical_cast.hpp>
#include <iostream>
#include <list>
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<char*>
{
};
/** test dummy, simply wrapping an STL container
* and exposing a range as Lumiera Forward Iterator
*/
struct WrappedList
{
list<int> data_;
WrappedList(uint num)
{
while (num)
data_.push_back(num--);
}
typedef list<int>::iterator sourceIter;
typedef RangeIter<sourceIter> iterator;
typedef list<int>::const_iterator const_sourceIter;
typedef RangeIter<const_sourceIter> 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<int>::iterator IntIter;
typedef IterSource<char*>::iterator StrIter;
virtual void
run (Arg arg)
{
if (0 < arg.size()) NUM_ELMS = lexical_cast<uint> (arg[0]);
// build the test data sources
WrappedList customList(NUM_ELMS);
TestSource dedicatedSource(NUM_ELMS);
IntIter iii (eachEntry (customList));
StrIter cii (IterSource<char*>::build(dedicatedSource));
ASSERT (!isnil (iii));
ASSERT (!isnil (cii));
pullOut (iii);
pullOut (cii);
ASSERT (!iii);
ASSERT (!cii);
}
template<class IT>
void
pullOut (IT const& ii)
{
for (IT iter(ii) ; iter; ++iter )
cout << "::" << *iter;
cout << endl;
}
};
LAUNCHER (IterSource_test, "unit common");
}} // namespace lib::test