llvm.org GIT mirror llvm / 48da1ed
Removed scoped_ptr, as its functionality is subsumed by OwningPtr. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@45274 91177308-0d34-0410-b5e6-96231b3b80d8 Ted Kremenek 12 years ago
1 changed file(s) with 0 addition(s) and 124 deletion(s). Raw diff Collapse all Expand all
+0
-124
include/llvm/ADT/scoped_ptr.h less more
None //===- llvm/ADT/scoped_ptr.h - basic smart pointer --------------*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines the scoped_ptr smart pointer: scoped_ptr mimics a built-in
10 // pointer except that it guarantees deletion of the object pointed to, either
11 // on destruction of the scoped_ptr or via an explicit reset(). scoped_ptr is a
12 // simple solution for simple needs.
13 //
14 //===----------------------------------------------------------------------===//
15 //
16 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
17 // Copyright (c) 2001, 2002 Peter Dimov
18 //
19 // Distributed under the Boost Software License, Version 1.0. (See
20 // accompanying file llvm/docs/BOOST_LICENSE_1_0.txt or copy at
21 // http://www.boost.org/LICENSE_1_0.txt )
22 //
23 // http://www.boost.org/libs/smart_ptr/scoped_ptr.htm
24 //
25 #ifndef LLVM_SCOPED_PTR_H_INCLUDED
26 #define LLVM_SCOPED_PTR_H_INCLUDED
27
28 #include
29
30 namespace llvm {
31
32 // verify that types are complete for increased safety
33 template
34 inline void checked_delete(T * x) {
35 // intentionally complex - simplification causes warnings in some compilers.
36 typedef char type_must_be_complete[sizeof(T) ? 1 : -1];
37 (void)sizeof(type_must_be_complete);
38 delete x;
39 }
40
41 /// scoped_ptr mimics a built-in pointer except that it guarantees deletion
42 /// of the object pointed to, either on destruction of the scoped_ptr or via
43 /// an explicit reset(). scoped_ptr is a simple solution for simple needs;
44 /// use shared_ptr or std::auto_ptr if your needs are more complex.
45 template
46 class scoped_ptr {// noncopyable
47 T *ptr;
48 scoped_ptr(scoped_ptr const &); // DO NOT IMPLEMENT
49 scoped_ptr & operator=(scoped_ptr const &); // DO NOT IMPLEMENT
50 typedef scoped_ptr this_type;
51 public:
52 typedef T element_type;
53
54 explicit scoped_ptr(T * p = 0): ptr(p) {} // never throws
55
56 ~scoped_ptr() { // never throws
57 llvm::checked_delete(ptr);
58 }
59
60 /// reset - Change the current pointee to the specified pointer. Note that
61 /// calling this with any pointer (including a null pointer) deletes the
62 /// current pointer.
63 void reset(T *p = 0) {
64 // catch self-reset errors
65 assert((p == 0 || p != ptr) && "scoped_ptr: self-reset error");
66 T *tmp = ptr;
67 ptr = p;
68 delete tmp;
69 }
70
71 /// take - Reset the scoped pointer to null and return its pointer. This does
72 /// not delete the pointer before returning it.
73 T *take() {
74 T *P = ptr;
75 ptr = 0;
76 return P;
77 }
78
79 T& operator*() const {
80 assert(ptr != 0 && "scoped_ptr: Trying to dereference a null pointer");
81 return *ptr;
82 }
83
84 T* operator->() const {
85 assert(ptr != 0 && "scoped_ptr: Trying to dereference a null pointer");
86 return ptr;
87 }
88
89 T* get() const {
90 return ptr;
91 }
92
93 // implicit conversion to "bool"
94 typedef T * this_type::*unspecified_bool_type;
95
96 operator unspecified_bool_type() const {// never throws
97 return ptr == 0? 0: &this_type::ptr;
98 }
99
100 bool operator!() const { // never throws
101 return ptr == 0;
102 }
103
104 void swap(scoped_ptr &b) {// never throws
105 T * tmp = b.ptr;
106 b.ptr = ptr;
107 ptr = tmp;
108 }
109 };
110
111 template inline void swap(scoped_ptr &a, scoped_ptr &b) {
112 // never throws
113 a.swap(b);
114 }
115
116 // get_pointer(p) is a generic way to say p.get()
117 template inline T * get_pointer(scoped_ptr const &p) {
118 return p.get();
119 }
120
121 } // namespace llvm
122
123 #endif // #ifndef LLVM_SCOPED_PTR_HPP_INCLUDED