3 /// Variable 'free' smart pointer
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__
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.
35 template <class T
, class FT
, void (*FreeFunc
)(FT
*pt
)>
41 vSmartPtr() : m_pt(0) {}
42 vSmartPtr(T
*pt
) : m_pt(pt
) {}
43 vSmartPtr(const vSmartPtr
&sp
) : m_pt(sp
.m_pt
)
52 vSmartPtr
& operator=(T
*pt
)
58 vSmartPtr
& operator=(const vSmartPtr
&sp
)
64 // Some non-standard APIs used by Barry
67 return this->release();
75 // std::auto_ptr<> style API
88 void reset(T
*new_obj
= 0)
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
>
107 FreeFuncPtrT m_FreeFuncPtr
;
110 explicit vLateSmartPtr(FreeFuncPtrT freefunc
= 0)
112 , m_FreeFuncPtr(freefunc
)
116 vLateSmartPtr(T
*pt
, FreeFuncPtrT freefunc
= 0)
118 , m_FreeFuncPtr(freefunc
)
122 vLateSmartPtr(const vLateSmartPtr
&sp
)
124 , m_FreeFuncPtr(sp
.m_FreeFuncPtr
)
134 void SetFreeFunc(FreeFuncPtrT freefunc
)
136 m_FreeFuncPtr
= freefunc
;
139 vLateSmartPtr
& operator=(T
*pt
)
145 vLateSmartPtr
& operator=(const vLateSmartPtr
&sp
)
148 m_FreeFuncPtr
= sp
.m_FreeFuncPtr
;
152 // Some non-standard APIs used by Barry
155 return this->release();
163 // std::auto_ptr<> style API
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
181 (*m_FreeFuncPtr
)(m_pt
);
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;