为write-to-pointer API创建一个行为良好的迭代器

时间:2015-01-15 17:50:50

标签: c++ pointers stl iterator ogr

我必须为API创建一个迭代器,它只具有旧式的“写出指针”访问器。有问题的API是OGR;其中一个类是OGRLineString(供参考:http://www.gdal.org/classOGRLineString.html)。该类存储了许多点,可以使用以下getter方法访问:

 void OGRLineString::getPoint(int pos, OGRPoint *out)

为了使用访问器,可以创建一个新的OGRPoint对象,并将指针传递给该方法,该方法将数据写入分配的对象。例如:

OGRPoint *p = new OGRPoint();
lineString->getPoint(0, p);

现在,我想实现一个类似STL的迭代器。即使我在所有地方都放置了大的警告标志,指出所提供的OGRPoint是不可修改的(即const),并且如果另一段代码修改了OGRLineString,则不会更新正在迭代,我得到OGRPoint const &operator*() const的内存泄漏问题,因为API要求我传递自定义分配的OGRPoint实例,但迭代器必须分配一个。另外,删除迭代器本身时,不应删除迭代器返回的OGRPoint。此外,OGRLineString不会存储为OGRPoint复制的getPoint的实际实例,而是存储x / y / z坐标的简单结构;所有必需的附加信息(例如,空间参考)都被复制到存取器中。因此,简单的#define private public黑客无济于事。

是否有任何理智/干净的方法来添加迭代器而不用修改OGRLineString的原始来源?例如,有没有办法向原始类添加功能,或修改它,就像Ruby的“猴子修补”功能一样?或者观察容器的生命周期,以便清理迭代器返回的OGRPoint实例?

2 个答案:

答案 0 :(得分:1)

class this_is_my_iterator;

class OutputOGRPoint {
    explicit OutputOGRPoint(this_is_my_iterator* parent_)
        :parent(parent_), p(new OGRPoint()) 
    {}
    ~OutputOGRPoint();
    operator OGRPoint *() {return p;}

    OutputOGRPoint(OutputOGRPoint &&)=default;
    OutputOGRPoint&operator=(OutputOGRPoint &&)=default;
private:    
    this_is_my_iterator* parent;
    std::unique_ptr<OGRPoint> p;
};

class this_is_my_iterator {
    OutputOGRPoint operator*()(return OutputOGRPoint(this);}
private:
    friend OutputOGRPoint;
    void apply_operator_star_changes(OGRPoint *p);
};

inline OutputOGRPoint::~OutputOGRPoint()
{parent->apply_operator_star_changes(p);}

当您需要管理返回值的生命周期的代码时,将使用此“伪指针”返回类型。它还可以用于为实际不存在的内部成员“返回可变引用”。 vector<bool>在内部使用位域而不是bool个对象,但使用相同的模式从operator[]返回可变引用。

答案 1 :(得分:1)

这假定OGRPoint是可复制构造的。如果没有,请使用智能指针。

#include <iterator>

#include <ogr_geometry.h>

struct OGR_SimpleCurve_Points_Iterator : std::iterator< std::random_access_iterator_tag, const OGRPoint >
{
    OGR_SimpleCurve_Points_Iterator( OGRSimpleCurve* curve=nullptr, int index=0 )
        : curve(curve), index(index) {}

    OGR_SimpleCurve_Points_Iterator& operator++() { ++index; return *this; }
    OGR_SimpleCurve_Points_Iterator operator++(int) { OGR_SimpleCurve_Points_Iterator ret(*this); ++index; return ret; }
    OGR_SimpleCurve_Points_Iterator& operator--() { --index; return *this; }
    OGR_SimpleCurve_Points_Iterator operator--(int) { OGR_SimpleCurve_Points_Iterator ret(*this); --index; return ret; }

    OGR_SimpleCurve_Points_Iterator& operator+=(int n) { index+=n; return *this; }
    OGR_SimpleCurve_Points_Iterator& operator-=(int n) { index-=n; return *this; }

    OGR_SimpleCurve_Points_Iterator operator+(int n) { return OGR_SimpleCurve_Points_Iterator{curve,index+n}; }
    OGR_SimpleCurve_Points_Iterator operator-(int n) { return OGR_SimpleCurve_Points_Iterator{curve,index-n}; }

    int operator-(const OGR_SimpleCurve_Points_Iterator& other) { return index-other.index; }

    OGRPoint operator*() { OGRPoint p; curve->getPoint(index,&p); return p; }

    OGRPoint operator[](int ofs) { OGRPoint p; curve->getPoint(index+ofs,&p); return p; }

    bool operator == ( const OGR_SimpleCurve_Points_Iterator& other ) { return index==other.index; }
    bool operator != ( const OGR_SimpleCurve_Points_Iterator& other ) { return index!=other.index; }
    bool operator  > ( const OGR_SimpleCurve_Points_Iterator& other ) { return index >other.index; }
    bool operator >= ( const OGR_SimpleCurve_Points_Iterator& other ) { return index>=other.index; }
    bool operator  < ( const OGR_SimpleCurve_Points_Iterator& other ) { return index <other.index; }
    bool operator <= ( const OGR_SimpleCurve_Points_Iterator& other ) { return index<=other.index; }

private:
    OGRSimpleCurve* curve;
    int index;
};

OGR_SimpleCurve_Points_Iterator begin( OGRSimpleCurve* curve )
{
    return OGR_SimpleCurve_Points_Iterator{curve};
}

OGR_SimpleCurve_Points_Iterator end( OGRSimpleCurve* curve )
{
    return OGR_SimpleCurve_Points_Iterator{curve,curve->getNumPoints()};
}