changed: update version strings for beta4
[xbmc.git] / xbmc / utils / AutoPtrHandle.h
blobff15c31355fb1771f66c3849d56ac566d630c617
1 #pragma once
3 /*
4 * Copyright (C) 2005-2008 Team XBMC
5 * http://www.xbmc.org
7 * This Program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2, or (at your option)
10 * any later version.
12 * This Program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with XBMC; see the file COPYING. If not, write to
19 * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
20 * http://www.gnu.org/copyleft/gpl.html
24 #include "system.h" // for HANDLE and SOCKET
26 namespace AUTOPTR
28 class CAutoPtrHandle
30 public:
31 CAutoPtrHandle(HANDLE hHandle);
32 virtual ~CAutoPtrHandle(void);
33 operator HANDLE();
34 void attach(HANDLE hHandle);
35 HANDLE release();
36 bool isValid() const;
37 void reset();
38 protected:
39 virtual void Cleanup();
40 HANDLE m_hHandle;
43 class CAutoPtrFind : public CAutoPtrHandle
45 public:
46 CAutoPtrFind(HANDLE hHandle);
47 virtual ~CAutoPtrFind(void);
48 protected:
49 virtual void Cleanup();
53 class CAutoPtrSocket
55 public:
56 CAutoPtrSocket(SOCKET hSocket);
57 virtual ~CAutoPtrSocket(void);
58 operator SOCKET();
59 void attach(SOCKET hSocket);
60 SOCKET release();
61 bool isValid() const;
62 void reset();
63 protected:
64 virtual void Cleanup();
65 SOCKET m_hSocket;
69 * This template class is very similar to the standard "auto_ptr", but it is
70 * used for *array* pointers rather than *object* pointers, i.e. the pointer
71 * passed to it must have been allocated with "new[]", and "auto_aptr" will
72 * delete it with "delete[]".
74 * Class released under GPL and was taken from:
75 * http://userpage.fu-berlin.de/~mbayer/tools/html2text.html
77 template <class T>
78 class auto_aptr
81 public:
83 // Constructor/copy/destroy
85 explicit auto_aptr(T *x = 0) : p(x) {}
86 auto_aptr(const auto_aptr<T> &x) : p(x.p) { ((auto_aptr<T> *) &x)->p = 0; }
87 auto_aptr<T>& operator=(const auto_aptr<T> &x)
88 { delete[] p; p = x.p; ((auto_aptr<T> *) &x)->p = 0; return *this; }
89 // Extension: "operator=(T *)" is identical to "auto_aptr::reset(T *)".
90 void operator=(T *x) { delete[] p; p = x; }
91 ~auto_aptr() { delete[] p; }
93 // Members
95 T &operator[](size_t idx) const { if (!p) abort(); return p[idx]; }
96 T *get() const { return (T *) p; }
97 T *release() { T *tmp = p; p = 0; return tmp; }
98 void reset(T *x = 0) { delete[] p; p = x; }
100 // These would make a nice extension, but are not provided by many other
101 // implementations.
102 //operator const void *() const { return p; }
103 //int operator!() const { return p == 0; }
105 private:
106 T *p;