2012-06-17 00:29:13 +02:00
|
|
|
/*
|
2013-02-24 18:40:43 +01:00
|
|
|
Minetest
|
2013-02-24 19:38:45 +01:00
|
|
|
Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
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
|
|
|
|
the Free Software Foundation; either version 2.1 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU Lesser General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public License along
|
|
|
|
with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
|
|
|
*/
|
|
|
|
|
2017-08-17 22:19:39 +02:00
|
|
|
#pragma once
|
2012-06-17 00:29:13 +02:00
|
|
|
|
2017-11-08 23:56:20 +01:00
|
|
|
#include "irrlichttypes.h"
|
|
|
|
#include "debug.h" // For assert()
|
2012-06-17 03:00:31 +02:00
|
|
|
#include <cstring>
|
2021-12-01 20:22:33 +01:00
|
|
|
#include <memory> // std::shared_ptr
|
|
|
|
|
|
|
|
|
|
|
|
template<typename T> class ConstSharedPtr {
|
|
|
|
public:
|
|
|
|
ConstSharedPtr(T *ptr) : ptr(ptr) {}
|
|
|
|
ConstSharedPtr(const std::shared_ptr<T> &ptr) : ptr(ptr) {}
|
|
|
|
|
|
|
|
const T* get() const noexcept { return ptr.get(); }
|
|
|
|
const T& operator*() const noexcept { return *ptr.get(); }
|
|
|
|
const T* operator->() const noexcept { return ptr.get(); }
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::shared_ptr<T> ptr;
|
|
|
|
};
|
2012-06-17 00:29:13 +02:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
class Buffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Buffer()
|
|
|
|
{
|
|
|
|
m_size = 0;
|
2022-07-13 11:57:12 +02:00
|
|
|
data = nullptr;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
Buffer(unsigned int size)
|
|
|
|
{
|
|
|
|
m_size = size;
|
|
|
|
if(size != 0)
|
|
|
|
data = new T[size];
|
|
|
|
else
|
2022-07-13 11:57:12 +02:00
|
|
|
data = nullptr;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
2021-12-01 20:22:33 +01:00
|
|
|
|
|
|
|
// Disable class copy
|
|
|
|
Buffer(const Buffer &) = delete;
|
|
|
|
Buffer &operator=(const Buffer &) = delete;
|
|
|
|
|
2021-09-17 18:14:25 +02:00
|
|
|
Buffer(Buffer &&buffer)
|
|
|
|
{
|
|
|
|
m_size = buffer.m_size;
|
|
|
|
if(m_size != 0)
|
|
|
|
{
|
|
|
|
data = buffer.data;
|
|
|
|
buffer.data = nullptr;
|
|
|
|
buffer.m_size = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
data = nullptr;
|
|
|
|
}
|
|
|
|
// Copies whole buffer
|
2012-06-17 00:29:13 +02:00
|
|
|
Buffer(const T *t, unsigned int size)
|
|
|
|
{
|
|
|
|
m_size = size;
|
|
|
|
if(size != 0)
|
|
|
|
{
|
|
|
|
data = new T[size];
|
|
|
|
memcpy(data, t, size);
|
|
|
|
}
|
|
|
|
else
|
2022-07-13 11:57:12 +02:00
|
|
|
data = nullptr;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
2021-09-17 18:14:25 +02:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
~Buffer()
|
|
|
|
{
|
|
|
|
drop();
|
|
|
|
}
|
2021-09-17 18:14:25 +02:00
|
|
|
|
|
|
|
Buffer& operator=(Buffer &&buffer)
|
|
|
|
{
|
|
|
|
if(this == &buffer)
|
|
|
|
return *this;
|
|
|
|
drop();
|
|
|
|
m_size = buffer.m_size;
|
|
|
|
if(m_size != 0)
|
|
|
|
{
|
|
|
|
data = buffer.data;
|
|
|
|
buffer.data = nullptr;
|
|
|
|
buffer.m_size = 0;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
data = nullptr;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-12-01 20:22:33 +01:00
|
|
|
void copyTo(Buffer &buffer) const
|
|
|
|
{
|
|
|
|
buffer.drop();
|
|
|
|
buffer.m_size = m_size;
|
|
|
|
if (m_size != 0) {
|
|
|
|
buffer.data = new T[m_size];
|
|
|
|
memcpy(buffer.data, data, m_size);
|
|
|
|
} else {
|
|
|
|
buffer.data = nullptr;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
T & operator[](unsigned int i) const
|
|
|
|
{
|
|
|
|
return data[i];
|
|
|
|
}
|
|
|
|
T * operator*() const
|
|
|
|
{
|
|
|
|
return data;
|
|
|
|
}
|
2021-09-17 18:14:25 +02:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
unsigned int getSize() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
2021-09-17 18:14:25 +02:00
|
|
|
|
2012-06-17 00:29:13 +02:00
|
|
|
private:
|
|
|
|
void drop()
|
|
|
|
{
|
2017-06-05 01:52:55 +02:00
|
|
|
delete[] data;
|
2012-06-17 00:29:13 +02:00
|
|
|
}
|
|
|
|
T *data;
|
|
|
|
unsigned int m_size;
|
|
|
|
};
|
|
|
|
|
2015-08-06 08:15:21 +02:00
|
|
|
/************************************************
|
|
|
|
* !!! W A R N I N G !!! *
|
|
|
|
* *
|
|
|
|
* This smart pointer class is NOT thread safe. *
|
|
|
|
* ONLY use in a single-threaded context! *
|
|
|
|
* *
|
|
|
|
************************************************/
|
2012-06-17 00:29:13 +02:00
|
|
|
template <typename T>
|
|
|
|
class SharedBuffer
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
SharedBuffer()
|
|
|
|
{
|
|
|
|
m_size = 0;
|
|
|
|
data = NULL;
|
|
|
|
refcount = new unsigned int;
|
|
|
|
(*refcount) = 1;
|
|
|
|
}
|
|
|
|
SharedBuffer(unsigned int size)
|
|
|
|
{
|
|
|
|
m_size = size;
|
|
|
|
if(m_size != 0)
|
|
|
|
data = new T[m_size];
|
|
|
|
else
|
2022-07-13 11:57:12 +02:00
|
|
|
data = nullptr;
|
2012-06-17 00:29:13 +02:00
|
|
|
refcount = new unsigned int;
|
2013-11-03 17:28:16 +01:00
|
|
|
memset(data,0,sizeof(T)*m_size);
|
2012-06-17 00:29:13 +02:00
|
|
|
(*refcount) = 1;
|
|
|
|
}
|
|
|
|
SharedBuffer(const SharedBuffer &buffer)
|
|
|
|
{
|
|
|
|
m_size = buffer.m_size;
|
|
|
|
data = buffer.data;
|
|
|
|
refcount = buffer.refcount;
|
|
|
|
(*refcount)++;
|
|
|
|
}
|
|
|
|
SharedBuffer & operator=(const SharedBuffer & buffer)
|
|
|
|
{
|
|
|
|
if(this == &buffer)
|
|
|
|
return *this;
|
|
|
|
drop();
|
|
|
|
m_size = buffer.m_size;
|
|
|
|
data = buffer.data;
|
|
|
|
refcount = buffer.refcount;
|
|
|
|
(*refcount)++;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
Copies whole buffer
|
|
|
|
*/
|
2012-11-26 10:39:52 +01:00
|
|
|
SharedBuffer(const T *t, unsigned int size)
|
2012-06-17 00:29:13 +02:00
|
|
|
{
|
|
|
|
m_size = size;
|
|
|
|
if(m_size != 0)
|
|
|
|
{
|
|
|
|
data = new T[m_size];
|
|
|
|
memcpy(data, t, m_size);
|
|
|
|
}
|
|
|
|
else
|
2022-07-13 11:57:12 +02:00
|
|
|
data = nullptr;
|
2012-06-17 00:29:13 +02:00
|
|
|
refcount = new unsigned int;
|
|
|
|
(*refcount) = 1;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
Copies whole buffer
|
|
|
|
*/
|
2017-09-05 22:14:56 +02:00
|
|
|
SharedBuffer(const Buffer<T> &buffer)
|
|
|
|
{
|
|
|
|
m_size = buffer.getSize();
|
|
|
|
if (m_size != 0) {
|
|
|
|
data = new T[m_size];
|
|
|
|
memcpy(data, *buffer, buffer.getSize());
|
|
|
|
}
|
|
|
|
else
|
2022-07-13 11:57:12 +02:00
|
|
|
data = nullptr;
|
2017-09-05 22:14:56 +02:00
|
|
|
refcount = new unsigned int;
|
|
|
|
(*refcount) = 1;
|
|
|
|
}
|
2012-06-17 00:29:13 +02:00
|
|
|
~SharedBuffer()
|
|
|
|
{
|
|
|
|
drop();
|
|
|
|
}
|
|
|
|
T & operator[](unsigned int i) const
|
|
|
|
{
|
2014-04-06 15:12:04 +02:00
|
|
|
assert(i < m_size);
|
2012-06-17 00:29:13 +02:00
|
|
|
return data[i];
|
|
|
|
}
|
|
|
|
T * operator*() const
|
|
|
|
{
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
unsigned int getSize() const
|
|
|
|
{
|
|
|
|
return m_size;
|
|
|
|
}
|
|
|
|
operator Buffer<T>() const
|
|
|
|
{
|
|
|
|
return Buffer<T>(data, m_size);
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
void drop()
|
|
|
|
{
|
|
|
|
assert((*refcount) > 0);
|
|
|
|
(*refcount)--;
|
|
|
|
if(*refcount == 0)
|
|
|
|
{
|
2017-06-05 01:52:55 +02:00
|
|
|
delete[] data;
|
2012-06-17 00:29:13 +02:00
|
|
|
delete refcount;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
T *data;
|
|
|
|
unsigned int m_size;
|
|
|
|
unsigned int *refcount;
|
|
|
|
};
|
2022-07-13 11:57:12 +02:00
|
|
|
|
2022-09-11 19:28:37 +02:00
|
|
|
// This class is not thread-safe!
|
|
|
|
class IntrusiveReferenceCounted {
|
|
|
|
public:
|
|
|
|
IntrusiveReferenceCounted() = default;
|
|
|
|
virtual ~IntrusiveReferenceCounted() = default;
|
|
|
|
void grab() noexcept { ++m_refcount; }
|
|
|
|
void drop() noexcept { if (--m_refcount == 0) delete this; }
|
|
|
|
|
|
|
|
// Preserve own reference count.
|
|
|
|
IntrusiveReferenceCounted(const IntrusiveReferenceCounted &) {}
|
|
|
|
IntrusiveReferenceCounted &operator=(const IntrusiveReferenceCounted &) { return *this; }
|
|
|
|
private:
|
|
|
|
u32 m_refcount = 1;
|
|
|
|
};
|