Search

Sponsored Links

Meta

Categories

Archives

Recent Posts

RSS Feeds

27
Oct

Reference Counting

Related Blog Items

//============================================================================

// Contents:

//

// Definition of ReferenceCounter abstract base class that confers reference

// counting features for derived classes. Global functions are provided that

// make any class derived from ReferenceCounter satisfy the ReferenceCounter

// requirements defined for any parameter of countable_ptr.

//

//

//============================================================================

#ifndef _REFERENCECOUNTER_H_

#define _REFERENCECOUNTER_H_

#include // for size_t

// Description:

//

// Definition of ReferenceCounter abstract base class, which provides a mechanism

// for reference counting for use by derived classes.

//

// Notes:

//

// The ReferenceCounter class is intended to be used as a mix-in, but without

// any polymorphic behaviour; its principle aim is to provide implementation

// for derived classes (ie a mix-imp). As such, there are no virtual

// functions. Making the constructor protected has the effect of making the

// class abstract, and making the destructor ensures that unsafe destruction

// via a counability * is not possible.

//

// Copying is not considered meaningful for such a class, and so copying

// semantics have been disabled. This does mean that derived classes are

// required to define their own copying semantics if that is what is

// required, but this is expected of any good citizen class and so is not

// considered an unreasnable imposition.

//

// ReferenceCounter is not seen as a concrete property of derived classes, and

// therefore is not part of an object’s logical state. This means that all

// the operations in ReferenceCounter are const, and the count member is mutable.

// If your compiler does not support mutable you can remove it and modify the

// implementation to use const_cast where appropriate.

//

// Related member functions (method categories) are placed under their own

// commented access specifiers.

//—————————————————————————-

class ReferenceCounter{

public: // manipulation

void acquire() const;void release() const;size_t acquired() const;

protected: // construction and destruction

ReferenceCounter();

~ReferenceCounter();

private: // representation

mutable size_t count;ReferenceCounter(const ReferenceCounter &);ReferenceCounter &

operator=(const ReferenceCounter &);};

//—————————————————————————-

// Description:

//

// Declaration of global functions that allow classes derived from

// ReferenceCounter to satisfy countable requirements.

//

//—————————————————————————-

void acquire(const ReferenceCounter *);

void release(const ReferenceCounter *);size_t acquired(const ReferenceCounter *);

#endif

?

//============================================================================

// Contents:

//

// Definition of ReferenceCounter class member and global functions.

//

// History:

//

// Initial version created by Kevlin Henney, kevlin@acm.org, January 1998.

//

// Permissions:

//

// Copyright Kevlin Henney, 1998. All rights reserved.

//

// Permission to use, copy, modify, and distribute this software for any

// purpose is hereby granted without fee, provided that this copyright and

// permissions notice appear in all copies and derivatives, and that no

// charge may be made for the software and its documentation except to cover

// cost of distribution.

//

// This software is provided “as is” without express or implied warranty.

//

// Notes:

//

// All the functions defined are likely candidates for explicit inlining, but

// for the purposes of demonstration such an optimisation has not been deemed

// necessary.

//

// This code has been written to conform to standard C++ (in final draft

// status at the time of writing). It has been compiled successfully using

// the operational subset of standard features implemented by Microsoft

// Visual C++ 5.0.

//============================================================================

#include “ReferenceCounter.hpp”

//—————————————————————————-

// Description:

//

// Definition of ReferenceCounter class member functions.

//

// Notes:

//

// The implementation presented is not guaranteed to count safely across

// threads. Threadsafe counting can be implemented either by modifying the

// code below (eg under Win32 using InterlockedIncrement instead of ++), or

// by providing an additional class (eg threadsafe_ReferenceCounter). Note that

// in the case of modifying code in this file, not having inline functions

// supports a binary compatible change, ie the decision as to whether or not

// to use a threadsafe version can be deferred to link time.

//—————————————————————————-

void ReferenceCounter::acquire() const

{

++count;

}

void ReferenceCounter::release() const

{

–count;

}

size_t ReferenceCounter::acquired() const

{

return count;}

ReferenceCounter::ReferenceCounter()

: count(0)

{

}

ReferenceCounter::~ReferenceCounter()

{

}

//—————————————————————————-

// Description:

//

// Definition of ReferenceCounter global non-template functions.

//—————————————————————————-

void acquire(const ReferenceCounter *ptr){

if(ptr){

ptr->acquire();

}

}

void release(const ReferenceCounter *ptr){

if(ptr){

ptr->release();

}

}

size_t acquired(const ReferenceCounter *ptr){

return ptr ? ptr->acquired() : 0;}

Popularity: 4%

You need to log on to convert this article into PDF


Related Blog Items

No Comments

No comments yet.

Leave a comment

*
To prove you're a person (not a spam script), type the security word shown in the picture.
Anti-spam image