Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace gu {
- template <typename ValueType>
- class Pointer final {
- std::size_t mPtrSize;
- ValueType* mPtr;
- //dealloc(): Internal helper function for alloc() and Dtor
- //Removes the need for mPtr emptiness checks since Ctor allocates
- auto dealloc() -> bool;
- public:
- Pointer();
- Pointer(std::size_t const inPtrSize);
- ~Pointer();
- //Q? Better name for this: reallocAndInit()?
- auto realloc(std::size_t const inPtrSize) -> bool;
- //showPtrValues() Only works for types implementing operator<<
- void showPtrValues();
- auto size() const -> std::size_t;
- auto nCopy_istream(std::istream_iterator<ValueType> const& beginIt, std::size_t const leap) -> bool;
- auto begin() const -> ValueType*;
- auto end() const -> ValueType*;
- auto operator[](std::size_t const index) -> ValueType&;
- auto operator[](std::size_t const index) const -> ValueType;
- };
- template<typename ValueType>
- inline Pointer<ValueType>::Pointer()
- : mPtrSize{ 1 },
- mPtr{ new ValueType{} }
- {
- }
- template<typename ValueType>
- inline Pointer<ValueType>::Pointer(std::size_t const inPtrSize)
- : mPtrSize{ inPtrSize },
- mPtr{ new ValueType[mPtrSize]{} }
- {
- }
- template<typename ValueType>
- inline Pointer<ValueType>::~Pointer()
- {
- dealloc();
- }
- template<typename ValueType>
- inline auto Pointer<ValueType>::realloc(std::size_t const inPtrSize) -> bool
- {
- if (inPtrSize) {
- dealloc();
- if (mPtrSize != inPtrSize) mPtrSize = inPtrSize;
- mPtr = new ValueType[mPtrSize]{};
- }
- return true;
- }
- template<typename ValueType>
- inline auto Pointer<ValueType>::dealloc() -> bool
- {
- mPtrSize > 1 ? delete[] mPtr : delete mPtr ;
- mPtr = nullptr;
- mPtrSize = 0;
- return true;
- }
- template<typename ValueType>
- inline auto Pointer<ValueType>::size() const -> std::size_t
- {
- return mPtrSize;
- }
- template<typename ValueType>
- inline auto Pointer<ValueType>::nCopy_istream(std::istream_iterator<ValueType> const& beginIt, std::size_t const leap) -> bool
- {
- if (leap > mPtrSize) {
- throw std::out_of_range("Leaping beyond pointer size.");
- return false;
- }
- std::copy_n(beginIt, leap, mPtr);
- return true;
- }
- template<typename ValueType>
- inline auto Pointer<ValueType>::begin() const -> ValueType *
- {
- if (!mPtr) throw std::exception("Pointer is empty.");;
- return mPtr;
- }
- template<typename ValueType>
- inline auto Pointer<ValueType>::end() const -> ValueType *
- {
- if (!mPtr) throw std::exception("Pointer is empty.");;
- return mPtr + mPtrSize;
- }
- template<typename ValueType>
- inline auto Pointer<ValueType>::operator[](std::size_t const index) -> ValueType&{
- if (index >= mPtrSize) throw std::out_of_range("Index out of bounds.");
- return mPtr[index];
- }
- template<typename ValueType>
- inline auto Pointer<ValueType>::operator[](std::size_t const index) const -> ValueType
- {
- if (index >= mPtrSize) throw std::out_of_range("Index out of bounds.");
- return mPtr[index];
- }
- template<typename ValueType>
- inline void Pointer<ValueType>::showPtrValues()
- {
- for (std::size_t i{}; i < mPtrSize; ++i)
- std::cout << mPtr[i] << ' ';
- std::cout << '\n';
- }
- };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement