12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182 |
- #pragma once
- namespace Framework
- {
- template<class T, class V>
- class CharMap
- {
- private:
- V *value;
- T **entries;
- int ref;
- public:
- CharMap()
- {
- entries = new T*[ 256 ];
- for( int i = 0; i < 256; i++ )
- entries[ i ] = 0;
- value = 0;
- ref = 1;
- }
- ~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 ] ? 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 ? value->getThis() : 0;
- }
- V *zValue()
- {
- return value;
- }
- CharMap *getThis()
- {
- ref++;
- return this;
- }
- CharMap *release()
- {
- if( !--ref )
- delete this;
- return 0;
- }
- };
- }
|