| OLD | NEW | 
|    1 /* |    1 /* | 
|    2  * Copyright 2006 The Android Open Source Project |    2  * Copyright 2006 The Android Open Source Project | 
|    3  * |    3  * | 
|    4  * Use of this source code is governed by a BSD-style license that can be |    4  * Use of this source code is governed by a BSD-style license that can be | 
|    5  * found in the LICENSE file. |    5  * found in the LICENSE file. | 
|    6  */ |    6  */ | 
|    7  |    7  | 
|    8 #ifndef SkRefCnt_DEFINED |    8 #ifndef SkRefCnt_DEFINED | 
|    9 #define SkRefCnt_DEFINED |    9 #define SkRefCnt_DEFINED | 
|   10  |   10  | 
| (...skipping 209 matching lines...) Expand 10 before | Expand all | Expand 10 after  Loading... | 
|  220             SkDEBUGCODE(fRefCnt = 1;)  // restore the 1 for our destructor's ass
     ert |  220             SkDEBUGCODE(fRefCnt = 1;)  // restore the 1 for our destructor's ass
     ert | 
|  221             delete (const Derived*)this; |  221             delete (const Derived*)this; | 
|  222         } |  222         } | 
|  223     } |  223     } | 
|  224     void  deref() const { this->unref(); } |  224     void  deref() const { this->unref(); } | 
|  225  |  225  | 
|  226 private: |  226 private: | 
|  227     mutable int32_t fRefCnt; |  227     mutable int32_t fRefCnt; | 
|  228 }; |  228 }; | 
|  229  |  229  | 
 |  230 ////////////////////////////////////////////////////////////////////////////////
     /////////////////// | 
 |  231  | 
 |  232 /** | 
 |  233  *  Shared pointer class to wrap classes that support a ref()/unref() interface. | 
 |  234  * | 
 |  235  *  This can be used for classes inheriting from SkRefCnt, but it also works for
      other | 
 |  236  *  classes that match the interface, but have different internal choices: e.g. 
     the hosted class | 
 |  237  *  may have its ref/unref be thread-safe, but that is not assumed/imposed by sk
     _sp. | 
 |  238  */ | 
 |  239 template <typename T> class sk_sp { | 
 |  240 public: | 
 |  241     sk_sp() : fPtr(nullptr) {} | 
 |  242     sk_sp(std::nullptr_t) : fPtr(nullptr) {} | 
 |  243  | 
 |  244     /** | 
 |  245      *  Shares the underlying object by calling ref(), so that both the argument
      and the newly | 
 |  246      *  created sk_sp both have a reference to it. | 
 |  247      */ | 
 |  248     sk_sp(const sk_sp<T>& that) : fPtr(SkSafeRef(that.get())) {} | 
 |  249  | 
 |  250     /** | 
 |  251      *  Move the underlying object from the argument to the newly created sk_sp.
      Afterwards only | 
 |  252      *  the new sk_sp will have a reference to the object, and the argument will
      point to null. | 
 |  253      *  No call to ref() or unref() will be made. | 
 |  254      */ | 
 |  255     sk_sp(sk_sp<T>&& that) : fPtr(that.release()) {} | 
 |  256  | 
 |  257     /** | 
 |  258      *  Adopt the bare pointer into the newly created sk_sp. | 
 |  259      *  No call to ref() or unref() will be made. | 
 |  260      */ | 
 |  261     explicit sk_sp(T* obj) : fPtr(obj) {} | 
 |  262  | 
 |  263     /** | 
 |  264      *  Calls unref() on the underlying object pointer. | 
 |  265      */ | 
 |  266     ~sk_sp() { | 
 |  267         SkSafeUnref(fPtr); | 
 |  268     } | 
 |  269  | 
 |  270     sk_sp<T>& operator=(std::nullptr_t) { this->reset(); } | 
 |  271  | 
 |  272     /** | 
 |  273      *  Shares the underlying object referenced by the argument by calling ref()
      on it. If this | 
 |  274      *  sk_sp previously had a reference to an object (i.e. not null) it will ca
     ll unref() on that | 
 |  275      *  object. | 
 |  276      */ | 
 |  277     sk_sp<T>& operator=(const sk_sp<T>& that) { | 
 |  278         this->reset(SkSafeRef(that.get())); | 
 |  279         return *this; | 
 |  280     } | 
 |  281  | 
 |  282     /** | 
 |  283      *  Move the underlying object from the argument to the sk_sp. If the sk_sp 
     previously held | 
 |  284      *  a reference to another object, unref() will be called on that object. No
      call to ref() | 
 |  285      *  will be made. | 
 |  286      */ | 
 |  287     sk_sp<T>& operator=(sk_sp<T>&& that) { | 
 |  288         this->reset(that.release()); | 
 |  289         return *this; | 
 |  290     } | 
 |  291  | 
 |  292     bool operator==(std::nullptr_t) const { return this->get() == nullptr; } | 
 |  293     bool operator!=(std::nullptr_t) const { return this->get() != nullptr; } | 
 |  294  | 
 |  295     bool operator==(const sk_sp<T>& that) const { return this->get() == that.get
     (); } | 
 |  296     bool operator!=(const sk_sp<T>& that) const { return this->get() != that.get
     (); } | 
 |  297  | 
 |  298     explicit operator bool() const { return this->get() != nullptr; } | 
 |  299  | 
 |  300     T* get() const { return fPtr; } | 
 |  301     T* operator->() const { return fPtr; } | 
 |  302  | 
 |  303     /** | 
 |  304      *  Adopt the new bare pointer, and call unref() on any previously held obje
     ct (if not null). | 
 |  305      *  No call to ref() will be made. | 
 |  306      */ | 
 |  307     void reset(T* ptr = nullptr) { | 
 |  308         if (fPtr != ptr) { | 
 |  309             SkSafeUnref(fPtr); | 
 |  310             fPtr = ptr; | 
 |  311         } | 
 |  312     } | 
 |  313  | 
 |  314     /** | 
 |  315      *  Return the bare pointer, and set the internal object pointer to nullptr. | 
 |  316      *  The caller must assume ownership of the object, and manage its reference
      count directly. | 
 |  317      *  No call to unref() will be made. | 
 |  318      */ | 
 |  319     T* SK_WARN_UNUSED_RESULT release() { | 
 |  320         T* ptr = fPtr; | 
 |  321         fPtr = nullptr; | 
 |  322         return ptr; | 
 |  323     } | 
 |  324  | 
 |  325 private: | 
 |  326     T*  fPtr; | 
 |  327 }; | 
 |  328  | 
|  230 #endif |  329 #endif | 
| OLD | NEW |