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

« back to all changes in this revision

Viewing changes to src/testdrivers/ex1/examp1.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 examp1.cpp
 
22
   This example is to show use of CoreLinux includes and define
 
23
   capability, while also setting up a build.
 
24
   
 
25
   CoreLinux C++ standards shown in this example:
 
26
   
 
27
   Section  3.0   Liberal comments
 
28
                  Code and comments visually seperated
 
29
                  Header comments (thats what this is)
 
30
                  Block comments
 
31
                  Spaces instead of tabs
 
32
                  3 spaces used for indentation
 
33
                  
 
34
   other examples are noted in the code.                  
 
35
   
 
36
*/                   
 
37
 
 
38
//
 
39
// Common.hpp included before anything else. This is not
 
40
// required but it makes things consistent. You can also
 
41
// wrap it with:
 
42
//
 
43
// #if   !defined(_COMMON_HPP)
 
44
// #include <Common.hpp>
 
45
// #endif
 
46
//
 
47
// if file i/o during compilation should be reduced.
 
48
// Each CoreLinux header has itself wrapped as well so
 
49
// multiple includes or cross-includes are harmless
 
50
//
 
51
 
 
52
#include <Common.hpp>          
 
53
 
 
54
#include <Person.hpp>
 
55
 
 
56
using namespace corelinux;
 
57
 
 
58
#include <iostream>
 
59
using namespace std;
 
60
#include <exception>
 
61
 
 
62
 
 
63
//
 
64
// In module function prototypes
 
65
//
 
66
 
 
67
int   main( void );                       // Aligned trailing comments 
 
68
void  checkWifesAge( AgeCref, AgeCref );  // (Section 3.0)
 
69
void  bar( void );   // WRONG! Comment should line up
 
70
 
 
71
//
 
72
// Assertion and Exception handlers
 
73
//
 
74
void  handleAssertion( AssertionCref aAssert );
 
75
void  handleException( ExceptionCref );   
 
76
 
 
77
 
 
78
int main( void )
 
79
{
 
80
   Age      oldAge(99);                   // Use constructor syntax
 
81
   Age      agetErr = 0;                  // WRONG! Use constructor syntax
 
82
                                          // We would construct and then
 
83
                                          // assign if real object.
 
84
 
 
85
   //
 
86
   // Practice gracefull exception management
 
87
   //
 
88
 
 
89
   try
 
90
   {
 
91
 
 
92
      Name     franksName(TEXT("Frank"));    
 
93
      Person   frank(franksName,42);         // Person instance
 
94
 
 
95
      //
 
96
      // Comment out the following if you want to
 
97
      // ignore test invarient and illegal attempts
 
98
      //
 
99
 
 
100
      cout  << frank.getName() << " is " << frank.getAge() << endl;
 
101
      try
 
102
      {
 
103
         frank.setAge(Limits::LONGMAX);
 
104
      }
 
105
      catch( AssertionRef aAssert )
 
106
      {
 
107
         handleAssertion(aAssert);
 
108
      }
 
109
      catch( ExceptionRef aException )
 
110
      {
 
111
         handleException(aException);
 
112
      }
 
113
      cout  << frank.getName() << " is " << frank.getAge() << endl;
 
114
 
 
115
      //
 
116
      // The following is to show that the developer
 
117
      // knows the intent of the if test, and clearly
 
118
      // shows this by the empty else (Section 4.3). 
 
119
      // See ex2 for assertion examples
 
120
      //
 
121
 
 
122
      if( frank < oldAge )
 
123
      {
 
124
         cout  << "Ahh, to be young again!" << endl;
 
125
      }
 
126
      else
 
127
      {
 
128
         ;  // do nothing
 
129
      }
 
130
 
 
131
      //
 
132
      // WRONG! Line too long, no continuation, etc.
 
133
      //
 
134
 
 
135
      if( frank > oldAge ) cout  << "Wow, they let you out in the world? " << agetErr << endl;
 
136
 
 
137
      //
 
138
      // This shows that we don't want to instantiate until needed
 
139
      //
 
140
      Name     wifesName(TEXT("Jane"));
 
141
      Person   franksWife(wifesName,21);
 
142
      Age      wifesRealAge(41);
 
143
 
 
144
      //
 
145
      // Test wife units age and call her on it (Section 3.0)
 
146
      //
 
147
 
 
148
      cout  << "My wife " << franksWife.getName() <<
 
149
         " says that she is " << AgeCref(franksWife) <<
 
150
         " years old. Lets see about that..." << endl;
 
151
 
 
152
      checkWifesAge( franksWife,wifesRealAge );
 
153
 
 
154
      //
 
155
      // Section 4.3
 
156
      //
 
157
 
 
158
      if( agetErr < 0 )
 
159
      {
 
160
         ;  // do nothing
 
161
      }
 
162
      else if( agetErr > 0 )
 
163
      {
 
164
         ;  // do nothing
 
165
      }
 
166
      else
 
167
      {
 
168
         // NEVER_GET_HERE;
 
169
         throw Exception(TEXT("Un-handled situation"),LOCATION);
 
170
      }
 
171
 
 
172
   }
 
173
   catch( AssertionRef aAssert )
 
174
   {
 
175
      handleAssertion(aAssert);
 
176
   }
 
177
   catch( ExceptionRef aException )
 
178
   {
 
179
      handleException(aException);
 
180
   }
 
181
   catch( std::exception & e )
 
182
   {
 
183
      cerr  << e.what() << endl;
 
184
   }
 
185
   catch( ... )
 
186
   {
 
187
      cerr  << "Unknown exception." << endl;
 
188
   }
 
189
   // No line exceeds the 78 column positions (Section 4.6)                --|
 
190
 
 
191
   return 0;               // Single exit point (Section 4.0)
 
192
}
 
