2013-07-12 2 views
5

속성 세트 관리를 담당하는 컨테이너가 있습니다. 이 클래스는 부분적으로 다음과 같은 :포인터의 반복자에 대한 참조 해제 어댑터

class AttributeSet 
{ 
public: 
    // ... interface is irrelevant for my question. 

private: 
    std::vector<boost::shared_ptr<Attribute> > m_attributes; 
}; 

특성은 다형성 그래서 속성은 NULL이 될 수 없다 그러나 포인터로 저장해야합니다. STL 컨테이너에 대한 지원은 매우 일반적입니다

, BOOST_FOREACH 문서에 따르면

BOOST_FOREACH(const Attribute &attribute, attributeSet) 
{ 
    ... 
} 

;

는이 같은 BOOST_FOREACH이 클래스를 사용하려면 STL 컨테이너와 비슷한 것으로 간주됩니다. iterator와 const_iterator 타입과 begin()과 end() 멤버 함수를 중첩했다면, BOOST_FOREACH는 그것을 반복하는 방법을 자동으로 알 수 있습니다.

BOOST_FOREACH(const boost::shared_ptr<Attribute> &attribute, attributeSet) 
{ 
    ... 
} 

이 좋은 그러나 나는 그것이 노출 것을 좋아하지 않는다 : 그래서

class AttributeSet 
{ 
public: 
    typedef std::vector<boost::shared_ptr<Attribute> > container; 
    typedef container::iterator iterator; 
    typedef container::const_iterator const_iterator; 

    iterator begin(); 
    iterator end(); 

    const_iterator begin() const; 
    const_iterator end() const; 

private: 
    container m_attributes; 
}; 

지금은이 작업을 수행 할 수 있습니다

그래서 나는이 같은 것을보고 내 클래스를 업데이트 포인터로서의 속성 호출자 쪽에서 이것은 잡음이며 무의미한 NULL 검사를 생성합니다.

문제를 해결하는 방법에 대한 몇 가지 아이디어가 있습니다. 예를 들어,이 같은 좋은 것 :

class AttributeSet 
{ 
public: 
    typedef std::vector<boost::shared_ptr<Attribute> > container; 
    typedef iterator_dereference_adapter<container::iterator> iterator; 
    typedef iterator_dereference_adapter<container::const_iterator> const_iterator; 

    iterator begin() { return iterator(m_attributes.begin()); } 
    iterator end() { return iterator(m_attributes.end()); } 

    const_iterator begin() const { return const_iterator(m_attributes.begin()); } 
    const_iterator end() const { return const_iterator(m_attributes.end()); } 

private: 
    container m_attributes; 
}; 

'iterator_dereference_adapter'클래스는 다소 설명이 필요하다. 포인터의 기존 반복자를 래핑하고 포인터 값을 역 참조합니다. 그래서 마지막으로

, 내 질문에 ...

내가 떨어져 가서이 어댑터를 작성하기 전에, STL에서 무언가 또는이 클래스 같은 부스트가?

다른 아이디어에 대해 열려 있습니다.

+5

부스트에는 [간접적 인 반복자] (http://www.boost.org/doc/libs/1_54_0/libs/iterator/doc/indirect_iterator.html)가 있습니다. –

+0

좋은 질문입니다. +1. –

답변

7

부스트는 반복기를 포인터 유형으로 랩핑하고 자동으로 역 참조하기 위해 정확히 의도 된 indirect_iterator입니다.

관련 문제