cartobase  5.0.5
scopedptr.h
Go to the documentation of this file.
1 /* This software and supporting documentation are distributed by
2  * Institut Federatif de Recherche 49
3  * CEA/NeuroSpin, Batiment 145,
4  * 91191 Gif-sur-Yvette cedex
5  * France
6  *
7  * This software is governed by the CeCILL-B license under
8  * French law and abiding by the rules of distribution of free software.
9  * You can use, modify and/or redistribute the software under the
10  * terms of the CeCILL-B license as circulated by CEA, CNRS
11  * and INRIA at the following URL "http://www.cecill.info".
12  *
13  * As a counterpart to the access to the source code and rights to copy,
14  * modify and redistribute granted by the license, users are provided only
15  * with a limited warranty and the software's author, the holder of the
16  * economic rights, and the successive licensors have only limited
17  * liability.
18  *
19  * In this respect, the user's attention is drawn to the risks associated
20  * with loading, using, modifying and/or developing or reproducing the
21  * software by the user in light of its specific status of free software,
22  * that may mean that it is complicated to manipulate, and that also
23  * therefore means that it is reserved for developers and experienced
24  * professionals having in-depth computer knowledge. Users are therefore
25  * encouraged to load and test the software's suitability as regards their
26  * requirements in conditions enabling the security of their systems and/or
27  * data to be ensured and, more generally, to use and operate it in the
28  * same conditions as regards security.
29  *
30  * The fact that you are presently reading this means that you have had
31  * knowledge of the CeCILL-B license and that you accept its terms.
32  */
33 
34 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
35 // Copyright (c) 2001, 2002 Peter Dimov
36 //
37 // Permission to copy, use, modify, sell and distribute this software
38 // is granted provided this copyright notice appears in all copies.
39 // This software is provided "as is" without express or implied
40 // warranty, and with no claim as to its suitability for any purpose.
41 
43 // The Loki Library
44 // Copyright (c) 2001 by Andrei Alexandrescu
45 // This code accompanies the book:
46 // Alexandrescu, Andrei. "Modern C++ Design: Generic Programming and Design
47 // Patterns Applied". Copyright (c) 2001. Addison-Wesley.
48 // Permission to use, copy, modify, distribute and sell this software for any
49 // purpose is hereby granted without fee, provided that the above copyright
50 // notice appear in all copies and that both that copyright notice and this
51 // permission notice appear in supporting documentation.
52 // The author or Addison-Welsey Longman make no representations about the
53 // suitability of this software for any purpose. It is provided "as is"
54 // without express or implied warranty.
56 
57 #ifndef CARTOBASE_SMART_SCOPEDPTR_H
58 #define CARTOBASE_SMART_SCOPEDPTR_H
59 
60 
62 #include <memory>
63 
64 
65 namespace carto {
66 
67 
68 template <typename T>
70 {
71  public:
72 
73  typedef T element_type;
74 
75 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
77  {
78  protected:
79  void operator delete( void* );
80  void operator delete[]( void* );
81  };
82 #endif
83 
84  explicit scoped_ptr( T* p = 0 ) : pointee( p )
85  {
86  }
87 
88 #if __cplusplus >= 201103L
89  // Only unique_ptr with the default deleter can be converted, because
90  // scoped_ptr does not support custom deleters (it calls the delete operator
91  // upon destruction, just like std::default_delete).
92  template< class U >
93  scoped_ptr( std::unique_ptr<U>&& r ) : pointee( r.release() )
94  {
95  }
96 #else
97  template< class U >
98  scoped_ptr( std::auto_ptr<U> r ) : pointee( r.release() )
99  {
100  }
101 #endif
102 
104  {
105  delete pointee;
106  }
107 
108  void reset( T* p = 0 )
109  {
110  if ( pointee != p )
111  {
112  delete pointee;
113  pointee = p;
114  }
115  }
116 
117  T& operator*() const
118  {
119  return *pointee;
120  }
121 
122  T* operator->() const
123  {
124  return pointee;
125  }
126 
127  T* get() const
128  {
129  return pointee;
130  }
131 
132 #ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
133  operator InsipidProxyPointer*() const
134  {
135  return reinterpret_cast<InsipidProxyPointer*>( pointee );
136  }
137 #endif
138 
139  void swap( scoped_ptr & r )
140  {
141  T* tmp( r.pointee );
142  r.pointee = pointee;
143  pointee = tmp;
144  }
145 
146  private:
147 
148  scoped_ptr( const scoped_ptr& )
149 #if __cplusplus >= 201103L
150  = delete
151 #endif
152  ;
153  const scoped_ptr& operator=( const scoped_ptr& )
154 #if __cplusplus >= 201103L
155  = delete
156 #endif
157  ;
158 
159  T* pointee;
160 
161 };
162 
163 
164 template <typename T>
165 inline
167 {
168  x.swap(y);
169 }
170 
171 
172 } // namespace carto
173 
174 
175 #endif
Holds a temporary object pointer in a scope.
Definition: scopedptr.h:69
scoped_ptr(T *p=0)
Definition: scopedptr.h:84
T * operator->() const
Definition: scopedptr.h:122
T & operator*() const
Definition: scopedptr.h:117
scoped_ptr(std::auto_ptr< U > r)
Definition: scopedptr.h:98
void reset(T *p=0)
Definition: scopedptr.h:108
void swap(scoped_ptr &r)
Definition: scopedptr.h:139