Disk ARchive  2.5.14-bis
Full featured and portable backup and archiving tool
smart_pointer.hpp
Go to the documentation of this file.
1 /*********************************************************************/
2 // dar - disk archive - a backup/restoration program
3 // Copyright (C) 2002-2052 Denis Corbin
4 //
5 // This program is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU General Public License
7 // as published by the Free Software Foundation; either version 2
8 // of the License, or (at your option) any later version.
9 //
10 // This program is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with this program; if not, write to the Free Software
17 // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
18 //
19 // to contact the author : http://dar.linux.free.fr/email.html
20 /*********************************************************************/
21 
25 
26 #ifndef SMART_POINTER_HPP
27 #define SMART_POINTER_HPP
28 
29 #include "../my_config.h"
30 
31 #include "infinint.hpp"
32 #include "erreurs.hpp"
33 #include "on_pool.hpp"
34 
35 namespace libdar
36 {
37 
41 
42  template <class T> class smart_node: public on_pool
43  {
44  public:
46  smart_node(T *arg): ptr(arg), count_ref(0) { if(arg == nullptr) throw SRC_BUG; };
47  ~smart_node() { if(ptr != nullptr) delete ptr; if(!count_ref.is_zero()) throw SRC_BUG; };
48 
49  void add_ref() { ++count_ref; };
50  void del_ref() { if(count_ref.is_zero()) throw SRC_BUG; --count_ref; if(count_ref.is_zero()) delete this; };
51  T & get_val() { return *ptr; };
52 
53  private:
54  T *ptr;
55  infinint count_ref;
56 
57  smart_node(const smart_node & ref) { throw SRC_BUG; };
58  const smart_node & operator = (const smart_node & ref) { throw SRC_BUG; };
59  };
60 
61 
69 
70  template <class T> class smart_pointer: public on_pool
71  {
72  public:
74  smart_pointer() { ptr = nullptr; };
75 
82  smart_pointer(T *arg)
83  {
84  if(arg != nullptr)
85  {
86  ptr = new (get_pool()) smart_node<T>(arg);
87  if(ptr == nullptr)
88  throw Ememory("smart_pointer::smart_pointer");
89  ptr->add_ref();
90  }
91  else
92  ptr = nullptr;
93  };
94 
96  smart_pointer(const smart_pointer & ref) { ptr = ref.ptr; if(ptr != nullptr) ptr->add_ref(); };
97 
99  ~smart_pointer() { if(ptr != nullptr) ptr->del_ref(); };
100 
102  const smart_pointer & operator = (const smart_pointer & ref)
103  {
104  if(ref.ptr != ptr)
105  {
106  if(ref.ptr != nullptr)
107  {
108  if(ptr != nullptr)
109  ptr->del_ref();
110  ptr = ref.ptr;
111  ptr->add_ref();
112  }
113  else
114  {
115  ptr->del_ref(); // ptr is no nullptr because ref.ptr != ptr
116  ptr = nullptr;
117  }
118  }
119  return *this;
120  };
121 
126  const smart_pointer & assign(T *arg)
127  {
128  smart_pointer<T> tmp(arg);
129  *this = tmp;
130  return *this;
131  }
132 
134  T & operator *() const { if(ptr == nullptr) throw SRC_BUG; return ptr->get_val(); };
135 
137  T* operator ->() const { if(ptr == nullptr) throw SRC_BUG; return &(ptr->get_val()); };
138 
140  bool is_null() const { return ptr == nullptr; };
141 
142  private:
143  smart_node<T> *ptr;
144  };
145 
146 } // end of namespace
147 
148 #endif
memory_pool * get_pool() const
Definition: on_pool.hpp:144
smart_pointer(const smart_pointer &ref)
copy constructor
smart_pointer()
creates a smart_pointer equivalent to a pointer to NULL
exception used when memory has been exhausted
Definition: erreurs.hpp:111
contains all the excetion class thrown by libdar
switch module to limitint (32 ou 64 bits integers) or infinint
bool is_null() const
return whether the smart_pointer is pointing to nullptr
const smart_pointer & assign(T *arg)
this is the base class of object that can be allocated on a memory pool
~smart_pointer()
destructor
the arbitrary large positive integer class
libdar namespace encapsulate all libdar symbols
Definition: archive.hpp:47