CharMap.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. #pragma once
  2. #include "ReferenceCounter.h"
  3. namespace Framework
  4. {
  5. template<class T, class V> class CharMap : public virtual ReferenceCounter
  6. {
  7. private:
  8. V* value;
  9. T** entries;
  10. public:
  11. CharMap()
  12. : ReferenceCounter()
  13. {
  14. entries = new T*[256];
  15. for (int i = 0; i < 256; i++)
  16. entries[i] = 0;
  17. value = 0;
  18. }
  19. ~CharMap()
  20. {
  21. for (int i = 0; i < 256; i++)
  22. {
  23. if (entries[i]) entries[i]->release();
  24. }
  25. delete[] entries;
  26. if (value) value->release();
  27. }
  28. T* get(unsigned char c)
  29. {
  30. return entries[(int)c]
  31. ? dynamic_cast<T*>(entries[(int)c]->getThis())
  32. : 0;
  33. }
  34. T* z(unsigned char c)
  35. {
  36. return entries[(int)c];
  37. }
  38. void set(unsigned char c, T* t)
  39. {
  40. if (entries[(int)c]) entries[(int)c]->release();
  41. entries[(int)c] = t;
  42. }
  43. void setValue(V* v)
  44. {
  45. if (value) value->release();
  46. value = v;
  47. }
  48. V* getValue()
  49. {
  50. return value ? dynamic_cast<V*>(value->getThis()) : 0;
  51. }
  52. V* zValue()
  53. {
  54. return value;
  55. }
  56. };
  57. } // namespace Framework