llvm.org GIT mirror llvm / ab64806
Import the boost scoped_ptr class to LLVM. This patch was prepared by C├ędric Venet. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@44161 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 11 years ago
3 changed file(s) with 155 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
6565 Autoconf llvm/autoconf
6666 llvm/projects/ModuleMaker/autoconf
6767 llvm/projects/sample/autoconf
68 Boost C++ Libraries llvm/include : docs/BOOST_LICENSE_1_0.txt
69
70
0 Boost Software License - Version 1.0 - August 17th, 2003
1
2 Permission is hereby granted, free of charge, to any person or organization
3 obtaining a copy of the software and accompanying documentation covered by
4 this license (the "Software") to use, reproduce, display, distribute,
5 execute, and transmit the Software, and to prepare derivative works of the
6 Software, and to permit third-parties to whom the Software is furnished to
7 do so, all subject to the following:
8
9 The copyright notices in the Software and this entire statement, including
10 the above license grant, this restriction and the following disclaimer,
11 must be included in all copies of the Software, in whole or in part, and
12 all derivative works of the Software, unless such copies or derivative
13 works are solely in the form of machine-executable object code generated by
14 a source language processor.
15
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
19 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
20 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
21 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22 DEALINGS IN THE SOFTWARE.
0 //===- 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
26 #ifndef LLVM_SCOPED_PTR_HPP_INCLUDED
27 #define LLVM_SCOPED_PTR_HPP_INCLUDED
28
29 #include
30
31 namespace llvm {
32
33 // verify that types are complete for increased safety
34 template inline void checked_delete(T * x) {
35 // intentionally complex - simplification causes regressions
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
46 template class scoped_ptr // noncopyable
47 {
48 private:
49
50 T * ptr;
51
52 scoped_ptr(scoped_ptr const &);
53 scoped_ptr & operator=(scoped_ptr const &);
54
55 typedef scoped_ptr this_type;
56
57 public:
58
59 typedef T element_type;
60
61 explicit scoped_ptr(T * p = 0): ptr(p) // never throws
62 {
63 }
64
65 ~scoped_ptr() // never throws
66 {
67 llvm::checked_delete(ptr);
68 }
69
70 void reset(T * p = 0) // never throws
71 {
72 assert( (p == 0 || p != ptr) && "scoped_ptr: self-reset error"); // catch self-reset errors
73 this_type(p).swap(*this);
74 }
75
76 T & operator*() const // never throws
77 {
78 assert(ptr != 0 && "scoped_ptr: Trying to dereference a null pointeur");
79 return *ptr;
80 }
81
82 T * operator->() const // never throws
83 {
84 assert(ptr != 0 && "scoped_ptr: Trying to dereference a null pointeur");
85 return ptr;
86 }
87
88 T * get() const // never throws
89 {
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 {
98 return ptr == 0? 0: &this_type::ptr;
99 }
100
101 bool operator! () const // never throws
102 {
103 return ptr == 0;
104 }
105
106 void swap(scoped_ptr & b) // never throws
107 {
108 T * tmp = b.ptr;
109 b.ptr = ptr;
110 ptr = tmp;
111 }
112 };
113
114 template inline void swap(scoped_ptr & a, scoped_ptr & b) // never throws
115 {
116 a.swap(b);
117 }
118
119 // get_pointer(p) is a generic way to say p.get()
120
121 template inline T * get_pointer(scoped_ptr const & p)
122 {
123 return p.get();
124 }
125
126 } // namespace llvm
127
128 #endif // #ifndef LLVM_SCOPED_PTR_HPP_INCLUDED