- more porting of OpenSync module to 0.30
[barry.git] / opensync-plugin / src / fsmartptr.h
blobcab7d639d87a636e5563223f3033b9ae803a37a7
1 ///
2 /// \file fsmartptr.h
3 /// C++ smart pointer template that can call unique free
4 /// functions.
5 ///
7 /*
8 Copyright (C) 2007,
9 Net Direct Inc. (http://www.netdirect.ca/),
10 and Chris Frey <cdfrey@foursquare.net>
12 This program is free software; you can redistribute it and/or modify
13 it under the terms of the GNU General Public License as published by
14 the Free Software Foundation; either version 2 of the License, or
15 (at your option) any later version.
17 This program is distributed in the hope that it will be useful,
18 but WITHOUT ANY WARRANTY; without even the implied warranty of
19 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
21 See the GNU General Public License in the COPYING file at the
22 root directory of this project for more details.
25 #ifndef __REUSE_FSMARTPTR_H__
26 #define __REUSE_FSMARTPTR_H__
28 // A special smart pointer for pointer handling.
29 // Behaves like std::auto_ptr<> in that only one object
30 // at a time owns the pointer, and destruction frees it.
31 template <class T, class FT, void (*FreeFunc)(FT *pt)>
32 class fSmartPtr
34 mutable T *m_pt;
36 public:
37 fSmartPtr() : m_pt(0) {}
38 fSmartPtr(T *pt) : m_pt(pt) {}
39 fSmartPtr(const fSmartPtr &sp) : m_pt(sp.m_pt)
41 sp.m_pt = 0;
43 ~fSmartPtr()
45 if( m_pt )
46 FreeFunc(m_pt);
49 fSmartPtr& operator=(T *pt)
51 Extract();
52 m_pt = pt;
53 return *this;
56 fSmartPtr& operator=(const fSmartPtr &sp)
58 Extract();
59 m_pt = sp.Extract();
60 return *this;
63 T* Extract()
65 T *rp = m_pt;
66 m_pt = 0;
67 return rp;
70 T* Get()
72 return m_pt;
76 #endif