GLProgramming.com

home :: about :: development guides :: irc :: forums :: search :: paste :: links :: contribute :: code dump

-> Click here to learn how to get live help <-


New Paste :: Recent Pastes:: Add Line Numbers


ListSetMgr Fix by Anonymous
template<class _RT, class _NT=std::string>
class ListSetMgr{
    struct _RH {
        _NT _NI;
        _RT* _RI;
        _RH( _RT* res, const _NT& name ) 
            : _NI(name), _RI(res) {}
        ~_RH() { delete _RI; }
    };
    std::list<_RH*> _RL;
public:
    ~ListSetMgr() { Clear(); }
    void Clear() {
        std::list<_RH*>::iterator it = _RL.begin();
        while( it != _RL.end() ) {
            delete *it;
            ++it;
        }
        _RL.clear();
    }
    _RT* operator[]( unsigned hdl ) { return reinterpret_cast<_RH*>(hdl)->_RI; }
    _RT* Get( unsigned hdl ) {
        std::list<_RH*>::iterator it = _RL.begin();
        while( it !=  _RL.end() )
            if( reinterpret_cast<_RH*>(hdl) == *it )
                return reinterpret_cast<_RH*>(hdl)->_RI; 
        return 0;
    }
    unsigned NameToHdl( const _NT& name ) {
        std::list<_RH*>::iterator it = _RL.begin();
        while( it != _RL.end() ) {
            if( (*it)->_NI == name )
                return reinterpret_cast<unsigned>(*it);
            ++it;
        }
        return 0;
    }
    _RT* NameToPtr( const _NT& name ) {
        if( unsigned h = NameToHdl( name ) )
            return operator[]( h );
    }
    void Delete( unsigned hdl ) {         
        _RL.remove( reinterpret_cast<_RH*>(hdl) ); 
        delete reinterpret_cast<_RH*>(hdl);
    }
    void Delete( const _NT& name ) { 
        std::list<_RH*>::iterator it = _RL.begin();
        while( it != _RL.end() )
        {
            if( (*it)->_NI == name ) {
                delete *it;
                _RL.erase( it );
                return;
            }
            ++it;
        }
    }
protected:
    unsigned AddNC( _RT* newRes, const _NT& name ) {
        _RH* tmp = new _RH( newRes, name );
        _RL.push_back( tmp );
        return reinterpret_cast<unsigned>(tmp);
    }
    unsigned Add( _RT* newRes, const _NT& name ) {
        if( unsigned h = NameToHdl( name ) ) {
            delete newRes;
            return h;
        }
        return AddNC( newRes, name );
    }
};