shared_array.inl 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. ///////////////////////////////////////////////////////////////////////////////////////////////////
  2. // OpenGL Image Copyright (c) 2008 - 2011 G-Truc Creation (www.g-truc.net)
  3. ///////////////////////////////////////////////////////////////////////////////////////////////////
  4. // Created : 2008-12-19
  5. // Updated : 2005-06-13
  6. // Licence : This source is under MIT License
  7. // File : gli/shared_array.inl
  8. ///////////////////////////////////////////////////////////////////////////////////////////////////
  9. namespace gli
  10. {
  11. template <typename T>
  12. shared_array<T>::shared_array() :
  13. Counter(0),
  14. Pointer(0)
  15. {}
  16. template <typename T>
  17. shared_array<T>::shared_array
  18. (
  19. shared_array<T> const & SharedArray
  20. )
  21. {
  22. this->Counter = SharedArray.Counter;
  23. this->Pointer = SharedArray.Pointer;
  24. (*this->Counter)++;
  25. }
  26. template <typename T>
  27. shared_array<T>::shared_array
  28. (
  29. T * Pointer
  30. )
  31. {
  32. this->reset(Pointer);
  33. }
  34. template <typename T>
  35. shared_array<T>::~shared_array()
  36. {
  37. this->reset();
  38. }
  39. template <typename T>
  40. void shared_array<T>::reset()
  41. {
  42. if(this->Pointer)
  43. {
  44. (*this->Counter)--;
  45. if(*this->Counter <= 0)
  46. {
  47. delete this->Counter;
  48. this->Counter = 0;
  49. delete[] this->Pointer;
  50. this->Pointer = 0;
  51. }
  52. }
  53. }
  54. template <typename T>
  55. void shared_array<T>::reset(T * Pointer)
  56. {
  57. this->Counter = new int;
  58. this->Pointer = Pointer;
  59. *this->Counter = 1;
  60. }
  61. template <typename T>
  62. shared_array<T>& shared_array<T>::operator=
  63. (
  64. shared_array<T> const & SharedArray
  65. )
  66. {
  67. this->reset();
  68. this->Counter = SharedArray.Counter;
  69. this->Pointer = SharedArray.Pointer;
  70. (*this->Counter)++;
  71. return *this;
  72. }
  73. //template <typename T>
  74. //shared_array<T> & shared_array<T>::operator=(T * Pointer)
  75. //{
  76. // if(this->Pointer)
  77. // {
  78. // (*this->Counter)--;
  79. // if(*this->Counter <= 0)
  80. // {
  81. // delete this->Counter;
  82. // delete[] this->Pointer;
  83. // }
  84. // }
  85. // this->Counter = new int;
  86. // this->Pointer = this->Pointer;
  87. // (*this->Counter) = 1;
  88. // return *this;
  89. //}
  90. template <typename T>
  91. bool shared_array<T>::operator==(shared_array<T> const & SharedArray) const
  92. {
  93. return this->Pointer == SharedArray.Pointer;
  94. }
  95. template <typename T>
  96. bool shared_array<T>::operator!=(shared_array<T> const & SharedArray) const
  97. {
  98. return this->Pointer != SharedArray.Pointer;
  99. }
  100. template <typename T>
  101. T & shared_array<T>::operator*()
  102. {
  103. return *this->Pointer;
  104. }
  105. template <typename T>
  106. T * shared_array<T>::operator->()
  107. {
  108. return this->Pointer;
  109. }
  110. template <typename T>
  111. T const & shared_array<T>::operator*() const
  112. {
  113. return * this->Pointer;
  114. }
  115. template <typename T>
  116. T const * const shared_array<T>::operator->() const
  117. {
  118. return this->Pointer;
  119. }
  120. template <typename T>
  121. T * shared_array<T>::get()
  122. {
  123. return this->Pointer;
  124. }
  125. template <typename T>
  126. T const * const shared_array<T>::get() const
  127. {
  128. return this->Pointer;
  129. }
  130. }//namespace gli