aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/utility/pointer.h
blob: 3b4d08df9068e7e23636f3f19ebfe6d034b3920d (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
/*!
 * @file pointer.h
 * @brief Contains a pointer class which has a counter-mechanism to prevent
 *     memory leak.
 *
 * @author cathook
 */

#ifndef __MEOWPP_UTILITY_POINTER_H__
#define __MEOWPP_UTILITY_POINTER_H__

#include <cstddef>
#include <cstdlib>

#include "object.h"

namespace meow {


/*!
 * @brief Types of pointer.
 */
enum PointerType {
  SINGLE = 0,
  ARRAY = 1
};


/*!
 * @brief A pointer points to the template `Type`.
 */
template<typename Type>
class Pointer : public Object {
 private:
  struct RealPointer {
    Type* address;

    //! The type of the address.
    PointerType type;

    //! Whether the address should be deleted when no one points to it.
    bool auto_delete;

    //! Stores number of pointers point to it.
    int counter;

    RealPointer(Type* arg_address,
                PointerType arg_type,
                bool arg_auto_delete,
                int arg_counter) :
        address(arg_address),
        type(arg_type),
        auto_delete(arg_auto_delete),
        counter(arg_counter) {}

    ~RealPointer() {
      if (auto_delete) {
        switch (type) {
        case SINGLE:
          delete address;
          break;
        case ARRAY:
          delete [] address;
          break;
        }
      }
    }
  };

  RealPointer* ptr_;

  void Attach(RealPointer* arg_ptr2) {
    ptr_ = arg_ptr2;
    ptr_->counter += 1;
  }

  void Detach() {
    ptr_->counter -= 1;
    if (ptr_->counter == 0) {
      delete ptr_;
    }
  }
 public:
  /*!
   * @brief Default constructor, let the pointer points to NULL.
   */
  Pointer() : Pointer(NULL, SINGLE, false) {}

  /*!
   * @brief Copy constructor.
   */
  Pointer(Pointer const& arg_ptr) {
    Attach(arg_ptr.ptr_);
  }

  /*!
   * @brief Constructor with gived address to point.
   *
   * If `arg_auto_delete` is `true`, it will automatically delete it when
   * there are no instance of Pointer\<Type\> points to that `address`
   *
   * @param [in] arg_address Points to the address.
   * @param [in] arg_type
   * @param [in] arg_auto_delete
   */
  Pointer(Type* arg_address, PointerType arg_type, bool arg_auto_delete) :
      ptr_(new RealPointer(arg_address, arg_type, arg_auto_delete, 1)) {}

  /*!
   * @brief Destructor.
   */
  ~Pointer() { Detach(); }

  /*!
   * @brief Gets whether it will delete the address automatically or not.
   */
  bool auto_delete() const {
    return ptr_->auto_delete;
  }

  /*!
   * @brief Gets the address it points to.
   */
  Type* address() const {
    return ptr_->address;
  }

  /*!
   * @brief Same as `address()`
   */
  operator Type*() const {
    return address();
  }

  /*!
   * @brief Gets the pointer points to the body.
   */
  Type* operator->() const {
    return address();
  }

  /*!
   * @brief Points to another instance of Pointer.
   */
  Pointer& operator=(Pointer const& b) {
    Detach();
    Attach(b.ptr_);
    return *this;
  }

  Object* Copy() const {
    return new Pointer(*this);
  }

  Object* CopyFrom(Object const* another_pointer) {
    (*this) = *dynamic_cast<Pointer const*>(another_pointer);
    return this;
  }

  bool Equals(Object const* another_pointer) {
    return (ptr_->address ==
            dynamic_cast<Pointer const*>(another_pointer)->ptr_->address);
  }

#ifdef MEOWPP_UTILITY_POINTER_TESTING
  friend class PointerTest;
#endif // MEOWPP_UTILLITY_POINTER_TESTING

};

}  // meow

#endif  // __MEOWPP_UTILITY_POINTER_H__