CharMap.h 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #pragma once
  2. #include "ReferenceCounter.h"
  3. namespace Framework
  4. {
  5. template<class T, class V>
  6. class CharMap : public virtual ReferenceCounter
  7. {
  8. private:
  9. V *value;
  10. T **entries;
  11. public:
  12. CharMap()
  13. : ReferenceCounter()
  14. {
  15. entries = new T * [ 256 ];
  16. for( int i = 0; i < 256; i++ )
  17. entries[ i ] = 0;
  18. value = 0;
  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 dynamic_cast<T *>( 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 ? dynamic_cast<V *>( value->getThis() ) : 0;
  54. }
  55. V *zValue()
  56. {
  57. return value;
  58. }
  59. };
  60. }