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 examp1.cpp
22
This example is to show use of CoreLinux includes and define
23
capability, while also setting up a build.
25
CoreLinux C++ standards shown in this example:
27
Section 3.0 Liberal comments
28
Code and comments visually seperated
29
Header comments (thats what this is)
31
Spaces instead of tabs
32
3 spaces used for indentation
34
other examples are noted in the code.
39
// Common.hpp included before anything else. This is not
40
// required but it makes things consistent. You can also
43
// #if !defined(_COMMON_HPP)
44
// #include <Common.hpp>
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
56
using namespace corelinux;
64
// In module function prototypes
67
int main( void ); // Aligned trailing comments
68
void checkWifesAge( AgeCref, AgeCref ); // (Section 3.0)
69
void bar( void ); // WRONG! Comment should line up
72
// Assertion and Exception handlers
74
void handleAssertion( AssertionCref aAssert );
75
void handleException( ExceptionCref );
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.
86
// Practice gracefull exception management
92
Name franksName(TEXT("Frank"));
93
Person frank(franksName,42); // Person instance
96
// Comment out the following if you want to
97
// ignore test invarient and illegal attempts
100
cout << frank.getName() << " is " << frank.getAge() << endl;
103
frank.setAge(Limits::LONGMAX);
105
catch( AssertionRef aAssert )
107
handleAssertion(aAssert);
109
catch( ExceptionRef aException )
111
handleException(aException);
113
cout << frank.getName() << " is " << frank.getAge() << endl;
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
124
cout << "Ahh, to be young again!" << endl;
132
// WRONG! Line too long, no continuation, etc.
135
if( frank > oldAge ) cout << "Wow, they let you out in the world? " << agetErr << endl;
138
// This shows that we don't want to instantiate until needed
140
Name wifesName(TEXT("Jane"));
141
Person franksWife(wifesName,21);
142
Age wifesRealAge(41);
145
// Test wife units age and call her on it (Section 3.0)
148
cout << "My wife " << franksWife.getName() <<
149
" says that she is " << AgeCref(franksWife) <<
150
" years old. Lets see about that..." << endl;
152
checkWifesAge( franksWife,wifesRealAge );
162
else if( agetErr > 0 )
169
throw Exception(TEXT("Un-handled situation"),LOCATION);
173
catch( AssertionRef aAssert )
175
handleAssertion(aAssert);
177
catch( ExceptionRef aException )
179
handleException(aException);
181
catch( std::exception & e )
183
cerr << e.what() << endl;
187
cerr << "Unknown exception." << endl;
189
// No line exceeds the 78 column positions (Section 4.6) --|
191
return 0; // Single exit point (Section 4.0)
195
// Routine to check what age was specified against
196
// the reality of it.
199
void checkWifesAge( AgeCref aToldAge, AgeCref aRealAge )
201
if( aToldAge < aRealAge )
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)
210
cout << "She has some explaining to do because " <<
211
aRealAge - aToldAge <<
212
" years is a big difference! ";
215
else if( aToldAge == aRealAge ) // Section 4.3
217
cout << "I'll never tell";
221
cout << "Don't worry, you look great.";
228
return; // not required on void
232
// Peform default (just show it)
235
void handleAssertion( AssertionCref aAssert )
237
cerr << aAssert.getFile() << ":" << aAssert.getLine() << ":" <<
240
if( aAssert.getType() == Assertion::NEVERGETHERE )
242
cerr << "NEVER_GET_HERE";
246
if( aAssert.getType() == Assertion::REQUIRE )
250
else if( aAssert.getType() == Assertion::ENSURE )
254
else if( aAssert.getType() == Assertion::CHECK )
262
cerr << "( " << aAssert.getWhy() << " )";
268
void handleException( ExceptionCref aExcp )
270
cerr << aExcp.getFile() << ":" << aExcp.getLine() << ":" <<
271
"Exception: " << aExcp.getWhy() << endl;
274
// Trailer CVS information only (Section 3.0)
277
Common rcs information do not modify
280
$Date: 2000/08/31 22:51:01 $