MADNESS  version 0.9
scopedptr.h
Go to the documentation of this file.
1 /*
2  This file is part of MADNESS.
3 
4  Copyright (C) 2007,2010 Oak Ridge National Laboratory
5 
6  This program is free software; you can redistribute it and/or modify
7  it under the terms of the GNU General Public License as published by
8  the Free Software Foundation; either version 2 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU General Public License for more details.
15 
16  You should have received a copy of the GNU General Public License
17  along with this program; if not, write to the Free Software
18  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 
20  For more information please contact:
21 
22  Robert J. Harrison
23  Oak Ridge National Laboratory
24  One Bethel Valley Road
25  P.O. Box 2008, MS-6367
26 
27  email: harrisonrj@ornl.gov
28  tel: 865-241-3937
29  fax: 865-572-0680
30 
31 
32  $Id $
33  */
34 
35 // (C) Copyright Greg Colvin and Beman Dawes 1998, 1999.
36 // Copyright (c) 2001, 2002 Peter Dimov
37 //
38 // Boost Software License - Version 1.0 - August 17th, 2003
39 //
40 // Permission is hereby granted, free of charge, to any person or organization
41 // obtaining a copy of the software and accompanying documentation covered by
42 // this license (the "Software") to use, reproduce, display, distribute,
43 // execute, and transmit the Software, and to prepare derivative works of the
44 // Software, and to permit third-parties to whom the Software is furnished to
45 // do so, all subject to the following:
46 //
47 // The copyright notices in the Software and this entire statement, including
48 // the above license grant, this restriction and the following disclaimer,
49 // must be included in all copies of the Software, in whole or in part, and
50 // all derivative works of the Software, unless such copies or derivative
51 // works are solely in the form of machine-executable object code generated by
52 // a source language processor.
53 //
54 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
55 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
56 // FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
57 // SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
58 // FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
59 // ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
60 // DEALINGS IN THE SOFTWARE.
61 //
62 
63 
64 #ifndef MADNESS_WORLD_SCOPED_PTR_H__INCLUDED
65 #define MADNESS_WORLD_SCOPED_PTR_H__INCLUDED
66 
67 #include <madness/world/worldexc.h>
69 #include <cstddef>
70 
71 namespace madness {
72 
73  // This code is based on Boost scoped_ptr and scoped_array.
74 
75  // Forward declarations
76  template<typename>
77  class ScopedPtr;
78  template<typename ptrT>
79  inline void swap(ScopedPtr<ptrT>&, ScopedPtr<ptrT>&);
80  template<typename ptrT>
81  inline ptrT* get_pointer(const ScopedPtr<ptrT>&);
82 
83 
84  template<typename>
85  class ScopedArray;
86  template<typename ptrT>
88  template<typename ptrT>
89  inline ptrT* get_pointer(const ScopedArray<ptrT>&);
90 
92 
100  template<typename ptrT>
101  class ScopedPtr {
102  private:
103  ptrT* ptr_;
104 
105  // Not allowed
106  ScopedPtr(const ScopedPtr<ptrT>&);
107  ScopedPtr& operator=(const ScopedPtr<ptrT>&);
108  void operator==(const ScopedPtr<ptrT>&) const;
109  void operator!=(const ScopedPtr<ptrT>&) const;
110 
111  public:
112 
113  typedef ptrT element_type;
114 
116 
119  explicit ScopedPtr(ptrT* p = NULL) : ptr_(p) { }
120 
122 
124  ~ScopedPtr() { madness::detail::checked_delete<ptrT>(ptr_); }
125 
127 
133  void reset(ptrT* p = NULL) {
134  MADNESS_ASSERT(p == NULL || p != ptr_); // catch self-reset errors
135  ScopedPtr<ptrT>(p).swap(*this);
136  }
137 
139 
142  ptrT& operator*() const {
143  MADNESS_ASSERT(ptr_ != NULL);
144  return *ptr_;
145  }
146 
148 
151  ptrT* operator->() const {
152  MADNESS_ASSERT(ptr_ != NULL);
153  return ptr_;
154  }
155 
157 
160  ptrT* get() const { return ptr_; }
161 
163 
166  operator bool() const { return (ptr_ != NULL); }
167 
169 
172  bool operator! () const { return (ptr_ == NULL); }
173 
175 
178  void swap(ScopedPtr<ptrT>& other) {
179  ptrT* p = ptr_;
180  ptr_ = other.ptr_;
181  other.ptr_ = p;
182  }
183  }; // class ScopedPtr
184 
186 
191  template<typename ptrT>
193  a.swap(b);
194  }
195 
197 
200  template<typename ptrT>
201  inline ptrT* get_pointer(const ScopedPtr<ptrT>& p) {
202  return p.get();
203  }
204 
206 
215  template<typename ptrT>
216  class ScopedArray {
217  private:
218  ptrT* ptr_;
219 
220  // Not allowed
221  ScopedArray(const ScopedArray<ptrT>&);
222  ScopedArray& operator=(const ScopedArray<ptrT>&);
223  void operator==(const ScopedArray<ptrT>&) const;
224  void operator!=(const ScopedArray<ptrT>&) const;
225 
226  public:
227 
228  typedef ptrT element_type;
229 
231 
234  explicit ScopedArray(ptrT* p = NULL) : ptr_(p) { }
235 
237 
239  ~ScopedArray() { madness::detail::checked_array_delete<ptrT>(ptr_); }
240 
242 
248  void reset(ptrT* p = NULL) {
249  MADNESS_ASSERT(p == NULL || p != ptr_); // catch self-reset errors
250  ScopedArray<ptrT>(p).swap(*this);
251  }
252 
254 
257  ptrT& operator[](std::size_t i) const {
258  MADNESS_ASSERT(ptr_ != NULL);
259  return ptr_[i];
260  }
261 
263 
266  ptrT* get() const { return ptr_; }
267 
269 
272  operator bool() const { return (ptr_ != NULL); }
273 
275 
278  bool operator! () const { return (ptr_ == NULL); }
279 
281 
284  void swap(ScopedArray<ptrT>& other) {
285  ptrT* p = ptr_;
286  ptr_ = other.ptr_;
287  other.ptr_ = p;
288  }
289  }; // class ScopedArray
290 
292 
297  template<typename ptrT>
299  a.swap(b);
300  }
301 
303 
307  template<typename ptrT>
308  inline ptrT* get_pointer(const ScopedArray<ptrT>& p) {
309  return p.get();
310  }
311 
312 } // namespace madness
313 
314 #endif // MADNESS_WORLD_SCOPED_PTR_H__INCLUDED
ptrT element_type
the pointer type
Definition: scopedptr.h:113
bool operator!() const
Not operator.
Definition: scopedptr.h:172
ptrT * operator->() const
Pointer member accessor.
Definition: scopedptr.h:151
ptrT * get() const
Pointer accessor.
Definition: scopedptr.h:160
void reset(ptrT *p=NULL)
Reset the current pointer.
Definition: scopedptr.h:248
bool operator!() const
Not operator.
Definition: scopedptr.h:278
ScopedArray(ptrT *p=NULL)
Construct a scoped pointer.
Definition: scopedptr.h:234
ptrT & operator[](std::size_t i) const
Array element accessor.
Definition: scopedptr.h:257
void reset(ptrT *p=NULL)
Reset the current pointer.
Definition: scopedptr.h:133
ScopedPtr(ptrT *p=NULL)
Construct a scoped pointer.
Definition: scopedptr.h:119
ptrT element_type
the pointer type
Definition: scopedptr.h:228
void swap(ScopedPtr< ptrT > &other)
Swap the pointers.
Definition: scopedptr.h:178
Scoped pointer.
Definition: scopedptr.h:77
FLOAT a(int j, FLOAT z)
Definition: y1.cc:86
~ScopedPtr()
Destructor.
Definition: scopedptr.h:124
ptrT & operator*() const
Dereference operator.
Definition: scopedptr.h:142
Scoped array.
Definition: scopedptr.h:85
void swap(ScopedArray< ptrT > &other)
Swap the pointers.
Definition: scopedptr.h:284
void swap(Vector< T, N > &l, Vector< T, N > &r)
Definition: array.h:308
Implements MadnessException.
~ScopedArray()
Destructor.
Definition: scopedptr.h:239
Holds machinery to set up Functions/FuncImpls using various Factories and Interfaces.
Definition: chem/atomutil.cc:45
T * get_pointer(const detail::ReferenceWrapper< T > &r)
Function for retreaving a pointer to the referenced object.
Definition: ref.h:207
FLOAT b(int j, FLOAT z)
Definition: y1.cc:79