simgear/structure/SGSharedPtr.hxx: reformatted to make it readable.

This commit is contained in:
Julian Smith 2021-05-04 07:36:22 +01:00
parent 46e79aba11
commit 0b6678f745

View File

@ -49,89 +49,166 @@ class SGWeakPtr;
template<typename T> template<typename T>
class SGSharedPtr { class SGSharedPtr {
public: public:
typedef T element_type; typedef T element_type;
SGSharedPtr(void) noexcept SGSharedPtr(void) noexcept
: _ptr(0) : _ptr(0)
{} {}
SGSharedPtr(T* ptr) : _ptr(ptr)
{ get(_ptr); }
SGSharedPtr(const SGSharedPtr& p) : _ptr(p.get())
{ get(_ptr); }
SGSharedPtr(SGSharedPtr&& other) noexcept
: SGSharedPtr()
{ swap(other); }
template<typename U>
SGSharedPtr(const SGSharedPtr<U>& p) : _ptr(p.get())
{ get(_ptr); }
template<typename U>
explicit SGSharedPtr(const SGWeakPtr<U>& p): _ptr(0)
{ reset(p.lock().get()); }
~SGSharedPtr(void)
{ reset(); }
SGSharedPtr& operator=(const SGSharedPtr& p) SGSharedPtr(T* ptr) : _ptr(ptr)
{ reset(p.get()); return *this; } {
get(_ptr);
SGSharedPtr& operator=(SGSharedPtr&& p) noexcept
{ // Whether self-move is to be supported at all is controversial, let's
// take the conservative approach for now
if (this != &p) {
swap(p);
p.reset();
} }
return *this;
}
template<typename U> SGSharedPtr(const SGSharedPtr& p)
SGSharedPtr& operator=(const SGSharedPtr<U>& p) : _ptr(p.get())
{ reset(p.get()); return *this; } {
template<typename U> get(_ptr);
SGSharedPtr& operator=(U* p) }
{ reset(p); return *this; }
T* operator->(void) const SGSharedPtr(SGSharedPtr&& other) noexcept
{ return _ptr; } : SGSharedPtr()
T& operator*(void) const {
{ return *_ptr; } swap(other);
operator T*(void) const }
{ return _ptr; }
T* ptr(void) const
{ return _ptr; }
T* get(void) const
{ return _ptr; }
T* release()
{ T* tmp = _ptr; _ptr = 0; T::put(tmp); return tmp; }
void reset() noexcept
{ if (!T::put(_ptr)) delete _ptr; _ptr = 0; }
void reset(T* p)
{ SGSharedPtr(p).swap(*this); }
bool isShared(void) const template<typename U>
{ return T::shared(_ptr); } SGSharedPtr(const SGSharedPtr<U>& p)
unsigned getNumRefs(void) const : _ptr(p.get())
{ return T::count(_ptr); } {
get(_ptr);
}
bool valid(void) const template<typename U>
{ return _ptr != (T*)0; } explicit SGSharedPtr(const SGWeakPtr<U>& p)
: _ptr(0)
{
reset(p.lock().get());
}
void clear() ~SGSharedPtr(void)
{ reset(); } {
void swap(SGSharedPtr& other) noexcept reset();
{ simgear::noexceptSwap(_ptr, other._ptr); } }
SGSharedPtr& operator=(const SGSharedPtr& p)
{
reset(p.get());
return *this;
}
SGSharedPtr& operator=(SGSharedPtr&& p) noexcept
{
// Whether self-move is to be supported at all is controversial, let's
// take the conservative approach for now
if (this != &p) {
swap(p);
p.reset();
}
return *this;
}
template<typename U>
SGSharedPtr& operator=(const SGSharedPtr<U>& p)
{
reset(p.get());
return *this;
}
template<typename U>
SGSharedPtr& operator=(U* p)
{
reset(p);
return *this;
}
T* operator->(void) const
{
return _ptr;
}
T& operator*(void) const
{
return *_ptr;
}
operator T*(void) const
{
return _ptr;
}
T* ptr(void) const
{
return _ptr;
}
T* get(void) const
{
return _ptr;
}
T* release()
{
T* tmp = _ptr;
_ptr = 0;
T::put(tmp);
return tmp;
}
void reset() noexcept
{
if (!T::put(_ptr)) delete _ptr;
_ptr = 0;
}
void reset(T* p)
{
SGSharedPtr(p).swap(*this);
}
bool isShared(void) const
{
return T::shared(_ptr);
}
unsigned getNumRefs(void) const
{
return T::count(_ptr);
}
bool valid(void) const
{
return _ptr != (T*)0;
}
void clear()
{
reset();
}
void swap(SGSharedPtr& other) noexcept
{
simgear::noexceptSwap(_ptr, other._ptr);
}
private: private:
void assignNonRef(T* p)
{ reset(); _ptr = p; } void assignNonRef(T* p)
{
reset();
_ptr = p;
}
void get(const T* p) const void get(const T* p) const
{ T::get(p); } {
T::get(p);
// The reference itself. }
T* _ptr;
template<typename U> // The reference itself.
friend class SGWeakPtr; T* _ptr;
template<typename U>
friend class SGWeakPtr;
}; };
template<class T> template<class T>