/*
Minetest
-Copyright (C) 2010-2012 celeron55, Perttu Ahola <celeron55@gmail.com>
+Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
#include "../debug.h" // For assert()
#include <cstring>
-template <typename T>
-class SharedPtr
-{
-public:
- SharedPtr(T *t=NULL)
- {
- refcount = new int;
- *refcount = 1;
- ptr = t;
- }
- SharedPtr(SharedPtr<T> &t)
- {
- //*this = t;
- drop();
- refcount = t.refcount;
- (*refcount)++;
- ptr = t.ptr;
- }
- ~SharedPtr()
- {
- drop();
- }
- SharedPtr<T> & operator=(T *t)
- {
- drop();
- refcount = new int;
- *refcount = 1;
- ptr = t;
- return *this;
- }
- SharedPtr<T> & operator=(SharedPtr<T> &t)
- {
- drop();
- refcount = t.refcount;
- (*refcount)++;
- ptr = t.ptr;
- return *this;
- }
- T* operator->()
- {
- return ptr;
- }
- T & operator*()
- {
- return *ptr;
- }
- bool operator!=(T *t)
- {
- return ptr != t;
- }
- bool operator==(T *t)
- {
- return ptr == t;
- }
- T & operator[](unsigned int i)
- {
- return ptr[i];
- }
-private:
- void drop()
- {
- assert((*refcount) > 0);
- (*refcount)--;
- if(*refcount == 0)
- {
- delete refcount;
- if(ptr != NULL)
- delete ptr;
- }
- }
- T *ptr;
- int *refcount;
-};
-
template <typename T>
class Buffer
{
private:
void drop()
{
- if(data)
- delete[] data;
+ delete[] data;
}
T *data;
unsigned int m_size;
};
+/************************************************
+ * !!! W A R N I N G !!! *
+ * !!! A C H T U N G !!! *
+ * *
+ * This smart pointer class is NOT thread safe. *
+ * ONLY use in a single-threaded context! *
+ * *
+ ************************************************/
template <typename T>
class SharedBuffer
{
else
data = NULL;
refcount = new unsigned int;
+ memset(data,0,sizeof(T)*m_size);
(*refcount) = 1;
}
SharedBuffer(const SharedBuffer &buffer)
}
T & operator[](unsigned int i) const
{
- //assert(i < m_size)
+ assert(i < m_size);
return data[i];
}
T * operator*() const
(*refcount)--;
if(*refcount == 0)
{
- if(data)
- delete[] data;
+ delete[] data;
delete refcount;
}
}