reference_count.h

Go to the documentation of this file.
00001 00004 /* 00005 * Copyright © 2000, 2001 Sofus Mortensen, Paul Hollingsworth 00006 * 00007 * This material is provided "as is", with absolutely no warranty 00008 * expressed or implied. Any use is at your own risk. Permission to 00009 * use or copy this software for any purpose is hereby granted without 00010 * fee, provided the above notices are retained on all copies. 00011 * Permission to modify the code and to distribute modified code is 00012 * granted, provided the above notices are retained, and a notice that 00013 * the code was modified is included with the above copyright notice. 00014 * 00015 * This header is part of comet. 00016 * http://www.lambdasoft.dk/comet 00017 */ 00018 00019 #ifndef COMET_REFERENCE_COUNT_H 00020 #define COMET_REFERENCE_COUNT_H 00021 00022 #include <comet/config.h> 00023 #include <algorithm> 00024 00025 namespace comet { 00029 00030 class reference_count 00031 { 00032 public: 00033 reference_count() : rc_(0) {}; 00034 explicit reference_count(size_t x) : rc_(reinterpret_cast<size_t*>(x)) {}; 00035 00036 reference_count(const reference_count& x) : rc_(x.rc_) {}; 00037 00038 enum { FLAGVALUE = 1 }; 00039 bool is_flag() const { 00040 return reinterpret_cast<size_t>(rc_) == 1; 00041 } 00042 00043 // is_null 00047 bool is_null() const { 00048 return rc_ == 0; 00049 } 00050 00052 size_t operator++() { 00053 if (!rc_) { 00054 rc_ = new size_t(1); 00055 } 00056 return ++*rc_; 00057 } 00058 00060 size_t operator--() { 00061 if (rc_) { 00062 if (--*rc_ == 0) { 00063 delete rc_; 00064 rc_ = 0; 00065 return 0; 00066 } 00067 return *rc_; 00068 } 00069 return 0; 00070 } 00071 00072 void clear() { 00073 rc_ = 0; 00074 } 00075 00076 void swap(reference_count &rhs) throw() { 00077 std::swap(rc_, rhs.rc_); 00078 } 00079 00080 reference_count& operator=(reference_count& x) 00081 { 00082 rc_ = x.rc_; 00083 return *this; 00084 } 00085 00086 private: 00087 size_t* rc_; 00088 }; // class reference_count 00090 } // namespace comet 00091 00092 namespace std { 00094 template<> inline void swap(comet::reference_count &lhs, comet::reference_count &rhs) COMET_STD_SWAP_NOTHROW 00095 { 00096 lhs.swap(rhs); 00097 } 00098 } 00099 00100 #endif