cartobase 6.0.20
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
65namespace carto {
66
67
68template <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 // Only unique_ptr with the default deleter can be converted, because
89 // scoped_ptr does not support custom deleters (it calls the delete operator
90 // upon destruction, just like std::default_delete).
91 template< class U >
92 scoped_ptr( std::unique_ptr<U>&& r ) : pointee( r.release() )
93 {
94 }
95
97 {
98 delete pointee;
99 }
100
101 void reset( T* p = 0 )
102 {
103 if ( pointee != p )
104 {
105 delete pointee;
106 pointee = p;
107 }
108 }
109
110 T& operator*() const
111 {
112 return *pointee;
113 }
114
115 T* operator->() const
116 {
117 return pointee;
118 }
119
120 T* get() const
121 {
122 return pointee;
123 }
124
125#ifndef DOXYGEN_HIDE_INTERNAL_CLASSES
126 operator InsipidProxyPointer*() const
127 {
128 return reinterpret_cast<InsipidProxyPointer*>( pointee );
129 }
130#endif
131
132 void swap( scoped_ptr & r )
133 {
134 T* tmp( r.pointee );
135 r.pointee = pointee;
136 pointee = tmp;
137 }
138
139 private:
140
141 scoped_ptr( const scoped_ptr& )
142#if __cplusplus >= 201103L
143 = delete
144#endif
145 ;
146 const scoped_ptr& operator=( const scoped_ptr& )
147#if __cplusplus >= 201103L
148 = delete
149#endif
150 ;
151
152 T* pointee;
153
154};
155
156
157template <typename T>
158inline
160{
161 x.swap(y);
162}
163
164
165} // namespace carto
166
167
168#endif
Holds a temporary object pointer in a scope.
Definition scopedptr.h:70
scoped_ptr(std::unique_ptr< U > &&r)
Definition scopedptr.h:92
scoped_ptr(T *p=0)
Definition scopedptr.h:84
T & operator*() const
Definition scopedptr.h:110
T * operator->() const
Definition scopedptr.h:115
void reset(T *p=0)
Definition scopedptr.h:101
void swap(scoped_ptr &r)
Definition scopedptr.h:132
T * get() const
Definition scopedptr.h:120
void swap(scoped_ptr< T > &x, scoped_ptr< T > &y)
Definition scopedptr.h:159