193
 
 
194
//
 
195
// Routine to check what age was specified against
 
196
// the reality of it.
 
197
//
 
198
 
 
199
void checkWifesAge( AgeCref aToldAge, AgeCref aRealAge )
 
200
{
 
201
   if( aToldAge < aRealAge  )
 
202
   {
 
203
      //
 
204
      // Two points:
 
205
      // 1. Comment block is indented to indicate what it is
 
206
      // that is being commented on. (Section 3.0)
 
207
      // 2. Wrapping indents continuation lines (Section 4.6)
 
208
      //
 
209
 
 
210
      cout  << "She has some explaining to do because " <<
 
211
         aRealAge  - aToldAge << 
 
212
            " years is a big difference! ";
 
213
 
 
214
   }
 
215
   else if( aToldAge == aRealAge )      // Section 4.3
 
216
   {
 
217
      cout  << "I'll never tell";
 
218
   }
 
219
   else
 
220
   {
 
221
      cout  << "Don't worry, you look great.";
 
222
   }
 
223
 
 
224
   // Flush buffer
 
225
 
 
226
   cout  << endl;
 
227
 
 
228
   return;  // not required on void 
 
229
}
 
230
 
 
231
//
 
232
// Peform default (just show it)
 
233
//
 
234
 
 
235
void  handleAssertion( AssertionCref aAssert )
 
236
{
 
237
   cerr << aAssert.getFile() << ":" << aAssert.getLine() << ":" << 
 
238
      "Assertion: ";
 
239
 
 
240
   if( aAssert.getType() == Assertion::NEVERGETHERE )
 
241
   {
 
242
      cerr << "NEVER_GET_HERE";
 
243
   }
 
244
   else
 
245
   {
 
246
      if( aAssert.getType() == Assertion::REQUIRE )
 
247
      {
 
248
         cerr  << "REQUIRE";
 
249
      }
 
250
      else if( aAssert.getType() == Assertion::ENSURE )
 
251
      {
 
252
         cerr  << "ENSURE";
 
253
      }
 
254
      else if( aAssert.getType() == Assertion::CHECK )
 
255
      {
 
256
         cerr  << "CHECK";
 
257
      }
 
258
      else 
 
259
      {
 
260
         cerr  << "ASSERT";
 
261
      }
 
262
      cerr << "( " << aAssert.getWhy() << " )";
 
263
   }
 
264
 
 
265
   cerr << endl;
 
266
}
 
267
 
 
268
void  handleException( ExceptionCref aExcp )
 
269
{
 
270
   cerr << aExcp.getFile() << ":" << aExcp.getLine() << ":" <<
 
271
      "Exception: " << aExcp.getWhy() << endl;
 
272
}
 
273
 
 
274
// Trailer CVS information only (Section 3.0)
 
275
 
 
276
/*
 
277
   Common rcs information do not modify
 
278
   $Author: prudhomm $
 
279
   $Revision: 1.3 $
 
280
   $Date: 2000/08/31 22:51:01 $
 
281
   $Locker:  $
 
282
*/
 
283
 
 
284