繁体   English   中英

为旧式C链表界面提供C ++迭代器

[英]Providing c++ iterators for a legacy C linked-list interface

我有一个Visual Studo 2008 C ++应用程序,试图在其中将迭代器支持添加到旧版C-API的链表结构中。 C接口如下所示:

typedef struct _LINKED_LIST_INFO {
    struct _LINKED_LIST_INFO* Next;
    const char* name;
    // more elements. some are fixed-size; others are pointers to other structures.
} LINKED_LIST_INFO;

DWORD GetLinkedList( LINKED_LIST_INFO* pInfo, PULONG pOutBufLen );

我希望能够像这样使用它:

int _tmain( int /*argc*/, _TCHAR* /*argv*/[] )
{
    MyLinkedList elements;
    for( MyLinkedList::const_iterator it = elements.begin();
         it != elements.end();
         ++it )
    {
        printf( "Name: %s\r\n", it->Name().c_str() );
    }

    return 0;
}

因此,我创建了这3个类。 但是,我的MyInfoIterator类的operator->()有问题。 我无法返回指向MyInfo的临时指针,所以出现错误: error C2440: 'return' : cannot convert from 'MyInfo' to 'const MyInfo *'

对这个问题有什么好的解决方案?

/// wrap the legacy C structure and provide C++ accessors
class MyInfo
{
public:

    MyInfo( const LINKED_LIST_INFO& info ) : elem_( info ) { };

    std::string Name() const { return elem_.name; };

private:
    /// one element of the linked list
    const LINKED_LIST_INFO& elem_;
}; // class MyInfo

namespace detail {

/// buffer to hold the legacy C linked-list
typedef std::vector< BYTE > MyBuffer;

/// iterator support for the legacy C linked-list
class MyInfoIterator 
    : public std::iterator< std::input_iterator_tag, MyInfo > 
{
public:
    explicit MyInfoIterator( MyBuffer& list ) : data_( list )
    {
        elem_ = reinterpret_cast< LINKED_LIST_INFO* >( &data_.front() );
    };

    MyInfoIterator() : elem_( NULL ) { };

    MyInfoIterator& operator++() 
    {
        elem_ = elem_->Next;
        return *this;
    };

    value_type operator*() { return *elem_; };

    //  error C2440: 'return' : cannot convert from 'MyInfo' to 'const MyInfo *'
    const value_type* operator->() { return elem_; };

    friend bool operator==( const MyInfoIterator& i, 
                            const MyInfoIterator& j ) 
    { 
        return i.elem_ == j.elem_;
    };

private:

    /// linked-list of elements
    MyBuffer data_;

    /// current position within the element list
    LINKED_LIST_INFO* elem_;

}; // class MyInfoIterator

bool operator!=( const MyInfoIterator& i, const MyInfoIterator& j ) 
{ 
    return !operator==( i, j );
}

}; // namespace detail

/// provide iterator access for the legacy C linked-list API
class MyLinkedList
{
public:
    typedef detail::MyInfoIterator const_iterator;

    const_iterator begin() const 
    { 
        ULONG size = sizeof( LINKED_LIST_INFO );
        detail::MyBuffer buffer( size );

        DWORD ec = ::GetLinkedList( 
            reinterpret_cast< LINKED_LIST_INFO* >( &buffer.front() ), &size );
        if( ERROR_BUFFER_OVERFLOW == ec )
        {
            buffer.resize( size );
            ec = ::GetLinkedList( 
                reinterpret_cast< LINKED_LIST_INFO* >( &buffer.front() ), &size );
        }

        if( ERROR_SUCCESS != ec )
            Win32Exception::Throw( ec );

        return const_iterator( buffer ); 
    };

    const_iterator end() const { return const_iterator(); };

}; // class MyInfo

谢谢PaulH

编辑:

我无法更改旧版API或它的关联结构。

EDIT2:

我认为我有一个可行的解决方案,可以保留隐藏底层链接列表实现的意图,并通过返回static MyInfo的地址来维护每个类的职责分离。

class MyInfo
{
   // ...
protected:
    MyInfo() : info_( NULL ) { };
    void Set( const LINKED_LIST_INFO* info ) { info_ = info; };
private:
    friend MyInfoIterator;
    const LINKED_LIST_INFO* info_;
};

const value_type& MyInfoIterator::operator*() const 
{ 
    static MyInfo info;
    info.Set( elem_ );
    return info;
};

const value_type* MyInfoIterator::operator->() const
{ 
    static MyInfo info;
    info.Set( elem_ );
    return &info; 
};

把事情简单化 :

class MyInfoIterator 
    : public std::iterator< std::input_iterator_tag, _LINKED_LIST_INFO >
{
   _LINKED_LIST_INFO* p;
public;
    MyInfoIterator(_LINKED_LIST_INFO* pointer) : p(pointer) {}

    [Same as you did]

    value_type& operator*() { return *p; }
}

我相信您的value_type是错误的...如果您将STL迭代器作为接口继承,则值类型必须为您所包含的类型。 在您的情况下,您说您包含MyInfo元素,但是您尝试返回LINKED_LIST_INFO*

返回MyInfo或声明您的迭代器容器的value_typeLINKED_LIST_INFO

可能需要第一个选项,因为您可能需要辅助类来使访问器方法能够正确地操纵结构的成员。

编辑:

自然地,您应该认识到,仅拥有std::vector<LINKED_LIST_INFO>std::vector<MyInfo>为您提供所需的所有功能,而没有实现和维护问题。

编辑#2:

实际上,您没有std::vector<MyInfo>因为它不是默认可构造的,因此一旦解决了当前错误,您将获得另一个基于typedef std::vector< MyInfo > MyBuffer; 在为MyInfo提供默认构造函数之前,它将无法在模板构造中解决

编辑#3:

您的value_type operator*() { return *elem_; } value_type operator*() { return *elem_; }的格式不正确。 在执行此二进制操作后,您不应返回内部对象的副本。 您应该返回参考。 在您的情况下,您将其视为deref而不是乘法运算,这很好,但是按值复制返回仍然是错误的。

看起来只需返回elem_的地址elem_

const value_type* operator->() { return &elem_; };

您确定将旧列表适当地更改为std::liststd::vector工作不会少吗?

这太可怕了(您不应该这样做):

class MyInfo {
   char* name() const {
      return ((_LINKED_LIST_INFO*)this)->name;
   }
};

class MyInfoIterator 
: public std::iterator< std::input_iterator_tag, MyInfo>
{
   _LINKED_LIST_INFO* p;
public:
    MyInfoIterator(LINKED_LIST_INFO* pointer) : p(pointer) {}

    [Same as you did]

    reference operator*() const { return *(MyInfo*)p; }
};

干净的方法可能是:

class MyInfo {
public:

  [...]

  private:
    const LINKED_LIST_INFO* elem_;
}

class MyInfoIterator 
: public std::iterator< std::input_iterator_tag, MyInfo>
{
   mutable MyInfo p;
public:
    MyInfoIterator(LINKED_LIST_INFO* pointer) : p(pointer) {}

    [...]

    reference& operator*() const { return p; }
    pointer_type operator->() const { return &p; }
};

这就是一些Boost迭代器的实现方式(我的意思是干净的方法,而不是第一种),例如,请参见[1]。

[1] http://www.boost.org/doc/libs/1_46_1/libs/iterator/doc/counting_iterator.html

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM