File: auto_ptr.h

package info (click to toggle)
html2text 1.3.2a-3
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 736 kB
  • ctags: 1,694
  • sloc: cpp: 9,311; yacc: 828; sh: 209; makefile: 144
file content (97 lines) | stat: -rw-r--r-- 3,384 bytes parent folder | download | duplicates (8)
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

 /***************************************************************************/

/*
 * Portions Copyright (c) 1999 GMRS Software GmbH
 * Carl-von-Linde-Str. 38, D-85716 Unterschleissheim, http://www.gmrs.de
 * All rights reserved.
 *
 * Author: Arno Unkrig <arno@unkrig.de>
 *
 * All advertising materials mentioning features or use of this software
 * must display the following acknowledgement:
 * "This product includes software developed by GMRS Software GmbH."
 * The name of GMRS Software GmbH may not be used to endorse or promote
 * products derived from this software without specific prior written
 * permission.
 */
 
/* 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 of the License, 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 in the file COPYING for more details.
 */

 /***************************************************************************/

/*
 * Changes to version 1.2.2 were made by Martin Bayer <mbayer@zedat.fu-berlin.de>
 * Dates and reasons of modifications:
 * Thu Oct  4 22:44:17 CEST 2001: ported to g++ 3.0
 */
  
 /***************************************************************************/


#ifndef __auto_ptr_h_INCLUDED__ /* { */
#define __auto_ptr_h_INCLUDED__

/* ------------------------------------------------------------------------- */

/*
 * Yet another implementation of the "auto_ptr" template... I am not sure
 * if the standard does specify "auto_ptr", and how, but this implementation
 * uses a scheme *without* an "owns" flag: When the ownership is taken away
 * from the "auto_ptr", its pointer *is set to 0*! This may seem odd, but
 * in practice, it saves you from problems because such an "auto_ptr" can
 * never be dangling, only "0", which is checked in "operator*()" and
 * "operator->()". I never found this scheme limiting.
 */

/* ------------------------------------------------------------------------- */

#include <stdlib.h>

template <class T>
class auto_ptr {

public:

  // Constructor/copy/destroy

  explicit auto_ptr(T *x = 0) : p(x) {}
  auto_ptr(const auto_ptr<T> &x) : p(x.p) { ((auto_ptr<T> *) &x)->p = 0; }
  void operator=(const auto_ptr<T> &x)
  { delete p; p = x.p; ((auto_ptr<T> *) &x)->p = 0; }
  // Would be a nice extension, but is not portable; use "reset()" instead:
//void operator=(T *x); { delete p; p = x; }
  ~auto_ptr() { delete p; }

  // Members

  T    &operator*() const  { if (!p) abort(); return *(T *) p; }
  T    *operator->() const { if (!p) abort(); return (T *) p; }
  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;
};

/* ------------------------------------------------------------------------- */

#endif /* } */

/* ------------------------------------------------------------------------- */