LevelS C++ support library  3.50
safe_ptr.h
Go to the documentation of this file.
1 /*
2  * This file is part of libcxxsupport.
3  *
4  * libcxxsupport is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * libcxxsupport is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with libcxxsupport; if not, write to the Free Software
16  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
17  */
18 
19 /*
20  * libcxxsupport is being developed at the Max-Planck-Institut fuer Astrophysik
21  * and financially supported by the Deutsches Zentrum fuer Luft- und Raumfahrt
22  * (DLR).
23  */
24 
25 /*! \file safe_ptr.h
26  * Pointer wrapper for better exception safety and leakage prevention
27  *
28  * Copyright (C) 2005-2015 Max-Planck-Society
29  * \author Martin Reinecke
30  */
31 
32 #ifndef PLANCK_SAFE_PTR_H
33 #define PLANCK_SAFE_PTR_H
34 
35 #if (__cplusplus>=201103L)
36 
37 #include <memory>
38 template<typename T> using safe_ptr = std::unique_ptr<T>;
39 
40 #else
41 
42 #include "error_handling.h"
43 
44 template <typename T> class safe_ptr
45  {
46  private:
47  T *p;
48  bool set;
49 
50  // forbid copying of safe_ptrs, at least until we know how to do it
51  safe_ptr (const safe_ptr &) {}
52  safe_ptr &operator= (const safe_ptr &) { return *this; }
53 
54  public:
55  safe_ptr() : p(0), set(false) {}
56  safe_ptr (T *p2) : p(p2), set(true) {}
57  ~safe_ptr() { delete p; }
58 
59  void reset (T *p2)
60  {
61  planck_assert (!set, "safe_ptr: already set");
62  set = true;
63  p=p2;
64  }
65 
66  void reset()
67  {
68  delete p;
69  p=0;
70  set=false;
71  }
72 
73  T * get() const
74  { return p; }
75 
76  operator T*() { return p; }
77  operator const T*() const { return p; }
78  T *operator->() { return p; }
79  const T *operator->() const { return p; }
80  };
81 
82 #endif
83 
84 #endif
#define planck_assert(testval, msg)

Generated on Mon Dec 10 2018 10:24:20 for LevelS C++ support library