Skip to content

Instantly share code, notes, and snippets.

@DCubix
Last active December 9, 2016 02:50
Show Gist options
  • Save DCubix/2c65ab4c9dae64d1f96b1fad76aa9a04 to your computer and use it in GitHub Desktop.
Save DCubix/2c65ab4c9dae64d1f96b1fad76aa9a04 to your computer and use it in GitHub Desktop.
Simple Smart Pointer CLass for C++
/*
* The MIT License
*
* Copyright 2016 twisterge.
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
/*
* File: Ptr.h
* Author: twisterge
*
* Created on December 8, 2016, 11:57 PM
*/
#ifndef PTR_H
#define PTR_H
typedef unsigned int uint;
#define SAFE_DELETE(x) if (x) { delete x; x = nullptr; }
class ReferenceCounter {
public:
void AddReference() { m_refs++; }
int Release() { return m_refs--; }
int GetReferenceCount() const { return m_refs; }
private:
uint m_refs;
};
template <typename T>
class Ptr {
public:
Ptr()
: m_data(nullptr), m_refCounter(nullptr)
{
m_refCounter = new ReferenceCounter();
m_refCounter->AddReference();
}
Ptr(T* val)
: m_data(val), m_refCounter(nullptr)
{
m_refCounter = new ReferenceCounter();
m_refCounter->AddReference();
}
Ptr(const Ptr<T>& v)
: m_data(v.m_data), m_refCounter(v.m_refCounter)
{
m_refCounter->AddReference();
}
~Ptr() {
if (m_refCounter->Release() <= 0) {
SAFE_DELETE(m_data);
SAFE_DELETE(m_refCounter);
}
}
T& operator* () {
return *m_data;
}
T& operator* () const {
return *m_data;
}
T* operator-> () {
return m_data;
}
T* operator-> () const {
return m_data;
}
explicit operator T*() {
m_refCounter->AddReference();
return m_data;
}
operator bool() {
return m_data != nullptr;
}
operator bool() const {
return m_data != nullptr;
}
bool operator !() {
return !m_data;
}
bool operator !() const {
return !m_data;
}
Ptr<T>& operator= (const Ptr<T>& v) {
if (this != &v) {
if (m_refCounter->Release() <= 0) {
SAFE_DELETE(m_data);
SAFE_DELETE(m_refCounter);
}
m_data = v.m_data;
m_refCounter = v.m_refCounter;
m_refCounter->AddReference();
}
return *this;
}
bool operator ==(const Ptr<T>& v) {
return m_data == v.m_data;
}
bool operator !=(const Ptr<T>& v) {
return m_data != v.m_data;
}
bool operator ==(T* v) {
return m_data == v;
}
bool operator !=(T* v) {
return m_data != v;
}
private:
T* m_data;
ReferenceCounter* m_refCounter;
};
#endif // PTR_H
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment