OPAL (Object Oriented Parallel Accelerator Library)  2.2.0
OPAL
my_auto_ptr.h
Go to the documentation of this file.
1 // -*- C++ -*-
2 /***************************************************************************
3  *
4  * The IPPL Framework
5  *
6  *
7  * Visit http://people.web.psi.ch/adelmann/ for more details
8  *
9  ***************************************************************************/
10 
11 #ifndef MY_AUTO_PTR_H
12 #define MY_AUTO_PTR_H
13 
15 /*
16  A simple compliant implementation of auto_ptr.
17  This is from Greg Colvin's implementation posted to comp.std.c++.
18 
19  Instead of using mutable this casts away const in release.
20 
21  We have to do this because we can't build containers of these
22  things otherwise.
23  */
25 
26 template<class X>
28 {
29  X* px;
30 public:
31  my_auto_ptr() : px(0) {}
32  my_auto_ptr(X* p) : px(p) {}
33  my_auto_ptr(const my_auto_ptr<X>& r) : px(r.release()) {}
35  {
36  if (&r != this)
37  {
38  delete px;
39  px = r.release();
40  }
41  return *this;
42  }
43  ~my_auto_ptr() { delete px; }
44  X& operator*() const { return *px; }
45  X* operator->() const { return px; }
46  X* get() const { return px; }
47  X* release() const { X *p=px; ((my_auto_ptr<X>*)(this))->px=0; return p; }
48 };
49 
50 #ifdef UNDEFINED
51 template<class X>
52 class my_auto_ptr
53 {
54  bool owner;
55  X* px;
56 public:
57  my_auto_ptr()
58  : owner(false), px(0) {}
59  my_auto_ptr(X* p)
60  : owner(p), px(p) {}
61  my_auto_ptr(const my_auto_ptr<X>& r)
62  : owner(r.owner), px(r.release()) {}
64  {
65  if (&r != this)
66  {
67  if (owner)
68  delete px;
69  owner = r.owner;
70  px = r.release();
71  }
72  return *this;
73  }
74  ~my_auto_ptr() { if (owner) delete px; }
75  X& operator*() const { return *px; }
76  X* operator->() const { return px; }
77  X* get() const { return px; }
78  X* release() const { ((my_auto_ptr<X>*)(this))->owner = false; return px;}
79 };
80 #endif // UNDEFINED
81 
82 #endif // MY_AUTO_PTR_H
83 
84 /***************************************************************************
85  * $RCSfile: my_auto_ptr.h,v $ $Author: adelmann $
86  * $Revision: 1.1.1.1 $ $Date: 2003/01/23 07:40:34 $
87  * IPPL_VERSION_ID: $Id: my_auto_ptr.h,v 1.1.1.1 2003/01/23 07:40:34 adelmann Exp $
88  ***************************************************************************/
X * release() const
Definition: my_auto_ptr.h:47
Definition: rbendmap.h:8
my_auto_ptr(X *p)
Definition: my_auto_ptr.h:32
my_auto_ptr(const my_auto_ptr< X > &r)
Definition: my_auto_ptr.h:33
X & operator*() const
Definition: my_auto_ptr.h:44
X * operator->() const
Definition: my_auto_ptr.h:45
my_auto_ptr & operator=(const my_auto_ptr< X > &r)
Definition: my_auto_ptr.h:34