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

« back to all changes in this revision

Viewing changes to src/testdrivers/ex1/Person.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
//
 
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
 
26
//
 
27
 
 
28
#if   !defined(__COMMON_HPP)                     
 
29
#include <Common.hpp>          
 
30
#endif
 
31
 
 
32
#if   !defined(__PERSON_HPP)
 
33
#include <Person.hpp>
 
34
#endif
 
35
 
 
36
using namespace corelinux;
 
37
 
 
38
//
 
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.
 
43
//
 
44
 
 
45
Person::Person( NameCref aName, Age aAge )
 
46
   :
 
47
   theName(aName),
 
48
   theAge( aAge )
 
49
{
 
50
   REQUIRE( theName.empty() == false );
 
51
   REQUIRE( theAge >= 0 && theAge < Limits::LONGMAX );
 
52
}
 
53
 
 
54
//
 
55
// Illegal constructors defined. You could optionally
 
56
// change the assertion to more explicit exceptions
 
57
// as shown in the copy constructor
 
58
//
 
59
Person::Person( void )
 
60
   :
 
61
   theName(TEXT("")),
 
62
   theAge(0)
 
63
{
 
64
   NEVER_GET_HERE;
 
65
}
 
66
 
 
67
Person::Person( PersonCref )
 
68
   :
 
69
   theName(TEXT("")),
 
70
   theAge(0)
 
71
{
 
72
   throw Exception
 
73
      (
 
74
         TEXT("No two Persons can have the same name in this domain"),
 
75
         LOCATION
 
76
      );
 
77
}
 
78
 
 
79
//
 
80
// Destructor. With Person there is really nothing to clean up
 
81
//
 
82
 
 
83
Person::~Person( void )
 
84
{
 
85
   ;  // do nothing
 
86
}
 
87
 
 
88
// Operator compares down to the name if applicable
 
89
 
 
90
bool  Person::operator==( PersonCref aPerson ) const
 
91
{
 
92
   bool  isMe(true);
 
93
 
 
94
   if( this != &aPerson ||
 
95
       this->getName() != aPerson.getName())
 
96
   {
 
97
      isMe = false;
 
98
   }
 
99
   else 
 
100
   {
 
101
      ;  // do nothing
 
102
   }
 
103
 
 
104
   return isMe;
 
105
}
 
106
 
 
107
// Illegal operator dealt with
 
108
 
 
109
PersonRef Person::operator=( PersonCref )
 
110
{
 
111
   NEVER_GET_HERE;   // Assignment no allowed
 
112
   return *this;     // avoid compiler errors
 
113
}
 
114
 
 
115
// Explicit name fetch
 
116
 
 
117
NameCref Person:: getName( void ) const
 
118
{
 
119
   return theName;
 
120
}
 
121
 
 
122
// Coercion name fetch
 
123
 
 
124
Person::operator NameCref( void ) const
 
125
{
 
126
   return theName;
 
127
}
 
128
 
 
129
// Explicit age fetch
 
130
 
 
131
AgeCref Person:: getAge( void ) const
 
132
{
 
133
   return theAge;
 
134
}
 
135
 
 
136
// Coercion age fetch
 
137
 
 
138
Person::operator AgeCref( void ) const
 
139
{
 
140
   return theAge;
 
141
}
 
142
 
 
143
//
 
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.
 
152
//
 
153
 
 
154
void  Person::setAge( Age aAge )
 
155
{
 
156
   REQUIRE( aAge >= 0 );
 
157
 
 
158
   // Copy for restore
 
159
 
 
160
   Age   aTempAge(this->getAge());
 
161
 
 
162
   theAge = aAge;
 
163
 
 
164
   try
 
165
   {
 
166
      ENSURE( theAge < Limits::LONGMAX );
 
167
   }
 
168
   catch( AssertionRef e )
 
169
   {
 
170
      theAge = aTempAge;
 
171
      throw;
 
172
   }
 
173
}
 
174
 
 
175
/*
 
176
   Common rcs information do not modify
 
177
   $Author: frankc $
 
178
   $Revision: 1.3 $
 
179
   $Date: 2000/01/19 14:30:07 $
 
180
   $Locker:  $
 
181
*/
 
182