1
0
Fork 0
mirror of https://github.com/luanti-org/luanti.git synced 2025-08-06 17:41:04 +00:00

Network: Delete copy constructor and use std::move instead (#11642)

This is a follow-up change which disables class copies where possible to avoid unnecessary memory movements.
This commit is contained in:
SmallJoker 2021-12-01 20:22:33 +01:00 committed by GitHub
parent 1dc1305ada
commit 57a59ae92d
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 600 additions and 552 deletions

View file

@ -22,6 +22,21 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes.h"
#include "debug.h" // For assert()
#include <cstring>
#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;
};
template <typename T>
class Buffer
@ -40,17 +55,11 @@ public:
else
data = NULL;
}
Buffer(const Buffer &buffer)
{
m_size = buffer.m_size;
if(m_size != 0)
{
data = new T[buffer.m_size];
memcpy(data, buffer.data, buffer.m_size);
}
else
data = NULL;
}
// Disable class copy
Buffer(const Buffer &) = delete;
Buffer &operator=(const Buffer &) = delete;
Buffer(Buffer &&buffer)
{
m_size = buffer.m_size;
@ -81,21 +90,6 @@ public:
drop();
}
Buffer& operator=(const Buffer &buffer)
{
if(this == &buffer)
return *this;
drop();
m_size = buffer.m_size;
if(m_size != 0)
{
data = new T[buffer.m_size];
memcpy(data, buffer.data, buffer.m_size);
}
else
data = NULL;
return *this;
}
Buffer& operator=(Buffer &&buffer)
{
if(this == &buffer)
@ -113,6 +107,18 @@ public:
return *this;
}
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;
}
}
T & operator[](unsigned int i) const
{
return data[i];