//============================================================================ // Contents: // // Definition of countability 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 "countability.hpp" //---------------------------------------------------------------------------- // Description: // // Definition of countability 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_countability). 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 countability::acquire() const { ++count; } void countability::release() const { --count; } size_t countability::acquired() const { return count; } countability::countability() : count(0) { } countability::~countability() { } //---------------------------------------------------------------------------- // Description: // // Definition of countability global non-template functions. //---------------------------------------------------------------------------- void acquire(const countability *ptr) { if(ptr) { ptr->acquire(); } } void release(const countability *ptr) { if(ptr) { ptr->release(); } } size_t acquired(const countability *ptr) { return ptr ? ptr->acquired() : 0; }