Templates -- Meow  2.0.-1
A C++ template contains kinds of interesting classes and functions
pointer.h
Go to the documentation of this file.
1 
9 #ifndef __MEOWPP_UTILITY_POINTER_H__
10 #define __MEOWPP_UTILITY_POINTER_H__
11 
12 #include <cstddef>
13 #include <cstdlib>
14 
15 #include "object.h"
16 
17 namespace meow {
18 
19 
24  SINGLE = 0,
25  ARRAY = 1
26 };
27 
28 
32 template<typename Type>
33 class Pointer : public Object {
34  private:
35  struct RealPointer {
36  Type* address;
37 
39  PointerType type;
40 
42  bool auto_delete;
43 
45  int counter;
46 
47  RealPointer(Type* arg_address,
48  PointerType arg_type,
49  bool arg_auto_delete,
50  int arg_counter) :
51  address(arg_address),
52  type(arg_type),
53  auto_delete(arg_auto_delete),
54  counter(arg_counter) {}
55 
56  ~RealPointer() {
57  if (auto_delete) {
58  switch (type) {
59  case SINGLE:
60  delete address;
61  break;
62  case ARRAY:
63  delete [] address;
64  break;
65  }
66  }
67  }
68  };
69 
70  RealPointer* ptr_;
71 
72  void Attach(RealPointer* arg_ptr2) {
73  ptr_ = arg_ptr2;
74  ptr_->counter += 1;
75  }
76 
77  void Detach() {
78  ptr_->counter -= 1;
79  if (ptr_->counter == 0) {
80  delete ptr_;
81  }
82  }
83  public:
87  Pointer() : Pointer(NULL, SINGLE, false) {}
88 
92  Pointer(Pointer const& arg_ptr) {
93  Attach(arg_ptr.ptr_);
94  }
95 
106  Pointer(Type* arg_address, PointerType arg_type, bool arg_auto_delete) :
107  ptr_(new RealPointer(arg_address, arg_type, arg_auto_delete, 1)) {}
108 
112  ~Pointer() { Detach(); }
113 
117  bool auto_delete() const {
118  return ptr_->auto_delete;
119  }
120 
124  Type* address() const {
125  return ptr_->address;
126  }
127 
131  operator Type*() const {
132  return address();
133  }
134 
138  Type* operator->() const {
139  return address();
140  }
141 
145  Pointer& operator=(Pointer const& b) {
146  Detach();
147  Attach(b.ptr_);
148  return *this;
149  }
150 
151  Object* Copy() const {
152  return new Pointer(*this);
153  }
154 
155  Object* CopyFrom(Object const* another_pointer) {
156  (*this) = *dynamic_cast<Pointer const*>(another_pointer);
157  return this;
158  }
159 
160  bool Equals(Object const* another_pointer) {
161  return (ptr_->address ==
162  dynamic_cast<Pointer const*>(another_pointer)->ptr_->address);
163  }
164 
165 #ifdef MEOWPP_UTILITY_POINTER_TESTING
166  friend class PointerTest;
167 #endif // MEOWPP_UTILLITY_POINTER_TESTING
168 
169 };
170 
171 } // meow
172 
173 #endif // __MEOWPP_UTILITY_POINTER_H__
174 
A pointer points to the template Type.
Definition: pointer.h:33
Type * address() const
Gets the address it points to.
Definition: pointer.h:124
Object * CopyFrom(Object const *another_pointer)
Copies data from another object.
Definition: pointer.h:155
Pointer()
Default constructor, let the pointer points to NULL.
Definition: pointer.h:87
PointerType
Types of pointer.
Definition: pointer.h:23
Pointer & operator=(Pointer const &b)
Points to another instance of Pointer.
Definition: pointer.h:145
bool Equals(Object const *another_pointer)
Definition: pointer.h:160
Type * operator->() const
Gets the pointer points to the body.
Definition: pointer.h:138
The base class.
Definition: object.h:20
Pointer(Pointer const &arg_ptr)
Copy constructor.
Definition: pointer.h:92
Contains a base class for most of all the classes in meowpp.
Object * Copy() const
Creates a copy of itself and return the pointer to it.
Definition: pointer.h:151
Pointer(Type *arg_address, PointerType arg_type, bool arg_auto_delete)
Constructor with gived address to point.
Definition: pointer.h:106
bool auto_delete() const
Gets whether it will delete the address automatically or not.
Definition: pointer.h:117
~Pointer()
Destructor.
Definition: pointer.h:112