Advertisement
Guest User

Untitled

a guest
Jan 25th, 2020
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. namespace gu {
  2.  
  3. template <typename ValueType>
  4. class Pointer final {
  5. std::size_t mPtrSize;
  6. ValueType* mPtr;
  7.  
  8. //dealloc(): Internal helper function for alloc() and Dtor
  9. //Removes the need for mPtr emptiness checks since Ctor allocates
  10. auto dealloc() -> bool;
  11.  
  12. public:
  13. Pointer();
  14. Pointer(std::size_t const inPtrSize);
  15. ~Pointer();
  16.  
  17. //Q? Better name for this: reallocAndInit()?
  18. auto realloc(std::size_t const inPtrSize) -> bool;
  19.  
  20. //showPtrValues() Only works for types implementing operator<<
  21. void showPtrValues();
  22.  
  23. auto size() const -> std::size_t;
  24. auto nCopy_istream(std::istream_iterator<ValueType> const& beginIt, std::size_t const leap) -> bool;
  25. auto begin() const -> ValueType*;
  26. auto end() const -> ValueType*;
  27.  
  28. auto operator[](std::size_t const index) -> ValueType&;
  29. auto operator[](std::size_t const index) const -> ValueType;
  30. };
  31. template<typename ValueType>
  32. inline Pointer<ValueType>::Pointer()
  33. : mPtrSize{ 1 },
  34. mPtr{ new ValueType{} }
  35. {
  36. }
  37.  
  38. template<typename ValueType>
  39. inline Pointer<ValueType>::Pointer(std::size_t const inPtrSize)
  40. : mPtrSize{ inPtrSize },
  41. mPtr{ new ValueType[mPtrSize]{} }
  42. {
  43. }
  44.  
  45. template<typename ValueType>
  46. inline Pointer<ValueType>::~Pointer()
  47. {
  48. dealloc();
  49. }
  50.  
  51. template<typename ValueType>
  52. inline auto Pointer<ValueType>::realloc(std::size_t const inPtrSize) -> bool
  53. {
  54. if (inPtrSize) {
  55. dealloc();
  56. if (mPtrSize != inPtrSize) mPtrSize = inPtrSize;
  57. mPtr = new ValueType[mPtrSize]{};
  58. }
  59. return true;
  60. }
  61.  
  62. template<typename ValueType>
  63. inline auto Pointer<ValueType>::dealloc() -> bool
  64. {
  65. mPtrSize > 1 ? delete[] mPtr : delete mPtr ;
  66. mPtr = nullptr;
  67. mPtrSize = 0;
  68. return true;
  69. }
  70.  
  71. template<typename ValueType>
  72. inline auto Pointer<ValueType>::size() const -> std::size_t
  73. {
  74. return mPtrSize;
  75. }
  76.  
  77. template<typename ValueType>
  78. inline auto Pointer<ValueType>::nCopy_istream(std::istream_iterator<ValueType> const& beginIt, std::size_t const leap) -> bool
  79. {
  80. if (leap > mPtrSize) {
  81. throw std::out_of_range("Leaping beyond pointer size.");
  82. return false;
  83. }
  84. std::copy_n(beginIt, leap, mPtr);
  85. return true;
  86. }
  87.  
  88. template<typename ValueType>
  89. inline auto Pointer<ValueType>::begin() const -> ValueType *
  90. {
  91. if (!mPtr) throw std::exception("Pointer is empty.");;
  92. return mPtr;
  93. }
  94.  
  95. template<typename ValueType>
  96. inline auto Pointer<ValueType>::end() const -> ValueType *
  97. {
  98. if (!mPtr) throw std::exception("Pointer is empty.");;
  99. return mPtr + mPtrSize;
  100. }
  101.  
  102. template<typename ValueType>
  103. inline auto Pointer<ValueType>::operator[](std::size_t const index) -> ValueType&{
  104. if (index >= mPtrSize) throw std::out_of_range("Index out of bounds.");
  105. return mPtr[index];
  106. }
  107.  
  108. template<typename ValueType>
  109. inline auto Pointer<ValueType>::operator[](std::size_t const index) const -> ValueType
  110. {
  111. if (index >= mPtrSize) throw std::out_of_range("Index out of bounds.");
  112. return mPtr[index];
  113. }
  114.  
  115. template<typename ValueType>
  116. inline void Pointer<ValueType>::showPtrValues()
  117. {
  118. for (std::size_t i{}; i < mPtrSize; ++i)
  119. std::cout << mPtr[i] << ' ';
  120. std::cout << '\n';
  121. }
  122.  
  123. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement