include/symbolism/refcounter.h

00001 #pragma once
00002 #include <iostream>
00003 
00004 namespace symbolism {
00005 namespace utility {
00006 
00007 template <class pData>
00008 class refcounted:
00009 {
00010     public:
00011         typedef size_t refcount_t;
00012 
00013         refcounted();
00014         refcounted(const refcounted& f);
00015         refcounted& operator= (const refcounted& f);
00016         ~refcounted();
00017         make_mutable();
00018 
00019     private:
00020         pData* pdata;
00021 };
00022 
00023 inline refcounted::refcounted(pData* data)
00024 : data_(data)
00025 {
00026     assert(data != NULL);
00027 }
00028 
00029 inline refcounted::refcounted(const refcounted<pData>& f)
00030 : data_(f.data_)
00031 {
00032     ++data_->count_;
00033 }
00034 
00035 inline refcounted& refcounted::operator= (const refcounted<pData>& f)
00036 {
00037     pData* const old = data_;
00038     data_ = f.data_;
00039     ++data_->count_;
00040     if (--old->count_ == 0) delete old;
00041     return *this;
00042 }
00043 
00044 inline refcounted::~refcounted()
00045 {
00046     if (--data_->count_ == 0)
00047     {
00048         delete data_;
00049     }
00050 }
00051 
00052 inline void refcounted::make_mutable()
00053 {
00054     if (data_->count_ > 1)
00055     {
00056         Data* d = new pData(*pdata);
00057         --pdata->refcount;
00058         data_ = d;
00059     }
00060     assert(data_->count_ == 1);
00061 }
00062 
00063 }}

Copyright © 2007-2008 Remco Bloemen.

Generated on Tue Jan 22 17:35:31 2008 for symbolism by doxygen 1.5.4

Hosted by SourceForge.net Logo