include/am-refcounting.h
author David Anderson <dvander@alliedmods.net>
Fri Aug 23 22:36:50 2013 -0700 (2013-08-23)
changeset 4 f3275c49eb7b
parent 0 ba3d4abaf3f1
child 16 c513ef40773e
permissions -rw-r--r--
Make error reporters on SystemAllocatorPolicy public.
     1 // vim: set sts=8 ts=2 sw=2 tw=99 et:
     2 //
     3 // Copyright (C) 2013, David Anderson and AlliedModders LLC
     4 // All rights reserved.
     5 // 
     6 // Redistribution and use in source and binary forms, with or without
     7 // modification, are permitted provided that the following conditions are met:
     8 // 
     9 //  * Redistributions of source code must retain the above copyright notice, this
    10 //    list of conditions and the following disclaimer.
    11 //  * Redistributions in binary form must reproduce the above copyright notice,
    12 //    this list of conditions and the following disclaimer in the documentation
    13 //    and/or other materials provided with the distribution.
    14 //  * Neither the name of AlliedModders LLC nor the names of its contributors
    15 //    may be used to endorse or promote products derived from this software
    16 //    without specific prior written permission.
    17 //
    18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
    19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
    20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
    21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
    22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
    23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
    24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
    25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
    26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
    27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
    28 // POSSIBILITY OF SUCH DAMAGE.
    29 
    30 #ifndef _include_amtl_refcounting_h_
    31 #define _include_amtl_refcounting_h_
    32 
    33 #include <am-utility.h>
    34 
    35 namespace ke {
    36 
    37 template <typename T> class Ref;
    38 
    39 // Holds a refcounted T without addrefing it. This is similar to PassRef<>
    40 // below, but is intended only for freshly allocated objects which start
    41 // with reference count 1, and we don't want to add an extra ref just by
    42 // assigning to PassRef<> or Ref<>.
    43 template <typename T>
    44 class Newborn
    45 {
    46   public:
    47     Newborn(T *t)
    48       : thing_(t)
    49     {
    50     }
    51 
    52     T *release() const {
    53         return ReturnAndVoid(thing_);
    54     }
    55 
    56   private:
    57     mutable T *thing_;
    58 };
    59 
    60 // When returning a value, we'd rather not be needlessly changing the refcount,
    61 // so we have a special type to use for returns.
    62 template <typename T>
    63 class PassRef
    64 {
    65   public:
    66     PassRef(T *thing)
    67       : thing_(thing)
    68     {
    69         AddRef();
    70     }
    71     PassRef()
    72       : thing_(NULL)
    73     {
    74     }
    75 
    76     PassRef(const Newborn<T *> &other)
    77       : thing_(other.release())
    78     {
    79         // Don't addref, newborn means already addref'd.
    80     }
    81 
    82     template <typename S>
    83     inline PassRef(const Ref<S> &other);
    84 
    85     PassRef(const PassRef &other)
    86       : thing_(other.release())
    87     {
    88     }
    89     template <typename S>
    90     PassRef(const PassRef<S> &other)
    91       : thing_(other.release())
    92     {
    93     }
    94     ~PassRef()
    95     {
    96         Release();
    97     }
    98 
    99     operator T &() {
   100         return *thing_;
   101     }
   102     operator T *() const {
   103         return thing_;
   104     }
   105     T *operator ->() const {
   106         return operator *();
   107     }
   108     T *operator *() const {
   109         return thing_;
   110     }
   111     bool operator !() const {
   112         return !thing_;
   113     }
   114 
   115     T *release() const {
   116         return ReturnAndVoid(thing_);
   117     }
   118 
   119     template <typename S>
   120     PassRef &operator =(const PassRef<S> &other) {
   121         Release();
   122         thing_ = other.release();
   123         return *this;
   124     }
   125 
   126   private:
   127     // Disallowed operators.
   128     PassRef &operator =(T *other);
   129     PassRef &operator =(Newborn<T> &other);
   130 
   131     void AddRef() {
   132         if (thing_)
   133             thing_->AddRef();
   134     }
   135     void Release() {
   136         if (thing_)
   137             thing_->Release();
   138     }
   139 
   140   private:
   141     mutable T *thing_;
   142 };
   143 
   144 // Classes which are refcounted should inherit from this.
   145 template <typename T>
   146 class Refcounted
   147 {
   148   public:
   149     Refcounted()
   150       : refcount_(1)
   151     {
   152     }
   153 
   154     void AddRef() {
   155         refcount_++;
   156     }
   157     void Release() {
   158         assert(refcount_ > 0);
   159         if (--refcount_ == 0)
   160             delete static_cast<T *>(this);
   161     }
   162 
   163   protected:
   164     ~Refcounted() {
   165     }
   166 
   167   private:
   168     uintptr_t refcount_;
   169 };
   170 
   171 // Simple class for automatic refcounting.
   172 template <typename T>
   173 class Ref
   174 {
   175   public:
   176     Ref(T *thing)
   177       : thing_(thing)
   178     {
   179         AddRef();
   180     }
   181 
   182     Ref()
   183       : thing_(NULL)
   184     {
   185     }
   186 
   187     Ref(const Ref &other)
   188       : thing_(other.thing_)
   189     {
   190         AddRef();
   191     }
   192     template <typename S>
   193     Ref(const Ref<S> &other)
   194       : thing_(*other)
   195     {
   196         AddRef();
   197     }
   198     Ref(const PassRef<T> &other)
   199       : thing_(other.release())
   200     {
   201     }
   202     template <typename S>
   203     Ref(const PassRef<S> &other)
   204       : thing_(other.release())
   205     {
   206     }
   207     Ref(const Newborn<T> &other)
   208       : thing_(other.release())
   209     {
   210     }
   211     ~Ref()
   212     {
   213         Release();
   214     }
   215 
   216     T *operator ->() const {
   217         return operator *();
   218     }
   219     T *operator *() const {
   220         return thing_;
   221     }
   222     operator T *() {
   223         return thing_;
   224     }
   225     bool operator !() const {
   226         return !thing_;
   227     }
   228 
   229     template <typename S>
   230     Ref &operator =(S *thing) {
   231         Release();
   232         thing_ = thing;
   233         AddRef();
   234         return *this;
   235     }
   236     
   237     template <typename S>
   238     Ref &operator =(const PassRef<S> &other) {
   239         Release();
   240         thing_ = other.release();
   241         return *this;
   242     }
   243 
   244     template <typename S>
   245     Ref &operator =(const Newborn<S> &other) {
   246         Release();
   247         thing_ = other.release();
   248         return *this;
   249     }
   250 
   251     Ref &operator =(const Ref &other) {
   252         Release();
   253         thing_ = other.thing_;
   254         AddRef();
   255         return *this;
   256     }
   257 
   258   private:
   259     void AddRef() {
   260         if (thing_)
   261             thing_->AddRef();
   262     }
   263     void Release() {
   264         if (thing_)
   265             thing_->Release();
   266     }
   267 
   268   protected:
   269     T *thing_;
   270 };
   271 
   272 template <typename T> template <typename S>
   273 PassRef<T>::PassRef(const Ref<S> &other)
   274   : thing_(*other)
   275 {
   276     AddRef();
   277 }
   278 
   279 } // namespace ke
   280 
   281 #endif // _include_amtl_refcounting_h_
   282