00001 /*************************************************************************** 00002 scoped_ptr - a not too smart pointer for exception safe heap objects 00003 ------------------------- 00004 begin : May 2019 00005 copyright : (C) 2019 Intermodalics 00006 00007 *************************************************************************** 00008 * This library is free software; you can redistribute it and/or * 00009 * modify it under the terms of the GNU Lesser General Public * 00010 * License as published by the Free Software Foundation; either * 00011 * version 2.1 of the License, or (at your option) any later version. * 00012 * * 00013 * This library is distributed in the hope that it will be useful, * 00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 00016 * Lesser General Public License for more details. * 00017 * * 00018 * You should have received a copy of the GNU Lesser General Public * 00019 * License along with this library; if not, write to the Free Software * 00020 * Foundation, Inc., 59 Temple Place, * 00021 * Suite 330, Boston, MA 02111-1307 USA * 00022 * * 00023 ***************************************************************************/ 00024 00025 #if (__cplusplus > 199711L) 00026 #include <utility> 00027 #else 00028 #include <algorithm> 00029 #endif 00030 00031 namespace KDL { 00032 00033 template<typename T> class scoped_ptr; 00034 00035 template<typename T> void swap(scoped_ptr<T>&, scoped_ptr<T>&); 00036 00037 template<typename T> 00038 class scoped_ptr { 00039 public: 00040 scoped_ptr() : ptr_(0) { } 00041 explicit scoped_ptr(T* p) : ptr_(p) { } 00042 00043 ~scoped_ptr() { delete ptr_; } 00044 00045 T* operator->() { return ptr_; } 00046 const T* operator->() const { return ptr_; } 00047 00048 T* get() const { return ptr_; } 00049 00050 void reset(T* p = 0) { 00051 T* old = ptr_; 00052 ptr_ = p; 00053 delete old; 00054 } 00055 00056 T* release() { 00057 T* old = ptr_; 00058 ptr_ = 0; 00059 return old; 00060 } 00061 00062 friend void swap<>(scoped_ptr<T>& a, scoped_ptr<T>& b); 00063 00064 private: 00065 scoped_ptr(const scoped_ptr&); // not-copyable 00066 scoped_ptr& operator=(const scoped_ptr&); // not-copyable 00067 00068 T* ptr_; 00069 }; 00070 00071 template<typename T> 00072 void swap(scoped_ptr<T>& a, scoped_ptr<T>& b) { 00073 using std::swap; 00074 swap(a.ptr_, b.ptr_); 00075 } 00076 00077 00078 } // namespace KDL