Reference counting

Scott Meyers created a class for reference counted objects. The code is from the book. A class simply inherits from RCObject in order to get the property of reference counting.

/******************************************************************************
*                                                                             *
*        Code from Item 29 ("Reference Counting") of MORE EFFECTIVE C++       *
*                                                                             *
*                               Scott Meyers                                  * 
 
*                                                                             *
*                             November 15, 1997                               *
*                                                                             *
*            Copyright 1996 (c) Addison-Wesley Publishing Company             *
*       You are free to use this code for non-commercial purposes only.       *
*                                                                             *
* This page contains the code for the classes and class templates making up   *
* the Reference Counting Item of MORE EFFECTIVE C++.  To use this code,       *
* either copy this page and paste it into a C++ source file or save the       *
* entire page as text into a C++ source file.  Don't save the HTML source     *
* and expect that to compile :-)                                              *
*                                                                             *
* Each class or template in this file follows a block comment that shows the  *
* corresponding pages in the book.  This page also contains a main function   *
* that performs a VERY limited test of the code in this file.  You can        *
* compile the code in this file as a stand-alone program, and you should get  *
* this output:                                                                *
*                                                                             *
*     String with no changes.                                                 *
*     String with    changes.                                                 *
*     10                                                                      *
*     -1                                                                      *
*                                                                             *
* The code here reflects all changes made to date in response to bug reports  *
* from readers of the book.  (To see a complete list of known bugs in MORE    *
* EFFECTIVE C++, as well as whether they've been fixed yet, visit the         *
* More Effective C++ Errata List.)  If you find any additional bugs, please   *
* send them to me.                                                            *
******************************************************************************/

/******************************************************************************
*                       Class RCObject (from pp. 204-205)                     *
******************************************************************************/
class RCObject
{                       // base class for reference-
public:                                // counted objects
    void addReference();
    void removeReference();
    void markUnshareable();
    bool isShareable() const;
    bool isShared() const;

protected:
    RCObject();
    RCObject(const RCObject& rhs);
    RCObject& operator=(const RCObject& rhs);
    virtual ~RCObject() = 0;

private:
    int refCount;
    bool shareable;
};

inline RCObject::RCObject()
        : refCount(0), shareable(true)
{}

inline RCObject::RCObject(const RCObject&)
        : refCount(0), shareable(true)
{}

inline RCObject& RCObject::operator=(const RCObject&)
{
    return *this;
}

inline RCObject::~RCObject()
{}

inline void RCObject::addReference()
{
    ++refCount;
}

inline void RCObject::removeReference()
{
    if (--refCount == 0)
        delete this;
}

inline void RCObject::markUnshareable()
{
    shareable = false;
}

inline bool RCObject::isShareable() const
{
    return shareable;
}

inline bool RCObject::isShared() const
{
    return refCount > 1;
}

-- ZsoltMolnar - 19 Oct 2007

Edit | Attach | Watch | Print version | History: r1 | Backlinks | Raw View | WYSIWYG | More topic actions
Topic revision: r1 - 2007-10-19 - ZsoltMolnar
 
    • Cern Search Icon Cern Search
    • TWiki Search Icon TWiki Search
    • Google Search Icon Google Search

    Sandbox All webs login

This site is powered by the TWiki collaboration platform Powered by PerlCopyright & 2008-2020 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
or Ideas, requests, problems regarding TWiki? use Discourse or Send feedback