3
Copyright (C) 1999 CoreLinux Consortium
5
The CoreLinux++ Library is free software; you can redistribute it and/or
6
modify it under the terms of the GNU Library General Public License as
7
published by the Free Software Foundation; either version 2 of the
8
License, or (at your option) any later version.
10
The CoreLinux++ Library Library is distributed in the hope that it will
11
be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
12
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13
Library General Public License for more details.
15
You should have received a copy of the GNU Library General Public
16
License along with the GNU C Library; see the file COPYING.LIB. If not,
17
write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18
Boston, MA 02111-1307, USA.
21
/** \example examp5.cpp
22
This example is to test the Iterator pattern implementations in
23
libcorelinux++. A more comprehensive test will be provided in
24
ex6 which is destined for the Composite pattern implementation.
32
#include <CoreLinuxIterator.hpp>
37
using namespace corelinux;
39
CORELINUX_LIST( Int , IntList );
40
CORELINUX_VECTOR( Real , RealVector );
43
// In module function prototypes
48
// Function template for iterating over the elements
51
void dumpIterator( Iterator<T> *aIteratorPtr );
57
void handleAssertion( AssertionCref aAssert );
58
void handleException( ExceptionCref aExcp );
64
// Practice gracefull exception management
70
// Our first test is with a Iterator for Integers
73
Iterator<Int> *aIntIterator(NULLPTR);
76
aIntList.push_back(5);
77
aIntList.push_back(6);
78
aIntList.push_back(7);
81
// Construct the STL list iterator
84
aIntIterator = new CoreLinuxIterator<IntListIterator,Int>
90
dumpIterator<Int>(aIntIterator);
95
// Now try it with a Iterator for Real
98
Iterator<Real> *aRealIterator( NULLPTR );
99
RealVector aRealVector;
101
aRealVector.push_back(0.7);
102
aRealVector.push_back(2.8);
103
aRealVector.push_back(7.19823);
106
// Construct the STL vector iterator
109
aRealIterator = new CoreLinuxIterator<RealVectorIterator,Real>
115
dumpIterator<Real>(aRealIterator);
116
delete aRealIterator;
120
// We want to reason with the exception type to give the
121
// user a more informative message.
124
catch( InvalidIteratorExceptionRef aInvalidIterator )
126
cerr << "Invalid iterator constructed." << endl;
127
handleException(aInvalidIterator);
129
catch( IteratorBoundsExceptionRef aBoundsException )
131
cerr << "Bounds exception occured during traversel." << endl;
132
handleException(aBoundsException);
134
catch( AssertionRef aAssert )
136
handleAssertion(aAssert);
138
catch( std::exception & e )
140
cerr << e.what() << endl;
144
cerr << "Unknown exception." << endl;
152
void dumpIterator( Iterator<T> *aIteratorPtr )
154
while( aIteratorPtr->isValid() )
156
cout << aIteratorPtr->getElement() << endl;
157
aIteratorPtr->setNext();
164
// Peform default (just show it)
167
void handleAssertion( AssertionCref aAssert )
169
cerr << aAssert.getFile() << ":" << aAssert.getLine() << ":" <<
172
if( aAssert.getType() == Assertion::NEVERGETHERE )
174
cerr << "NEVER_GET_HERE";
178
if( aAssert.getType() == Assertion::REQUIRE )
182
else if( aAssert.getType() == Assertion::ENSURE )
186
else if( aAssert.getType() == Assertion::CHECK )
194
cerr << "( " << aAssert.getWhy() << " )";
200
void handleException( ExceptionCref aExcp )
202
cerr << aExcp.getFile() << ":" << aExcp.getLine() << ":" <<
203
"Exception: " << aExcp.getWhy() << endl;
207
Common rcs information do not modify
210
$Date: 2000/08/31 22:49:01 $