CppCMS
booster/refcounted.h
00001 
00002 //
00003 //  Copyright (C) 2008-2012 Artyom Beilis (Tonkikh)
00004 //
00005 //  Distributed under the Boost Software License, Version 1.0. (See
00006 //  accompanying file LICENSE_1_0.txt or copy at
00007 //  http://www.boost.org/LICENSE_1_0.txt)
00008 //                                                                             
00009 #ifndef BOOSTER_REFCOUNTED_H
00010 #define BOOSTER_REFCOUNTED_H
00011 
00012 #include <booster/atomic_counter.h>
00013 
00014 namespace booster {
00015 
00016         class refcounted;
00017         void intrusive_ptr_add_ref(refcounted *ptr);
00018         void intrusive_ptr_release(refcounted *ptr);
00019 
00025         class refcounted {
00026         public:
00027                 refcounted() :
00028                         refs_(0)
00029                 {
00030                 }
00031 
00032                 virtual ~refcounted()
00033                 {
00034                 }
00035 
00036         private:
00037                 friend void intrusive_ptr_add_ref(refcounted *);
00038                 friend void intrusive_ptr_release(refcounted *);
00039         
00040                 refcounted(refcounted const &other);
00041                 refcounted const &operator=(refcounted const &other);
00042                 atomic_counter refs_;
00043         };
00044 
00048         inline void intrusive_ptr_add_ref(refcounted *p)
00049         {
00050                 ++p->refs_;
00051         }
00055         inline void intrusive_ptr_release(refcounted *p)
00056         {
00057                 if(p && --p->refs_ == 0)
00058                         delete p;
00059         }
00060 
00061 }
00062 
00063 
00064 #endif