aboutsummaryrefslogtreecommitdiff
path: root/src/utils/AutoPtrHandle.h
blob: cd72c3ae640b01525276137b16a75cc3f1672455 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#pragma once

/*
 *      Copyright (C) 2005-2013 Team XBMC
 *      http://xbmc.org
 *
 *  This Program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2, or (at your option)
 *  any later version.
 *
 *  This Program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with XBMC; see the file COPYING.  If not, see
 *  <http://www.gnu.org/licenses/>.
 *
 */

#include "system.h" // for HANDLE and SOCKET
#include <stdlib.h>

namespace AUTOPTR
{
class CAutoPtrHandle
{
public:
  CAutoPtrHandle(HANDLE hHandle);
  virtual ~CAutoPtrHandle(void);
  operator HANDLE();
  void attach(HANDLE hHandle);
  HANDLE release();
  bool isValid() const;
  void reset();
protected:
  virtual void Cleanup();
  HANDLE m_hHandle;
};

class CAutoPtrFind : public CAutoPtrHandle
{
public:
  CAutoPtrFind(HANDLE hHandle);
  virtual ~CAutoPtrFind(void);
protected:
  virtual void Cleanup();
};


class CAutoPtrSocket
{
public:
  CAutoPtrSocket(SOCKET hSocket);
  virtual ~CAutoPtrSocket(void);
  operator SOCKET();
  void attach(SOCKET hSocket);
  SOCKET release();
  bool isValid() const;
  void reset();
protected:
  virtual void Cleanup();
  SOCKET m_hSocket;
};

/*
 * This template class is very similar to the standard "auto_ptr", but it is
 * used for *array* pointers rather than *object* pointers, i.e. the pointer
 * passed to it must have been allocated with "new[]", and "auto_aptr" will
 * delete it with "delete[]".
 *
 * Class released under GPL and was taken from:
 * http://userpage.fu-berlin.de/~mbayer/tools/html2text.html
 */
template <class T>
class auto_aptr
{

public:

  // Constructor/copy/destroy

  explicit auto_aptr(T *x = 0) : p(x) {}
  auto_aptr(const auto_aptr<T> &x) : p(x.p) { ((auto_aptr<T> *) &x)->p = 0; }
  auto_aptr<T>& operator=(const auto_aptr<T> &x)
  { delete[] p; p = x.p; ((auto_aptr<T> *) &x)->p = 0; return *this; }
  // Extension: "operator=(T *)" is identical to "auto_aptr::reset(T *)".
  void operator=(T *x) { delete[] p; p = x; }
  ~auto_aptr() { delete[] p; }

  // Members

  T &operator[](size_t idx) const { if (!p) abort(); return p[idx]; }
T *get() const { return (T *) p; }
  T *release() { T *tmp = p; p = 0; return tmp; }
  void reset(T *x = 0) { delete[] p; p = x; }

  // These would make a nice extension, but are not provided by many other
  // implementations.
  //operator const void *() const { return p; }
  //int operator!() const { return p == 0; }

private:
  T *p;
};


}