All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
shared_ptr.hpp
Go to the documentation of this file.
#ifndef VIENNACL_TOOLS_SHARED_PTR_HPP
#define VIENNACL_TOOLS_SHARED_PTR_HPP
/* =========================================================================
Copyright (c) 2010-2016, Institute for Microelectronics,
Institute for Analysis and Scientific Computing,
TU Wien.
Portions of this software are copyright by UChicago Argonne, LLC.
-----------------
ViennaCL - The Vienna Computing Library
-----------------
Project Head: Karl Rupp rupp@iue.tuwien.ac.at
(A list of authors and contributors can be found in the manual)
License: MIT (X11), see file LICENSE in the base directory
============================================================================= */
#include <cstdlib>
#include <algorithm>
namespace viennacl
{
namespace tools
{
namespace detail
{
class count
{
public:
count(unsigned int val) : val_(val){ }
void dec(){ --val_; }
void inc(){ ++val_; }
bool is_null(){ return val_ == 0; }
unsigned int val(){ return val_; }
private:
unsigned int val_;
};
struct aux
{
detail::count count;
aux() :count(1) {}
virtual void destroy()=0;
virtual ~aux() {}
};
template<class U, class Deleter>
struct auximpl: public detail::aux
{
U* p;
Deleter d;
auximpl(U* pu, Deleter x) :p(pu), d(x) {}
virtual void destroy() { d(p); }
};
template<class U>
struct default_deleter
{
void operator()(U* p) const { delete p; }
};
}
template<class T>
class shared_ptr
{
template<class U>
friend class shared_ptr;
detail::aux* pa;
T* pt;
public:
shared_ptr() :pa(NULL), pt(NULL) {}
template<class U, class Deleter>
shared_ptr(U* pu, Deleter d) : pa(new detail::auximpl<U, Deleter>(pu, d)), pt(pu) {}
template<class U>
explicit shared_ptr(U* pu) : pa(new detail::auximpl<U, detail::default_deleter<U> >(pu, detail::default_deleter<U>())), pt(pu) {}
T* get() const { return pt; }
T* operator->() const { return pt; }
T& operator*() const { return *pt; }
shared_ptr(const shared_ptr& s) :pa(s.pa), pt(s.pt)
{
inc();
}
template<class U>
shared_ptr(const shared_ptr<U>& s) :pa(s.pa), pt(s.pt)
{
inc();
}
~shared_ptr()
{
dec();
}
void reset()
{
shared_ptr<T>().swap(*this);
}
void reset(T * ptr)
{
shared_ptr<T>(ptr).swap(*this);
}
void swap(shared_ptr<T> & other)
{
std::swap(pt,other.pt);
std::swap(pa, other.pa);
}
shared_ptr& operator=(const shared_ptr& s)
{
if (this!=&s)
{
dec();
pa = s.pa;
pt = s.pt;
inc();
}
return *this;
}
void inc()
{
if (pa) pa->count.inc();
}
void dec()
{
if (pa)
{
pa->count.dec();
if (pa->count.is_null())
{
pa->destroy();
delete pa;
pa = NULL;
}
}
}
};
}
}
#endif // VIENNACL_UTILS_SHARED_PTR_HPP
viennacl::tools::detail::aux::aux
aux()
Definition: shared_ptr.hpp:56
viennacl::tools::shared_ptr::operator=
shared_ptr & operator=(const shared_ptr &s)
Definition: shared_ptr.hpp:140
viennacl::tools::detail::aux::~aux
virtual ~aux()
Definition: shared_ptr.hpp:58
viennacl::tools::detail::aux::count
detail::count count
Definition: shared_ptr.hpp:54
viennacl::tools::shared_ptr::reset
void reset(T *ptr)
Definition: shared_ptr.hpp:128
viennacl::tools::shared_ptr::inc
void inc()
Definition: shared_ptr.hpp:154
viennacl::tools::detail::auximpl::d
Deleter d
Definition: shared_ptr.hpp:66
viennacl::tools::shared_ptr::dec
void dec()
Definition: shared_ptr.hpp:159
viennacl::tools::detail::default_deleter::operator()
void operator()(U *p) const
Definition: shared_ptr.hpp:76
viennacl::tools::detail::aux
Interface for the reference counter inside the shared_ptr.
Definition: shared_ptr.hpp:52
viennacl::tools::shared_ptr::swap
void swap(shared_ptr< T > &other)
Definition: shared_ptr.hpp:133
viennacl::tools::detail::auximpl
Implementation helper for the reference counting mechanism inside shared_ptr.
Definition: shared_ptr.hpp:63
viennacl::tools::shared_ptr::shared_ptr
shared_ptr(U *pu)
Definition: shared_ptr.hpp:99
viennacl::tools::detail::default_deleter
Default deleter class for a pointer. The default is to just call 'delete' on the pointer. Provide your own implementations for 'delete[]' and 'free'.
Definition: shared_ptr.hpp:74
viennacl::tools::detail::auximpl::destroy
virtual void destroy()
Definition: shared_ptr.hpp:69
viennacl::tools::detail::auximpl::p
U * p
Definition: shared_ptr.hpp:65
viennacl::tools::shared_ptr::operator->
T * operator->() const
Definition: shared_ptr.hpp:103
viennacl::tools::shared_ptr::~shared_ptr
~shared_ptr()
Definition: shared_ptr.hpp:118
viennacl::tools::shared_ptr
A shared pointer class similar to boost::shared_ptr. Reimplemented in order to avoid a Boost-dependen...
Definition: shared_ptr.hpp:83
viennacl::tools::shared_ptr::operator*
T & operator*() const
Definition: shared_ptr.hpp:105
viennacl::linalg::cuda::swap
viennacl::enable_if< viennacl::is_scalar< ScalarT1 >::value &&viennacl::is_scalar< ScalarT2 >::value >::type swap(ScalarT1 &s1, ScalarT2 &s2)
Swaps the contents of two scalars, data is copied.
Definition: scalar_operations.hpp:361
viennacl::tools::detail::count::val
unsigned int val()
Definition: shared_ptr.hpp:46
viennacl::tools::detail::count
Reference counting class for the shared_ptr implementation.
Definition: shared_ptr.hpp:39
viennacl::tools::shared_ptr::shared_ptr
shared_ptr(U *pu, Deleter d)
Definition: shared_ptr.hpp:96
viennacl::tools::detail::aux::destroy
virtual void destroy()=0
viennacl::tools::detail::count::inc
void inc()
Definition: shared_ptr.hpp:44
viennacl::tools::shared_ptr::reset
void reset()
Definition: shared_ptr.hpp:123
viennacl::tools::detail::count::count
count(unsigned int val)
Definition: shared_ptr.hpp:42
viennacl::tools::detail::count::is_null
bool is_null()
Definition: shared_ptr.hpp:45
viennacl::tools::detail::count::dec
void dec()
Definition: shared_ptr.hpp:43
viennacl::tools::detail::auximpl::auximpl
auximpl(U *pu, Deleter x)
Definition: shared_ptr.hpp:68
viennacl::tools::shared_ptr::shared_ptr
shared_ptr()
Definition: shared_ptr.hpp:93
viennacl::tools::shared_ptr::shared_ptr
shared_ptr(const shared_ptr< U > &s)
Definition: shared_ptr.hpp:113
viennacl::tools::shared_ptr::shared_ptr
shared_ptr(const shared_ptr &s)
Definition: shared_ptr.hpp:107