shared_ptr 例子

template <class T>
class ITERATOR_CLASS IteratorBase
{
public:
    virtual bool hasNext()  = 0;


    virtual T next()  = 0;


    virtual int length() = 0;
    virtual ~IteratorBase() {}
};

template <class T>
class ITERATOR_CLASS GeneralIterator
{
public:
    explicit GeneralIterator(IteratorBase<T>* iter): _iter(iter) {}
    GeneralIterator(const GeneralIterator& other) { this->_iter = other._iter; }
    ~GeneralIterator() { _iter = nullptr; }


    bool hasNext()  { return _iter->hasNext();}
    T next()  { return _iter->next(); }
    int length() { return _iter->length(); }


private:
    std::shared_ptr<IteratorBase<T> > _iter;
};



template <class _Key, class T >

class ITERATOR_CLASS MapIterator : public IteratorBase<T>
{
    const static int MAX_SUBSTRATUM = 3;
    const map<_Key,T>* _substratum[MAX_SUBSTRATUM];
    int _currentSubstratum;
    typename map<_Key,T>::const_iterator _it;
    MapIterator(const map<_Key,T>* v0, const map<_Key,T>* v1=NULL, const map<_Key,T>* v2=NULL) {
        _substratum[0] = v0;
        _substratum[1] = v1;
        _substratum[2] = v2;
        _currentSubstratum = 0;
        _it = _substratum[_currentSubstratum]->begin();
    }



public:


    bool hasNext() {
        // if(_it != _substratum[_currentSubstratum]->end()) // jdchen - 04/03/2007
        if (_currentSubstratum >= MAX_SUBSTRATUM)
            return false;
        if (_it != (_substratum[_currentSubstratum] ? _substratum[_currentSubstratum]->end() : _it))
            return true;
        else {
            _currentSubstratum++;
            if(_currentSubstratum == MAX_SUBSTRATUM)
                return false;
            // _it = _substratum[_currentSubstratum]->begin(); // jdchen - 04/03/2007
            _it = _substratum[_currentSubstratum] ? _substratum[_currentSubstratum]->begin() : _it;
            return hasNext();
        }
    }


    //##ModelId=45F5182502E8
    T next() {
        T tmp = (*_it).second;
        _it++;
        return tmp;
    }


    int length() {
        int len = 0;
        for(int i=0; i<MAX_SUBSTRATUM; i++) {
            if(_substratum[i] != NULL)
                len += (int)_substratum[i]->size();
            else
                break;
        }
        return len;
    }


    static GeneralIterator<T> createIterator(const map<_Key,T>* v0, const map<_Key,T>* v1=NULL, const map<_Key,T>* v2=NULL) {
        return GeneralIterator<T>(new MapIterator<_Key, T>(v0, v1, v2));
    }

};


GeneralIterator<Block*> Network::getAllBlocks() const
{
return MapIterator<AString, Block*>::createIterator(&_blocks);
}



template <typename T ,class Compare = less<T> >
class ITERATOR_CLASS SetIterator : public IteratorBase<T>
{
    const static int MAX_SUBSTRATUM = 3;
    const set<T, Compare>* _substratum[MAX_SUBSTRATUM];
    int _currentSubstratum;
    typename set<T, Compare>::const_iterator _it;
    SetIterator(const set<T, Compare>* v0, const set<T, Compare>* v1=NULL, const set<T, Compare>* v2=NULL) {
        _substratum[0] = v0;
        _substratum[1] = v1;
        _substratum[2] = v2;
        _currentSubstratum = 0;
        _it = _substratum[_currentSubstratum]->begin();
    }


    //MapIterator(const MapIterator& other)
    //{
    //  _substratum[0] = other._substratum[0];
    //  _substratum[1] = other._substratum[1];
    //  _substratum[2] = other._substratum[2];
    //  _currentSubstratum = 0;
    //  _it = _substratum[_currentSubstratum]->begin();
    //}
public:


    bool hasNext() {
        // if(_it != _substratum[_currentSubstratum]->end()) // jdchen - 04/03/2007
        if (_currentSubstratum == MAX_SUBSTRATUM)
            return false;
        if (_it != (_substratum[_currentSubstratum] ? _substratum[_currentSubstratum]->end() : _it))
            return true;
        else {
            _currentSubstratum++;
            if(_currentSubstratum == MAX_SUBSTRATUM)
                return false;
            // _it = _substratum[_currentSubstratum]->begin(); // jdchen - 04/03/2007
            _it = _substratum[_currentSubstratum] ? _substratum[_currentSubstratum]->begin() : _it;
            return hasNext();
        }
    }


    //##ModelId=45F5182502E8
    T next() {
        T tmp = (*_it);
        _it++;
        return tmp;
    }


    int length() {
        int len = 0;
        for(int i=0; i<MAX_SUBSTRATUM; i++) {
            if(_substratum[i] != NULL)
                len += (int)_substratum[i]->size();
            else
                break;
        }
        return len;
    }


    static GeneralIterator<T> createIterator(const set<T, Compare>* v0, const set<T,Compare>* v1=NULL, const set<T,Compare>* v2=NULL) {
        return GeneralIterator<T>(new SetIterator<T, Compare>(v0, v1, v2));
    }
};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值