This file is indexed.

/usr/share/doc/libntl-dev/NTL/SmartPtr.cpp.html is in libntl-dev 9.9.1-3.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>/Volumes/unix-files/u/ntl-new/ntl-9.9.0dev/doc/SmartPtr.cpp.html</title>
<meta name="Generator" content="Vim/7.1">
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
</head>
<body bgcolor="#ffffff" text="#000000"><font face="monospace">
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>***************************************************************************</i></font><br>
<br>
<font color="#0000ed"><i>SmartPtr: a smart pointer class.</i></font><br>
<br>
<font color="#0000ed"><i>Synopsis: provides a reference counted smart pointer, similar to shared_ptr</i></font><br>
<font color="#0000ed"><i>in the standard library.&nbsp;&nbsp;It is provided here to minimize reliance</i></font><br>
<font color="#0000ed"><i>on the standard library, especially for older C++ compilers, which may</i></font><br>
<font color="#0000ed"><i>not provide shared_ptr, or it may be in TR1, which gets messy.</i></font><br>
<br>
<br>
<font color="#0000ed"><i>Examples:</i></font><br>
<br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp;SmartPtr&lt;T&gt; p1;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // initialize to null</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;SmartPtr&lt;T&gt; p2 = 0;</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp;SmartPtr&lt;T&gt; p3(p1);&nbsp;&nbsp;&nbsp;&nbsp; // copy constructor</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp;T *rp;</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;SmartPtr&lt;T&gt; p4(rp);&nbsp;&nbsp;&nbsp;&nbsp; // construct using raw pointer (explicit): better </i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// to use MakeSmart below</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp;p1 = MakeSmart&lt;T&gt;(...); // build new T object by invoking constructor</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// T(...) with pseudo-variadic templates.</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// This is safer and more efficient that</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// using the raw-pointer constructor</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;p1 = p2;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// assignment</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;p1 = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // assign null</i></font><br>
<br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp;if (!p1) ...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;test for null</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;if (p1 == 0) ... </i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp;if (p1) ...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // test for not null ... </i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;if (p1 != 0) ... </i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp;if (p1 == p2) ...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // test for equality </i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;if (p1 != p2) </i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp;*p1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // dereferencing</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;p1-&gt;...</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp;p1.get();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // return the underlying raw pointer...dangerous!</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp;p1.swap(p2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// fast swap</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;swap(p1, p2);</i></font><br>
<br>
<br>
<font color="#0000ed"><i>Automatic Conversions:</i></font><br>
<br>
<font color="#0000ed"><i>If S is another class, SmartPtr&lt;S&gt; converts to SmartPtr&lt;T&gt; if S* converts to T*</i></font><br>
<font color="#0000ed"><i>(for example, if S is a subclass of T).&nbsp;&nbsp;Similarly, SmartPtr&lt;S&gt; and SmartPtr&lt;T&gt;</i></font><br>
<font color="#0000ed"><i>may be compared if S* and T* may be compared.</i></font><br>
<br>
<font color="#0000ed"><i>MakeSmart:</i></font><br>
<br>
<font color="#0000ed"><i>One can write SmartPtr&lt;T&gt; p = MakeSmart&lt;T&gt;(x1, ..., xn), and this will create a</i></font><br>
<font color="#0000ed"><i>smart pointer to an object constructed as T(x1, ..., xn).&nbsp;&nbsp;Besides notational</i></font><br>
<font color="#0000ed"><i>convenience, it also reduces the number of memory allocations from 2 to 1, as</i></font><br>
<font color="#0000ed"><i>the data and control block can be allocated in one chunck of memory.</i></font><br>
<br>
<font color="#0000ed"><i>This is implemented without reliance on C++11 features, which means that there</i></font><br>
<font color="#0000ed"><i>are limitations.&nbsp;&nbsp;First, the number n of arguments is limited to 9.&nbsp;&nbsp;And</i></font><br>
<font color="#0000ed"><i>second, all arguments are pass by const reference. However, you can work around</i></font><br>
<font color="#0000ed"><i>this by using the helper function Fwd.&nbsp;&nbsp;For example, if T has a 2-argument</i></font><br>
<font color="#0000ed"><i>constructor where the second must be a non-const reference of some type, and x2</i></font><br>
<font color="#0000ed"><i>is a variable of that type, you can write MakeSmart&lt;T&gt;(x1, Fwd(x2)), to forward</i></font><br>
<font color="#0000ed"><i>that reference through all the template nonsense in a typesafe manner.</i></font><br>
<br>
<font color="#0000ed"><i>MakeRaw:</i></font><br>
<br>
<font color="#0000ed"><i>One can also write T *p = MakeRaw&lt;T&gt;(x1, ..., xn) to create a </i></font><br>
<font color="#0000ed"><i>raw pointer.&nbsp;&nbsp;This is the same as writing T *p = new T(x1, ..., xn),</i></font><br>
<font color="#0000ed"><i>except that error handling is determined by the NTL_EXCEPTION</i></font><br>
<font color="#0000ed"><i>flag (on =&gt; bad_alloc exception is thrown, off =&gt; error message</i></font><br>
<font color="#0000ed"><i>and abort).</i></font><br>
<br>
<font color="#0000ed"><i>MakeRawArray:</i></font><br>
<br>
<font color="#0000ed"><i>Another utility routine: one can write T *p = MakeRawArray&lt;T&gt;(n)</i></font><br>
<font color="#0000ed"><i>to make a plain array of n T objects.&nbsp;&nbsp;Error handling is the same</i></font><br>
<font color="#0000ed"><i>as for MakeRaw.</i></font><br>
<br>
<font color="#0000ed"><i>Dynamic casting:</i></font><br>
<br>
<font color="#0000ed"><i>I've also supplied a dynamic cast operation for smart pointers.</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; SmartPtr&lt;Derived&gt; d = MakeSmart&lt;Derived&gt;(); // d points to Derived</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; SmartPtr&lt;Base&gt; b = d; // implicit upcast: OK</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; SmartPtr&lt;Derived&gt; d1 = DynamicCast&lt;Derived&gt;(b);</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// downcast to a Derived object -- returns null for a bad cast</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; </i></font><br>
<br>
<br>
<br>
<font color="#0000ed"><i>Implementation notes:</i></font><br>
<br>
<font color="#0000ed"><i>If NTL is compiled with the NTL_THREADS option, then the reference counting</i></font><br>
<font color="#0000ed"><i>will be thread safe.</i></font><br>
<br>
<font color="#0000ed"><i>The SmartPtrControl class heirarchy is used to make sure the right destructor</i></font><br>
<font color="#0000ed"><i>is called when the ref count goes to zero.&nbsp;&nbsp;This can be an issue for forward</i></font><br>
<font color="#0000ed"><i>declared classes and for subclasses.&nbsp;&nbsp;For example, if T is forward declared in</i></font><br>
<font color="#0000ed"><i>a context where the ref count goes to zero, or if the object's actual type is a</i></font><br>
<font color="#0000ed"><i>subclass of T and T's destructor was not declared virtual.&nbsp;&nbsp;The implementation</i></font><br>
<font color="#0000ed"><i>of SmartPtr guarantees correct behavior in these situations.</i></font><br>
<br>
<font color="#0000ed"><i>The null tests p, !p, p == 0, are all effected via an implicit conversion from</i></font><br>
<font color="#0000ed"><i>SmartPtr&lt;T&gt; to a funny pointer type (a pointer to a member function, which</i></font><br>
<font color="#0000ed"><i>avoids other, unwanted implicit conversions: this is the so-called &quot;safe bool</i></font><br>
<font color="#0000ed"><i>idiom&quot;);</i></font><br>
<br>
<font color="#0000ed"><i>Also, there is an implicit conversion from the same, funny pointer type to</i></font><br>
<font color="#0000ed"><i>SmartPtr&lt;T&gt;, which is how one can use 0 to initialize and assign to a</i></font><br>
<font color="#0000ed"><i>SmartPtr&lt;T&gt;.</i></font><br>
<br>
<font color="#0000ed"><i>In C++11 both of the above effects could perhaps be achieved more directly.</i></font><br>
<font color="#0000ed"><i>The new &quot;explict bool&quot; operator can replace the &quot;safe bool idiom&quot;, and I would</i></font><br>
<font color="#0000ed"><i>think that the new null pointer could be used to get the conversion from &quot;0&quot; to</i></font><br>
<font color="#0000ed"><i>work.</i></font><br>
<br>
<font color="#0000ed"><i>NOTES: See <a href="http://www.artima.com/cppsource/safebool.html">http://www.artima.com/cppsource/safebool.html</a> for more on the &quot;safe</i></font><br>
<font color="#0000ed"><i>bool idiom&quot;.&nbsp;&nbsp;</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;</i></font><br>
<br>
<br>
<font color="#0000ed"><i>****************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T&gt;<br>
<font color="#008b00"><b>class</b></font>&nbsp;SmartPtr {<br>
<font color="#b02f60"><b>public</b></font>:<br>
&nbsp;&nbsp; <font color="#008b00"><b>explicit</b></font>&nbsp;SmartPtr(T* p);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// construct smart pointer from raw pointer (allocated with new)</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// EXCEPTIONS: a control block is dynamically allocated;</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>//&nbsp;&nbsp;&nbsp;&nbsp;if this allocation fails, the object pointed to by p is destroyed</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>//&nbsp;&nbsp;&nbsp;&nbsp;and a bad_alloc exception is thrown</i></font><br>
<br>
&nbsp;&nbsp; SmartPtr();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// initial value null</i></font><br>
<br>
&nbsp;&nbsp; ~SmartPtr();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// if ref count drops to zero, then delete referent</i></font><br>
<br>
&nbsp;&nbsp; SmartPtr(<font color="#008b00"><b>const</b></font>&nbsp;SmartPtr&amp; other);<br>
&nbsp;&nbsp; SmartPtr&amp; <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font>&nbsp;SmartPtr&amp; other);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// copy and asignment</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;Y&gt; SmartPtr(<font color="#008b00"><b>const</b></font>&nbsp;SmartPtr&lt;Y&gt;&amp; other);<br>
&nbsp;&nbsp; <font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;Y&gt; SmartPtr&amp; <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font>&nbsp;SmartPtr&lt;Y&gt;&amp; other);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// copy and asignment</i></font><br>
<br>
&nbsp;&nbsp; T&amp; <font color="#b02f60"><b>operator</b></font>*()&nbsp;&nbsp;<font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; T* <font color="#b02f60"><b>operator</b></font>-&gt;() <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// indirection</i></font><br>
<br>
&nbsp;&nbsp; T* get() <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// get underlying raw pointer</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;swap(SmartPtr&amp; other);<br>
<br>
&nbsp;&nbsp; SmartPtr(fake_null_type);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// allows assignment and initialization from 0</i></font><br>
<br>
&nbsp;&nbsp; <font color="#b02f60"><b>operator</b></font>&nbsp;fake_null_type() <font color="#008b00"><b>const</b></font>; <br>
&nbsp;&nbsp; <font color="#0000ed"><i>// allows comparisons to 0</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;Y&gt; SmartPtr&lt;Y&gt; DynamicCast() <font color="#008b00"><b>const</b></font>; <br>
};<br>
<br>
<br>
<font color="#0000ed"><i>// free swap function</i></font><br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T&gt; <font color="#008b00"><b>void</b></font>&nbsp;swap(SmartPtr&lt;T&gt;&amp; p, SmartPtr&lt;T&gt;&amp; q);<br>
<br>
<font color="#0000ed"><i>// free dynamic cast function</i></font><br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;X, <font color="#008b00"><b>class</b></font>&nbsp;Y&gt; SmartPtr&lt;X&gt; DynamicCast(<font color="#008b00"><b>const</b></font>&nbsp;SmartPtr&lt;Y&gt;&amp; p);<br>
<br>
<br>
<font color="#0000ed"><i>// Equality testing</i></font><br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;X, <font color="#008b00"><b>class</b></font>&nbsp;Y&gt;<br>
<font color="#008b00"><b>bool</b></font>&nbsp;<font color="#b02f60"><b>operator</b></font>==(<font color="#008b00"><b>const</b></font>&nbsp;SmartPtr&lt;X&gt;&amp; a, <font color="#008b00"><b>const</b></font>&nbsp;SmartPtr&lt;Y&gt;&amp; b);<br>
<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;X, <font color="#008b00"><b>class</b></font>&nbsp;Y&gt;<br>
<font color="#008b00"><b>bool</b></font>&nbsp;<font color="#b02f60"><b>operator</b></font>!=(<font color="#008b00"><b>const</b></font>&nbsp;SmartPtr&lt;X&gt;&amp; a, <font color="#008b00"><b>const</b></font>&nbsp;SmartPtr&lt;Y&gt;&amp; b);<br>
<br>
<font color="#0000ed"><i>// MakeSmart psuedo-variadic template</i></font><br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T, <font color="#008b00"><b>class</b></font>&nbsp;X1, ..., <font color="#008b00"><b>class</b></font>&nbsp;Xn&gt; <br>
SmartPtr&lt;T&gt; MakeSmart(<font color="#008b00"><b>const</b></font>&nbsp;X1&amp; x1, ..., <font color="#008b00"><b>const</b></font>&nbsp;Xn&amp; xn);<br>
<font color="#0000ed"><i>// EXCEPTIONS: may throw if dynamic constrction of T(x1, ..., xn) fails</i></font><br>
<br>
<br>
<font color="#0000ed"><i>// EXCEPTIONS: unless otherwise specified, the methods above</i></font><br>
<font color="#0000ed"><i>// never throw an exception (under C++11 rules, if a destructor</i></font><br>
<font color="#0000ed"><i>// is invoked that throws an exception, the program will terminate).</i></font><br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>***************************************************************************</i></font><br>
<br>
<font color="#0000ed"><i>Experimantal: CloneablePtr&lt;T&gt; ...essentially same interface as SmartPtr, but </i></font><br>
<font color="#0000ed"><i>allows cloning of complete objects.&nbsp;&nbsp;The differences:</i></font><br>
<font color="#0000ed"><i>*&nbsp;&nbsp;must construct using MakeCloneable</i></font><br>
<font color="#0000ed"><i>*&nbsp;&nbsp;a clone method is provided</i></font><br>
<font color="#0000ed"><i>*&nbsp;&nbsp;implicit conversion from CloneablePtr to SmartPtr is allowed</i></font><br>
<br>
<font color="#0000ed"><i>Example:</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; CloneablePtr&lt;Derived&gt; d = MakeCloneable&lt;Derived&gt;(); </i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; // d points to Derived</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; CloneablePtr&lt;Base&gt; b = d; // implicit upcast: OK</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; CloneablePtr&lt;Base&gt; b1 = b.clone(); </i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; // clone of b, which is really a Derived object</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; CloneablePtr&lt;Derived&gt; d1 = DynamicCast&lt;Derived&gt;(b1);</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; // downcast to a Derived object -- returns null for a bad cast</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; SmartPtr&lt;Base&gt; b2 = d1;</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; </i></font><br>
<br>
<br>
<font color="#0000ed"><i>Implementation:</i></font><br>
<br>
<font color="#0000ed"><i>In the clone method, the object is constructed using the copy constructor for</i></font><br>
<font color="#0000ed"><i>the type T, where T is the compile-time type with which the first smart pointer</i></font><br>
<font color="#0000ed"><i>to this object was was created, even if the pointer has been subsequently</i></font><br>
<font color="#0000ed"><i>upcasted to a base type S.&nbsp;&nbsp;Such objects must have been initially created using</i></font><br>
<font color="#0000ed"><i>the MakeCloneable function.&nbsp;&nbsp;It turns out, this is hard to do in a completely</i></font><br>
<font color="#0000ed"><i>standards-compliant way, because of the type erasure going on.&nbsp;&nbsp;So I settled on</i></font><br>
<font color="#0000ed"><i>the current method, which does some low-level pointer arithmetic.&nbsp;&nbsp;Even with</i></font><br>
<font color="#0000ed"><i>fancy things like multiple and virtual inheritance, it should work, under the</i></font><br>
<font color="#0000ed"><i>assumption that if two objects have the same (runtime) type, then their memory</i></font><br>
<font color="#0000ed"><i>layout is the same.&nbsp;&nbsp;I don't think anything like that is guaranteed by the</i></font><br>
<font color="#0000ed"><i>standard, but this seems reasonable, and it seems to work.&nbsp;&nbsp;Like I said, it is</i></font><br>
<font color="#0000ed"><i>experimental, and I would appreciate feedback from C++ gurus.</i></font><br>
<br>
<font color="#0000ed"><i>Note that NTL does not use this feature, but I do have applications where this</i></font><br>
<font color="#0000ed"><i>is convenient.</i></font><br>
<br>
<br>
<font color="#0000ed"><i>*********************************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T&gt;<br>
<font color="#008b00"><b>class</b></font>&nbsp;CloneablePtr {<br>
<font color="#b02f60"><b>public</b></font>:<br>
&nbsp;&nbsp; CloneablePtr();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// initial value null</i></font><br>
<br>
&nbsp;&nbsp; ~CloneablePtr();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// if ref count drops to zero, then delete referent</i></font><br>
<br>
&nbsp;&nbsp; CloneablePtr(<font color="#008b00"><b>const</b></font>&nbsp;CloneablePtr&amp; other);<br>
&nbsp;&nbsp; CloneablePtr&amp; <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font>&nbsp;CloneablePtr&amp; other);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// copy and asignment</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;Y&gt; CloneablePtr(<font color="#008b00"><b>const</b></font>&nbsp;CloneablePtr&lt;Y&gt;&amp; other);<br>
&nbsp;&nbsp; <font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;Y&gt; CloneablePtr&amp; <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font>&nbsp;CloneablePtr&lt;Y&gt;&amp; other);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// copy and asignment</i></font><br>
<br>
&nbsp;&nbsp; T&amp; <font color="#b02f60"><b>operator</b></font>*()&nbsp;&nbsp;<font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; T* <font color="#b02f60"><b>operator</b></font>-&gt;() <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// indirection</i></font><br>
<br>
&nbsp;&nbsp; T* get() <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// get underlying raw pointer</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;swap(CloneablePtr&amp; other);<br>
<br>
&nbsp;&nbsp; CloneablePtr(fake_null_type);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// allows assignment and initialization from 0</i></font><br>
<br>
&nbsp;&nbsp; <font color="#b02f60"><b>operator</b></font>&nbsp;fake_null_type() <font color="#008b00"><b>const</b></font>; <br>
&nbsp;&nbsp; <font color="#0000ed"><i>// allows comparisons to 0</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;Y&gt; CloneablePtr&lt;Y&gt; DynamicCast() <font color="#008b00"><b>const</b></font>; <br>
<br>
&nbsp;&nbsp; CloneablePtr clone() <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// construct a clone, using the copy constructor</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// EXCEPTIONS: may throw if copy construction fails</i></font><br>
<br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;Y&gt; <font color="#b02f60"><b>operator</b></font>&nbsp;SmartPtr&lt;Y&gt;();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// implicit conversion from CloneablePtr&lt;T&gt; to SmartPtr&lt;Y&gt;,</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// allowed if T* converts implicitly to Y*.</i></font><br>
};<br>
<br>
<br>
<font color="#0000ed"><i>// free swap function</i></font><br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T&gt; <font color="#008b00"><b>void</b></font>&nbsp;swap(CloneablePtr&lt;T&gt;&amp; p, CloneablePtr&lt;T&gt;&amp; q);<br>
<br>
<font color="#0000ed"><i>// free dynamic cast function</i></font><br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;X, <font color="#008b00"><b>class</b></font>&nbsp;Y&gt; CloneablePtr&lt;X&gt; DynamicCast(<font color="#008b00"><b>const</b></font>&nbsp;CloneablePtr&lt;Y&gt;&amp; p);<br>
<br>
<br>
<font color="#0000ed"><i>// Equality testing</i></font><br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;X, <font color="#008b00"><b>class</b></font>&nbsp;Y&gt;<br>
<font color="#008b00"><b>bool</b></font>&nbsp;<font color="#b02f60"><b>operator</b></font>==(<font color="#008b00"><b>const</b></font>&nbsp;CloneablePtr&lt;X&gt;&amp; a, <font color="#008b00"><b>const</b></font>&nbsp;CloneablePtr&lt;Y&gt;&amp; b);<br>
<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;X, <font color="#008b00"><b>class</b></font>&nbsp;Y&gt;<br>
<font color="#008b00"><b>bool</b></font>&nbsp;<font color="#b02f60"><b>operator</b></font>!=(<font color="#008b00"><b>const</b></font>&nbsp;CloneablePtr&lt;X&gt;&amp; a, <font color="#008b00"><b>const</b></font>&nbsp;CloneablePtr&lt;Y&gt;&amp; b);<br>
<br>
<font color="#0000ed"><i>// MakeCloneable psuedo-variadic template</i></font><br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T, <font color="#008b00"><b>class</b></font>&nbsp;X1, ..., <font color="#008b00"><b>class</b></font>&nbsp;Xn&gt; <br>
CloneablePtr&lt;T&gt; MakeCloneable(<font color="#008b00"><b>const</b></font>&nbsp;X1&amp; x1, ..., <font color="#008b00"><b>const</b></font>&nbsp;Xn&amp; xn);<br>
<font color="#0000ed"><i>// EXCEPTIONS: may throw if dynamic constrction of T(x1, ..., xn) fails</i></font><br>
<br>
<br>
<font color="#0000ed"><i>// EXCEPTIONS: unless otherwise specified, the methods above</i></font><br>
<font color="#0000ed"><i>// never throw an exception (under C++11 rules, if a destructor</i></font><br>
<font color="#0000ed"><i>// is invoked that throws an exception, the program will terminate).</i></font><br>
<br>
<br>
<br>
<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*********************************************************************</i></font><br>
<br>
<font color="#0000ed"><i>UniquePtr&lt;T&gt; -- unique pointer to object with copying disabled.</i></font><br>
<font color="#0000ed"><i>Useful for pointers inside classes so that we can</i></font><br>
<font color="#0000ed"><i>automatically destruct them.&nbsp;&nbsp;</i></font><br>
<br>
<font color="#0000ed"><i>Constructors:</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; UniquePtr&lt;T&gt; p1;&nbsp;&nbsp;&nbsp;&nbsp; // initialize with null</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; T* rp;</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; UniquePtr&lt;T&gt; p1(rp); // construct using raw pointer (explicit)</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1 = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// destroy's p1's referent and assigns null</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.make(...);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// destroy's p1's referent and assigns</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// a fresh objected constructed via T(...),</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// using psuedo-variadic templates</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.reset(rp);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// destroy's p1's referent and assign rp</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (!p1) ...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // test for null</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1 == 0) ...</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1) ...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// test for nonnull</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1 != 0) ...</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1 == p2) ...&nbsp;&nbsp;&nbsp;&nbsp;// test for equality</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1 != p2) ...&nbsp;&nbsp; </i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; *p1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// dereferencing</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1-&gt;...</i></font><br>
<br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; rp = p1.get();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // fetch raw pointer</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; rp = p1.release();&nbsp;&nbsp; // fetch raw pointer, and set to null</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.move(p2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // move p2 to p1, destroying p1's referent</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp; if p1 != p2</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.swap(p2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // swap pointers</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; swap(p1, p2);</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; </i></font><br>
<font color="#0000ed"><i>*********************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T&gt;<br>
<font color="#008b00"><b>class</b></font>&nbsp;UniquePtr {<br>
<font color="#b02f60"><b>public</b></font>:&nbsp;&nbsp; <br>
&nbsp;&nbsp; <font color="#008b00"><b>explicit</b></font>&nbsp;UniquePtr(T *p);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// construct UniquePtr from raw pointer (allocated with new)</i></font><br>
<br>
&nbsp;&nbsp; UniquePtr();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// initial value is null</i></font><br>
<br>
&nbsp;&nbsp; ~UniquePtr();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// destroys referent</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;reset(T* p = <font color="#ff8b00">0</font>);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// reset underlying pointer to p, destroying original referent</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T, <font color="#008b00"><b>class</b></font>&nbsp;X1, ..., <font color="#008b00"><b>class</b></font>&nbsp;Xn&gt; <br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;make(<font color="#008b00"><b>const</b></font>&nbsp;X1&amp; x1, ..., <font color="#008b00"><b>const</b></font>&nbsp;Xn&amp; xn);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// pseudo-variadic template, roughly equivalent to</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// reset(new T(x1, ..., xn))</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// EXCEPTIONS: this may throw (but provides strong ES guarantee)</i></font><br>
<br>
&nbsp;&nbsp; T&amp; <font color="#b02f60"><b>operator</b></font>*()&nbsp;&nbsp;<font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; T* <font color="#b02f60"><b>operator</b></font>-&gt;() <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// indirection</i></font><br>
<br>
&nbsp;&nbsp; T* get() <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// get raw pointer</i></font><br>
<br>
&nbsp;&nbsp; T* release();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// returns raw pointer, and sets the raw pointer to null</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;move(UniquePtr&amp; other);<br>
&nbsp;&nbsp; <font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;Y&gt; <font color="#008b00"><b>void</b></font>&nbsp;move(UniqePtr&lt;Y&gt;&amp; other);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// move other to *this</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// in the second version, Y* should be convertable to T*</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// NOTE: if Y is a subclass of T, then typically, ~T() should</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// be virtual, to ensure that the destructor for Y is called</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;swap(UniquePtr&amp; other);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// swap raw pointers</i></font><br>
<br>
&nbsp;&nbsp; UniquePtr&amp; <font color="#b02f60"><b>operator</b></font>=(fake_null_type);<br>
&nbsp;&nbsp; UniquePtr(fake_null_type);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// allows initialization and assignment of 0</i></font><br>
<br>
&nbsp;&nbsp; <font color="#b02f60"><b>operator</b></font>&nbsp;fake_null_type() <font color="#008b00"><b>const</b></font>; <br>
&nbsp;&nbsp; <font color="#0000ed"><i>// allows comparison with 0</i></font><br>
<br>
<font color="#b02f60"><b>private</b></font>:<br>
&nbsp;&nbsp; UniquePtr(<font color="#008b00"><b>const</b></font>&nbsp;UniquePtr&amp;); <font color="#0000ed"><i>// disabled</i></font><br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;<font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font>&nbsp;UniquePtr&amp;); <font color="#0000ed"><i>// disabled</i></font><br>
};<br>
<br>
<br>
<font color="#0000ed"><i>// free swap function</i></font><br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T&gt; <font color="#008b00"><b>void</b></font>&nbsp;swap(UniquePtr&lt;T&gt;&amp; p, UniquePtr&lt;T&gt;&amp; q);<br>
<br>
<br>
<br>
<font color="#0000ed"><i>// Equality testing</i></font><br>
<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;X&gt; <font color="#008b00"><b>bool</b></font>&nbsp;<font color="#b02f60"><b>operator</b></font>==(<font color="#008b00"><b>const</b></font>&nbsp;UniquePtr&lt;X&gt;&amp; a, <font color="#008b00"><b>const</b></font>&nbsp;UniquePtr&lt;X&gt;&amp; b);<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;X&gt; <font color="#008b00"><b>bool</b></font>&nbsp;<font color="#b02f60"><b>operator</b></font>!=(<font color="#008b00"><b>const</b></font>&nbsp;UniquePtr&lt;X&gt;&amp; a, <font color="#008b00"><b>const</b></font>&nbsp;UniquePtr&lt;X&gt;&amp; b);<br>
<br>
<br>
<font color="#0000ed"><i>// EXCEPTIONS: unless otherwise specified, the methods above</i></font><br>
<font color="#0000ed"><i>// never throw an exception (under C++11 rules, if a destructor</i></font><br>
<font color="#0000ed"><i>// is invoked that throws an exception, the program will terminate).</i></font><br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*********************************************************************</i></font><br>
<br>
<font color="#0000ed"><i>OptionalVal&lt;T&gt; -- unique pointer to object with copying enabled.</i></font><br>
<br>
<font color="#0000ed"><i>Constructors:</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; OptionalVal&lt;T&gt; p1;&nbsp;&nbsp;&nbsp;&nbsp; // initialize with null</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; T* rp;</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; OptionalVal&lt;T&gt; p1(rp); // construct using raw pointer (explicit)</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; OptionalVal&lt;T&gt; p2(p1); // construct a copy of p1's referent</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.make(...);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// destroy's p1's referent and assigns</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// a fresh objected constructed via T(...),</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// using psuedo variadic templates</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.reset(rp);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// destroy's p1's referent and assign rp</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1.exists()) ... // test for null</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.val()&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // dereference</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; rp = p1.get();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // fetch raw pointer</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; rp = p1.release();&nbsp;&nbsp; // fetch raw pointer, and set to NULL</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.move(p2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // move p2 to p1, destroying p1's referent</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp; if p1 != p2</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1 = p2;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // deep copy, using T's copy constructor</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.swap(p2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // swap pointers</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; swap(p1, p2);</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; </i></font><br>
<font color="#0000ed"><i>*********************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T&gt;<br>
<font color="#008b00"><b>class</b></font>&nbsp;OptionalVal {<br>
<font color="#b02f60"><b>public</b></font>:&nbsp;&nbsp; <br>
&nbsp;&nbsp; <font color="#008b00"><b>explicit</b></font>&nbsp;OptionalVal(T *p);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// initialize using raw pointer (allocated with new)</i></font><br>
<br>
&nbsp;&nbsp; OptionalVal();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// initialize to null</i></font><br>
<br>
&nbsp;&nbsp; OptionalVal(<font color="#008b00"><b>const</b></font>&nbsp;OptionalVal&amp; other); <br>
&nbsp;&nbsp; <font color="#0000ed"><i>// initialize using a deep copy (via T's copy constructor)</i></font><br>
<br>
&nbsp;&nbsp; OptionalVal&amp; <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font>&nbsp;OptionalVal&amp; other);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// assignment using a deep copy (via T's copy constructor)</i></font><br>
<br>
&nbsp;&nbsp; ~OptionalVal();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// destroys the referent</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;reset(T* p = <font color="#ff8b00">0</font>);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// resets the referent</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T, <font color="#008b00"><b>class</b></font>&nbsp;X1, ..., <font color="#008b00"><b>class</b></font>&nbsp;Xn&gt; <br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;make(<font color="#008b00"><b>const</b></font>&nbsp;X1&amp; x1, ..., <font color="#008b00"><b>const</b></font>&nbsp;Xn&amp; xn);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// pseudo-variadic template.</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// resets the referent to a new object T(x1, ..., xn)</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// EXCEPTIONS: may throw an exception (but provides strong ES guarantee)</i></font><br>
<br>
&nbsp;&nbsp; T&amp; val() <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// returns reference to referent </i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// if underlying pointer p is null, the indirection *p</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// is undefined behavior, but most likely leads to program termination</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>bool</b></font>&nbsp;exists() <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// checks that underlying pointer is not null</i></font><br>
<br>
&nbsp;&nbsp; T* get() <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// returns underlying raw pointer</i></font><br>
<br>
&nbsp;&nbsp; T* release();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// returns raw pointer, and sets the raw pointer to null</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;move(OptionalVal&amp; other);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// performs a (shallow) pointer move</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;swap(OptionalVal&amp; other);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// performs a (shallow) pointer swap</i></font><br>
<br>
};<br>
<br>
<br>
<font color="#0000ed"><i>// free swap function</i></font><br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T&gt; <font color="#008b00"><b>void</b></font>&nbsp;swap(OptionalVal&lt;T&gt;&amp; p, OptionalVal&lt;T&gt;&amp; q);<br>
<br>
<br>
<br>
<font color="#0000ed"><i>// EXCEPTIONS: unless otherwise specified, the methods above</i></font><br>
<font color="#0000ed"><i>// never throw an exception (under C++11 rules, if a destructor</i></font><br>
<font color="#0000ed"><i>// is invoked that throws an exception, the program will terminate).</i></font><br>
<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*********************************************************************</i></font><br>
<br>
<font color="#0000ed"><i>UniqueArray&lt;T&gt; -- unique pointer to array of objects with copying disabled.</i></font><br>
<font color="#0000ed"><i>Useful for pointers inside classes so that we can</i></font><br>
<font color="#0000ed"><i>automatically destruct them.&nbsp;&nbsp;</i></font><br>
<br>
<font color="#0000ed"><i>Constructors:</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; UniqueArray&lt;T&gt; p1;&nbsp;&nbsp;&nbsp;&nbsp; // initialize with null</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; T* rp;</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; UniqueArray&lt;T&gt; p1(rp); // construct using raw pointer (explicit)</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1 = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// destroy's p1's referent and assigns null</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.SetLength(n);&nbsp;&nbsp;&nbsp;&nbsp; // destroy's p1's referent and assigns</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// a fresh objected constructed via new T[n]</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.reset(rp);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// destroy's p1's referent and assign rp</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (!p1) ...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // test for null</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1 == 0) ...</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1) ...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// test for nonnull</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1 != 0) ...</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1 == p2) ...&nbsp;&nbsp;&nbsp;&nbsp;// test for equality</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1 != p2) ...&nbsp;&nbsp; </i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1[i]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// array indexing</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; rp = p1.get();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // fetch raw pointer</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; rp = p1.release();&nbsp;&nbsp; // fetch raw pointer, and set to null</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.move(p2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // move p2 to p1, destroying p1's referent </i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp; if p1 != p2</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.swap(p2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // fast swap</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; swap(p1, p2);</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; </i></font><br>
<font color="#0000ed"><i>*********************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T&gt;<br>
<font color="#008b00"><b>class</b></font>&nbsp;UniqueArray {<br>
<font color="#b02f60"><b>public</b></font>:&nbsp;&nbsp; <br>
&nbsp;&nbsp; <font color="#008b00"><b>explicit</b></font>&nbsp;UniqueArray(T *p);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// construct from raw pointer (allocated with new[])</i></font><br>
<br>
&nbsp;&nbsp; UniqueArray();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// initially null</i></font><br>
<br>
&nbsp;&nbsp; ~UniqueArray();<br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;reset(T* p = <font color="#ff8b00">0</font>);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// reset with raw pointer, destryong referent</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;SetLength(<font color="#008b00"><b>long</b></font>&nbsp;n);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// destroys referent and allocates an array of size n</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// EXCEPTIONS: this may throw (but provides strong ES guarantee)</i></font><br>
<br>
&nbsp;&nbsp; T&amp; <font color="#b02f60"><b>operator</b></font>[](<font color="#008b00"><b>long</b></font>&nbsp;i) <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// accesses ith element in the array (currently no range checking)</i></font><br>
<br>
&nbsp;&nbsp; T* get() <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// get raw pointer</i></font><br>
<br>
&nbsp;&nbsp; T* elts() <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// get raw pointer (for compatibility with the Vec class)</i></font><br>
<br>
&nbsp;&nbsp; T* release();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// get raw pointer and reset to null</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;move(UniqueArray&amp; other);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// move raw pointer</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;swap(UniqueArray&amp; other);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// swap raw pointer</i></font><br>
<br>
&nbsp;&nbsp; UniqueArray&amp; <font color="#b02f60"><b>operator</b></font>=(fake_null_type);<br>
&nbsp;&nbsp; UniqueArray(fake_null_type);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// allows initialization and assignment of 0</i></font><br>
<br>
&nbsp;&nbsp; <font color="#b02f60"><b>operator</b></font>&nbsp;fake_null_type() <font color="#008b00"><b>const</b></font>; <br>
&nbsp;&nbsp; <font color="#0000ed"><i>// allows comparison to 0</i></font><br>
<br>
};<br>
<br>
<br>
<br>
<font color="#0000ed"><i>// free swap function</i></font><br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T&gt; <font color="#008b00"><b>void</b></font>&nbsp;swap(UniqueArray&lt;T&gt;&amp; p, UniqueArray&lt;T&gt;&amp; q);<br>
<br>
<br>
<br>
<font color="#0000ed"><i>// Equality testing</i></font><br>
<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;X&gt; <font color="#008b00"><b>bool</b></font>&nbsp;<font color="#b02f60"><b>operator</b></font>==(<font color="#008b00"><b>const</b></font>&nbsp;UniqueArray&lt;X&gt;&amp; a, <font color="#008b00"><b>const</b></font>&nbsp;UniqueArray&lt;X&gt;&amp; b);<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;X&gt; <font color="#008b00"><b>bool</b></font>&nbsp;<font color="#b02f60"><b>operator</b></font>!=(<font color="#008b00"><b>const</b></font>&nbsp;UniqueArray&lt;X&gt;&amp; a, <font color="#008b00"><b>const</b></font>&nbsp;UniqueArray&lt;X&gt;&amp; b);<br>
<br>
<br>
<br>
<br>
<font color="#0000ed"><i>/*</i></font><font color="#0000ed"><i>*********************************************************************</i></font><br>
<br>
<font color="#0000ed"><i>Unique2DArray&lt;T&gt; -- unique pointer to array of arrays.</i></font><br>
<br>
<font color="#0000ed"><i>This is very similar to UniqueArray&lt; UniqueArray&lt;T&gt; &gt;, except that </i></font><br>
<font color="#0000ed"><i>we can retrofit old code that accepts objects of type T**.</i></font><br>
<br>
<font color="#0000ed"><i>Constructors:</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; Unique2DArray&lt;T&gt; p1;&nbsp;&nbsp;&nbsp;&nbsp; // initialize with null</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1 = 0;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// destroy's p1's referent and assigns null</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.reset();</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.SetLength(n);&nbsp;&nbsp;&nbsp;&nbsp; // destroy's p1's referent and assigns</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// a fresh array of null pointers</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.SetDims(n, m)&nbsp;&nbsp;&nbsp;&nbsp; // creates an n x m array</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (!p1) ...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // test for null</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1 == 0) ...</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1) ...&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// test for nonnull</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1 != 0) ...</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1 == p2) ...&nbsp;&nbsp;&nbsp;&nbsp;// test for equality</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; if (p1 != p2) ...&nbsp;&nbsp; </i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1[i]&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;// array indexing</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; T **rp;</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; rp = p1.get();&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // fetch raw pointer</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; rp = p1.release();&nbsp;&nbsp; // fetch raw pointer, and set to null</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.move(p2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // if p1 != p2 then:</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;makes p1 point to p2's referent,</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;setting p2 to null and destroying</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;//&nbsp;&nbsp;&nbsp;&nbsp;p1's referent</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; p1.swap(p2);&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; // fast swap</i></font><br>
<font color="#0000ed"><i>&nbsp;&nbsp; swap(p1, p2);</i></font><br>
<br>
<font color="#0000ed"><i>&nbsp;&nbsp; </i></font><br>
<font color="#0000ed"><i>*********************************************************************</i></font><font color="#0000ed"><i>*/</i></font><br>
<br>
<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T&gt;<br>
<font color="#008b00"><b>class</b></font>&nbsp;Unique2DArray {<br>
<font color="#b02f60"><b>public</b></font>:<br>
&nbsp;&nbsp; <font color="#008b00"><b>typedef</b></font>&nbsp;T *T_ptr;<br>
<br>
&nbsp;&nbsp; Unique2DArray(); <br>
&nbsp;&nbsp; <font color="#0000ed"><i>// initially null</i></font><br>
<br>
&nbsp;&nbsp; ~Unique2DArray();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// destroys the entire array and each row in the array</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;reset();<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// reset to null</i></font><br>
<br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;SetLength(<font color="#008b00"><b>long</b></font>&nbsp;n);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// resets the array to a vector of length n,</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// each entry initialized to null.</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// EXCEPTIONS: may throw (provides strong ES guarantee)</i></font><br>
&nbsp;&nbsp; <br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;SetDims(<font color="#008b00"><b>long</b></font>&nbsp;n, <font color="#008b00"><b>long</b></font>&nbsp;m); <br>
&nbsp;&nbsp; <font color="#0000ed"><i>// resets the array to a 2D array with n rows and m columns.</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// EXCEPTIONS: may throw (provides strong ES guarantee)</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;SetDimsFrom1(<font color="#008b00"><b>long</b></font>&nbsp;n, <font color="#008b00"><b>long</b></font>&nbsp;m); <br>
&nbsp;&nbsp; <font color="#0000ed"><i>// same as above, but only initializes rows 1..n-1.</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// this helps with some legacy code.</i></font><br>
&nbsp;&nbsp; <font color="#0000ed"><i>// EXCEPTIONS: may throw (provides strong ES guarantee)</i></font><br>
<br>
&nbsp;&nbsp; T_ptr&amp; <font color="#b02f60"><b>operator</b></font>[](<font color="#008b00"><b>long</b></font>&nbsp;i) <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// array indexing, no range checking</i></font><br>
<br>
&nbsp;&nbsp; T_ptr* get() <font color="#008b00"><b>const</b></font>;<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// return underlying pointer</i></font><br>
<br>
&nbsp;&nbsp; T_ptr* release() { len = <font color="#ff8b00">0</font>; <font color="#b02f60"><b>return</b></font>&nbsp;dp.release(); }<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// return underlying pointer and reset to null</i></font><br>
<br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;move(Unique2DArray&amp; other); <br>
&nbsp;&nbsp; <font color="#0000ed"><i>// move pointers</i></font><br>
<br>
&nbsp;&nbsp; <font color="#008b00"><b>void</b></font>&nbsp;swap(Unique2DArray&amp; other);<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// swap pointers</i></font><br>
<br>
&nbsp;&nbsp; Unique2DArray&amp; <font color="#b02f60"><b>operator</b></font>=(fake_null_type);<br>
&nbsp;&nbsp; Unique2DArray(fake_null_type) : dp(<font color="#ff8b00">0</font>), len(<font color="#ff8b00">0</font>) { }<br>
&nbsp;&nbsp; <font color="#0000ed"><i>// allows initialization and assignment of 0</i></font><br>
<br>
&nbsp;&nbsp; <font color="#b02f60"><b>operator</b></font>&nbsp;fake_null_type() <font color="#008b00"><b>const</b></font>; <br>
&nbsp;&nbsp; <font color="#0000ed"><i>// allows comparison to 0</i></font><br>
<br>
};<br>
<br>
<br>
<font color="#0000ed"><i>// free swap function</i></font><br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;T&gt; <font color="#008b00"><b>void</b></font>&nbsp;swap(Unique2DArray&lt;T&gt;&amp; p, Unique2DArray&lt;T&gt;&amp; q);<br>
<br>
<br>
<br>
<font color="#0000ed"><i>// Equality testing</i></font><br>
<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;X&gt; <font color="#008b00"><b>bool</b></font>&nbsp;<font color="#b02f60"><b>operator</b></font>==(<font color="#008b00"><b>const</b></font>&nbsp;Unique2DArray&lt;X&gt;&amp; a, <font color="#008b00"><b>const</b></font>&nbsp;Unique2DArray&lt;X&gt;&amp; b);<br>
<font color="#008b00"><b>template</b></font>&lt;<font color="#008b00"><b>class</b></font>&nbsp;X&gt; <font color="#008b00"><b>bool</b></font>&nbsp;<font color="#b02f60"><b>operator</b></font>!=(<font color="#008b00"><b>const</b></font>&nbsp;Unique2DArray&lt;X&gt;&amp; a, <font color="#008b00"><b>const</b></font>&nbsp;Unique2DArray&lt;X&gt;&amp; b);<br>
<br>
<br>
<br>
</font></body>
</html>