/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. 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> SmartPtr<T> p1; // initialize to null</i></font><br>
<font color="#0000ed"><i> SmartPtr<T> p2 = 0;</i></font><br>
<br>
<font color="#0000ed"><i> SmartPtr<T> p3(p1); // copy constructor</i></font><br>
<br>
<font color="#0000ed"><i> T *rp;</i></font><br>
<font color="#0000ed"><i> SmartPtr<T> p4(rp); // construct using raw pointer (explicit): better </i></font><br>
<font color="#0000ed"><i> // to use MakeSmart below</i></font><br>
<br>
<font color="#0000ed"><i> p1 = MakeSmart<T>(...); // build new T object by invoking constructor</i></font><br>
<font color="#0000ed"><i> // T(...) with pseudo-variadic templates.</i></font><br>
<font color="#0000ed"><i> // This is safer and more efficient that</i></font><br>
<font color="#0000ed"><i> // using the raw-pointer constructor</i></font><br>
<font color="#0000ed"><i> </i></font><br>
<font color="#0000ed"><i> p1 = p2; // assignment</i></font><br>
<font color="#0000ed"><i> p1 = 0; // assign null</i></font><br>
<br>
<br>
<font color="#0000ed"><i> if (!p1) ... // test for null</i></font><br>
<font color="#0000ed"><i> if (p1 == 0) ... </i></font><br>
<br>
<font color="#0000ed"><i> if (p1) ... // test for not null ... </i></font><br>
<font color="#0000ed"><i> if (p1 != 0) ... </i></font><br>
<br>
<font color="#0000ed"><i> if (p1 == p2) ... // test for equality </i></font><br>
<font color="#0000ed"><i> if (p1 != p2) </i></font><br>
<br>
<font color="#0000ed"><i> *p1 // dereferencing</i></font><br>
<font color="#0000ed"><i> p1->...</i></font><br>
<br>
<font color="#0000ed"><i> p1.get(); // return the underlying raw pointer...dangerous!</i></font><br>
<br>
<font color="#0000ed"><i> p1.swap(p2); // fast swap</i></font><br>
<font color="#0000ed"><i> 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<S> converts to SmartPtr<T> if S* converts to T*</i></font><br>
<font color="#0000ed"><i>(for example, if S is a subclass of T). Similarly, SmartPtr<S> and SmartPtr<T></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<T> p = MakeSmart<T>(x1, ..., xn), and this will create a</i></font><br>
<font color="#0000ed"><i>smart pointer to an object constructed as T(x1, ..., xn). 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. First, the number n of arguments is limited to 9. 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. 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<T>(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<T>(x1, ..., xn) to create a </i></font><br>
<font color="#0000ed"><i>raw pointer. 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 => bad_alloc exception is thrown, off => 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<T>(n)</i></font><br>
<font color="#0000ed"><i>to make a plain array of n T objects. 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> SmartPtr<Derived> d = MakeSmart<Derived>(); // d points to Derived</i></font><br>
<font color="#0000ed"><i> SmartPtr<Base> b = d; // implicit upcast: OK</i></font><br>
<br>
<font color="#0000ed"><i> SmartPtr<Derived> d1 = DynamicCast<Derived>(b);</i></font><br>
<font color="#0000ed"><i> // downcast to a Derived object -- returns null for a bad cast</i></font><br>
<font color="#0000ed"><i> </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. This can be an issue for forward</i></font><br>
<font color="#0000ed"><i>declared classes and for subclasses. 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. 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<T> 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 "safe bool</i></font><br>
<font color="#0000ed"><i>idiom");</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<T>, which is how one can use 0 to initialize and assign to a</i></font><br>
<font color="#0000ed"><i>SmartPtr<T>.</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 "explict bool" operator can replace the "safe bool idiom", and I would</i></font><br>
<font color="#0000ed"><i>think that the new null pointer could be used to get the conversion from "0" 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 "safe</i></font><br>
<font color="#0000ed"><i>bool idiom". </i></font><br>
<br>
<font color="#0000ed"><i> </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><<font color="#008b00"><b>class</b></font> T><br>
<font color="#008b00"><b>class</b></font> SmartPtr {<br>
<font color="#b02f60"><b>public</b></font>:<br>
<font color="#008b00"><b>explicit</b></font> SmartPtr(T* p);<br>
<font color="#0000ed"><i>// construct smart pointer from raw pointer (allocated with new)</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: a control block is dynamically allocated;</i></font><br>
<font color="#0000ed"><i>// if this allocation fails, the object pointed to by p is destroyed</i></font><br>
<font color="#0000ed"><i>// and a bad_alloc exception is thrown</i></font><br>
<br>
SmartPtr();<br>
<font color="#0000ed"><i>// initial value null</i></font><br>
<br>
~SmartPtr();<br>
<font color="#0000ed"><i>// if ref count drops to zero, then delete referent</i></font><br>
<br>
SmartPtr(<font color="#008b00"><b>const</b></font> SmartPtr& other);<br>
SmartPtr& <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font> SmartPtr& other);<br>
<font color="#0000ed"><i>// copy and asignment</i></font><br>
<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> Y> SmartPtr(<font color="#008b00"><b>const</b></font> SmartPtr<Y>& other);<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> Y> SmartPtr& <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font> SmartPtr<Y>& other);<br>
<font color="#0000ed"><i>// copy and asignment</i></font><br>
<br>
T& <font color="#b02f60"><b>operator</b></font>*() <font color="#008b00"><b>const</b></font>;<br>
T* <font color="#b02f60"><b>operator</b></font>->() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// indirection</i></font><br>
<br>
T* get() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// get underlying raw pointer</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> swap(SmartPtr& other);<br>
<br>
SmartPtr(fake_null_type);<br>
<font color="#0000ed"><i>// allows assignment and initialization from 0</i></font><br>
<br>
<font color="#b02f60"><b>operator</b></font> fake_null_type() <font color="#008b00"><b>const</b></font>; <br>
<font color="#0000ed"><i>// allows comparisons to 0</i></font><br>
<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> Y> SmartPtr<Y> 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><<font color="#008b00"><b>class</b></font> T> <font color="#008b00"><b>void</b></font> swap(SmartPtr<T>& p, SmartPtr<T>& q);<br>
<br>
<font color="#0000ed"><i>// free dynamic cast function</i></font><br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> X, <font color="#008b00"><b>class</b></font> Y> SmartPtr<X> DynamicCast(<font color="#008b00"><b>const</b></font> SmartPtr<Y>& p);<br>
<br>
<br>
<font color="#0000ed"><i>// Equality testing</i></font><br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> X, <font color="#008b00"><b>class</b></font> Y><br>
<font color="#008b00"><b>bool</b></font> <font color="#b02f60"><b>operator</b></font>==(<font color="#008b00"><b>const</b></font> SmartPtr<X>& a, <font color="#008b00"><b>const</b></font> SmartPtr<Y>& b);<br>
<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> X, <font color="#008b00"><b>class</b></font> Y><br>
<font color="#008b00"><b>bool</b></font> <font color="#b02f60"><b>operator</b></font>!=(<font color="#008b00"><b>const</b></font> SmartPtr<X>& a, <font color="#008b00"><b>const</b></font> SmartPtr<Y>& b);<br>
<br>
<font color="#0000ed"><i>// MakeSmart psuedo-variadic template</i></font><br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> T, <font color="#008b00"><b>class</b></font> X1, ..., <font color="#008b00"><b>class</b></font> Xn> <br>
SmartPtr<T> MakeSmart(<font color="#008b00"><b>const</b></font> X1& x1, ..., <font color="#008b00"><b>const</b></font> Xn& 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<T> ...essentially same interface as SmartPtr, but </i></font><br>
<font color="#0000ed"><i>allows cloning of complete objects. The differences:</i></font><br>
<font color="#0000ed"><i>* must construct using MakeCloneable</i></font><br>
<font color="#0000ed"><i>* a clone method is provided</i></font><br>
<font color="#0000ed"><i>* 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> CloneablePtr<Derived> d = MakeCloneable<Derived>(); </i></font><br>
<font color="#0000ed"><i> // d points to Derived</i></font><br>
<br>
<font color="#0000ed"><i> CloneablePtr<Base> b = d; // implicit upcast: OK</i></font><br>
<br>
<font color="#0000ed"><i> CloneablePtr<Base> b1 = b.clone(); </i></font><br>
<font color="#0000ed"><i> // clone of b, which is really a Derived object</i></font><br>
<br>
<font color="#0000ed"><i> CloneablePtr<Derived> d1 = DynamicCast<Derived>(b1);</i></font><br>
<font color="#0000ed"><i> // downcast to a Derived object -- returns null for a bad cast</i></font><br>
<br>
<font color="#0000ed"><i> SmartPtr<Base> b2 = d1;</i></font><br>
<font color="#0000ed"><i> </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. Such objects must have been initially created using</i></font><br>
<font color="#0000ed"><i>the MakeCloneable function. 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. So I settled on</i></font><br>
<font color="#0000ed"><i>the current method, which does some low-level pointer arithmetic. 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. 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. 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><<font color="#008b00"><b>class</b></font> T><br>
<font color="#008b00"><b>class</b></font> CloneablePtr {<br>
<font color="#b02f60"><b>public</b></font>:<br>
CloneablePtr();<br>
<font color="#0000ed"><i>// initial value null</i></font><br>
<br>
~CloneablePtr();<br>
<font color="#0000ed"><i>// if ref count drops to zero, then delete referent</i></font><br>
<br>
CloneablePtr(<font color="#008b00"><b>const</b></font> CloneablePtr& other);<br>
CloneablePtr& <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font> CloneablePtr& other);<br>
<font color="#0000ed"><i>// copy and asignment</i></font><br>
<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> Y> CloneablePtr(<font color="#008b00"><b>const</b></font> CloneablePtr<Y>& other);<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> Y> CloneablePtr& <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font> CloneablePtr<Y>& other);<br>
<font color="#0000ed"><i>// copy and asignment</i></font><br>
<br>
T& <font color="#b02f60"><b>operator</b></font>*() <font color="#008b00"><b>const</b></font>;<br>
T* <font color="#b02f60"><b>operator</b></font>->() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// indirection</i></font><br>
<br>
T* get() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// get underlying raw pointer</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> swap(CloneablePtr& other);<br>
<br>
CloneablePtr(fake_null_type);<br>
<font color="#0000ed"><i>// allows assignment and initialization from 0</i></font><br>
<br>
<font color="#b02f60"><b>operator</b></font> fake_null_type() <font color="#008b00"><b>const</b></font>; <br>
<font color="#0000ed"><i>// allows comparisons to 0</i></font><br>
<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> Y> CloneablePtr<Y> DynamicCast() <font color="#008b00"><b>const</b></font>; <br>
<br>
CloneablePtr clone() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// construct a clone, using the copy constructor</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: may throw if copy construction fails</i></font><br>
<br>
<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> Y> <font color="#b02f60"><b>operator</b></font> SmartPtr<Y>();<br>
<font color="#0000ed"><i>// implicit conversion from CloneablePtr<T> to SmartPtr<Y>,</i></font><br>
<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><<font color="#008b00"><b>class</b></font> T> <font color="#008b00"><b>void</b></font> swap(CloneablePtr<T>& p, CloneablePtr<T>& q);<br>
<br>
<font color="#0000ed"><i>// free dynamic cast function</i></font><br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> X, <font color="#008b00"><b>class</b></font> Y> CloneablePtr<X> DynamicCast(<font color="#008b00"><b>const</b></font> CloneablePtr<Y>& p);<br>
<br>
<br>
<font color="#0000ed"><i>// Equality testing</i></font><br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> X, <font color="#008b00"><b>class</b></font> Y><br>
<font color="#008b00"><b>bool</b></font> <font color="#b02f60"><b>operator</b></font>==(<font color="#008b00"><b>const</b></font> CloneablePtr<X>& a, <font color="#008b00"><b>const</b></font> CloneablePtr<Y>& b);<br>
<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> X, <font color="#008b00"><b>class</b></font> Y><br>
<font color="#008b00"><b>bool</b></font> <font color="#b02f60"><b>operator</b></font>!=(<font color="#008b00"><b>const</b></font> CloneablePtr<X>& a, <font color="#008b00"><b>const</b></font> CloneablePtr<Y>& b);<br>
<br>
<font color="#0000ed"><i>// MakeCloneable psuedo-variadic template</i></font><br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> T, <font color="#008b00"><b>class</b></font> X1, ..., <font color="#008b00"><b>class</b></font> Xn> <br>
CloneablePtr<T> MakeCloneable(<font color="#008b00"><b>const</b></font> X1& x1, ..., <font color="#008b00"><b>const</b></font> Xn& 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<T> -- 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. </i></font><br>
<br>
<font color="#0000ed"><i>Constructors:</i></font><br>
<font color="#0000ed"><i> UniquePtr<T> p1; // initialize with null</i></font><br>
<br>
<font color="#0000ed"><i> T* rp;</i></font><br>
<font color="#0000ed"><i> UniquePtr<T> p1(rp); // construct using raw pointer (explicit)</i></font><br>
<br>
<font color="#0000ed"><i> p1 = 0; // destroy's p1's referent and assigns null</i></font><br>
<br>
<font color="#0000ed"><i> p1.make(...); // destroy's p1's referent and assigns</i></font><br>
<font color="#0000ed"><i> // a fresh objected constructed via T(...),</i></font><br>
<font color="#0000ed"><i> // using psuedo-variadic templates</i></font><br>
<font color="#0000ed"><i> </i></font><br>
<font color="#0000ed"><i> p1.reset(rp); // destroy's p1's referent and assign rp</i></font><br>
<br>
<font color="#0000ed"><i> if (!p1) ... // test for null</i></font><br>
<font color="#0000ed"><i> if (p1 == 0) ...</i></font><br>
<br>
<font color="#0000ed"><i> if (p1) ... // test for nonnull</i></font><br>
<font color="#0000ed"><i> if (p1 != 0) ...</i></font><br>
<br>
<font color="#0000ed"><i> if (p1 == p2) ... // test for equality</i></font><br>
<font color="#0000ed"><i> if (p1 != p2) ... </i></font><br>
<br>
<font color="#0000ed"><i> *p1 // dereferencing</i></font><br>
<font color="#0000ed"><i> p1->...</i></font><br>
<br>
<br>
<font color="#0000ed"><i> rp = p1.get(); // fetch raw pointer</i></font><br>
<font color="#0000ed"><i> rp = p1.release(); // fetch raw pointer, and set to null</i></font><br>
<br>
<font color="#0000ed"><i> p1.move(p2); // move p2 to p1, destroying p1's referent</i></font><br>
<font color="#0000ed"><i> // if p1 != p2</i></font><br>
<br>
<font color="#0000ed"><i> p1.swap(p2); // swap pointers</i></font><br>
<font color="#0000ed"><i> swap(p1, p2);</i></font><br>
<br>
<font color="#0000ed"><i> </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><<font color="#008b00"><b>class</b></font> T><br>
<font color="#008b00"><b>class</b></font> UniquePtr {<br>
<font color="#b02f60"><b>public</b></font>: <br>
<font color="#008b00"><b>explicit</b></font> UniquePtr(T *p);<br>
<font color="#0000ed"><i>// construct UniquePtr from raw pointer (allocated with new)</i></font><br>
<br>
UniquePtr();<br>
<font color="#0000ed"><i>// initial value is null</i></font><br>
<br>
~UniquePtr();<br>
<font color="#0000ed"><i>// destroys referent</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> reset(T* p = <font color="#ff8b00">0</font>);<br>
<font color="#0000ed"><i>// reset underlying pointer to p, destroying original referent</i></font><br>
<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> T, <font color="#008b00"><b>class</b></font> X1, ..., <font color="#008b00"><b>class</b></font> Xn> <br>
<font color="#008b00"><b>void</b></font> make(<font color="#008b00"><b>const</b></font> X1& x1, ..., <font color="#008b00"><b>const</b></font> Xn& xn);<br>
<font color="#0000ed"><i>// pseudo-variadic template, roughly equivalent to</i></font><br>
<font color="#0000ed"><i>// reset(new T(x1, ..., xn))</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: this may throw (but provides strong ES guarantee)</i></font><br>
<br>
T& <font color="#b02f60"><b>operator</b></font>*() <font color="#008b00"><b>const</b></font>;<br>
T* <font color="#b02f60"><b>operator</b></font>->() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// indirection</i></font><br>
<br>
T* get() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// get raw pointer</i></font><br>
<br>
T* release();<br>
<font color="#0000ed"><i>// returns raw pointer, and sets the raw pointer to null</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> move(UniquePtr& other);<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> Y> <font color="#008b00"><b>void</b></font> move(UniqePtr<Y>& other);<br>
<font color="#0000ed"><i>// move other to *this</i></font><br>
<font color="#0000ed"><i>// in the second version, Y* should be convertable to T*</i></font><br>
<font color="#0000ed"><i>// NOTE: if Y is a subclass of T, then typically, ~T() should</i></font><br>
<font color="#0000ed"><i>// be virtual, to ensure that the destructor for Y is called</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> swap(UniquePtr& other);<br>
<font color="#0000ed"><i>// swap raw pointers</i></font><br>
<br>
UniquePtr& <font color="#b02f60"><b>operator</b></font>=(fake_null_type);<br>
UniquePtr(fake_null_type);<br>
<font color="#0000ed"><i>// allows initialization and assignment of 0</i></font><br>
<br>
<font color="#b02f60"><b>operator</b></font> fake_null_type() <font color="#008b00"><b>const</b></font>; <br>
<font color="#0000ed"><i>// allows comparison with 0</i></font><br>
<br>
<font color="#b02f60"><b>private</b></font>:<br>
UniquePtr(<font color="#008b00"><b>const</b></font> UniquePtr&); <font color="#0000ed"><i>// disabled</i></font><br>
<font color="#008b00"><b>void</b></font> <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font> UniquePtr&); <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><<font color="#008b00"><b>class</b></font> T> <font color="#008b00"><b>void</b></font> swap(UniquePtr<T>& p, UniquePtr<T>& q);<br>
<br>
<br>
<br>
<font color="#0000ed"><i>// Equality testing</i></font><br>
<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> X> <font color="#008b00"><b>bool</b></font> <font color="#b02f60"><b>operator</b></font>==(<font color="#008b00"><b>const</b></font> UniquePtr<X>& a, <font color="#008b00"><b>const</b></font> UniquePtr<X>& b);<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> X> <font color="#008b00"><b>bool</b></font> <font color="#b02f60"><b>operator</b></font>!=(<font color="#008b00"><b>const</b></font> UniquePtr<X>& a, <font color="#008b00"><b>const</b></font> UniquePtr<X>& 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<T> -- unique pointer to object with copying enabled.</i></font><br>
<br>
<font color="#0000ed"><i>Constructors:</i></font><br>
<font color="#0000ed"><i> OptionalVal<T> p1; // initialize with null</i></font><br>
<br>
<font color="#0000ed"><i> T* rp;</i></font><br>
<font color="#0000ed"><i> OptionalVal<T> p1(rp); // construct using raw pointer (explicit)</i></font><br>
<br>
<font color="#0000ed"><i> OptionalVal<T> p2(p1); // construct a copy of p1's referent</i></font><br>
<br>
<font color="#0000ed"><i> </i></font><br>
<br>
<font color="#0000ed"><i> p1.make(...); // destroy's p1's referent and assigns</i></font><br>
<font color="#0000ed"><i> // a fresh objected constructed via T(...),</i></font><br>
<font color="#0000ed"><i> // using psuedo variadic templates</i></font><br>
<font color="#0000ed"><i> </i></font><br>
<font color="#0000ed"><i> p1.reset(rp); // destroy's p1's referent and assign rp</i></font><br>
<br>
<font color="#0000ed"><i> if (p1.exists()) ... // test for null</i></font><br>
<br>
<font color="#0000ed"><i> p1.val() // dereference</i></font><br>
<br>
<font color="#0000ed"><i> rp = p1.get(); // fetch raw pointer</i></font><br>
<font color="#0000ed"><i> rp = p1.release(); // fetch raw pointer, and set to NULL</i></font><br>
<font color="#0000ed"><i> p1.move(p2); // move p2 to p1, destroying p1's referent</i></font><br>
<font color="#0000ed"><i> // if p1 != p2</i></font><br>
<br>
<font color="#0000ed"><i> p1 = p2; // deep copy, using T's copy constructor</i></font><br>
<br>
<font color="#0000ed"><i> p1.swap(p2); // swap pointers</i></font><br>
<font color="#0000ed"><i> swap(p1, p2);</i></font><br>
<br>
<font color="#0000ed"><i> </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><<font color="#008b00"><b>class</b></font> T><br>
<font color="#008b00"><b>class</b></font> OptionalVal {<br>
<font color="#b02f60"><b>public</b></font>: <br>
<font color="#008b00"><b>explicit</b></font> OptionalVal(T *p);<br>
<font color="#0000ed"><i>// initialize using raw pointer (allocated with new)</i></font><br>
<br>
OptionalVal();<br>
<font color="#0000ed"><i>// initialize to null</i></font><br>
<br>
OptionalVal(<font color="#008b00"><b>const</b></font> OptionalVal& other); <br>
<font color="#0000ed"><i>// initialize using a deep copy (via T's copy constructor)</i></font><br>
<br>
OptionalVal& <font color="#b02f60"><b>operator</b></font>=(<font color="#008b00"><b>const</b></font> OptionalVal& other);<br>
<font color="#0000ed"><i>// assignment using a deep copy (via T's copy constructor)</i></font><br>
<br>
~OptionalVal();<br>
<font color="#0000ed"><i>// destroys the referent</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> reset(T* p = <font color="#ff8b00">0</font>);<br>
<font color="#0000ed"><i>// resets the referent</i></font><br>
<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> T, <font color="#008b00"><b>class</b></font> X1, ..., <font color="#008b00"><b>class</b></font> Xn> <br>
<font color="#008b00"><b>void</b></font> make(<font color="#008b00"><b>const</b></font> X1& x1, ..., <font color="#008b00"><b>const</b></font> Xn& xn);<br>
<font color="#0000ed"><i>// pseudo-variadic template.</i></font><br>
<font color="#0000ed"><i>// resets the referent to a new object T(x1, ..., xn)</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: may throw an exception (but provides strong ES guarantee)</i></font><br>
<br>
T& val() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// returns reference to referent </i></font><br>
<font color="#0000ed"><i>// if underlying pointer p is null, the indirection *p</i></font><br>
<font color="#0000ed"><i>// is undefined behavior, but most likely leads to program termination</i></font><br>
<br>
<font color="#008b00"><b>bool</b></font> exists() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// checks that underlying pointer is not null</i></font><br>
<br>
T* get() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// returns underlying raw pointer</i></font><br>
<br>
T* release();<br>
<font color="#0000ed"><i>// returns raw pointer, and sets the raw pointer to null</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> move(OptionalVal& other);<br>
<font color="#0000ed"><i>// performs a (shallow) pointer move</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> swap(OptionalVal& other);<br>
<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><<font color="#008b00"><b>class</b></font> T> <font color="#008b00"><b>void</b></font> swap(OptionalVal<T>& p, OptionalVal<T>& 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<T> -- 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. </i></font><br>
<br>
<font color="#0000ed"><i>Constructors:</i></font><br>
<font color="#0000ed"><i> UniqueArray<T> p1; // initialize with null</i></font><br>
<br>
<font color="#0000ed"><i> T* rp;</i></font><br>
<font color="#0000ed"><i> UniqueArray<T> p1(rp); // construct using raw pointer (explicit)</i></font><br>
<br>
<font color="#0000ed"><i> p1 = 0; // destroy's p1's referent and assigns null</i></font><br>
<br>
<font color="#0000ed"><i> p1.SetLength(n); // destroy's p1's referent and assigns</i></font><br>
<font color="#0000ed"><i> // a fresh objected constructed via new T[n]</i></font><br>
<font color="#0000ed"><i> </i></font><br>
<font color="#0000ed"><i> p1.reset(rp); // destroy's p1's referent and assign rp</i></font><br>
<br>
<font color="#0000ed"><i> if (!p1) ... // test for null</i></font><br>
<font color="#0000ed"><i> if (p1 == 0) ...</i></font><br>
<br>
<font color="#0000ed"><i> if (p1) ... // test for nonnull</i></font><br>
<font color="#0000ed"><i> if (p1 != 0) ...</i></font><br>
<br>
<font color="#0000ed"><i> if (p1 == p2) ... // test for equality</i></font><br>
<font color="#0000ed"><i> if (p1 != p2) ... </i></font><br>
<br>
<font color="#0000ed"><i> p1[i] // array indexing</i></font><br>
<br>
<font color="#0000ed"><i> rp = p1.get(); // fetch raw pointer</i></font><br>
<font color="#0000ed"><i> rp = p1.release(); // fetch raw pointer, and set to null</i></font><br>
<font color="#0000ed"><i> p1.move(p2); // move p2 to p1, destroying p1's referent </i></font><br>
<font color="#0000ed"><i> // if p1 != p2</i></font><br>
<br>
<font color="#0000ed"><i> p1.swap(p2); // fast swap</i></font><br>
<font color="#0000ed"><i> swap(p1, p2);</i></font><br>
<br>
<font color="#0000ed"><i> </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><<font color="#008b00"><b>class</b></font> T><br>
<font color="#008b00"><b>class</b></font> UniqueArray {<br>
<font color="#b02f60"><b>public</b></font>: <br>
<font color="#008b00"><b>explicit</b></font> UniqueArray(T *p);<br>
<font color="#0000ed"><i>// construct from raw pointer (allocated with new[])</i></font><br>
<br>
UniqueArray();<br>
<font color="#0000ed"><i>// initially null</i></font><br>
<br>
~UniqueArray();<br>
<br>
<font color="#008b00"><b>void</b></font> reset(T* p = <font color="#ff8b00">0</font>);<br>
<font color="#0000ed"><i>// reset with raw pointer, destryong referent</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> SetLength(<font color="#008b00"><b>long</b></font> n);<br>
<font color="#0000ed"><i>// destroys referent and allocates an array of size n</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: this may throw (but provides strong ES guarantee)</i></font><br>
<br>
T& <font color="#b02f60"><b>operator</b></font>[](<font color="#008b00"><b>long</b></font> i) <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// accesses ith element in the array (currently no range checking)</i></font><br>
<br>
T* get() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// get raw pointer</i></font><br>
<br>
T* elts() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// get raw pointer (for compatibility with the Vec class)</i></font><br>
<br>
T* release();<br>
<font color="#0000ed"><i>// get raw pointer and reset to null</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> move(UniqueArray& other);<br>
<font color="#0000ed"><i>// move raw pointer</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> swap(UniqueArray& other);<br>
<font color="#0000ed"><i>// swap raw pointer</i></font><br>
<br>
UniqueArray& <font color="#b02f60"><b>operator</b></font>=(fake_null_type);<br>
UniqueArray(fake_null_type);<br>
<font color="#0000ed"><i>// allows initialization and assignment of 0</i></font><br>
<br>
<font color="#b02f60"><b>operator</b></font> fake_null_type() <font color="#008b00"><b>const</b></font>; <br>
<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><<font color="#008b00"><b>class</b></font> T> <font color="#008b00"><b>void</b></font> swap(UniqueArray<T>& p, UniqueArray<T>& q);<br>
<br>
<br>
<br>
<font color="#0000ed"><i>// Equality testing</i></font><br>
<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> X> <font color="#008b00"><b>bool</b></font> <font color="#b02f60"><b>operator</b></font>==(<font color="#008b00"><b>const</b></font> UniqueArray<X>& a, <font color="#008b00"><b>const</b></font> UniqueArray<X>& b);<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> X> <font color="#008b00"><b>bool</b></font> <font color="#b02f60"><b>operator</b></font>!=(<font color="#008b00"><b>const</b></font> UniqueArray<X>& a, <font color="#008b00"><b>const</b></font> UniqueArray<X>& 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<T> -- unique pointer to array of arrays.</i></font><br>
<br>
<font color="#0000ed"><i>This is very similar to UniqueArray< UniqueArray<T> >, 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> Unique2DArray<T> p1; // initialize with null</i></font><br>
<br>
<font color="#0000ed"><i> p1 = 0; // destroy's p1's referent and assigns null</i></font><br>
<font color="#0000ed"><i> p1.reset();</i></font><br>
<br>
<font color="#0000ed"><i> p1.SetLength(n); // destroy's p1's referent and assigns</i></font><br>
<font color="#0000ed"><i> // a fresh array of null pointers</i></font><br>
<br>
<font color="#0000ed"><i> p1.SetDims(n, m) // creates an n x m array</i></font><br>
<font color="#0000ed"><i> </i></font><br>
<font color="#0000ed"><i> if (!p1) ... // test for null</i></font><br>
<font color="#0000ed"><i> if (p1 == 0) ...</i></font><br>
<br>
<font color="#0000ed"><i> if (p1) ... // test for nonnull</i></font><br>
<font color="#0000ed"><i> if (p1 != 0) ...</i></font><br>
<br>
<font color="#0000ed"><i> if (p1 == p2) ... // test for equality</i></font><br>
<font color="#0000ed"><i> if (p1 != p2) ... </i></font><br>
<br>
<font color="#0000ed"><i> p1[i] // array indexing</i></font><br>
<br>
<font color="#0000ed"><i> T **rp;</i></font><br>
<font color="#0000ed"><i> rp = p1.get(); // fetch raw pointer</i></font><br>
<font color="#0000ed"><i> rp = p1.release(); // fetch raw pointer, and set to null</i></font><br>
<font color="#0000ed"><i> p1.move(p2); // if p1 != p2 then:</i></font><br>
<font color="#0000ed"><i> // makes p1 point to p2's referent,</i></font><br>
<font color="#0000ed"><i> // setting p2 to null and destroying</i></font><br>
<font color="#0000ed"><i> // p1's referent</i></font><br>
<br>
<font color="#0000ed"><i> p1.swap(p2); // fast swap</i></font><br>
<font color="#0000ed"><i> swap(p1, p2);</i></font><br>
<br>
<font color="#0000ed"><i> </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><<font color="#008b00"><b>class</b></font> T><br>
<font color="#008b00"><b>class</b></font> Unique2DArray {<br>
<font color="#b02f60"><b>public</b></font>:<br>
<font color="#008b00"><b>typedef</b></font> T *T_ptr;<br>
<br>
Unique2DArray(); <br>
<font color="#0000ed"><i>// initially null</i></font><br>
<br>
~Unique2DArray();<br>
<font color="#0000ed"><i>// destroys the entire array and each row in the array</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> reset();<br>
<font color="#0000ed"><i>// reset to null</i></font><br>
<br>
<br>
<font color="#008b00"><b>void</b></font> SetLength(<font color="#008b00"><b>long</b></font> n);<br>
<font color="#0000ed"><i>// resets the array to a vector of length n,</i></font><br>
<font color="#0000ed"><i>// each entry initialized to null.</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: may throw (provides strong ES guarantee)</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> SetDims(<font color="#008b00"><b>long</b></font> n, <font color="#008b00"><b>long</b></font> m); <br>
<font color="#0000ed"><i>// resets the array to a 2D array with n rows and m columns.</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: may throw (provides strong ES guarantee)</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> SetDimsFrom1(<font color="#008b00"><b>long</b></font> n, <font color="#008b00"><b>long</b></font> m); <br>
<font color="#0000ed"><i>// same as above, but only initializes rows 1..n-1.</i></font><br>
<font color="#0000ed"><i>// this helps with some legacy code.</i></font><br>
<font color="#0000ed"><i>// EXCEPTIONS: may throw (provides strong ES guarantee)</i></font><br>
<br>
T_ptr& <font color="#b02f60"><b>operator</b></font>[](<font color="#008b00"><b>long</b></font> i) <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// array indexing, no range checking</i></font><br>
<br>
T_ptr* get() <font color="#008b00"><b>const</b></font>;<br>
<font color="#0000ed"><i>// return underlying pointer</i></font><br>
<br>
T_ptr* release() { len = <font color="#ff8b00">0</font>; <font color="#b02f60"><b>return</b></font> dp.release(); }<br>
<font color="#0000ed"><i>// return underlying pointer and reset to null</i></font><br>
<br>
<br>
<font color="#008b00"><b>void</b></font> move(Unique2DArray& other); <br>
<font color="#0000ed"><i>// move pointers</i></font><br>
<br>
<font color="#008b00"><b>void</b></font> swap(Unique2DArray& other);<br>
<font color="#0000ed"><i>// swap pointers</i></font><br>
<br>
Unique2DArray& <font color="#b02f60"><b>operator</b></font>=(fake_null_type);<br>
Unique2DArray(fake_null_type) : dp(<font color="#ff8b00">0</font>), len(<font color="#ff8b00">0</font>) { }<br>
<font color="#0000ed"><i>// allows initialization and assignment of 0</i></font><br>
<br>
<font color="#b02f60"><b>operator</b></font> fake_null_type() <font color="#008b00"><b>const</b></font>; <br>
<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><<font color="#008b00"><b>class</b></font> T> <font color="#008b00"><b>void</b></font> swap(Unique2DArray<T>& p, Unique2DArray<T>& q);<br>
<br>
<br>
<br>
<font color="#0000ed"><i>// Equality testing</i></font><br>
<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> X> <font color="#008b00"><b>bool</b></font> <font color="#b02f60"><b>operator</b></font>==(<font color="#008b00"><b>const</b></font> Unique2DArray<X>& a, <font color="#008b00"><b>const</b></font> Unique2DArray<X>& b);<br>
<font color="#008b00"><b>template</b></font><<font color="#008b00"><b>class</b></font> X> <font color="#008b00"><b>bool</b></font> <font color="#b02f60"><b>operator</b></font>!=(<font color="#008b00"><b>const</b></font> Unique2DArray<X>& a, <font color="#008b00"><b>const</b></font> Unique2DArray<X>& b);<br>
<br>
<br>
<br>
</font></body>
</html>
|