File: smart_pointer.hpp

package info (click to toggle)
dar 2.6.13-2
  • links: PTS
  • area: main
  • in suites: bullseye
  • size: 10,364 kB
  • sloc: cpp: 77,385; sh: 6,192; ansic: 776; makefile: 435; python: 242; csh: 95; perl: 43; sed: 16
file content (175 lines) | stat: -rw-r--r-- 5,776 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2020 Denis Corbin
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
//
// to contact the author : http://dar.linux.free.fr/email.html
/*********************************************************************/

    /// \file smart_pointer.hpp
    /// \brief template class implementing memory efficient smart pointer
    /// \ingroup Private
    /// \note Why not using std::shared_ptr? because I needed to have a warranty on the
    /// scalability in term of max number of smart_pointers that can be bound to an object

#ifndef SMART_POINTER_HPP
#define SMART_POINTER_HPP

#include "../my_config.h"

#include "infinint.hpp"
#include "erreurs.hpp"

namespace libdar
{


	/// \addtogroup Private
        /// @{

	/// class which holds the address of the allocated memory for many smart_pointers

	/// \note it should not be used directly, rather see below the smart_pointer class template
    template <class T> class smart_node
    {
    public:
	    /// \note the given pointed to object passes under the responsibility of the smart_node
	smart_node(T *arg): ptr(arg), count_ref(0) { if(arg == nullptr) throw SRC_BUG; };
	smart_node(const smart_node & ref) = delete;
	smart_node(smart_node && ref) noexcept = delete;
	smart_node & operator = (const smart_node & ref) = delete;
	smart_node & operator = (smart_node && ref) noexcept = delete;
        ~smart_node() noexcept(false) { if(ptr != nullptr) delete ptr; if(!count_ref.is_zero()) throw SRC_BUG; };

	void add_ref() { ++count_ref; };
	void del_ref() { if(count_ref.is_zero()) throw SRC_BUG; --count_ref; if(count_ref.is_zero()) delete this; };
	T & get_val() { return *ptr; };

    private:
	T *ptr;
	infinint count_ref;

    };


	/// smart pointer class to be used to automagically manage multiple time pointed to address

	/// this class tend to mimic normal pointer with the additional feature of automatically releasing
	/// the pointed to object when no more smart_pointer point to it. In consequence:
	/// - it must not be used to point to non dynamically allocated memory using the "new" operator,
	/// - pointed to memory must never be deleted manually, the last smart_pointer will do it at its
	///   destruction time
	/// \note IMPORTANT: smart_pointer cannot be shared between different threads, for efficiency,
	/// the smart_pointer nor the pointed to object is protected against concurrent access of several
	/// threads
    template <class T> class smart_pointer
    {
    public:
	    /// creates a smart_pointer equivalent to a pointer to NULL
	smart_pointer() { ptr = nullptr; };

	    /// creates a smart_pointer pointing to an allocated memory

	    /// \param[in] arg is the address of the allocated memory the smart_pointer must manage,
	    /// nullptr is allowed and lead to the same behavior as the constructor without argument
	    /// \note the given pointed to object, passes under the responsibility of the smart_pointer
	    /// and must not be deleted any further
	smart_pointer(T *arg)
	{
	    if(arg != nullptr)
	    {
		ptr = new (std::nothrow) smart_node<T>(arg);
		if(ptr == nullptr)
		    throw Ememory("smart_pointer::smart_pointer");
		ptr->add_ref();
	    }
	    else
		ptr = nullptr;
	};

	    /// copy constructor
	smart_pointer(const smart_pointer & ref) { ptr = ref.ptr; if(ptr != nullptr) ptr->add_ref(); };

	    /// move constructor
	smart_pointer(smart_pointer && ref) noexcept { ptr = ref.ptr; ref.ptr = nullptr; };

	    /// destructor
	~smart_pointer() { if(ptr != nullptr) ptr->del_ref(); };

	    /// assignment operator
	smart_pointer & operator = (const smart_pointer & ref)
	{
	    if(ref.ptr != ptr)
	    {
		if(ref.ptr != nullptr)
		{
		    if(ptr != nullptr)
			ptr->del_ref();
		    ptr = ref.ptr;
		    ptr->add_ref();
		}
		else
		{
		    ptr->del_ref(); // ptr is no nullptr because ref.ptr != ptr
		    ptr = nullptr;
		}
	    }
	    return *this;
	};

	    /// move assignment operator
	smart_pointer & operator = (smart_pointer && ref) noexcept
	{
	    if(ptr != ref.ptr)
	    {
		if(ptr != nullptr)
		    ptr->del_ref();
		ptr = ref.ptr;
		ref.ptr = nullptr;
	    }

	    return *this;
	};

	    /// assignment operator from a base type pointer (not from a smart_pointer)

	    /// \note choice has been not to overload/use operator= to avoid risk of error
	    /// that would lead to create independent smart_pointer sets accidentally
	const smart_pointer & assign(T *arg)
	{
	    smart_pointer<T> tmp(arg);
	    *this = tmp;
	    return *this;
	}

	    /// content-of operator
	T & operator *() const { if(ptr == nullptr) throw SRC_BUG; return ptr->get_val(); };

	    /// content-of field operator (when the pointed to object is a struct or class
	T* operator ->() const { if(ptr == nullptr) throw SRC_BUG; return &(ptr->get_val()); };

	    /// return whether the smart_pointer is pointing to nullptr
	bool is_null() const { return ptr == nullptr; };

    private:
	smart_node<T> *ptr;
    };

	/// @}

} // end of namespace

#endif