]> git.lizzy.rs Git - minetest.git/blobdiff - src/util/pointer.h
Initialize wield mesh colors when changing item. (#12254)
[minetest.git] / src / util / pointer.h
index 775f0a3362de5fe0ae853ca3e5e53f2e049b4f15..245ac85bfbc028ab808e28ac5353053a331542f8 100644 (file)
@@ -1,6 +1,6 @@
 /*
-Minetest-c55
-Copyright (C) 2010-2012 celeron55, Perttu Ahola <celeron55@gmail.com>
+Minetest
+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
@@ -17,85 +17,25 @@ with this program; if not, write to the Free Software Foundation, Inc.,
 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */
 
-#ifndef UTIL_POINTER_HEADER
-#define UTIL_POINTER_HEADER
+#pragma once
 
-#include "../irrlichttypes.h"
-#include "../debug.h" // For assert()
+#include "irrlichttypes.h"
+#include "debug.h" // For assert()
 #include <cstring>
+#include <memory> // std::shared_ptr
 
-template <typename T>
-class SharedPtr
-{
+
+template<typename T> class ConstSharedPtr {
 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];
-       }
+       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:
-       void drop()
-       {
-               assert((*refcount) > 0);
-               (*refcount)--;
-               if(*refcount == 0)
-               {
-                       delete refcount;
-                       if(ptr != NULL)
-                               delete ptr;
-               }
-       }
-       T *ptr;
-       int *refcount;
+       std::shared_ptr<T> ptr;
 };
 
 template <typename T>
@@ -115,17 +55,24 @@ class Buffer
                else
                        data = NULL;
        }
-       Buffer(const Buffer &buffer)
+
+       // Disable class copy
+       Buffer(const Buffer &) = delete;
+       Buffer &operator=(const Buffer &) = delete;
+
+       Buffer(Buffer &&buffer)
        {
                m_size = buffer.m_size;
                if(m_size != 0)
                {
-                       data = new T[buffer.m_size];
-                       memcpy(data, buffer.data, buffer.m_size);
+                       data = buffer.data;
+                       buffer.data = nullptr;
+                       buffer.m_size = 0;
                }
                else
-                       data = NULL;
+                       data = nullptr;
        }
+       // Copies whole buffer
        Buffer(const T *t, unsigned int size)
        {
                m_size = size;
@@ -137,11 +84,13 @@ class Buffer
                else
                        data = NULL;
        }
+
        ~Buffer()
        {
                drop();
        }
-       Buffer& operator=(const Buffer &buffer)
+
+       Buffer& operator=(Buffer &&buffer)
        {
                if(this == &buffer)
                        return *this;
@@ -149,13 +98,27 @@ class Buffer
                m_size = buffer.m_size;
                if(m_size != 0)
                {
-                       data = new T[buffer.m_size];
-                       memcpy(data, buffer.data, buffer.m_size);
+                       data = buffer.data;
+                       buffer.data = nullptr;
+                       buffer.m_size = 0;
                }
                else
-                       data = NULL;
+                       data = nullptr;
                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];
@@ -164,20 +127,28 @@ class Buffer
        {
                return data;
        }
+
        unsigned int getSize() const
        {
                return m_size;
        }
+
 private:
        void drop()
        {
-               if(data)
-                       delete[] data;
+               delete[] data;
        }
        T *data;
        unsigned int m_size;
 };
 
+/************************************************
+ *           !!!  W A R N I N G  !!!            *
+ *                                              *
+ * This smart pointer class is NOT thread safe. *
+ * ONLY use in a single-threaded context!       *
+ *                                              *
+ ************************************************/
 template <typename T>
 class SharedBuffer
 {
@@ -197,11 +168,11 @@ class SharedBuffer
                else
                        data = NULL;
                refcount = new unsigned int;
+               memset(data,0,sizeof(T)*m_size);
                (*refcount) = 1;
        }
        SharedBuffer(const SharedBuffer &buffer)
        {
-               //std::cout<<"SharedBuffer(const SharedBuffer &buffer)"<<std::endl;
                m_size = buffer.m_size;
                data = buffer.data;
                refcount = buffer.refcount;
@@ -209,7 +180,6 @@ class SharedBuffer
        }
        SharedBuffer & operator=(const SharedBuffer & buffer)
        {
-               //std::cout<<"SharedBuffer & operator=(const SharedBuffer & buffer)"<<std::endl;
                if(this == &buffer)
                        return *this;
                drop();
@@ -241,10 +211,9 @@ class SharedBuffer
        SharedBuffer(const Buffer<T> &buffer)
        {
                m_size = buffer.getSize();
-               if(m_size != 0)
-               {
-                       data = new T[m_size];
-                       memcpy(data, *buffer, buffer.getSize());
+               if (m_size != 0) {
+                               data = new T[m_size];
+                               memcpy(data, *buffer, buffer.getSize());
                }
                else
                        data = NULL;
@@ -257,7 +226,7 @@ class SharedBuffer
        }
        T & operator[](unsigned int i) const
        {
-               //assert(i < m_size)
+               assert(i < m_size);
                return data[i];
        }
        T * operator*() const
@@ -279,8 +248,7 @@ class SharedBuffer
                (*refcount)--;
                if(*refcount == 0)
                {
-                       if(data)
-                               delete[] data;
+                       delete[] data;
                        delete refcount;
                }
        }
@@ -288,12 +256,3 @@ class SharedBuffer
        unsigned int m_size;
        unsigned int *refcount;
 };
-
-inline SharedBuffer<u8> SharedBufferFromString(const char *string)
-{
-       SharedBuffer<u8> b((u8*)string, strlen(string)+1);
-       return b;
-}
-
-#endif
-