0

我必须为仅具有旧式“写出指针”访问器的 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 的)迭代器。即使我在各处放置大警告标志,说明提供的sOGRPoint是不可修改的(即const,要求我传递一个自定义分配的实例,但迭代器必须分配一个。另外,当迭代器本身被删除时,迭代器返回的 s 不应该被删除。此外,不存储为 复制的实际实例,而是存储 x/y/z 坐标的简单结构;所有需要的附加信息(例如,空间参考)都复制到访问器中。因此,一个简单的OGRLineStringOGRPoint const &operator*() constOGRPointOGRPointOGRLineStringOGRPointgetPoint#define private publichack 无济于事。

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

4

2 回答 2

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[].

于 2015-01-15T18:20:32.577 回答
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()};
}
于 2015-01-15T18:36:11.957 回答