123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164 |
- #ifndef API_SCOPED_REFPTR_H_
- #define API_SCOPED_REFPTR_H_
- #include <memory>
- #include <utility>
- namespace rtc {
- template <class T>
- class scoped_refptr {
- public:
- typedef T element_type;
- scoped_refptr() : ptr_(nullptr) {}
- scoped_refptr(T* p) : ptr_(p) {
- if (ptr_)
- ptr_->AddRef();
- }
- scoped_refptr(const scoped_refptr<T>& r) : ptr_(r.ptr_) {
- if (ptr_)
- ptr_->AddRef();
- }
- template <typename U>
- scoped_refptr(const scoped_refptr<U>& r) : ptr_(r.get()) {
- if (ptr_)
- ptr_->AddRef();
- }
-
- scoped_refptr(scoped_refptr<T>&& r) noexcept : ptr_(r.release()) {}
- template <typename U>
- scoped_refptr(scoped_refptr<U>&& r) noexcept : ptr_(r.release()) {}
- ~scoped_refptr() {
- if (ptr_)
- ptr_->Release();
- }
- T* get() const { return ptr_; }
- operator T*() const { return ptr_; }
- T* operator->() const { return ptr_; }
-
-
-
-
-
- T* release() {
- T* retVal = ptr_;
- ptr_ = nullptr;
- return retVal;
- }
- scoped_refptr<T>& operator=(T* p) {
-
- if (p)
- p->AddRef();
- if (ptr_)
- ptr_->Release();
- ptr_ = p;
- return *this;
- }
- scoped_refptr<T>& operator=(const scoped_refptr<T>& r) {
- return *this = r.ptr_;
- }
- template <typename U>
- scoped_refptr<T>& operator=(const scoped_refptr<U>& r) {
- return *this = r.get();
- }
- scoped_refptr<T>& operator=(scoped_refptr<T>&& r) noexcept {
- scoped_refptr<T>(std::move(r)).swap(*this);
- return *this;
- }
- template <typename U>
- scoped_refptr<T>& operator=(scoped_refptr<U>&& r) noexcept {
- scoped_refptr<T>(std::move(r)).swap(*this);
- return *this;
- }
- void swap(T** pp) noexcept {
- T* p = ptr_;
- ptr_ = *pp;
- *pp = p;
- }
- void swap(scoped_refptr<T>& r) noexcept { swap(&r.ptr_); }
- protected:
- T* ptr_;
- };
- }
- #endif
|