CppCMS
booster/hold_ptr.h
00001 //
00002 //  Copyright (C) 2009-2012 Artyom Beilis (Tonkikh)
00003 //
00004 //  Distributed under the Boost Software License, Version 1.0. (See
00005 //  accompanying file LICENSE_1_0.txt or copy at
00006 //  http://www.boost.org/LICENSE_1_0.txt)
00007 //
00008 #ifndef BOOSTER_HOLD_PTR_H
00009 #define BOOSTER_HOLD_PTR_H
00010 
00011 namespace booster { 
00012 
00017         template<typename T>
00018         class hold_ptr {
00019                 T *ptr_;
00020                 hold_ptr(hold_ptr const &other); // non copyable 
00021                 hold_ptr const &operator=(hold_ptr const &other); // non assignable
00022         public:
00023                 hold_ptr() : ptr_(0) {}
00024                 explicit hold_ptr(T *v) : ptr_(v) {}
00025                 ~hold_ptr() 
00026                 {
00027                         if(ptr_) delete ptr_;
00028                 }
00029 
00030                 T const *get() const { return ptr_; }
00031                 T *get() { return ptr_; }
00032 
00033                 T const &operator *() const { return *ptr_; }
00034                 T &operator *() { return *ptr_; }
00035                 T const *operator->() const { return ptr_; }
00036                 T *operator->() { return ptr_; }
00037                 T *release() { T *tmp=ptr_; ptr_=0; return tmp; }
00038                 void reset(T *p=0)
00039                 {
00040                         if(ptr_) delete ptr_;
00041                         ptr_=p;
00042                 }
00043                 void swap(hold_ptr &other)
00044                 {
00045                         T *tmp=other.ptr_;
00046                         other.ptr_=ptr_;
00047                         ptr_=tmp;
00048                 }
00049         };
00050 } // booster
00051 
00052 #endif