aboutsummaryrefslogtreecommitdiffstats
path: root/meowpp/utility/pointer.h
diff options
context:
space:
mode:
Diffstat (limited to 'meowpp/utility/pointer.h')
-rw-r--r--meowpp/utility/pointer.h174
1 files changed, 0 insertions, 174 deletions
diff --git a/meowpp/utility/pointer.h b/meowpp/utility/pointer.h
deleted file mode 100644
index 3b4d08d..0000000
--- a/meowpp/utility/pointer.h
+++ /dev/null
@@ -1,174 +0,0 @@
-/*!
- * @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__
-