File: auto_ptr.h

package info (click to toggle)
html2text 2.4.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,144 kB
  • sloc: cpp: 5,891; sh: 5,747; yacc: 891; makefile: 84
file content (59 lines) | stat: -rw-r--r-- 1,974 bytes parent folder | download | duplicates (2)
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
/*
 * 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>
 *
 * 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.
 */

#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; }
  ~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; }

private:
  T *p;
};

#endif /* } */