ppp: minor cleanups to match other chatscripts order and documentation
[barry.git] / src / vsmartptr.h
blob2eb6298b9c87dcad5ea91a99765566ac12a2fe26
1 ///
2 /// \file vsmartptr
3 /// Variable 'free' smart pointer
4 ///
6 /*
7 Copyright (C) 2006-2010, Net Direct Inc. (http://www.netdirect.ca/)
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
18 See the GNU General Public License in the COPYING file at the
19 root directory of this project for more details.
22 #ifndef __BARRY_VSMARTPTR_H__
23 #define __BARRY_VSMARTPTR_H__
25 namespace Barry {
28 // vSmartPtr
30 /// A special smart pointer for variables that have their own
31 /// special 'free' functions. Behaves like std::auto_ptr<>
32 /// in that only one object at a time owns the pointer,
33 /// and destruction frees it by calling the given FreeFunc.
34 ///
35 template <class T, class FT, void (*FreeFunc)(FT *pt)>
36 class vSmartPtr
38 mutable T *m_pt;
40 public:
41 vSmartPtr() : m_pt(0) {}
42 vSmartPtr(T *pt) : m_pt(pt) {}
43 vSmartPtr(const vSmartPtr &sp) : m_pt(sp.m_pt)
45 sp.m_pt = 0;
47 ~vSmartPtr()
49 reset();
52 vSmartPtr& operator=(T *pt)
54 reset(pt);
55 return *this;
58 vSmartPtr& operator=(const vSmartPtr &sp)
60 reset(sp.release());
61 return *this;
64 // Some non-standard APIs used by Barry
65 T* Extract()
67 return this->release();
70 T* Get()
72 return this->get();
75 // std::auto_ptr<> style API
76 T* get()
78 return m_pt;
81 T* release()
83 T *rp = m_pt;
84 m_pt = 0;
85 return rp;
88 void reset(T *new_obj = 0)
90 if( m_pt )
91 FreeFunc(m_pt);
92 m_pt = new_obj;
97 // vLateSmartPtr
99 /// Variation of the above smart pointer that allows the user to
100 /// assign a free function after construction, in the case of
101 /// dlopen()'d frees.
103 template <class T, class FreeFuncPtrT>
104 class vLateSmartPtr
106 mutable T *m_pt;
107 FreeFuncPtrT m_FreeFuncPtr;
109 public:
110 explicit vLateSmartPtr(FreeFuncPtrT freefunc = 0)
111 : m_pt(0)
112 , m_FreeFuncPtr(freefunc)
116 vLateSmartPtr(T *pt, FreeFuncPtrT freefunc = 0)
117 : m_pt(pt)
118 , m_FreeFuncPtr(freefunc)
122 vLateSmartPtr(const vLateSmartPtr &sp)
123 : m_pt(sp.m_pt)
124 , m_FreeFuncPtr(sp.m_FreeFuncPtr)
126 sp.m_pt = 0;
129 ~vLateSmartPtr()
131 reset();
134 void SetFreeFunc(FreeFuncPtrT freefunc)
136 m_FreeFuncPtr = freefunc;
139 vLateSmartPtr& operator=(T *pt)
141 reset(pt);
142 return *this;
145 vLateSmartPtr& operator=(const vLateSmartPtr &sp)
147 reset(sp.release());
148 m_FreeFuncPtr = sp.m_FreeFuncPtr;
149 return *this;
152 // Some non-standard APIs used by Barry
153 T* Extract()
155 return this->release();
158 T* Get()
160 return this->get();
163 // std::auto_ptr<> style API
164 T* get()
166 return m_pt;
169 T* release()
171 T *rp = m_pt;
172 m_pt = 0;
173 return rp;
176 void reset(T *new_obj = 0)
178 // don't check for null m_FreeFuncPtr, since
179 // that should be an obvious crash and requires fixing
180 if( m_pt )
181 (*m_FreeFuncPtr)(m_pt);
182 m_pt = new_obj;
188 Example usage:
190 typedef vSmartPtr<b_VFormatAttribute, b_VFormatAttribute, &b_vformat_attribute_free> vAttrPtr;
191 typedef vSmartPtr<b_VFormatParam, b_VFormatParam, &b_vformat_attribute_param_free> vParamPtr;
192 typedef vSmartPtr<char, void, &g_free> gStringPtr;
196 } // namespace Barry
198 #endif