| Disk ARchive 2.8.5 Full featured and portable backup and archiving tool

Loading...
Searching...
No Matches
smart_pointer.hpp
Go to the documentation of this file.
1/*********************************************************************/
2// dar - disk archive - a backup/restoration program
3// Copyright (C) 2002-2026 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18//
19// to contact the author, see the AUTHOR file
20/*********************************************************************/
21
27
28#ifndef SMART_POINTER_HPP
29#define SMART_POINTER_HPP
30
31#include "../my_config.h"
32
33#include "infinint.hpp"
34#include "erreurs.hpp"
35
36 namespace libdar
37{
38
39
42
44
46 template <class T> class smart_node
47 {
48 public:
50 smart_node(T *arg): ptr(arg), count_ref(0) { if(arg == nullptr) throw SRC_BUG; };
51 smart_node(const smart_node & ref) = delete;
52 smart_node(smart_node && ref) noexcept = delete;
53 smart_node & operator = (const smart_node & ref) = delete;
54 smart_node & operator = (smart_node && ref) = delete;
55 ~smart_node() noexcept(false) { if(ptr != nullptr) delete ptr; if(!count_ref.is_zero()) throw SRC_BUG; };
56
57 void add_ref() { ++count_ref; };
58 void del_ref() { if(count_ref.is_zero()) throw SRC_BUG; --count_ref; if(count_ref.is_zero()) delete this; };
59 T & get_val() { return *ptr; };
60
61 private:
62 T *ptr;
63 infinint count_ref;
64
65 };
66
67
69
78 template <class T> class smart_pointer
79 {
80 public:
82 smart_pointer() { ptr = nullptr; };
83
85
90 smart_pointer(T *arg)
91 {
92 if(arg != nullptr)
93 {
94 ptr = new (std::nothrow) smart_node<T>(arg);
95 if(ptr == nullptr)
96 throw Ememory("smart_pointer::smart_pointer");
97 ptr->add_ref();
98 }
99 else
100 ptr = nullptr;
101 };
102
104 smart_pointer(const smart_pointer & ref) { ptr = ref.ptr; if(ptr != nullptr) ptr->add_ref(); };
105
107 smart_pointer(smart_pointer && ref) noexcept { ptr = ref.ptr; ref.ptr = nullptr; };
108
110 ~smart_pointer() { if(ptr != nullptr) ptr->del_ref(); };
111
113 smart_pointer & operator = (const smart_pointer & ref)
114 {
115 if(ref.ptr != ptr)
116 {
117 if(ref.ptr != nullptr)
118 {
119 if(ptr != nullptr)
120 ptr->del_ref();
121 ptr = ref.ptr;
122 ptr->add_ref();
123 }
124 else
125 {
126 ptr->del_ref(); // ptr is no nullptr because ref.ptr != ptr
127 ptr = nullptr;
128 }
129 }
130 return *this;
131 };
132
134 smart_pointer & operator = (smart_pointer && ref)
135 {
136 if(ptr != ref.ptr)
137 {
138 if(ptr != nullptr)
139 ptr->del_ref();
140 ptr = ref.ptr;
141 ref.ptr = nullptr;
142 }
143
144 return *this;
145 };
146
148
151 const smart_pointer & assign(T *arg)
152 {
153 smart_pointer<T> tmp(arg);
154 *this = tmp;
155 return *this;
156 }
157
159 T & operator *() const { if(ptr == nullptr) throw SRC_BUG; return ptr->get_val(); };
160
162 T* operator ->() const { if(ptr == nullptr) throw SRC_BUG; return &(ptr->get_val()); };
163
165 bool is_null() const { return ptr == nullptr; };
166
167 private:
168 smart_node<T> *ptr;
169 };
170
172
173} // end of namespace
174
175#endif
libdar::Ememory
exception used when memory has been exhausted
Definition erreurs.hpp:127
libdar::smart_node
class which holds the address of the allocated memory for many smart_pointers
Definition smart_pointer.hpp:47
libdar::smart_node::smart_node
smart_node(T *arg)
Definition smart_pointer.hpp:50
libdar::smart_pointer::operator=
smart_pointer & operator=(const smart_pointer &ref)
assignment operator
Definition smart_pointer.hpp:113
libdar::smart_pointer::is_null
bool is_null() const
return whether the smart_pointer is pointing to nullptr
Definition smart_pointer.hpp:165
libdar::smart_pointer::smart_pointer
smart_pointer(T *arg)
creates a smart_pointer pointing to an allocated memory
Definition smart_pointer.hpp:90
libdar::smart_pointer::operator*
T & operator*() const
content-of operator
Definition smart_pointer.hpp:159
libdar::smart_pointer::assign
const smart_pointer & assign(T *arg)
assignment operator from a base type pointer (not from a smart_pointer)
Definition smart_pointer.hpp:151
libdar::smart_pointer::smart_pointer
smart_pointer(smart_pointer &&ref) noexcept
move constructor
Definition smart_pointer.hpp:107
libdar::smart_pointer::smart_pointer
smart_pointer(const smart_pointer &ref)
copy constructor
Definition smart_pointer.hpp:104
libdar::smart_pointer::operator->
T * operator->() const
content-of field operator (when the pointed to object is a struct or class
Definition smart_pointer.hpp:162
libdar::smart_pointer::smart_pointer
smart_pointer()
creates a smart_pointer equivalent to a pointer to NULL
Definition smart_pointer.hpp:82
libdar::smart_pointer::~smart_pointer
~smart_pointer()
destructor
Definition smart_pointer.hpp:110
erreurs.hpp
contains all the excetion class thrown by libdar
libdar::infinint::is_zero
bool is_zero() const
infinint.hpp
switch module to limitint (32 ou 64 bits integers) or infinint
my_config.h
include macro defined by the configure script and some specific additional ones
libdar
libdar namespace encapsulate all libdar symbols
Definition archive.hpp:47