add jes::Ref template
This commit is contained in:
parent
4ebedcf301
commit
9abd4ef04b
@ -1,6 +1,8 @@
|
|||||||
#ifndef JES_FILEREADER_H
|
#ifndef JES_FILEREADER_H
|
||||||
#define JES_FILEREADER_H
|
#define JES_FILEREADER_H
|
||||||
|
|
||||||
|
#include "jes/Ref.h"
|
||||||
|
|
||||||
namespace jes
|
namespace jes
|
||||||
{
|
{
|
||||||
class FileReader
|
class FileReader
|
||||||
@ -9,6 +11,7 @@ namespace jes
|
|||||||
bool load(const char * fname);
|
bool load(const char * fname);
|
||||||
protected:
|
protected:
|
||||||
};
|
};
|
||||||
|
typedef Ref<FileReader> FileReaderRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
102
src/lib/include/jes/Ref.h
Normal file
102
src/lib/include/jes/Ref.h
Normal file
@ -0,0 +1,102 @@
|
|||||||
|
#ifndef JES_REF_H
|
||||||
|
#define JES_REF_H
|
||||||
|
|
||||||
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
namespace jes
|
||||||
|
{
|
||||||
|
template <typename T>
|
||||||
|
class Ref
|
||||||
|
{
|
||||||
|
public:
|
||||||
|
Ref<T>();
|
||||||
|
Ref<T>(T * ptr);
|
||||||
|
Ref<T>(const Ref<T> & orig);
|
||||||
|
Ref<T> & operator=(const Ref<T> & orig);
|
||||||
|
Ref<T> & operator=(T * ptr);
|
||||||
|
~Ref<T>();
|
||||||
|
T & operator*() const { return *m_ptr; }
|
||||||
|
T * operator->() const { return m_ptr; }
|
||||||
|
bool is_null() const { return m_ptr == NULL; }
|
||||||
|
|
||||||
|
private:
|
||||||
|
void clone_from(const Ref<T> & orig);
|
||||||
|
void destroy();
|
||||||
|
|
||||||
|
T * m_ptr;
|
||||||
|
unsigned int * m_ref_count;
|
||||||
|
};
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Ref<T>::Ref()
|
||||||
|
{
|
||||||
|
m_ptr = NULL;
|
||||||
|
m_ref_count = NULL;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Ref<T>::Ref(T * ptr)
|
||||||
|
{
|
||||||
|
m_ptr = ptr;
|
||||||
|
m_ref_count = new unsigned int;
|
||||||
|
*m_ref_count = 1u;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Ref<T>::Ref(const Ref<T> & orig)
|
||||||
|
{
|
||||||
|
clone_from(orig);
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Ref<T> & Ref<T>::operator=(const Ref<T> & orig)
|
||||||
|
{
|
||||||
|
destroy();
|
||||||
|
clone_from(orig);
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Ref<T> & Ref<T>::operator=(T * ptr)
|
||||||
|
{
|
||||||
|
destroy();
|
||||||
|
m_ptr = ptr;
|
||||||
|
m_ref_count = new unsigned int;
|
||||||
|
*m_ref_count = 1u;
|
||||||
|
return *this;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void Ref<T>::clone_from(const Ref<T> & orig)
|
||||||
|
{
|
||||||
|
this->m_ptr = orig.m_ptr;
|
||||||
|
this->m_ref_count = orig.m_ref_count;
|
||||||
|
if (m_ref_count != NULL)
|
||||||
|
(*m_ref_count)++;
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
Ref<T>::~Ref()
|
||||||
|
{
|
||||||
|
destroy();
|
||||||
|
}
|
||||||
|
|
||||||
|
template <typename T>
|
||||||
|
void Ref<T>::destroy()
|
||||||
|
{
|
||||||
|
if (m_ref_count != NULL)
|
||||||
|
{
|
||||||
|
if (*m_ref_count <= 1u)
|
||||||
|
{
|
||||||
|
delete m_ptr;
|
||||||
|
delete m_ref_count;
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
(*m_ref_count)--;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
@ -1,11 +1,14 @@
|
|||||||
#ifndef JES_TEXT_H
|
#ifndef JES_TEXT_H
|
||||||
#define JES_TEXT_H
|
#define JES_TEXT_H
|
||||||
|
|
||||||
|
#include "jes/Ref.h"
|
||||||
|
|
||||||
namespace jes
|
namespace jes
|
||||||
{
|
{
|
||||||
class Text
|
class Text
|
||||||
{
|
{
|
||||||
};
|
};
|
||||||
|
typedef Ref<Text> TextRef;
|
||||||
}
|
}
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
x
Reference in New Issue
Block a user