Loading...
Searching...
No Matches
smart_pointer.hpp
Go to the documentation of this file.
/*********************************************************************/
// dar - disk archive - a backup/restoration program
// Copyright (C) 2002-2026 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, see the AUTHOR file
/*********************************************************************/
#ifndef SMART_POINTER_HPP
#define SMART_POINTER_HPP
#include "../my_config.h"
#include "infinint.hpp"
#include "erreurs.hpp"
namespace libdar
{
template <class T> class smart_node
{
public:
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) = 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;
};
template <class T> class smart_pointer
{
public:
smart_pointer() { ptr = nullptr; };
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;
};
smart_pointer(const smart_pointer & ref) { ptr = ref.ptr; if(ptr != nullptr) ptr->add_ref(); };
smart_pointer(smart_pointer && ref) noexcept { ptr = ref.ptr; ref.ptr = nullptr; };
~smart_pointer() { if(ptr != nullptr) ptr->del_ref(); };
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;
};
smart_pointer & operator = (smart_pointer && ref)
{
if(ptr != ref.ptr)
{
if(ptr != nullptr)
ptr->del_ref();
ptr = ref.ptr;
ref.ptr = nullptr;
}
return *this;
};
const smart_pointer & assign(T *arg)
{
smart_pointer<T> tmp(arg);
*this = tmp;
return *this;
}
T & operator *() const { if(ptr == nullptr) throw SRC_BUG; return ptr->get_val(); };
T* operator ->() const { if(ptr == nullptr) throw SRC_BUG; return &(ptr->get_val()); };
bool is_null() const { return ptr == nullptr; };
private:
smart_node<T> *ptr;
};
} // end of namespace
#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