CharMap.h 1.5 KB

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