#ifndef MICROBIT_MANAGED_TYPE_H #define MICROBIT_MANAGED_TYPE_H /** * Class definition for a Generic Managed Type. * * Represents a reference counted object. * * @info When the destructor is called delete is called on the object - implicitly calling the given objects destructor. */ template class ManagedType { private: int *ref; public: T *object; /** * Constructor for the managed type, given a class space T. * @param object the object that you would like to be ref counted - of class T * * Example: * @code * T object = new T(); * ManagedType mt(t); * @endcode */ ManagedType(T* object); /** * Default constructor for the managed type, given a class space T. */ ManagedType(); /** * Copy constructor for the managed type, given a class space T. * @param t another managed type instance of class type T. * * Example: * @code * T* object = new T(); * ManagedType mt(t); * ManagedType mt1(mt); * @endcode */ ManagedType(const ManagedType &t); /** * Destructor for the managed type, given a class space T. */ ~ManagedType(); /** * Copy-assign member function for the managed type, given a class space. * * Example: * @code * T* object = new T(); * ManagedType mt(t); * ManagedType mt1 = mt; * @endcode */ ManagedType& operator = (const ManagedType&i); /** * Returns the references to this ManagedType * * Example: * @code * T* object = new T(); * ManagedType mt(t); * ManagedType mt1(mt); * * mt.getReferences // this will be 2! * @endcode */ int getReferences(); T* operator->() { return object; } T* get() { return object; } bool operator!=(const ManagedType& x) { return !(this == x); } bool operator==(const ManagedType& x) { return this->object == x.object; } }; /** * Constructor for the managed type, given a class space. */ template ManagedType::ManagedType(T* object) { this->object = object; ref = (int *)malloc(sizeof(int)); *ref = 1; } /** * Default constructor for the managed type, given a class space. */ template ManagedType::ManagedType() { this->object = NULL; ref = (int *)malloc(sizeof(int)); *ref = 0; } /** * Copy constructor for the managed type, given a class space. */ template ManagedType::ManagedType(const ManagedType &t) { this->object = t.object; this->ref = t.ref; (*ref)++; } /** * Destructor for the managed type, given a class space. */ template ManagedType::~ManagedType() { if (--(*ref) == 0) { delete object; free(ref); } } /** * Copy-assign member function for the managed type, given a class space. */ template ManagedType& ManagedType::operator = (const ManagedType&t) { if (this == &t) return *this; if (--(*ref) == 0) { delete object; free(ref); } object = t.object; ref = t.ref; (*ref)++; return *this; } /** * Returns the references to this ManagedType */ template int ManagedType::getReferences() { return (*ref); } #endif