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.
22
// Implementation of Person. Note the pre and post invariants
23
// enforcing age changes, as well as the assertion for illegal
24
// construction or assignment. See notes in header concerning
25
// real world application which enforces uniqueness in a domain
28
#if !defined(__COMMON_HPP)
32
#if !defined(__PERSON_HPP)
36
using namespace corelinux;
39
// The default (only!) constructor. Note: This is for example
40
// only. Clearly the fact that we are using a constant string ref
41
// no way insures that the originating string will be removed from
42
// the stack without violating our invarience.
45
Person::Person( NameCref aName, Age aAge )
50
REQUIRE( theName.empty() == false );
51
REQUIRE( theAge >= 0 && theAge < Limits::LONGMAX );
55
// Illegal constructors defined. You could optionally
56
// change the assertion to more explicit exceptions
57
// as shown in the copy constructor
59
Person::Person( void )
67
Person::Person( PersonCref )
74
TEXT("No two Persons can have the same name in this domain"),
80
// Destructor. With Person there is really nothing to clean up
83
Person::~Person( void )
88
// Operator compares down to the name if applicable
90
bool Person::operator==( PersonCref aPerson ) const
94
if( this != &aPerson ||
95
this->getName() != aPerson.getName())
107
// Illegal operator dealt with
109
PersonRef Person::operator=( PersonCref )
111
NEVER_GET_HERE; // Assignment no allowed
112
return *this; // avoid compiler errors
115
// Explicit name fetch
117
NameCref Person:: getName( void ) const
122
// Coercion name fetch
124
Person::operator NameCref( void ) const
129
// Explicit age fetch
131
AgeCref Person:: getAge( void ) const
136
// Coercion age fetch
138
Person::operator AgeCref( void ) const
144
// Here we can change the age while at
145
// the same time showing a good practice,
146
// mainly INSURING THAT THE STATE OF THE
147
// PERSON IS PRESERVED!!! Another note
148
// is that the assertions from Assertion.hpp
149
// can be compiled away. It is up to the
150
// developer to understand what type constraints
151
// should live forever, or just during development.
154
void Person::setAge( Age aAge )
156
REQUIRE( aAge >= 0 );
160
Age aTempAge(this->getAge());
166
ENSURE( theAge < Limits::LONGMAX );
168
catch( AssertionRef e )
176
Common rcs information do not modify
179
$Date: 2000/01/19 14:30:07 $