Sans Pareil Technologies, Inc.

Key To Your Business

RefCountedObject



RefCountedObject is intended for use with AutoPtr. This interface is taken from Poco. Since Arduino does not support threads, we use a simple 8 bit integer counter instead of an atomic counter.

    1: /*
    2: * RefCountedObject.h
    3: *
    4: * Definition of the RefCountedObject class.
    5: *
    6: * Copyright (c) 2004-2009, Applied Informatics Software Engineering GmbH.
    7: * and Contributors.
    8: *
    9: * Permission is hereby granted, free of charge, to any person or organization
   10: * obtaining a copy of the software and accompanying documentation covered by
   11: * this license (the "Software") to use, reproduce, display, distribute,
   12: * execute, and transmit the Software, and to prepare derivative works of the
   13: * Software, and to permit third-parties to whom the Software is furnished to
   14: * do so, all subject to the following:
   15: *
   16: * The copyright notices in the Software and this entire statement, including
   17: * the above license grant, this restriction and the following disclaimer,
   18: * must be included in all copies of the Software, in whole or in part, and
   19: * all derivative works of the Software, unless such copies or derivative
   20: * works are solely in the form of machine-executable object code generated by
   21: * a source language processor.
   22: *
   23: * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   24: * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   25: * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
   26: * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
   27: * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
   28: * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
   29: * DEALINGS IN THE SOFTWARE.
   30: */
   31:
   32: #ifndef SPT_REFCOUNTEDOBJECT
   33: #define SPT_REFCOUNTEDOBJECT
   34:
   35:
   36: namespace spt
   37: {
   38:   /// A base class for objects that employ
   39:   /// reference counting based garbage collection.
   40:   ///
   41:   /// Reference-counted objects inhibit construction
   42:   /// by copying and assignment.
   43:   class RefCountedObject
   44:   {
   45:   public:
   46:     /// Creates the RefCountedObject.
   47:     /// The initial reference count is one.
   48:     RefCountedObject() : refcount( 1 ) {}
   49:
   50:     /// Increments the object's reference count.
   51:     void duplicate() const { ++refcount; }
   52:
   53:     /// Decrements the object's reference count
   54:     /// and deletes the object if the count
   55:     /// reaches zero.
   56:     void release() const;
   57:
   58:     /// Returns the reference count.
   59:     int referenceCount() const { return refcount; }
   60:
   61:   protected:
   62:     /// Destroys the RefCountedObject.
   63:     virtual ~RefCountedObject() {}
   64:
   65:   private:
   66:     RefCountedObject( const RefCountedObject& );
   67:     RefCountedObject& operator = ( const RefCountedObject& );
   68:
   69:     mutable uint8_t refcount;
   70:   };
   71:
   72:
   73:   inline void RefCountedObject::release() const
   74:   {
   75:     if  ( --refcount == 0 ) delete this;
   76:   }
   77:
   78: } // namespace spt
   79:
   80:
   81: #endif // SPT_REFCOUNTEDOBJECT