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 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817
|
/* ScummVM - Graphic Adventure Engine
*
* ScummVM is the legal property of its developers, whose names
* are too numerous to list here. Please refer to the COPYRIGHT
* file distributed with this source distribution.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
#ifndef COMMON_PTR_H
#define COMMON_PTR_H
#include "common/scummsys.h"
#include "common/noncopyable.h"
#include "common/safe-bool.h"
#include "common/types.h"
/* For nullptr_t */
#include <cstddef>
namespace Common {
/**
* @defgroup common_ptr Pointers
* @ingroup common
*
* @brief API and templates for pointers.
* @{
*/
class BasePtrTrackerInternal {
public:
typedef int RefValue;
BasePtrTrackerInternal() : _weakRefCount(1), _strongRefCount(1) {}
virtual ~BasePtrTrackerInternal() {}
void incWeak() {
_weakRefCount++;
}
void decWeak() {
if (--_weakRefCount == 0)
delete this;
}
void incStrong() {
_strongRefCount++;
}
void decStrong() {
if (--_strongRefCount == 0) {
destructObject();
decWeak();
}
}
bool isAlive() const {
return _strongRefCount > 0;
}
RefValue getStrongCount() const {
return _strongRefCount;
}
protected:
virtual void destructObject() = 0;
private:
RefValue _weakRefCount; // Weak ref count + 1 if object ref count > 0
RefValue _strongRefCount;
};
template<class T>
class BasePtrTrackerImpl : public BasePtrTrackerInternal {
public:
BasePtrTrackerImpl(T *ptr) : _ptr(ptr) {}
protected:
void destructObject() override {
STATIC_ASSERT(sizeof(T) > 0, SharedPtr_cannot_delete_incomplete_type);
delete _ptr;
}
T *_ptr;
};
template<class T, class DL>
class BasePtrTrackerDeletionImpl : public BasePtrTrackerInternal {
public:
BasePtrTrackerDeletionImpl(T *ptr, DL d) : _ptr(ptr), _deleter(d) {}
private:
void destructObject() override {
_deleter(_ptr);
}
T *_ptr;
DL _deleter;
};
template<class T>
class WeakPtr;
/**
* A simple shared pointer implementation modelled after boost.
*
* This object keeps track of the assigned pointer and automatically
* frees it when no more SharedPtr references to it exist.
*
* To achieve that the object implements an internal reference counting.
* Thus you should try to avoid using the plain pointer after assigning
* it to a SharedPtr object for the first time. If you still use the
* plain pointer be sure you do not delete it on your own. You may also
* not use the plain pointer to create a new SharedPtr object, since that
* would result in a double deletion of the pointer sooner or later.
*
* Example creation:
* Common::SharedPtr<int> pointer(new int(1));
* would create a pointer to int. Later on usage via *pointer is the same
* as for a normal pointer. If you need to access the plain pointer value
* itself later on use the get method. The class also supplies a operator
* ->, which does the same as the -> operator on a normal pointer.
*
* Be sure you are using new to initialize the pointer you want to manage.
* If you do not use new for allocating, you have to supply a deleter as
* second parameter when creating a SharedPtr object. The deleter has to
* implement operator() which takes the pointer it should free as argument.
*
* Note that you have to specify the type itself not the pointer type as
* template parameter.
*
* When creating a SharedPtr object from a normal pointer you need a real
* definition of the type you want SharedPtr to manage, a simple forward
* definition is not enough.
*
* The class has implicit upcast support, so if you got a class B derived
* from class A, you can assign a pointer to B without any problems to a
* SharedPtr object with template parameter A. The very same applies to
* assignment of a SharedPtr<B> object to a SharedPtr<A> object.
*
* There are also operators != and == to compare two SharedPtr objects
* with compatible pointers. Comparison between a SharedPtr object and
* a plain pointer is only possible via SharedPtr::get.
*/
template<class T>
class SharedPtr : public SafeBool<SharedPtr<T> > {
template<class T2>
friend class WeakPtr;
template<class T2>
friend class SharedPtr;
public:
// Invariant: If _tracker is non-null, then the object is alive
typedef T *PointerType;
typedef T &ReferenceType;
typedef BasePtrTrackerInternal::RefValue RefValue;
SharedPtr() : _pointer(nullptr), _tracker(nullptr) {
}
SharedPtr(std::nullptr_t) : _pointer(nullptr), _tracker(nullptr) {
}
~SharedPtr() {
if (_tracker)
_tracker->decStrong();
}
template<class T2>
explicit SharedPtr(T2 *p) : _pointer(p), _tracker(p ? (new BasePtrTrackerImpl<T2>(p)) : nullptr) {
}
template<class T2, class DL>
SharedPtr(T2 *p, DL d) : _pointer(p), _tracker(p ? (new BasePtrTrackerDeletionImpl<T2, DL>(p, d)) : nullptr) {
}
SharedPtr(const SharedPtr<T> &r) : _pointer(r._pointer), _tracker(r._tracker) {
if (_tracker)
_tracker->incStrong();
}
template<class T2>
SharedPtr(const SharedPtr<T2> &r) : _pointer(r._pointer), _tracker(r._tracker) {
if (_tracker)
_tracker->incStrong();
}
template<class T2>
explicit SharedPtr(const WeakPtr<T2> &r) : _pointer(nullptr), _tracker(nullptr) {
if (r._tracker && r._tracker->isAlive()) {
_pointer = r._pointer;
_tracker = r._tracker;
_tracker->incStrong();
}
}
SharedPtr &operator=(const SharedPtr &r) {
reset(r);
return *this;
}
template<class T2>
SharedPtr &operator=(const SharedPtr<T2> &r) {
reset(r);
return *this;
}
T &operator*() const { assert(_pointer); return *_pointer; }
T *operator->() const { assert(_pointer); return _pointer; }
/**
* Returns the plain pointer value. Be sure you know what you
* do if you are continuing to use that pointer.
*
* @return the pointer the SharedPtr object manages
*/
PointerType get() const { return _pointer; }
template<class T2>
bool operator==(const SharedPtr<T2> &r) const {
return _pointer == r.get();
}
template<class T2>
bool operator!=(const SharedPtr<T2> &r) const {
return _pointer != r.get();
}
bool operator==(std::nullptr_t) const {
return _pointer == nullptr;
}
bool operator!=(std::nullptr_t) const {
return _pointer != nullptr;
}
/**
* Implicit conversion operator to bool for convenience, to make
* checks like "if (sharedPtr) ..." possible.
*/
bool operator_bool() const {
return _pointer != nullptr;
}
/**
* Returns the number of strong references to the object.
*/
int refCount() const {
if (_tracker == nullptr)
return 0;
return _tracker->getStrongCount();
}
/**
* Checks if the object is the only object referring
* to the assigned pointer. This should just be used for
* debugging purposes.
*/
bool unique() const {
return refCount() == 1;
}
/**
* Resets the object to a NULL pointer.
*/
void reset() {
if (_tracker)
_tracker->decStrong();
_tracker = nullptr;
_pointer = nullptr;
}
/**
* Resets the object to the specified shared pointer
*/
template<class T2>
void reset(const SharedPtr<T2> &r) {
BasePtrTrackerInternal *oldTracker = _tracker;
_pointer = r._pointer;
_tracker = r._tracker;
if (_tracker)
_tracker->incStrong();
if (oldTracker)
oldTracker->decStrong();
}
/**
* Resets the object to the specified weak pointer
*/
template<class T2>
void reset(const WeakPtr<T2> &r) {
BasePtrTrackerInternal *oldTracker = _tracker;
if (r._tracker && r._tracker->isAlive()) {
_tracker = r._tracker;
_pointer = r._pointer;
_tracker->incStrong();
} else {
_tracker = nullptr;
_pointer = nullptr;
}
if (oldTracker)
oldTracker->decStrong();
}
/**
* Resets the object to the specified pointer
*/
void reset(T *ptr) {
if (_tracker)
_tracker->decStrong();
_pointer = ptr;
_tracker = new BasePtrTrackerImpl<T>(ptr);
}
/**
* Performs the equivalent of static_cast to a new pointer type
*/
template<class T2>
SharedPtr<T2> staticCast() const {
return SharedPtr<T2>(static_cast<T2 *>(_pointer), _tracker);
}
/**
* Performs the equivalent of dynamic_cast to a new pointer type
*/
template<class T2>
SharedPtr<T2> dynamicCast() const {
return SharedPtr<T2>(dynamic_cast<T2 *>(_pointer), _tracker);
}
/**
* Performs the equivalent of const_cast to a new pointer type
*/
template<class T2>
SharedPtr<T2> constCast() const {
return SharedPtr<T2>(const_cast<T2 *>(_pointer), _tracker);
}
/**
* Performs the equivalent of const_cast to a new pointer type
*/
template<class T2>
SharedPtr<T2> reinterpretCast() const {
return SharedPtr<T2>(reinterpret_cast<T2 *>(_pointer), _tracker);
}
private:
SharedPtr(T *pointer, BasePtrTrackerInternal *tracker) : _pointer(pointer), _tracker(tracker) {
if (tracker)
tracker->incStrong();
}
T *_pointer;
BasePtrTrackerInternal *_tracker;
};
/**
* Implements a smart pointer that holds a non-owning ("weak") reference to
* a pointer. It needs to be converted to a SharedPtr to access it.
*/
template<class T>
class WeakPtr {
template<class T2>
friend class WeakPtr;
template<class T2>
friend class SharedPtr;
public:
WeakPtr() : _pointer(nullptr), _tracker(nullptr) {
}
WeakPtr(std::nullptr_t) : _pointer(nullptr), _tracker(nullptr) {
}
WeakPtr(const WeakPtr<T> &r) : _pointer(r._pointer), _tracker(r._tracker) {
if (_tracker)
_tracker->incWeak();
}
~WeakPtr() {
if (_tracker)
_tracker->decWeak();
}
template<class T2>
WeakPtr(const WeakPtr<T2> &r) : _pointer(r._pointer), _tracker(r._tracker) {
if (_tracker)
_tracker->incWeak();
}
template<class T2>
WeakPtr(const SharedPtr<T2> &r) : _pointer(r._pointer), _tracker(r._tracker) {
if (_tracker)
_tracker->incWeak();
}
/**
* Performs the equivalent of static_cast to a new pointer type
*/
template<class T2>
WeakPtr<T2> staticCast() const {
return WeakPtr<T2>(expired() ? nullptr : static_cast<T2 *>(_pointer), _tracker);
}
/**
* Performs the equivalent of dynamic_cast to a new pointer type
*/
template<class T2>
WeakPtr<T2> dynamicCast() const {
return WeakPtr<T2>(expired() ? nullptr : dynamic_cast<T2 *>(_pointer), _tracker);
}
/**
* Performs the equivalent of const_cast to a new pointer type
*/
template<class T2>
WeakPtr<T2> constCast() const {
return WeakPtr<T2>(expired() ? nullptr : const_cast<T2 *>(_pointer), _tracker);
}
/**
* Performs the equivalent of const_cast to a new pointer type
*/
template<class T2>
WeakPtr<T2> reinterpretCast() const {
return WeakPtr<T2>(expired() ? nullptr : reinterpret_cast<T2 *>(_pointer), _tracker);
}
/**
* Creates a SharedPtr that manages the referenced object
*/
SharedPtr<T> lock() const {
return SharedPtr<T>(*this);
}
/**
* Returns the number of strong references to the object.
*/
int refCount() const {
if (_tracker == nullptr)
return 0;
return _tracker->getStrongCount();
}
/**
* Returns whether the referenced object isn't valid
*/
bool expired() const {
return _tracker == nullptr || _tracker->getStrongCount() == 0;
}
/**
* Returns whether this precedes another weak pointer in owner-based order
*/
template<class T2>
bool owner_before(const WeakPtr<T2>& other) const {
return _tracker < other._tracker;
}
/**
* Returns whether this precedes a shared pointer in owner-based order
*/
template<class T2>
bool owner_before(const SharedPtr<T2> &other) const {
return _tracker < other._tracker;
}
WeakPtr<T> &operator=(const WeakPtr<T> &r) {
reset(r);
return *this;
}
template<class T2>
WeakPtr<T> &operator=(const WeakPtr<T2> &r) {
reset(r);
return *this;
}
template<class T2>
WeakPtr<T> &operator=(const SharedPtr<T2> &r) {
reset(r);
return *this;
}
/**
* Resets the object to a NULL pointer.
*/
void reset() {
if (_tracker)
_tracker->decWeak();
_tracker = nullptr;
_pointer = nullptr;
}
/**
* Resets the object to the specified shared pointer
*/
template<class T2>
void reset(const SharedPtr<T2> &r) {
BasePtrTrackerInternal *oldTracker = _tracker;
_pointer = r._pointer;
_tracker = r._tracker;
if (_tracker)
_tracker->incWeak();
if (oldTracker)
oldTracker->decWeak();
}
/**
* Resets the object to the specified weak pointer
*/
template<class T2>
void reset(const WeakPtr<T2> &r) {
BasePtrTrackerInternal *oldTracker = _tracker;
_pointer = r._pointer;
_tracker = r._tracker;
if (_tracker)
_tracker->incWeak();
if (oldTracker)
oldTracker->decWeak();
}
private:
WeakPtr(T *pointer, BasePtrTrackerInternal *tracker) : _pointer(pointer), _tracker(tracker) {
if (tracker)
tracker->incWeak();
}
T *_pointer;
BasePtrTrackerInternal *_tracker;
};
template <typename T>
struct DefaultDeleter {
inline void operator()(T *object) {
STATIC_ASSERT(sizeof(T) > 0, cannot_delete_incomplete_type);
delete object;
}
};
template <typename T>
struct ArrayDeleter {
inline void operator()(T *object) {
STATIC_ASSERT(sizeof(T) > 0, cannot_delete_incomplete_type);
delete[] object;
}
};
template<typename T, class DL = DefaultDeleter<T> >
class ScopedPtr : private NonCopyable, public SafeBool<ScopedPtr<T, DL> > {
template<class T2, class DL2>
friend class ScopedPtr;
public:
typedef T ValueType;
typedef T *PointerType;
typedef T &ReferenceType;
explicit ScopedPtr(PointerType o = nullptr) : _pointer(o) {}
ScopedPtr(std::nullptr_t) : _pointer(nullptr) {}
/**
* Move constructor
*/
template<class T2>
ScopedPtr(ScopedPtr<T2> &&o) : _pointer(o._pointer) {
o._pointer = nullptr;
}
ReferenceType operator*() const { return *_pointer; }
PointerType operator->() const { return _pointer; }
/**
* Implicit conversion operator to bool for convenience, to make
* checks like "if (scopedPtr) ..." possible.
*/
bool operator_bool() const { return _pointer != nullptr; }
~ScopedPtr() {
DL()(_pointer);
}
/**
* Resets the pointer with the new value. Old object will be destroyed
*/
void reset(PointerType o = nullptr) {
DL()(_pointer);
_pointer = o;
}
/**
* Affectation with nullptr
*/
ScopedPtr &operator=(std::nullptr_t) {
reset(nullptr);
}
/**
* Replaces the ScopedPtr with another scoped ScopedPtr.
*/
template<class T2>
ScopedPtr &operator=(ScopedPtr<T2> &&other) {
PointerType oldPointer = _pointer;
_pointer = other._pointer;
other._pointer = nullptr;
DL()(oldPointer);
return *this;
}
/**
* Returns the plain pointer value.
*
* @return the pointer the ScopedPtr manages
*/
PointerType get() const { return _pointer; }
/**
* Returns the plain pointer value and releases ScopedPtr.
* After release() call you need to delete object yourself
*
* @return the pointer the ScopedPtr manages
*/
PointerType release() {
PointerType r = _pointer;
_pointer = nullptr;
return r;
}
private:
PointerType _pointer;
};
template<typename T, class DL = DefaultDeleter<T> >
class DisposablePtr : private NonCopyable, public SafeBool<DisposablePtr<T, DL> > {
public:
typedef T ValueType;
typedef T *PointerType;
typedef T &ReferenceType;
explicit DisposablePtr(PointerType o, DisposeAfterUse::Flag dispose) : _pointer(o), _dispose(dispose), _shared() {}
explicit DisposablePtr(SharedPtr<T> o) : _pointer(o.get()), _dispose(DisposeAfterUse::NO), _shared(o) {}
DisposablePtr(DisposablePtr<T, DL>&& o) : _pointer(o._pointer), _dispose(o._dispose), _shared(o._shared) {
o._pointer = nullptr;
o._dispose = DisposeAfterUse::NO;
o._shared.reset();
}
~DisposablePtr() {
if (_dispose) DL()(_pointer);
}
ReferenceType operator*() const { return *_pointer; }
PointerType operator->() const { return _pointer; }
/**
* Implicit conversion operator to bool for convenience, to make
* checks like "if (scopedPtr) ..." possible.
*/
bool operator_bool() const { return _pointer != nullptr; }
/**
* Resets the pointer with the new value. Old object will be destroyed
*/
void reset(PointerType o, DisposeAfterUse::Flag dispose) {
if (_dispose) DL()(_pointer);
_pointer = o;
_dispose = dispose;
_shared.reset();
}
/**
* Clears the pointer. Old object will be destroyed
*/
void reset() {
reset(nullptr, DisposeAfterUse::NO);
}
template <class T2>
bool isDynamicallyCastable() {
return dynamic_cast<T2 *>(_pointer) != nullptr;
}
/* Destroys the smart pointer while returning a pointer to
assign to a new object.
*/
template <class T2, class DL2 = DefaultDeleter<T2> >
DisposablePtr<T2, DL2> moveAndDynamicCast() {
DisposablePtr<T2, DL2> ret(nullptr, DisposeAfterUse::NO);
ret._pointer = dynamic_cast<T2 *>(_pointer);
ret._dispose = _dispose;
ret._shared = _shared.template dynamicCast<T2>();
_pointer = nullptr;
_dispose = DisposeAfterUse::NO;
_shared.reset();
return ret;
}
/**
* Returns the plain pointer value.
*
* @return the pointer the DisposablePtr manages
*/
PointerType get() const { return _pointer; }
template <class T2, class DL2>
friend class DisposablePtr;
private:
DisposablePtr() : _pointer(nullptr), _dispose(DisposeAfterUse::NO), _shared() {}
PointerType _pointer;
DisposeAfterUse::Flag _dispose;
SharedPtr<T> _shared;
bool _isvalid;
};
/**
* UnalignedPtr: Allows pointers to and access of memory addresses where the underlying data
* doesn't have proper alignment.
*/
#if defined(_MSC_VER) && (defined(_M_X64) || defined(_M_ARM) || defined(_M_ARM64))
template<class T>
class UnalignedPtr {
public:
UnalignedPtr();
UnalignedPtr(__unaligned T *ptr);
T load() const;
void store(const T &value) const;
private:
__unaligned T *_ptr;
};
template<class T>
UnalignedPtr<T>::UnalignedPtr() : _ptr(nullptr) {
}
template<class T>
UnalignedPtr<T>::UnalignedPtr(__unaligned T *ptr) : _ptr(ptr) {
}
template<class T>
T UnalignedPtr<T>::load() const {
return *_ptr;
}
template<class T>
void UnalignedPtr<T>::store(const T &value) const {
*_ptr = value;
}
#else
template<class T>
class UnalignedPtr {
public:
UnalignedPtr();
UnalignedPtr(T *ptr);
T load() const;
void store(const T &value) const;
private:
void *_ptr;
};
template<class T>
UnalignedPtr<T>::UnalignedPtr() : _ptr(nullptr) {
}
template<class T>
UnalignedPtr<T>::UnalignedPtr(T *ptr) : _ptr(ptr) {
}
template<class T>
T UnalignedPtr<T>::load() const {
T result;
memcpy(&result, _ptr, sizeof(T));
return result;
}
template<class T>
void UnalignedPtr<T>::store(const T &value) const {
memcpy(_ptr, &value, sizeof(T));
}
#endif
/** @} */
} // End of namespace Common
#endif
|