-
Notifications
You must be signed in to change notification settings - Fork 320
/
Copy pathpkg_customer.h
447 lines (383 loc) · 15 KB
/
pkg_customer.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
// pkg_customer.h -*-C++-*-
#ifndef INCLUDED_PKG_CUSTOMER
#define INCLUDED_PKG_CUSTOMER
//@PURPOSE: Provide an attribute class for characterizing a bank's customers.
//
//@CLASSES:
// pkg::Customer: attributes characterizing a bank's customer
//
//@DESCRIPTION: This component provides a single, simply constrained
// (value-semantic) attribute class, 'pkg::Customer', that is used to
// characterize the customers of an organization.
//
///Attributes
///----------
//..
// Name Type Default Simple Constraints
// ------------------ ----------- ------- ------------------
// firstName bsl::string "" none
// lastName bsl::string "" none
// accounts bsl::vector<int> vector of length 0 none
// id int 0 id >= 0
//..
//: o 'firstName': The first name of the customer.
//:
//: o 'lastName': The last name of the customer.
//:
//: o 'accounts': The list of accounts associated with a customer.
//:
//: o 'id': The customer's identification number.
//
///Usage
///-----
// This section illustrates intended use of this component.
//
///Example 1: Creating, Storing, and Displaying Customers
///- - - - - - - - - - - - - - - - - - - - - - - - - - -
// Many organizations are required to store and display a list of their
// customers. The following code shows how that can be done using the
// 'Customer' 'class'.
//
// First, we define a 'Customer' object that stores the information about a
// bank customer:
//..
// const char *firstName = "John";
// const char *lastName = "Smith";
// const int checkingAccountNum = 12345;
// const int savingsAccountNum = 98765;
// const int customerId = 18360;
//
// bsl::vector<int> accounts;
// accounts.push_back(checkingAccountNum);
// accounts.push_back(savingsAccountNum);
//
// Customer customer(firstName, lastName, accounts, customerId);
//
// assert(firstName == customer.firstName());
// assert(lastName == customer.lastName());
// assert(accounts == customer.accounts());
// assert(customerId == customer.id());
//..
// Then, we can store this customer object. We assume that all of the
// customers are stored in memory in a 'vector' named 'allCustomers':
//..
// bsl::vector<Customer> allCustomers;
//
// // Process 'allCustomers'
//
// allCustomers.push_back(customer);
//..
// Now, we display all customers to 'stdout':
//..
// for (bsl::vector<Customer>::const_iterator iter = allCustomers.begin();
// iter != allCustomers.end();
// ++iter) {
// bsl::cout << *iter << bsl::endl;
// }
//..
#ifndef INCLUDED_BSLALG_SWAPUTIL
#include <bslalg_swaputil.h>
#endif
#ifndef INCLUDED_BSLMA_USESBSLMAALLOCATOR
#include <bslma_usesbslmaallocator.h>
#endif
#ifndef INCLUDED_BSLS_ASSERT
#include <bsls_assert.h>
#endif
#ifndef INCLUDED_BSL_STRING
#include <bsl_string.h>
#endif
#ifndef INCLUDED_BSL_VECTOR
#include <bsl_vector.h>
#endif
namespace BloombergLP {
namespace bslma { class Allocator; }
}
namespace Enterprise {
namespace pkg {
// ==============
// class Customer
// ==============
class Customer {
// This simply constrained (value-semantic) attribute class represents the
// information about a bank's customer. A customer's first and last name
// are represented as 'bsl::string' objects, the associated accounts are
// stored in a 'bsl::vector<int>', and the employee identification number
// is represented by an 'int'. Note that the class invariants are
// identically the constraints on the individual attributes.
//
// This class:
//: o supports a complete set of *value-semantic* operations
//: o except for BDEX serialization
// DATA
bsl::string d_firstName; // first name
bsl::string d_lastName; // last name
bsl::vector<int> d_accounts; // account numbers
int d_id; // customer identification number
public:
// CREATORS
explicit Customer(BloombergLP::bslma::Allocator *basicAllocator = 0);
// Create a 'Customer' object having the (default) attribute values:
//..
// firstName() == ""
// lastName() == ""
// accounts() == vector of length 0
// id() == 0
//..
// Optionally specify a 'basicAllocator' used to supply memory. If
// 'basicAllocator' is 0, the currently installed default allocator is
// used.
Customer(const BloombergLP::bslstl::StringRef& firstName,
const BloombergLP::bslstl::StringRef& lastName,
const bsl::vector<int>& accounts,
int id,
BloombergLP::bslma::Allocator *basicAllocator = 0);
// Create a 'Customer' object having the specified 'firstName',
// 'lastName', 'accounts', and 'id' attribute values. Optionally
// specify a 'basicAllocator' used to supply memory. If
// 'basicAllocator' is 0, the currently installed default allocator is
// used. The behavior is undefined unless 'id >= 0'.
Customer(const Customer& original,
BloombergLP::bslma::Allocator *basicAllocator = 0);
// Create a 'Customer' object having the same value as the specified
// 'original' object. Optionally specify a 'basicAllocator' used to
// supply memory. If 'basicAllocator' is 0, the currently installed
// default allocator is used.
//! ~Customer() = default;
// Destroy this object.
// MANIPULATORS
Customer& operator=(const Customer& rhs);
// Assign to this object the value of the specified 'rhs' object, and
// return a reference providing modifiable access to this object.
void setAccounts(const bsl::vector<int>& value);
// Set the 'accounts' attribute of this object to the specified
// 'value'.
void setFirstName(const BloombergLP::bslstl::StringRef& value);
// Set the 'firstName' attribute of this object to the specified
// 'value'.
void setId(int value);
// Set the 'id' attribute of this object to the specified 'value'.
// The behavior is undefined unless 'value >= 0'.
void setLastName(const BloombergLP::bslstl::StringRef& value);
// Set the 'lastName' attribute of this object to the specified
// 'value'.
// Aspects
void swap(Customer& other);
// Efficiently exchange the value of this object with the value of the
// specified 'other' object. This method provides the no-throw
// exception-safety guarantee. The behavior is undefined unless this
// object was created with the same allocator as 'other'.
// ACCESSORS
const bsl::vector<int>& accounts() const;
// Return a reference providing non-modifiable access to the
// 'accounts' attribute of this object.
const bsl::string& firstName() const;
// Return a reference providing non-modifiable access to the
// 'firstName' attribute of this object.
int id() const;
// Return the value of the 'id' attribute of this object.
const bsl::string& lastName() const;
// Return a reference providing non-modifiable access to the
// 'lastName' attribute of this object.
// Aspects
BloombergLP::bslma::Allocator *allocator() const;
// Return the allocator used by this object to supply memory. Note
// that if no allocator was supplied at construction the currently
// installed default allocator is used.
bsl::ostream& print(bsl::ostream& stream,
int level = 0,
int spacesPerLevel = 4) const;
// Write the value of this object to the specified output 'stream' in a
// human-readable format, and return a reference to 'stream'.
// Optionally specify an initial indentation 'level', whose absolute
// value is incremented recursively for nested objects. If 'level' is
// specified, optionally specify 'spacesPerLevel', whose absolute value
// indicates the number of spaces per indentation level for this and
// all of its nested objects. If 'level' is negative, suppress
// indentation of the first line. If 'spacesPerLevel' is negative,
// format the entire output on one line, suppressing all but the
// initial indentation (as governed by 'level'). If 'stream' is not
// valid on entry, this operation has no effect. Note that the format
// is not fully specified, and can change without notice.
};
// FREE OPERATORS
bool operator==(const Customer& lhs, const Customer& rhs);
// Return 'true' if the specified 'lhs' and 'rhs' objects have the same
// value, and 'false' otherwise. Two 'Customer' objects have the same
// value if all of the corresponding values of their 'firstName',
// 'lastName', 'accounts', and 'id' attributes are the same.
bool operator!=(const Customer& lhs, const Customer& rhs);
// Return 'true' if the specified 'lhs' and 'rhs' objects do not have the
// same value, and 'false' otherwise. Two 'Customer' objects do not have
// the same value if any of the corresponding values of their 'firstName',
// 'lastName', 'accounts', or 'id' attributes are not the same.
bsl::ostream& operator<<(bsl::ostream& stream, const Customer& object);
// Write the value of the specified 'object' to the specified output
// 'stream' in a single-line format, and return a reference providing
// modifiable access to 'stream'. If 'stream' is not valid on entry, this
// operation has no effect. Note that this human-readable format is not
// fully specified and can change without notice. Also note that this
// method has the same behavior as 'object.print(stream, 0, -1)', but with
// the attribute names elided.
// FREE FUNCTIONS
void swap(Customer& a, Customer& b);
// Efficiently exchange the values of the specified 'a' and 'b' objects.
// This function provides the no-throw exception-safety guarantee. The
// behavior is undefined unless the two objects were created with the same
// allocator.
} // close package namespace
} // close enterprise namespace
// TRAITS
namespace BloombergLP {
namespace bslma {
template <> struct UsesBslmaAllocator<Enterprise::pkg::Customer>
: bsl::true_type {};
} // close package namespace
} // close enterprise namespace
// ============================================================================
// INLINE FUNCTION DEFINITIONS
// ============================================================================
// --------------
// class Customer
// --------------
namespace Enterprise {
namespace pkg {
// CREATORS
inline
Customer::Customer(BloombergLP::bslma::Allocator *basicAllocator)
: d_firstName(basicAllocator)
, d_lastName(basicAllocator)
, d_accounts(basicAllocator)
, d_id(0)
{
}
inline
Customer::Customer(const BloombergLP::bslstl::StringRef& firstName,
const BloombergLP::bslstl::StringRef& lastName,
const bsl::vector<int>& accounts,
int id,
BloombergLP::bslma::Allocator *basicAllocator)
: d_firstName(firstName.begin(), firstName.end(), basicAllocator)
, d_lastName(lastName.begin(), lastName.end(), basicAllocator)
, d_accounts(accounts, basicAllocator)
, d_id(id)
{
}
inline
Customer::Customer(const Customer& original,
BloombergLP::bslma::Allocator *basicAllocator)
: d_firstName(original.d_firstName, basicAllocator)
, d_lastName(original.d_lastName, basicAllocator)
, d_accounts(original.d_accounts, basicAllocator)
, d_id(original.d_id)
{
}
// MANIPULATORS
inline
Customer& Customer::operator=(const Customer& rhs)
{
d_firstName = rhs.d_firstName;
d_lastName = rhs.d_lastName;
d_accounts = rhs.d_accounts;
d_id = rhs.d_id;
return *this;
}
inline
void Customer::setAccounts(const bsl::vector<int>& value)
{
d_accounts = value;
}
inline
void Customer::setFirstName(const BloombergLP::bslstl::StringRef& value)
{
d_firstName.assign(value.begin(), value.end());
}
inline
void Customer::setId(int value)
{
BSLS_ASSERT_SAFE(value >= 0);
d_id = value;
}
inline
void Customer::setLastName(const BloombergLP::bslstl::StringRef& value)
{
d_lastName.assign(value.begin(), value.end());
}
// Aspects
inline
void Customer::swap(Customer& other)
{
BSLS_ASSERT_SAFE(allocator() == other.allocator());
BloombergLP::bslalg::SwapUtil::swap(&d_firstName, &other.d_firstName);
BloombergLP::bslalg::SwapUtil::swap(&d_lastName, &other.d_lastName);
BloombergLP::bslalg::SwapUtil::swap(&d_accounts, &other.d_accounts);
BloombergLP::bslalg::SwapUtil::swap(&d_id, &other.d_id);
}
// ACCESSORS
inline
const bsl::vector<int>& Customer::accounts() const
{
return d_accounts;
}
inline
const bsl::string& Customer::firstName() const
{
return d_firstName;
}
inline
int Customer::id() const
{
return d_id;
}
inline
const bsl::string& Customer::lastName() const
{
return d_lastName;
}
// Aspects
inline
BloombergLP::bslma::Allocator *Customer::allocator() const
{
return d_firstName.get_allocator().mechanism();
}
// FREE OPERATORS
inline
bool operator==(const Customer& lhs, const Customer& rhs)
{
return lhs.firstName() == rhs.firstName()
&& lhs.lastName() == rhs.lastName()
&& lhs.accounts() == rhs.accounts()
&& lhs.id() == rhs.id();
}
inline
bool operator!=(const Customer& lhs, const Customer& rhs)
{
return lhs.firstName() != rhs.firstName()
|| lhs.lastName() != rhs.lastName()
|| lhs.accounts() != rhs.accounts()
|| lhs.id() != rhs.id();
}
// FREE FUNCTIONS
inline
void swap(Customer& a, Customer& b)
{
a.swap(b);
}
} // close package namespace
} // close enterprise namespace
#endif
// ----------------------------------------------------------------------------
// Copyright 2013 Bloomberg Finance L.P.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// ----------------------------- END-OF-FILE ----------------------------------