LCOV - code coverage report
Current view: top level - src/util - pointer.h (source / functions) Hit Total Coverage
Test: report Lines: 98 113 86.7 %
Date: 2015-07-11 18:23:49 Functions: 38 39 97.4 %

          Line data    Source code
       1             : /*
       2             : Minetest
       3             : Copyright (C) 2010-2013 celeron55, Perttu Ahola <celeron55@gmail.com>
       4             : 
       5             : This program is free software; you can redistribute it and/or modify
       6             : it under the terms of the GNU Lesser General Public License as published by
       7             : the Free Software Foundation; either version 2.1 of the License, or
       8             : (at your option) any later version.
       9             : 
      10             : This program is distributed in the hope that it will be useful,
      11             : but WITHOUT ANY WARRANTY; without even the implied warranty of
      12             : MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
      13             : GNU Lesser General Public License for more details.
      14             : 
      15             : You should have received a copy of the GNU Lesser General Public License along
      16             : with this program; if not, write to the Free Software Foundation, Inc.,
      17             : 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
      18             : */
      19             : 
      20             : #ifndef UTIL_POINTER_HEADER
      21             : #define UTIL_POINTER_HEADER
      22             : 
      23             : #include "../irrlichttypes.h"
      24             : #include "../debug.h" // For assert()
      25             : #include <cstring>
      26             : 
      27             : template <typename T>
      28             : class SharedPtr
      29             : {
      30             : public:
      31             :         SharedPtr(T *t=NULL)
      32             :         {
      33             :                 refcount = new int;
      34             :                 *refcount = 1;
      35             :                 ptr = t;
      36             :         }
      37             :         SharedPtr(SharedPtr<T> &t)
      38             :         {
      39             :                 //*this = t;
      40             :                 drop();
      41             :                 refcount = t.refcount;
      42             :                 (*refcount)++;
      43             :                 ptr = t.ptr;
      44             :         }
      45             :         ~SharedPtr()
      46             :         {
      47             :                 drop();
      48             :         }
      49             :         SharedPtr<T> & operator=(T *t)
      50             :         {
      51             :                 drop();
      52             :                 refcount = new int;
      53             :                 *refcount = 1;
      54             :                 ptr = t;
      55             :                 return *this;
      56             :         }
      57             :         SharedPtr<T> & operator=(SharedPtr<T> &t)
      58             :         {
      59             :                 drop();
      60             :                 refcount = t.refcount;
      61             :                 (*refcount)++;
      62             :                 ptr = t.ptr;
      63             :                 return *this;
      64             :         }
      65             :         T* operator->()
      66             :         {
      67             :                 return ptr;
      68             :         }
      69             :         T & operator*()
      70             :         {
      71             :                 return *ptr;
      72             :         }
      73             :         bool operator!=(T *t)
      74             :         {
      75             :                 return ptr != t;
      76             :         }
      77             :         bool operator==(T *t)
      78             :         {
      79             :                 return ptr == t;
      80             :         }
      81             :         T & operator[](unsigned int i)
      82             :         {
      83             :                 return ptr[i];
      84             :         }
      85             : private:
      86             :         void drop()
      87             :         {
      88             :                 assert((*refcount) > 0);
      89             :                 (*refcount)--;
      90             :                 if(*refcount == 0)
      91             :                 {
      92             :                         delete refcount;
      93             :                         if(ptr != NULL)
      94             :                                 delete ptr;
      95             :                 }
      96             :         }
      97             :         T *ptr;
      98             :         int *refcount;
      99             : };
     100             : 
     101             : template <typename T>
     102             : class Buffer
     103             : {
     104             : public:
     105       10505 :         Buffer()
     106             :         {
     107       10505 :                 m_size = 0;
     108       10505 :                 data = NULL;
     109       10505 :         }
     110      136145 :         Buffer(unsigned int size)
     111             :         {
     112      136145 :                 m_size = size;
     113      136145 :                 if(size != 0)
     114      136145 :                         data = new T[size];
     115             :                 else
     116           0 :                         data = NULL;
     117      136145 :         }
     118       24926 :         Buffer(const Buffer &buffer)
     119             :         {
     120       24926 :                 m_size = buffer.m_size;
     121       24926 :                 if(m_size != 0)
     122             :                 {
     123       23724 :                         data = new T[buffer.m_size];
     124       23725 :                         memcpy(data, buffer.data, buffer.m_size);
     125             :                 }
     126             :                 else
     127        1202 :                         data = NULL;
     128       24927 :         }
     129        8999 :         Buffer(const T *t, unsigned int size)
     130             :         {
     131        8999 :                 m_size = size;
     132        8999 :                 if(size != 0)
     133             :                 {
     134        8999 :                         data = new T[size];
     135        8999 :                         memcpy(data, t, size);
     136             :                 }
     137             :                 else
     138           0 :                         data = NULL;
     139        8999 :         }
     140      180574 :         ~Buffer()
     141             :         {
     142      180574 :                 drop();
     143      180576 :         }
     144       10530 :         Buffer& operator=(const Buffer &buffer)
     145             :         {
     146       10530 :                 if(this == &buffer)
     147           0 :                         return *this;
     148       10530 :                 drop();
     149       10531 :                 m_size = buffer.m_size;
     150       10531 :                 if(m_size != 0)
     151             :                 {
     152        8512 :                         data = new T[buffer.m_size];
     153        8512 :                         memcpy(data, buffer.data, buffer.m_size);
     154             :                 }
     155             :                 else
     156        2019 :                         data = NULL;
     157       10531 :                 return *this;
     158             :         }
     159      267459 :         T & operator[](unsigned int i) const
     160             :         {
     161      267459 :                 return data[i];
     162             :         }
     163       19291 :         T * operator*() const
     164             :         {
     165       19291 :                 return data;
     166             :         }
     167       17144 :         unsigned int getSize() const
     168             :         {
     169       17144 :                 return m_size;
     170             :         }
     171             : private:
     172      191104 :         void drop()
     173             :         {
     174      191104 :                 if(data)
     175      177378 :                         delete[] data;
     176      191106 :         }
     177             :         T *data;
     178             :         unsigned int m_size;
     179             : };
     180             : 
     181             : template <typename T>
     182             : class SharedBuffer
     183             : {
     184             : public:
     185        6920 :         SharedBuffer()
     186             :         {
     187        6920 :                 m_size = 0;
     188        6920 :                 data = NULL;
     189        6920 :                 refcount = new unsigned int;
     190        6920 :                 (*refcount) = 1;
     191        6920 :         }
     192       31855 :         SharedBuffer(unsigned int size)
     193             :         {
     194       31855 :                 m_size = size;
     195       31855 :                 if(m_size != 0)
     196       31855 :                         data = new T[m_size];
     197             :                 else
     198           0 :                         data = NULL;
     199       31859 :                 refcount = new unsigned int;
     200       31858 :                 memset(data,0,sizeof(T)*m_size);
     201       31858 :                 (*refcount) = 1;
     202       31858 :         }
     203       59139 :         SharedBuffer(const SharedBuffer &buffer)
     204             :         {
     205             :                 //std::cout<<"SharedBuffer(const SharedBuffer &buffer)"<<std::endl;
     206       59139 :                 m_size = buffer.m_size;
     207       59139 :                 data = buffer.data;
     208       59139 :                 refcount = buffer.refcount;
     209       59139 :                 (*refcount)++;
     210       59139 :         }
     211        3343 :         SharedBuffer & operator=(const SharedBuffer & buffer)
     212             :         {
     213             :                 //std::cout<<"SharedBuffer & operator=(const SharedBuffer & buffer)"<<std::endl;
     214        3343 :                 if(this == &buffer)
     215           0 :                         return *this;
     216        3343 :                 drop();
     217        3343 :                 m_size = buffer.m_size;
     218        3343 :                 data = buffer.data;
     219        3343 :                 refcount = buffer.refcount;
     220        3343 :                 (*refcount)++;
     221        3343 :                 return *this;
     222             :         }
     223             :         /*
     224             :                 Copies whole buffer
     225             :         */
     226           0 :         SharedBuffer(const T *t, unsigned int size)
     227             :         {
     228           0 :                 m_size = size;
     229           0 :                 if(m_size != 0)
     230             :                 {
     231           0 :                         data = new T[m_size];
     232           0 :                         memcpy(data, t, m_size);
     233             :                 }
     234             :                 else
     235           0 :                         data = NULL;
     236           0 :                 refcount = new unsigned int;
     237           0 :                 (*refcount) = 1;
     238           0 :         }
     239             :         /*
     240             :                 Copies whole buffer
     241             :         */
     242        4602 :         SharedBuffer(const Buffer<T> &buffer)
     243             :         {
     244        4602 :                 m_size = buffer.getSize();
     245        4602 :                 if(m_size != 0)
     246             :                 {
     247        4602 :                         data = new T[m_size];
     248        4602 :                         memcpy(data, *buffer, buffer.getSize());
     249             :                 }
     250             :                 else
     251           0 :                         data = NULL;
     252        4602 :                 refcount = new unsigned int;
     253        4602 :                 (*refcount) = 1;
     254        4602 :         }
     255      102460 :         ~SharedBuffer()
     256             :         {
     257      102460 :                 drop();
     258      102375 :         }
     259     9802881 :         T & operator[](unsigned int i) const
     260             :         {
     261             :                 assert(i < m_size);
     262     9802881 :                 return data[i];
     263             :         }
     264       45751 :         T * operator*() const
     265             :         {
     266       45751 :                 return data;
     267             :         }
     268       71586 :         unsigned int getSize() const
     269             :         {
     270       71586 :                 return m_size;
     271             :         }
     272        4935 :         operator Buffer<T>() const
     273             :         {
     274        4935 :                 return Buffer<T>(data, m_size);
     275             :         }
     276             : private:
     277      105687 :         void drop()
     278             :         {
     279             :                 assert((*refcount) > 0);
     280      105687 :                 (*refcount)--;
     281      105687 :                 if(*refcount == 0)
     282             :                 {
     283       43375 :                         if(data)
     284       36454 :                                 delete[] data;
     285       43381 :                         delete refcount;
     286             :                 }
     287      105686 :         }
     288             :         T *data;
     289             :         unsigned int m_size;
     290             :         unsigned int *refcount;
     291             : };
     292             : 
     293             : inline SharedBuffer<u8> SharedBufferFromString(const char *string)
     294             : {
     295             :         SharedBuffer<u8> b((u8*)string, strlen(string)+1);
     296             :         return b;
     297             : }
     298             : 
     299             : #endif
     300             : 

Generated by: LCOV version 1.11