-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSmartPtr.cpp.html
760 lines (504 loc) · 39.4 KB
/
SmartPtr.cpp.html
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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<title>/Volumes/Unix/unix-files.noindex/ntl-new/ntl-9.6.0/doc/SmartPtr.cpp.html</title>
<meta name="Generator" content="Vim/7.3">
<meta name="plugin-version" content="vim7.3_v6">
<meta name="syntax" content="cpp">
<meta name="settings" content="use_css">
<style type="text/css">
<!--
pre { font-family: monospace; color: #000000; background-color: #ffffff; }
body { font-family: monospace; color: #000000; background-color: #ffffff; }
.Constant { color: #ff8c00; }
.Statement { color: #b03060; font-weight: bold; }
.Type { color: #008b00; font-weight: bold; }
.Comment { color: #0000ee; font-style: italic; }
-->
</style>
</head>
<body>
<pre>
<span class="Comment">/*</span><span class="Comment">***************************************************************************</span>
<span class="Comment">SmartPtr: a smart pointer class.</span>
<span class="Comment">Synopsis: provides a reference counted smart pointer, similar to shared_ptr</span>
<span class="Comment">in the standard library. It is provided here to minimize reliance</span>
<span class="Comment">on the standard library, especially for older C++ compilers, which may</span>
<span class="Comment">not provide shared_ptr, or it may be in TR1, which gets messy.</span>
<span class="Comment">Examples:</span>
<span class="Comment"> SmartPtr<T> p1; // initialize to null</span>
<span class="Comment"> SmartPtr<T> p2 = 0;</span>
<span class="Comment"> SmartPtr<T> p3(p1); // copy constructor</span>
<span class="Comment"> T *rp;</span>
<span class="Comment"> SmartPtr<T> p4(rp); // construct using raw pointer (explicit): better </span>
<span class="Comment"> // to use MakeSmart below</span>
<span class="Comment"> p1 = MakeSmart<T>(...); // build new T object by invoking constructor</span>
<span class="Comment"> // T(...) with pseudo-variadic templates.</span>
<span class="Comment"> // This is safer and more efficient that</span>
<span class="Comment"> // using the raw-pointer constructor</span>
<span class="Comment"> </span>
<span class="Comment"> p1 = p2; // assignment</span>
<span class="Comment"> p1 = 0; // assign null</span>
<span class="Comment"> if (!p1) ... // test for null</span>
<span class="Comment"> if (p1 == 0) ... </span>
<span class="Comment"> if (p1) ... // test for not null ... </span>
<span class="Comment"> if (p1 != 0) ... </span>
<span class="Comment"> if (p1 == p2) ... // test for equality </span>
<span class="Comment"> if (p1 != p2) </span>
<span class="Comment"> *p1 // dereferencing</span>
<span class="Comment"> p1->...</span>
<span class="Comment"> p1.get(); // return the underlying raw pointer...dangerous!</span>
<span class="Comment"> p1.swap(p2); // fast swap</span>
<span class="Comment"> swap(p1, p2);</span>
<span class="Comment">Automatic Conversions:</span>
<span class="Comment">If S is another class, SmartPtr<S> converts to SmartPtr<T> if S* converts to T*</span>
<span class="Comment">(for example, if S is a subclass of T). Similarly, SmartPtr<S> and SmartPtr<T></span>
<span class="Comment">may be compared if S* and T* may be compared.</span>
<span class="Comment">MakeSmart:</span>
<span class="Comment">One can write SmartPtr<T> p = MakeSmart<T>(x1, ..., xn), and this will create a</span>
<span class="Comment">smart pointer to an object constructed as T(x1, ..., xn). Besides notational</span>
<span class="Comment">convenience, it also reduces the number of memory allocations from 2 to 1, as</span>
<span class="Comment">the data and control block can be allocated in one chunck of memory.</span>
<span class="Comment">This is implemented without reliance on C++11 features, which means that there</span>
<span class="Comment">are limitations. First, the number n of arguments is limited to 9. And</span>
<span class="Comment">second, all arguments are pass by const reference. However, you can work around</span>
<span class="Comment">this by using the helper function Fwd. For example, if T has a 2-argument</span>
<span class="Comment">constructor where the second must be a non-const reference of some type, and x2</span>
<span class="Comment">is a variable of that type, you can write MakeSmart<T>(x1, Fwd(x2)), to forward</span>
<span class="Comment">that reference through all the template nonsense in a typesafe manner.</span>
<span class="Comment">MakeRaw:</span>
<span class="Comment">One can also write T *p = MakeRaw<T>(x1, ..., xn) to create a </span>
<span class="Comment">raw pointer. This is the same as writing T *p = new T(x1, ..., xn),</span>
<span class="Comment">except that error handling is determined by the NTL_EXCEPTION</span>
<span class="Comment">flag (on => bad_alloc exception is thrown, off => error message</span>
<span class="Comment">and abort).</span>
<span class="Comment">MakeRawArray:</span>
<span class="Comment">Another utility routine: one can write T *p = MakeRawArray<T>(n)</span>
<span class="Comment">to make a plain array of n T objects. Error handling is the same</span>
<span class="Comment">as for MakeRaw.</span>
<span class="Comment">Dynamic casting:</span>
<span class="Comment">I've also supplied a dynamic cast operation for smart pointers.</span>
<span class="Comment"> SmartPtr<Derived> d = MakeSmart<Derived>(); // d points to Derived</span>
<span class="Comment"> SmartPtr<Base> b = d; // implicit upcast: OK</span>
<span class="Comment"> SmartPtr<Derived> d1 = DynamicCast<Derived>(b);</span>
<span class="Comment"> // downcast to a Derived object -- returns null for a bad cast</span>
<span class="Comment"> </span>
<span class="Comment">Implementation notes:</span>
<span class="Comment">If NTL is compiled with the NTL_THREADS option, then the reference counting</span>
<span class="Comment">will be thread safe.</span>
<span class="Comment">The SmartPtrControl class heirarchy is used to make sure the right destructor</span>
<span class="Comment">is called when the ref count goes to zero. This can be an issue for forward</span>
<span class="Comment">declared classes and for subclasses. For example, if T is forward declared in</span>
<span class="Comment">a context where the ref count goes to zero, or if the object's actual type is a</span>
<span class="Comment">subclass of T and T's destructor was not declared virtual. The implementation</span>
<span class="Comment">of SmartPtr guarantees correct behavior in these situations.</span>
<span class="Comment">The null tests p, !p, p == 0, are all effected via an implicit conversion from</span>
<span class="Comment">SmartPtr<T> to a funny pointer type (a pointer to a member function, which</span>
<span class="Comment">avoids other, unwanted implicit conversions: this is the so-called "safe bool</span>
<span class="Comment">idiom");</span>
<span class="Comment">Also, there is an implicit conversion from the same, funny pointer type to</span>
<span class="Comment">SmartPtr<T>, which is how one can use 0 to initialize and assign to a</span>
<span class="Comment">SmartPtr<T>.</span>
<span class="Comment">In C++11 both of the above effects could perhaps be achieved more directly.</span>
<span class="Comment">The new "explict bool" operator can replace the "safe bool idiom", and I would</span>
<span class="Comment">think that the new null pointer could be used to get the conversion from "0" to</span>
<span class="Comment">work.</span>
<span class="Comment">NOTES: See <a href="http://www.artima.com/cppsource/safebool.html">http://www.artima.com/cppsource/safebool.html</a> for more on the "safe</span>
<span class="Comment">bool idiom". </span>
<span class="Comment"> </span>
<span class="Comment">****************************************************************************</span><span class="Comment">*/</span>
<span class="Type">template</span><<span class="Type">class</span> T>
<span class="Type">class</span> SmartPtr {
<span class="Statement">public</span>:
<span class="Type">explicit</span> SmartPtr(T* p);
<span class="Comment">// construct smart pointer from raw pointer (allocated with new)</span>
<span class="Comment">// EXCEPTIONS: a control block is dynamically allocated;</span>
<span class="Comment">// if this allocation fails, the object pointed to by p is destroyed</span>
<span class="Comment">// and a bad_alloc exception is thrown</span>
SmartPtr();
<span class="Comment">// initial value null</span>
~SmartPtr();
<span class="Comment">// if ref count drops to zero, then delete referent</span>
SmartPtr(<span class="Type">const</span> SmartPtr& other);
SmartPtr& <span class="Statement">operator</span>=(<span class="Type">const</span> SmartPtr& other);
<span class="Comment">// copy and asignment</span>
<span class="Type">template</span><<span class="Type">class</span> Y> SmartPtr(<span class="Type">const</span> SmartPtr<Y>& other);
<span class="Type">template</span><<span class="Type">class</span> Y> SmartPtr& <span class="Statement">operator</span>=(<span class="Type">const</span> SmartPtr<Y>& other);
<span class="Comment">// copy and asignment</span>
T& <span class="Statement">operator</span>*() <span class="Type">const</span>;
T* <span class="Statement">operator</span>->() <span class="Type">const</span>;
<span class="Comment">// indirection</span>
T* get() <span class="Type">const</span>;
<span class="Comment">// get underlying raw pointer</span>
<span class="Type">void</span> swap(SmartPtr& other);
SmartPtr(fake_null_type);
<span class="Comment">// allows assignment and initialization from 0</span>
<span class="Statement">operator</span> fake_null_type() <span class="Type">const</span>;
<span class="Comment">// allows comparisons to 0</span>
<span class="Type">template</span><<span class="Type">class</span> Y> SmartPtr<Y> DynamicCast() <span class="Type">const</span>;
};
<span class="Comment">// free swap function</span>
<span class="Type">template</span><<span class="Type">class</span> T> <span class="Type">void</span> swap(SmartPtr<T>& p, SmartPtr<T>& q);
<span class="Comment">// free dynamic cast function</span>
<span class="Type">template</span><<span class="Type">class</span> X, <span class="Type">class</span> Y> SmartPtr<X> DynamicCast(<span class="Type">const</span> SmartPtr<Y>& p);
<span class="Comment">// Equality testing</span>
<span class="Type">template</span><<span class="Type">class</span> X, <span class="Type">class</span> Y>
<span class="Type">bool</span> <span class="Statement">operator</span>==(<span class="Type">const</span> SmartPtr<X>& a, <span class="Type">const</span> SmartPtr<Y>& b);
<span class="Type">template</span><<span class="Type">class</span> X, <span class="Type">class</span> Y>
<span class="Type">bool</span> <span class="Statement">operator</span>!=(<span class="Type">const</span> SmartPtr<X>& a, <span class="Type">const</span> SmartPtr<Y>& b);
<span class="Comment">// MakeSmart psuedo-variadic template</span>
<span class="Type">template</span><<span class="Type">class</span> T, <span class="Type">class</span> X1, ..., <span class="Type">class</span> Xn>
SmartPtr<T> MakeSmart(<span class="Type">const</span> X1& x1, ..., <span class="Type">const</span> Xn& xn);
<span class="Comment">// EXCEPTIONS: may throw if dynamic constrction of T(x1, ..., xn) fails</span>
<span class="Comment">// EXCEPTIONS: unless otherwise specified, the methods above</span>
<span class="Comment">// never throw an exception (under C++11 rules, if a destructor</span>
<span class="Comment">// is invoked that throws an exception, the program will terminate).</span>
<span class="Comment">/*</span><span class="Comment">***************************************************************************</span>
<span class="Comment">Experimantal: CloneablePtr<T> ...essentially same interface as SmartPtr, but </span>
<span class="Comment">allows cloning of complete objects. The differences:</span>
<span class="Comment">* must construct using MakeCloneable</span>
<span class="Comment">* a clone method is provided</span>
<span class="Comment">* implicit conversion from CloneablePtr to SmartPtr is allowed</span>
<span class="Comment">Example:</span>
<span class="Comment"> CloneablePtr<Derived> d = MakeCloneable<Derived>(); </span>
<span class="Comment"> // d points to Derived</span>
<span class="Comment"> CloneablePtr<Base> b = d; // implicit upcast: OK</span>
<span class="Comment"> CloneablePtr<Base> b1 = b.clone(); </span>
<span class="Comment"> // clone of b, which is really a Derived object</span>
<span class="Comment"> CloneablePtr<Derived> d1 = DynamicCast<Derived>(b1);</span>
<span class="Comment"> // downcast to a Derived object -- returns null for a bad cast</span>
<span class="Comment"> SmartPtr<Base> b2 = d1;</span>
<span class="Comment"> </span>
<span class="Comment">Implementation:</span>
<span class="Comment">In the clone method, the object is constructed using the copy constructor for</span>
<span class="Comment">the type T, where T is the compile-time type with which the first smart pointer</span>
<span class="Comment">to this object was was created, even if the pointer has been subsequently</span>
<span class="Comment">upcasted to a base type S. Such objects must have been initially created using</span>
<span class="Comment">the MakeCloneable function. It turns out, this is hard to do in a completely</span>
<span class="Comment">standards-compliant way, because of the type erasure going on. So I settled on</span>
<span class="Comment">the current method, which does some low-level pointer arithmetic. Even with</span>
<span class="Comment">fancy things like multiple and virtual inheritance, it should work, under the</span>
<span class="Comment">assumption that if two objects have the same (runtime) type, then their memory</span>
<span class="Comment">layout is the same. I don't think anything like that is guaranteed by the</span>
<span class="Comment">standard, but this seems reasonable, and it seems to work. Like I said, it is</span>
<span class="Comment">experimental, and I would appreciate feedback from C++ gurus.</span>
<span class="Comment">Note that NTL does not use this feature, but I do have applications where this</span>
<span class="Comment">is convenient.</span>
<span class="Comment">*********************************************************************************</span><span class="Comment">*/</span>
<span class="Type">template</span><<span class="Type">class</span> T>
<span class="Type">class</span> CloneablePtr {
<span class="Statement">public</span>:
CloneablePtr();
<span class="Comment">// initial value null</span>
~CloneablePtr();
<span class="Comment">// if ref count drops to zero, then delete referent</span>
CloneablePtr(<span class="Type">const</span> CloneablePtr& other);
CloneablePtr& <span class="Statement">operator</span>=(<span class="Type">const</span> CloneablePtr& other);
<span class="Comment">// copy and asignment</span>
<span class="Type">template</span><<span class="Type">class</span> Y> CloneablePtr(<span class="Type">const</span> CloneablePtr<Y>& other);
<span class="Type">template</span><<span class="Type">class</span> Y> CloneablePtr& <span class="Statement">operator</span>=(<span class="Type">const</span> CloneablePtr<Y>& other);
<span class="Comment">// copy and asignment</span>
T& <span class="Statement">operator</span>*() <span class="Type">const</span>;
T* <span class="Statement">operator</span>->() <span class="Type">const</span>;
<span class="Comment">// indirection</span>
T* get() <span class="Type">const</span>;
<span class="Comment">// get underlying raw pointer</span>
<span class="Type">void</span> swap(CloneablePtr& other);
CloneablePtr(fake_null_type);
<span class="Comment">// allows assignment and initialization from 0</span>
<span class="Statement">operator</span> fake_null_type() <span class="Type">const</span>;
<span class="Comment">// allows comparisons to 0</span>
<span class="Type">template</span><<span class="Type">class</span> Y> CloneablePtr<Y> DynamicCast() <span class="Type">const</span>;
CloneablePtr clone() <span class="Type">const</span>;
<span class="Comment">// construct a clone, using the copy constructor</span>
<span class="Comment">// EXCEPTIONS: may throw if copy construction fails</span>
<span class="Type">template</span><<span class="Type">class</span> Y> <span class="Statement">operator</span> SmartPtr<Y>();
<span class="Comment">// implicit conversion from CloneablePtr<T> to SmartPtr<Y>,</span>
<span class="Comment">// allowed if T* converts implicitly to Y*.</span>
};
<span class="Comment">// free swap function</span>
<span class="Type">template</span><<span class="Type">class</span> T> <span class="Type">void</span> swap(CloneablePtr<T>& p, CloneablePtr<T>& q);
<span class="Comment">// free dynamic cast function</span>
<span class="Type">template</span><<span class="Type">class</span> X, <span class="Type">class</span> Y> CloneablePtr<X> DynamicCast(<span class="Type">const</span> CloneablePtr<Y>& p);
<span class="Comment">// Equality testing</span>
<span class="Type">template</span><<span class="Type">class</span> X, <span class="Type">class</span> Y>
<span class="Type">bool</span> <span class="Statement">operator</span>==(<span class="Type">const</span> CloneablePtr<X>& a, <span class="Type">const</span> CloneablePtr<Y>& b);
<span class="Type">template</span><<span class="Type">class</span> X, <span class="Type">class</span> Y>
<span class="Type">bool</span> <span class="Statement">operator</span>!=(<span class="Type">const</span> CloneablePtr<X>& a, <span class="Type">const</span> CloneablePtr<Y>& b);
<span class="Comment">// MakeCloneable psuedo-variadic template</span>
<span class="Type">template</span><<span class="Type">class</span> T, <span class="Type">class</span> X1, ..., <span class="Type">class</span> Xn>
CloneablePtr<T> MakeCloneable(<span class="Type">const</span> X1& x1, ..., <span class="Type">const</span> Xn& xn);
<span class="Comment">// EXCEPTIONS: may throw if dynamic constrction of T(x1, ..., xn) fails</span>
<span class="Comment">// EXCEPTIONS: unless otherwise specified, the methods above</span>
<span class="Comment">// never throw an exception (under C++11 rules, if a destructor</span>
<span class="Comment">// is invoked that throws an exception, the program will terminate).</span>
<span class="Comment">/*</span><span class="Comment">*********************************************************************</span>
<span class="Comment">UniquePtr<T> -- unique pointer to object with copying disabled.</span>
<span class="Comment">Useful for pointers inside classes so that we can</span>
<span class="Comment">automatically destruct them. </span>
<span class="Comment">Constructors:</span>
<span class="Comment"> UniquePtr<T> p1; // initialize with null</span>
<span class="Comment"> T* rp;</span>
<span class="Comment"> UniquePtr<T> p1(rp); // construct using raw pointer (explicit)</span>
<span class="Comment"> p1 = 0; // destroy's p1's referent and assigns null</span>
<span class="Comment"> p1.make(...); // destroy's p1's referent and assigns</span>
<span class="Comment"> // a fresh objected constructed via T(...),</span>
<span class="Comment"> // using psuedo-variadic templates</span>
<span class="Comment"> </span>
<span class="Comment"> p1.reset(rp); // destroy's p1's referent and assign rp</span>
<span class="Comment"> if (!p1) ... // test for null</span>
<span class="Comment"> if (p1 == 0) ...</span>
<span class="Comment"> if (p1) ... // test for nonnull</span>
<span class="Comment"> if (p1 != 0) ...</span>
<span class="Comment"> if (p1 == p2) ... // test for equality</span>
<span class="Comment"> if (p1 != p2) ... </span>
<span class="Comment"> *p1 // dereferencing</span>
<span class="Comment"> p1->...</span>
<span class="Comment"> rp = p1.get(); // fetch raw pointer</span>
<span class="Comment"> rp = p1.release(); // fetch raw pointer, and set to null</span>
<span class="Comment"> p1.move(p2); // move p2 to p1, destroying p1's referent</span>
<span class="Comment"> // if p1 != p2</span>
<span class="Comment"> p1.swap(p2); // swap pointers</span>
<span class="Comment"> swap(p1, p2);</span>
<span class="Comment"> </span>
<span class="Comment">*********************************************************************</span><span class="Comment">*/</span>
<span class="Type">template</span><<span class="Type">class</span> T>
<span class="Type">class</span> UniquePtr {
<span class="Statement">public</span>:
<span class="Type">explicit</span> UniquePtr(T *p);
<span class="Comment">// construct UniquePtr from raw pointer (allocated with new)</span>
UniquePtr();
<span class="Comment">// initial value is null</span>
~UniquePtr();
<span class="Comment">// destroys referent</span>
<span class="Type">void</span> reset(T* p = <span class="Constant">0</span>);
<span class="Comment">// reset underlying pointer to p, destroying original referent</span>
<span class="Type">template</span><<span class="Type">class</span> T, <span class="Type">class</span> X1, ..., <span class="Type">class</span> Xn>
<span class="Type">void</span> make(<span class="Type">const</span> X1& x1, ..., <span class="Type">const</span> Xn& xn);
<span class="Comment">// pseudo-variadic template, roughly equivalent to</span>
<span class="Comment">// reset(new T(x1, ..., xn))</span>
<span class="Comment">// EXCEPTIONS: this may throw (but provides strong ES guarantee)</span>
T& <span class="Statement">operator</span>*() <span class="Type">const</span>;
T* <span class="Statement">operator</span>->() <span class="Type">const</span>;
<span class="Comment">// indirection</span>
T* get() <span class="Type">const</span>;
<span class="Comment">// get raw pointer</span>
T* release();
<span class="Comment">// returns raw pointer, and sets the raw pointer to null</span>
<span class="Type">void</span> move(UniquePtr& other);
<span class="Comment">// move other to *this</span>
<span class="Type">void</span> swap(UniquePtr& other);
<span class="Comment">// swap raw pointers</span>
UniquePtr& <span class="Statement">operator</span>=(fake_null_type);
UniquePtr(fake_null_type);
<span class="Comment">// allows initialization and assignment of 0</span>
<span class="Statement">operator</span> fake_null_type() <span class="Type">const</span>;
<span class="Comment">// allows comparison with 0</span>
<span class="Statement">private</span>:
UniquePtr(<span class="Type">const</span> UniquePtr&); <span class="Comment">// disabled</span>
<span class="Type">void</span> <span class="Statement">operator</span>=(<span class="Type">const</span> UniquePtr&); <span class="Comment">// disabled</span>
};
<span class="Comment">// free swap function</span>
<span class="Type">template</span><<span class="Type">class</span> T> <span class="Type">void</span> swap(UniquePtr<T>& p, UniquePtr<T>& q);
<span class="Comment">// Equality testing</span>
<span class="Type">template</span><<span class="Type">class</span> X> <span class="Type">bool</span> <span class="Statement">operator</span>==(<span class="Type">const</span> UniquePtr<X>& a, <span class="Type">const</span> UniquePtr<X>& b);
<span class="Type">template</span><<span class="Type">class</span> X> <span class="Type">bool</span> <span class="Statement">operator</span>!=(<span class="Type">const</span> UniquePtr<X>& a, <span class="Type">const</span> UniquePtr<X>& b);
<span class="Comment">// EXCEPTIONS: unless otherwise specified, the methods above</span>
<span class="Comment">// never throw an exception (under C++11 rules, if a destructor</span>
<span class="Comment">// is invoked that throws an exception, the program will terminate).</span>
<span class="Comment">/*</span><span class="Comment">*********************************************************************</span>
<span class="Comment">OptionalVal<T> -- unique pointer to object with copying enabled.</span>
<span class="Comment">Constructors:</span>
<span class="Comment"> OptionalVal<T> p1; // initialize with null</span>
<span class="Comment"> T* rp;</span>
<span class="Comment"> OptionalVal<T> p1(rp); // construct using raw pointer (explicit)</span>
<span class="Comment"> OptionalVal<T> p2(p1); // construct a copy of p1's referent</span>
<span class="Comment"> </span>
<span class="Comment"> p1.make(...); // destroy's p1's referent and assigns</span>
<span class="Comment"> // a fresh objected constructed via T(...),</span>
<span class="Comment"> // using psuedo variadic templates</span>
<span class="Comment"> </span>
<span class="Comment"> p1.reset(rp); // destroy's p1's referent and assign rp</span>
<span class="Comment"> if (p1.exists()) ... // test for null</span>
<span class="Comment"> p1.val() // dereference</span>
<span class="Comment"> p1.move(p2); // move p2 to p1, destroying p1's referent</span>
<span class="Comment"> // if p1 != p2</span>
<span class="Comment"> p1 = p2; // deep copy, using T's copy constructor</span>
<span class="Comment"> p1.swap(p2); // swap pointers</span>
<span class="Comment"> swap(p1, p2);</span>
<span class="Comment"> </span>
<span class="Comment">*********************************************************************</span><span class="Comment">*/</span>
<span class="Type">template</span><<span class="Type">class</span> T>
<span class="Type">class</span> OptionalVal {
<span class="Statement">public</span>:
<span class="Type">explicit</span> OptionalVal(T *p);
<span class="Comment">// initialize using raw pointer (allocated with new)</span>
OptionalVal();
<span class="Comment">// initialize to null</span>
OptionalVal(<span class="Type">const</span> OptionalVal& other);
<span class="Comment">// initialize using a deep copy (via T's copy constructor)</span>
OptionalVal& <span class="Statement">operator</span>=(<span class="Type">const</span> OptionalVal& other);
<span class="Comment">// assignment using a deep copy (via T's copy constructor)</span>
~OptionalVal();
<span class="Comment">// destroys the referent</span>
<span class="Type">void</span> reset(T* p = <span class="Constant">0</span>);
<span class="Comment">// resets the referent</span>
<span class="Type">template</span><<span class="Type">class</span> T, <span class="Type">class</span> X1, ..., <span class="Type">class</span> Xn>
<span class="Type">void</span> make(<span class="Type">const</span> X1& x1, ..., <span class="Type">const</span> Xn& xn);
<span class="Comment">// pseudo-variadic template.</span>
<span class="Comment">// resets the referent to a new object T(x1, ..., xn)</span>
<span class="Comment">// EXCEPTIONS: may throw an exception (but provides strong ES guarantee)</span>
T& val() <span class="Type">const</span>;
<span class="Comment">// returns reference to referent </span>
<span class="Comment">// if underlying pointer p is null, the indirection *p</span>
<span class="Comment">// is undefined behavior, but most likely leads to program termination</span>
<span class="Type">bool</span> exists() <span class="Type">const</span>;
<span class="Comment">// checks that underlying pointer is not null</span>
<span class="Type">void</span> move(OptionalVal& other);
<span class="Comment">// performs a (shallow) pointer move</span>
<span class="Type">void</span> swap(OptionalVal& other);
<span class="Comment">// performs a (shallow) pointer swap</span>
};
<span class="Comment">// free swap function</span>
<span class="Type">template</span><<span class="Type">class</span> T> <span class="Type">void</span> swap(OptionalVal<T>& p, OptionalVal<T>& q);
<span class="Comment">// EXCEPTIONS: unless otherwise specified, the methods above</span>
<span class="Comment">// never throw an exception (under C++11 rules, if a destructor</span>
<span class="Comment">// is invoked that throws an exception, the program will terminate).</span>
<span class="Comment">/*</span><span class="Comment">*********************************************************************</span>
<span class="Comment">UniqueArray<T> -- unique pointer to array of objects with copying disabled.</span>
<span class="Comment">Useful for pointers inside classes so that we can</span>
<span class="Comment">automatically destruct them. </span>
<span class="Comment">Constructors:</span>
<span class="Comment"> UniqueArray<T> p1; // initialize with null</span>
<span class="Comment"> T* rp;</span>
<span class="Comment"> UniqueArray<T> p1(rp); // construct using raw pointer (explicit)</span>
<span class="Comment"> p1 = 0; // destroy's p1's referent and assigns null</span>
<span class="Comment"> p1.SetLength(n); // destroy's p1's referent and assigns</span>
<span class="Comment"> // a fresh objected constructed via new T[n]</span>
<span class="Comment"> </span>
<span class="Comment"> p1.reset(rp); // destroy's p1's referent and assign rp</span>
<span class="Comment"> if (!p1) ... // test for null</span>
<span class="Comment"> if (p1 == 0) ...</span>
<span class="Comment"> if (p1) ... // test for nonnull</span>
<span class="Comment"> if (p1 != 0) ...</span>
<span class="Comment"> if (p1 == p2) ... // test for equality</span>
<span class="Comment"> if (p1 != p2) ... </span>
<span class="Comment"> p1[i] // array indexing</span>
<span class="Comment"> rp = p1.get(); // fetch raw pointer</span>
<span class="Comment"> rp = p1.release(); // fetch raw pointer, and set to null</span>
<span class="Comment"> p1.move(p2); // move p2 to p1, destroying p1's referent </span>
<span class="Comment"> // if p1 != p2</span>
<span class="Comment"> p1.swap(p2); // fast swap</span>
<span class="Comment"> swap(p1, p2);</span>
<span class="Comment"> </span>
<span class="Comment">*********************************************************************</span><span class="Comment">*/</span>
<span class="Type">template</span><<span class="Type">class</span> T>
<span class="Type">class</span> UniqueArray {
<span class="Statement">public</span>:
<span class="Type">explicit</span> UniqueArray(T *p);
<span class="Comment">// construct from raw pointer (allocated with new[])</span>
UniqueArray();
<span class="Comment">// initially null</span>
~UniqueArray();
<span class="Type">void</span> reset(T* p = <span class="Constant">0</span>);
<span class="Comment">// reset with raw pointer, destryong referent</span>
<span class="Type">void</span> SetLength(<span class="Type">long</span> n);
<span class="Comment">// destroys referent and allocates an array of size n</span>
<span class="Comment">// EXCEPTIONS: this may throw (but provides strong ES guarantee)</span>
T& <span class="Statement">operator</span>[](<span class="Type">long</span> i) <span class="Type">const</span>;
<span class="Comment">// accesses ith element in the array (currently no range checking)</span>
T* get() <span class="Type">const</span>;
<span class="Comment">// get raw pointer</span>
T* release();
<span class="Comment">// get raw pointer and reset to null</span>
<span class="Type">void</span> move(UniqueArray& other);
<span class="Comment">// move raw pointer</span>
<span class="Type">void</span> swap(UniqueArray& other);
<span class="Comment">// swap raw pointer</span>
UniqueArray& <span class="Statement">operator</span>=(fake_null_type);
UniqueArray(fake_null_type);
<span class="Comment">// allows initialization and assignment of 0</span>
<span class="Statement">operator</span> fake_null_type() <span class="Type">const</span>;
<span class="Comment">// allows comparison to 0</span>
};
<span class="Comment">// free swap function</span>
<span class="Type">template</span><<span class="Type">class</span> T> <span class="Type">void</span> swap(UniqueArray<T>& p, UniqueArray<T>& q);
<span class="Comment">// Equality testing</span>
<span class="Type">template</span><<span class="Type">class</span> X> <span class="Type">bool</span> <span class="Statement">operator</span>==(<span class="Type">const</span> UniqueArray<X>& a, <span class="Type">const</span> UniqueArray<X>& b);
<span class="Type">template</span><<span class="Type">class</span> X> <span class="Type">bool</span> <span class="Statement">operator</span>!=(<span class="Type">const</span> UniqueArray<X>& a, <span class="Type">const</span> UniqueArray<X>& b);
<span class="Comment">/*</span><span class="Comment">*********************************************************************</span>
<span class="Comment">Unique2DArray<T> -- unique pointer to array of arrays.</span>
<span class="Comment">This is very similar to UniqueArray< UniqueArray<T> >, except that </span>
<span class="Comment">we can retrofit old code that accepts objects of type T**.</span>
<span class="Comment">Constructors:</span>
<span class="Comment"> Unique2DArray<T> p1; // initialize with null</span>
<span class="Comment"> p1 = 0; // destroy's p1's referent and assigns null</span>
<span class="Comment"> p1.reset();</span>
<span class="Comment"> p1.SetLength(n); // destroy's p1's referent and assigns</span>
<span class="Comment"> // a fresh array of null pointers</span>
<span class="Comment"> p1.SetDims(n, m) // creates an n x m array</span>
<span class="Comment"> </span>
<span class="Comment"> if (!p1) ... // test for null</span>
<span class="Comment"> if (p1 == 0) ...</span>
<span class="Comment"> if (p1) ... // test for nonnull</span>
<span class="Comment"> if (p1 != 0) ...</span>
<span class="Comment"> if (p1 == p2) ... // test for equality</span>
<span class="Comment"> if (p1 != p2) ... </span>
<span class="Comment"> p1[i] // array indexing</span>
<span class="Comment"> T **rp;</span>
<span class="Comment"> rp = p1.get(); // fetch raw pointer</span>
<span class="Comment"> rp = p1.release(); // fetch raw pointer, and set to null</span>
<span class="Comment"> p1.move(p2); // if p1 != p2 then:</span>
<span class="Comment"> // makes p1 point to p2's referent,</span>
<span class="Comment"> // setting p2 to null and destroying</span>
<span class="Comment"> // p1's referent</span>
<span class="Comment"> p1.swap(p2); // fast swap</span>
<span class="Comment"> swap(p1, p2);</span>
<span class="Comment"> </span>
<span class="Comment">*********************************************************************</span><span class="Comment">*/</span>
<span class="Type">template</span><<span class="Type">class</span> T>
<span class="Type">class</span> Unique2DArray {
<span class="Statement">public</span>:
<span class="Type">typedef</span> T *T_ptr;
Unique2DArray();
<span class="Comment">// initially null</span>
~Unique2DArray();
<span class="Comment">// destroys the entire array and each row in the array</span>
<span class="Type">void</span> reset();
<span class="Comment">// reset to null</span>
<span class="Type">void</span> SetLength(<span class="Type">long</span> n);
<span class="Comment">// resets the array to a vector of length n,</span>
<span class="Comment">// each entry initialized to null.</span>
<span class="Comment">// EXCEPTIONS: may throw (provides strong ES guarantee)</span>
<span class="Type">void</span> SetDims(<span class="Type">long</span> n, <span class="Type">long</span> m);
<span class="Comment">// resets the array to a 2D array with n rows and m columns.</span>
<span class="Comment">// EXCEPTIONS: may throw (provides strong ES guarantee)</span>
<span class="Type">void</span> SetDimsFrom1(<span class="Type">long</span> n, <span class="Type">long</span> m);
<span class="Comment">// same as above, but only initializes rows 1..n-1.</span>
<span class="Comment">// this helps with some legacy code.</span>
<span class="Comment">// EXCEPTIONS: may throw (provides strong ES guarantee)</span>
T_ptr& <span class="Statement">operator</span>[](<span class="Type">long</span> i) <span class="Type">const</span>;
<span class="Comment">// array indexing, no range checking</span>
T_ptr* get() <span class="Type">const</span>;
<span class="Comment">// return underlying pointer</span>
T_ptr* release() { len = <span class="Constant">0</span>; <span class="Statement">return</span> dp.release(); }
<span class="Comment">// return underlying pointer and reset to null</span>
<span class="Type">void</span> move(Unique2DArray& other);
<span class="Comment">// move pointers</span>
<span class="Type">void</span> swap(Unique2DArray& other);
<span class="Comment">// swap pointers</span>
Unique2DArray& <span class="Statement">operator</span>=(fake_null_type);
Unique2DArray(fake_null_type) : dp(<span class="Constant">0</span>), len(<span class="Constant">0</span>) { }
<span class="Comment">// allows initialization and assignment of 0</span>
<span class="Statement">operator</span> fake_null_type() <span class="Type">const</span>;
<span class="Comment">// allows comparison to 0</span>
};
<span class="Comment">// free swap function</span>
<span class="Type">template</span><<span class="Type">class</span> T> <span class="Type">void</span> swap(Unique2DArray<T>& p, Unique2DArray<T>& q);
<span class="Comment">// Equality testing</span>
<span class="Type">template</span><<span class="Type">class</span> X> <span class="Type">bool</span> <span class="Statement">operator</span>==(<span class="Type">const</span> Unique2DArray<X>& a, <span class="Type">const</span> Unique2DArray<X>& b);
<span class="Type">template</span><<span class="Type">class</span> X> <span class="Type">bool</span> <span class="Statement">operator</span>!=(<span class="Type">const</span> Unique2DArray<X>& a, <span class="Type">const</span> Unique2DArray<X>& b);
</pre>
</body>
</html>