00001 // 00002 // File: include/odbcpp/object.h 00003 // Object: Define the base object of the odbcpp library 00004 // Project: http://www.m2osw.com/odbcpp 00005 // Author: alexis_wilke@sourceforge.net 00006 // 00007 // Copyright (C) 2008 Made to Order Software Corp. 00008 // 00009 // This program is free software: you can redistribute it and/or modify 00010 // it under the terms of the GNU General Public License as published by 00011 // the Free Software Foundation, either version 3 of the License, or 00012 // (at your option) any later version. 00013 // 00014 // This program is distributed in the hope that it will be useful, 00015 // but WITHOUT ANY WARRANTY; without even the implied warranty of 00016 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00017 // GNU General Public License for more details. 00018 // 00019 // You should have received a copy of the GNU General Public License 00020 // along with this program. If not, see <http://www.gnu.org/licenses/> 00021 // or <http://gpl3.m2osw.com/>. 00022 // 00023 #ifndef ODBCPP_OBJECT 00024 #define ODBCPP_OBJECT 00025 00026 00027 00028 00029 namespace odbcpp 00030 { 00031 00032 00033 00034 class object 00035 { 00036 public: 00037 object(); 00038 object(const object& obj); 00039 ~object(); 00040 00041 object& operator = (const object& obj); 00042 00043 unsigned long addref() const; 00044 unsigned long release() const; 00045 00046 private: 00047 mutable unsigned long f_refcount; 00048 }; 00049 00050 00052 template<class T> class smartptr 00053 { 00054 public: 00056 smartptr() : f_ptr(0) {} 00057 00059 // \param[in] obj The object to be managed by smart pointers 00060 smartptr(T *obj) : f_ptr(obj) { f_ptr->addref(); } 00061 00063 // \param[in] ptr The object to be copied in another smart pointer 00064 smartptr(const smartptr<T>& ptr) : f_ptr(ptr.f_ptr) { f_ptr->addref(); } 00065 00067 ~smartptr() { f_ptr->release(); } 00068 00070 // \param[in] obj The object to be manage by smart pointers 00071 void reset(T *obj = 0) { obj->addref(); f_ptr->release(); f_ptr = obj; } 00072 00074 // \param[in] obj The object to be manage by smart pointers 00075 smartptr& operator = (T *obj) { reset(obj); return *this; } 00076 00078 // \param[in] ptr The smart pointer to copy in another smart pointer 00079 smartptr& operator = (const smartptr& ptr) { reset(ptr.f_ptr); return *this; } 00080 00082 // \param[in] obj Check whether \p obj is equal to the pointer in this smart pointer 00083 bool operator == (const T *obj) const { return f_ptr == obj; } 00084 00086 // \param[in] obj Check whether \p obj is equal to the pointer in this smart pointer 00087 bool operator == (const T& obj) const { return f_ptr == &obj; } 00088 00090 // \param[in] ptr Check whether both smart pointer are managing the same pointer 00091 bool operator == (const smartptr& ptr) const { return f_ptr == ptr.f_ptr; } 00092 00094 operator bool () const { return f_ptr != 0; } 00095 00097 bool operator ! () const { return f_ptr == 0; } 00098 00100 const T *operator -> () const { return f_ptr; } 00101 00103 T *operator -> () { return f_ptr; } 00104 00106 operator T * () const { return f_ptr; } 00107 00109 operator T * () { return f_ptr; } 00110 00111 private: 00113 T *f_ptr; 00114 }; 00115 00116 00117 00118 } // namespace odbcpp 00119 00120 #endif // #ifndef ODBCPP_OBJECT 00121 00122