Array.tpp
  1  /* ************************************************************************** */
  2  /*                                                                            */
  3  /*                                                        :::      ::::::::   */
  4  /*   Array.tpp                                          :+:      :+:    :+:   */
  5  /*                                                    +:+ +:+         +:+     */
  6  /*   By: gychoi <gychoi@student.42seoul.kr>         +#+  +:+       +#+        */
  7  /*                                                +#+#+#+#+#+   +#+           */
  8  /*   Created: 2023/12/25 17:01:24 by gychoi            #+#    #+#             */
  9  /*   Updated: 2023/12/25 19:32:39 by gychoi           ###   ########.fr       */
 10  /*                                                                            */
 11  /* ************************************************************************** */
 12  
 13  #pragma once
 14  #ifndef __ARRAY_TPP__
 15  #define __ARRAY_TPP__
 16  
 17  /* ************************************************************************** */
 18  /*                          Constructor & Destructor                          */
 19  /* ************************************************************************** */
 20  
 21  template <typename T>
 22  Array<T>::Array() : mSize(0)
 23  {
 24  	this->mData = new T[this->mSize];
 25  }
 26  
 27  template <typename T>
 28  Array<T>::Array(unsigned int n) : mData(0) // for throw-destruction safety
 29  {
 30  	if (static_cast<int>(n) < 0)
 31  	{
 32  		throw std::out_of_range("Invalid index");
 33  	}
 34  	else
 35  	{
 36  		this->mSize = n;
 37  		this->mData = new T[this->mSize]();
 38  	}
 39  }
 40  
 41  template <typename T>
 42  Array<T>::Array(Array const& target) : mSize(target.mSize)
 43  {
 44  	this->mData = new T[this->mSize]();
 45  	for (unsigned int i = 0; i < this->mSize; i++)
 46  	{
 47  		this->mData[i] = target.mData[i];
 48  	}
 49  }
 50  
 51  template <typename T>
 52  Array<T>&	Array<T>::operator=(Array const& target)
 53  {
 54  	if (this != &target)
 55  	{
 56  		delete[] mData;
 57  		this->mSize = target.mSize;
 58  		this->mData = new T[this->mSize]();
 59  		for (unsigned int i = 0; i < this->mSize; i++)
 60  		{
 61  			this->mData[i] = target.mData[i];
 62  		}
 63  	}
 64  	return *this;
 65  }
 66  
 67  template <typename T>
 68  Array<T>::~Array()
 69  {
 70  	delete[] mData;
 71  }
 72  
 73  /* ************************************************************************** */
 74  /*                           Public Member Function                           */
 75  /* ************************************************************************** */
 76  
 77  template <typename T>
 78  unsigned int const&	Array<T>::size() const
 79  {
 80  	return this->mSize;
 81  }
 82  
 83  template <typename T>
 84  T&	Array<T>::operator[](unsigned int n)
 85  {
 86  	if (static_cast<int>(n) < 0)
 87  	{
 88  		throw std::out_of_range("Invalid index");
 89  	}
 90  	else if (n >= this->mSize)
 91  	{
 92  		throw std::out_of_range("Index is out of range");
 93  	}
 94  	else
 95  	{
 96  		// valid index
 97  	}
 98  	return this->mData[n];
 99  }
100  
101  template <typename T>
102  T const&	Array<T>::operator[](unsigned int n) const
103  {
104  	if (static_cast<int>(n) < 0)
105  	{
106  		throw std::out_of_range("Invalid index");
107  	}
108  	else if (n >= this->mSize)
109  	{
110  		throw std::out_of_range("Index is out of range");
111  	}
112  	else
113  	{
114  		// valid index
115  	}
116  	return this->mData[n];
117  }
118  
119  /* ************************************************************************** */
120  /*                           Global Member Function                           */
121  /* ************************************************************************** */
122  
123  template <typename T>
124  std::ostream&	operator<<(std::ostream& os, Array<T> const& arr)
125  {
126  	os << &arr;
127  	return os;
128  }
129  
130  
131  #endif /* __ARRAY_TPP__ */