#include "Betriebssystem.h"

namespace Framework
{
    template<typename T> class RCPointer
    {
    private:
        T* pointer;

    public:
        static RCPointer<T> of(T* pointer)
        {
            return RCPointer<T>() = pointer;
        }

        RCPointer()
        {
            pointer = 0;
        }

        RCPointer(const RCPointer<T>& ptr)
        {
            this->pointer
                = ptr.pointer ? dynamic_cast<T*>(ptr.pointer->getThis()) : 0;
        }

        ~RCPointer()
        {
            if (pointer) pointer->release();
        }

        RCPointer<T>& operator=(RCPointer<T>& ptr)
        {
            if (this->pointer) this->pointer->release();
            this->pointer
                = ptr.pointer ? dynamic_cast<T*>(ptr.pointer->getThis()) : 0;
            return *this;
        }

        RCPointer<T>& operator=(T* ptr)
        {
            if (this->pointer) this->pointer->release();
            this->pointer = ptr;
            return *this;
        }

        T* operator->()
        {
            return pointer;
        }

        operator T*()
        {
            return pointer;
        }
    };
} // namespace Framework