#pragma once

#include "ReferenceCounter.h"

namespace Framework
{
    template<class T, class V> class CharMap : public virtual ReferenceCounter
    {
    private:
        V* value;
        T** entries;

    public:
        CharMap()
            : ReferenceCounter()
        {
            entries = new T*[256];
            for (int i = 0; i < 256; i++)
                entries[i] = 0;
            value = 0;
        }

        ~CharMap()
        {
            for (int i = 0; i < 256; i++)
            {
                if (entries[i]) entries[i]->release();
            }
            delete[] entries;
            if (value) value->release();
        }

        T* get(unsigned char c)
        {
            return entries[(int)c]
                     ? dynamic_cast<T*>(entries[(int)c]->getThis())
                     : 0;
        }

        T* z(unsigned char c)
        {
            return entries[(int)c];
        }

        void set(unsigned char c, T* t)
        {
            if (entries[(int)c]) entries[(int)c]->release();
            entries[(int)c] = t;
        }

        void setValue(V* v)
        {
            if (value) value->release();
            value = v;
        }

        V* getValue()
        {
            return value ? dynamic_cast<V*>(value->getThis()) : 0;
        }

        V* zValue()
        {
            return value;
        }
    };
} // namespace Framework