CharMap.h 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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. }
  27. T* get(unsigned char c)
  28. {
  29. return entries[(int)c]
  30. ? dynamic_cast<T*>(entries[(int)c]->getThis())
  31. : 0;
  32. }
  33. T* z(unsigned char c) const
  34. {
  35. return entries[(int)c];
  36. }
  37. void set(unsigned char c, T* t)
  38. {
  39. if (entries[(int)c]) entries[(int)c]->release();
  40. entries[(int)c] = t;
  41. }
  42. void setValue(V v)
  43. {
  44. value = v;
  45. }
  46. V getValue() const
  47. {
  48. return value;
  49. }
  50. };
  51. template<class T, class V> class RCCharMap : public virtual ReferenceCounter
  52. {
  53. private:
  54. V* value;
  55. T** entries;
  56. public:
  57. RCCharMap()
  58. : ReferenceCounter()
  59. {
  60. entries = new T*[256];
  61. for (int i = 0; i < 256; i++)
  62. entries[i] = 0;
  63. value = 0;
  64. }
  65. ~RCCharMap()
  66. {
  67. for (int i = 0; i < 256; i++)
  68. {
  69. if (entries[i]) entries[i]->release();
  70. }
  71. delete[] entries;
  72. if (value) value->release();
  73. }
  74. T* get(unsigned char c)
  75. {
  76. return entries[(int)c]
  77. ? dynamic_cast<T*>(entries[(int)c]->getThis())
  78. : 0;
  79. }
  80. T* z(unsigned char c) const
  81. {
  82. return entries[(int)c];
  83. }
  84. void set(unsigned char c, T* t)
  85. {
  86. if (entries[(int)c]) entries[(int)c]->release();
  87. entries[(int)c] = t;
  88. }
  89. void setValue(V* v)
  90. {
  91. if (value) value->release();
  92. value = v;
  93. }
  94. V* getValue()
  95. {
  96. return value ? dynamic_cast<V*>(value->getThis()) : 0;
  97. }
  98. V* zValue()
  99. {
  100. return value;
  101. }
  102. };
  103. } // namespace Framework