~ubuntu-branches/ubuntu/gutsy/libcorelinux/gutsy

« back to all changes in this revision

Viewing changes to src/testdrivers/ex5/examp5.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Christophe Prud'homme
  • Date: 2001-12-29 17:43:34 UTC
  • Revision ID: james.westby@ubuntu.com-20011229174334-ejlsuilsiro5vmzr
Tags: 0.4.32-4
* fix config.{guess,sub} out of date on hppa and s390 (closes: #124296,#121830)
* try to be more descriptive in the description of the package  (closes: #115758)

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
  CoreLinux++ 
 
3
  Copyright (C) 1999 CoreLinux Consortium
 
4
  
 
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.
 
9
 
 
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.
 
14
 
 
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.  
 
19
*/   
 
20
 
 
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.
 
25
*/                   
 
26
 
 
27
 
 
28
#include <Common.hpp>
 
29
#include <List.hpp>
 
30
#include <Vector.hpp>
 
31
 
 
32
#include <CoreLinuxIterator.hpp>
 
33
 
 
34
#include <iostream>
 
35
#include <exception>
 
36
 
 
37
using namespace corelinux;
 
38
 
 
39
CORELINUX_LIST(  Int ,  IntList );
 
40
CORELINUX_VECTOR(  Real ,  RealVector );
 
41
 
 
42
//
 
43
// In module function prototypes
 
44
//
 
45
 
 
46
int   main( void );                       
 
47
 
 
48
// Function template for iterating over the elements
 
49
 
 
50
template<class T>
 
51
   void  dumpIterator( Iterator<T> *aIteratorPtr );
 
52
 
 
53
//
 
54
// Standard issue
 
55
//
 
56
 
 
57
void  handleAssertion( AssertionCref aAssert );
 
58
void  handleException( ExceptionCref aExcp );
 
59
 
 
60
 
 
61
int main( void )
 
62
{
 
63
   //
 
64
   // Practice gracefull exception management
 
65
   //
 
66
 
 
67
   try
 
68
   {
 
69
      //
 
70
      // Our first test is with a Iterator for Integers
 
71
      //
 
72
 
 
73
      Iterator<Int>  *aIntIterator(NULLPTR);
 
74
      IntList        aIntList;
 
75
 
 
76
      aIntList.push_back(5);
 
77
      aIntList.push_back(6);
 
78
      aIntList.push_back(7);
 
79
 
 
80
      //
 
81
      // Construct the STL list iterator
 
82
      //
 
83
 
 
84
      aIntIterator = new CoreLinuxIterator<IntListIterator,Int>
 
85
         (
 
86
            aIntList.begin(),
 
87
            aIntList.end()
 
88
         );
 
89
 
 
90
      dumpIterator<Int>(aIntIterator);
 
91
 
 
92
      delete aIntIterator;
 
93
 
 
94
      //
 
95
      // Now try it with a Iterator for Real
 
96
      //
 
97
 
 
98
      Iterator<Real> *aRealIterator( NULLPTR );
 
99
      RealVector     aRealVector;
 
100
 
 
101
      aRealVector.push_back(0.7);
 
102
      aRealVector.push_back(2.8);
 
103
      aRealVector.push_back(7.19823);
 
104
 
 
105
      //
 
106
      // Construct the STL vector iterator
 
107
      //
 
108
 
 
109
      aRealIterator = new CoreLinuxIterator<RealVectorIterator,Real>
 
110
         (
 
111
            aRealVector.begin(),
 
112
            aRealVector.end()
 
113
         );
 
114
 
 
115
      dumpIterator<Real>(aRealIterator);
 
116
      delete aRealIterator;
 
117
   }
 
118
 
 
119
   //
 
120
   // We want to reason with the exception type to give the
 
121
   // user a more informative message.
 
122
   //
 
123
 
 
124
   catch( InvalidIteratorExceptionRef aInvalidIterator )
 
125
   {
 
126
      cerr << "Invalid iterator constructed." << endl;
 
127
      handleException(aInvalidIterator);
 
128
   }
 
129
   catch( IteratorBoundsExceptionRef aBoundsException )
 
130
   {
 
131
      cerr << "Bounds exception occured during traversel." << endl;
 
132
      handleException(aBoundsException);
 
133
   }
 
134
   catch( AssertionRef aAssert )
 
135
   {
 
136
      handleAssertion(aAssert);
 
137
   }
 
138
   catch( std::exception & e )
 
139
   {
 
140
      cerr  << e.what() << endl;
 
141
   }
 
142
   catch( ... )
 
143
   {
 
144
      cerr  << "Unknown exception." << endl;
 
145
   }
 
146
 
 
147
   return 0;               
 
148
}
 
149
 
 
150
 
 
151
template<class T>
 
152
   void  dumpIterator( Iterator<T> *aIteratorPtr )
 
153
   {
 
154
      while( aIteratorPtr->isValid() )
 
155
      {
 
156
         cout  << aIteratorPtr->getElement() << endl;
 
157
         aIteratorPtr->setNext();
 
158
      }
 
159
   }
 
160
 
 
161
 
 
162
 
 
163
//
 
164
// Peform default (just show it)
 
165
//
 
166
 
 
167
void  handleAssertion( AssertionCref aAssert )
 
168
{
 
169
   cerr << aAssert.getFile() << ":" << aAssert.getLine() << ":" << 
 
170
      "Assertion: ";
 
171
 
 
172
   if( aAssert.getType() == Assertion::NEVERGETHERE )
 
173
   {
 
174
      cerr << "NEVER_GET_HERE";
 
175
   }
 
176
   else
 
177
   {
 
178
      if( aAssert.getType() == Assertion::REQUIRE )
 
179
      {
 
180
         cerr  << "REQUIRE";
 
181
      }
 
182
      else if( aAssert.getType() == Assertion::ENSURE )
 
183
      {
 
184
         cerr  << "ENSURE";
 
185
      }
 
186
      else if( aAssert.getType() == Assertion::CHECK )
 
187
      {
 
188
         cerr  << "CHECK";
 
189
      }
 
190
      else 
 
191
      {
 
192
         cerr  << "ASSERT";
 
193
      }
 
194
      cerr << "( " << aAssert.getWhy() << " )";
 
195
   }
 
196
 
 
197
   cerr << endl;
 
198
}
 
199
 
 
200
void  handleException( ExceptionCref aExcp )
 
201
{
 
202
   cerr << aExcp.getFile() << ":" << aExcp.getLine() << ":" <<
 
203
      "Exception: " << aExcp.getWhy() << endl;
 
204
}
 
205
 
 
206
/*
 
207
   Common rcs information do not modify
 
208
   $Author: prudhomm $
 
209
   $Revision: 1.2 $
 
210
   $Date: 2000/08/31 22:49:01 $
 
211
   $Locker:  $
 
212
*/
 
213
 
 
214