22
33#include < cstddef>
44#include < utility>
5+ #include < vector>
56
67namespace cs106l {
78
@@ -10,10 +11,12 @@ namespace cs106l {
1011 * @tparam T The type of the object to manage.
1112 * @note This class is a simpler version of `std::unique_ptr`.
1213 */
13- template <typename T> class unique_ptr {
14+ template <typename T, typename Deleter = std::default_delete<T>> // c++ library for #include <memory>
15+ class unique_ptr {
1416private:
1517 /* STUDENT TODO: What data must a unique_ptr keep track of? */
16-
18+ T* ptr;
19+ Deleter deleter;
1720public:
1821 /* *
1922 * @brief Constructs a new `unique_ptr` from the given pointer.
@@ -22,30 +25,32 @@ template <typename T> class unique_ptr {
2225 */
2326 unique_ptr (T* ptr) {
2427 /* STUDENT TODO: Implement the constructor */
25- throw std::runtime_error ( " Not implemented: unique_ptr(T* ptr) " );
28+
2629 }
2730
2831 /* *
2932 * @brief Constructs a new `unique_ptr` from `nullptr`.
3033 */
3134 unique_ptr (std::nullptr_t ) {
3235 /* STUDENT TODO: Implement the nullptr constructor */
33- throw std::runtime_error ( " Not implemented: unique_ptr(std:: nullptr_t) " ) ;
36+ return nullptr_t ;
3437 }
3538
3639 /* *
3740 * @brief Constructs an empty `unique_ptr`.
3841 * @note By default, a `unique_ptr` points to `nullptr`.
3942 */
40- unique_ptr () : unique_ptr(nullptr ) {}
43+ unique_ptr () : unique_ptr(nullptr ) {
44+
45+ }
4146
4247 /* *
4348 * @brief Dereferences a `unique_ptr` and returns a reference to the object.
4449 * @return A reference to the object.
4550 */
4651 T& operator *() {
4752 /* STUDENT TODO: Implement the dereference operator */
48- throw std::runtime_error ( " Not implemented: operator*() " ) ;
53+ return *ptr ;
4954 }
5055
5156 /* *
@@ -54,7 +59,7 @@ template <typename T> class unique_ptr {
5459 */
5560 const T& operator *() const {
5661 /* STUDENT TODO: Implement the dereference operator (const) */
57- throw std::runtime_error ( " Not implemented: operator*() const " );
62+
5863 }
5964
6065 /* *
@@ -64,7 +69,7 @@ template <typename T> class unique_ptr {
6469 */
6570 T* operator ->() {
6671 /* STUDENT TODO: Implement the arrow operator */
67- throw std::runtime_error ( " Not implemented: operator->() " ) ;
72+ return ptr ;
6873 }
6974
7075 /* *
@@ -74,7 +79,7 @@ template <typename T> class unique_ptr {
7479 */
7580 const T* operator ->() const {
7681 /* STUDENT TODO: Implement the arrow operator */
77- throw std::runtime_error ( " Not implemented: operator->() const " );
82+
7883 }
7984
8085 /* *
@@ -84,7 +89,6 @@ template <typename T> class unique_ptr {
8489 */
8590 operator bool () const {
8691 /* STUDENT TODO: Implement the boolean conversion operator */
87- throw std::runtime_error (" Not implemented: operator bool() const" );
8892 }
8993
9094 /* * STUDENT TODO: In the space below, do the following:
@@ -94,6 +98,25 @@ template <typename T> class unique_ptr {
9498 * - Implement the move constructor
9599 * - Implement the move assignment operator
96100 */
101+ ~unique_ptr () {
102+ if (ptr) deleter (ptr);
103+ }
104+
105+ unique_ptr (const unique_ptr& other) {
106+ deleter ();
107+ }
108+
109+ unique_ptr& operator =(const unique_ptr& other) {
110+ deleter ();
111+ }
112+
113+ unique_ptr (unique_ptr&& other) {
114+ std::move ()
115+ }
116+
117+ unique_ptr& operator =(unique_ptr&& other) {
118+
119+ }
97120};
98121
99122/* *
0 commit comments