smartPointer.hpp
Go to the documentation of this file.
1 #ifndef JULIAN_SMARTPOINTER_HPP
2 #define JULIAN_SMARTPOINTER_HPP
3 
4 namespace julian {
13  template< class T>
14  class SmartPointer {
15  public:
19  data_ptr_ = 0;
20  }
23  SmartPointer(const T& inner) {
24  data_ptr_ = inner.clone();
25  }
29  if (data_ptr_ != 0)
30  delete data_ptr_;
31  }
34  SmartPointer(const SmartPointer<T>& original) {
35  if (original.data_ptr_ != 0) {
36  data_ptr_ = original.data_ptr_->clone();
37  } else {
38  data_ptr_=0;
39  }
40  }
41 
45  if (this != &original) {
46  if (data_ptr_ != 0) {
47  delete data_ptr_;
48  }
49  data_ptr_ = (original.data_ptr_ != 0) ? original.data_ptr_->clone() : 0;
50  }
51  return *this;
52  }
53 
56  T& operator*() {
57  return *data_ptr_;
58  }
59 
62  const T& operator*() const {
63  return *data_ptr_;
64  }
65 
68  T* operator->() {
69  return data_ptr_;
70  }
71 
74  const T* operator->() const {
75  return data_ptr_;
76  }
77 
80  bool isEmpty() const {
81  if (data_ptr_) {
82  return false;
83  }
84  return true;
85  }
86 
87  private:
88  T* data_ptr_;
89  };
90 
91 } // namespace julian
92 #endif
93 
94 /*
95  *
96  * Copyright (c) 2002
97  * Mark Joshi
98  *
99  * Permission to use, copy, modify, distribute and sell this
100  * software for any purpose is hereby
101  * granted without fee, provided that the above copyright notice
102  * appear in all copies and that both that copyright notice and
103  * this permission notice appear in supporting documentation.
104  * Mark Joshi makes no representations about the
105  * suitability of this software for any purpose. It is provided
106  * "as is" without express or implied warranty.
107  */
108 
T & operator*()
Method dereferencing pointer.
Definition: smartPointer.hpp:56
SmartPointer & operator=(const SmartPointer< T > &original)
Assignment operator.
Definition: smartPointer.hpp:44
const T * operator->() const
Method dereferencing pointer.
Definition: smartPointer.hpp:74
Definition: cadHoliday.cpp:3
bool isEmpty() const
Check if pointer owns any object.
Definition: smartPointer.hpp:80
Template of deep-coping smart pointer.
Definition: smartPointer.hpp:14
T * data_ptr_
Pointer to data owned by pointer.
Definition: smartPointer.hpp:88
~SmartPointer()
Destructor.
Definition: smartPointer.hpp:28
T * operator->()
Method dereferencing pointer.
Definition: smartPointer.hpp:68
SmartPointer()
Default constructor.
Definition: smartPointer.hpp:18
SmartPointer(const T &inner)
Constructor.
Definition: smartPointer.hpp:23
SmartPointer(const SmartPointer< T > &original)
Copy constructor.
Definition: smartPointer.hpp:34
const T & operator*() const
Method dereferencing pointer.
Definition: smartPointer.hpp:62