Create a well-behaved iterator for a write-to-pointer API

断了今生、忘了曾经 提交于 2019-12-12 04:45:20

问题


I have to create an iterator for an API that features old-style "write-out to pointer" accessors only. The API in question is OGR's; one of the classes in question is OGRLineString (For reference: http://www.gdal.org/classOGRLineString.html). This class stores a number of points, which can be accessed using the following getter method:

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

In order to use the accessor one creates a new OGRPoint object and passes the pointer to it to the method, which writes the data to the allocated object. For example:

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

Now, I'd like to implement a (STL-like) iterator. Even if I place big warning signs everywhere stating that the supplied OGRPoints are non-modifiable (i.e., const), and won't update if another piece of code modifies the OGRLineString that is being iterated, I get a memory leak problem with OGRPoint const &operator*() const, because the API requires me to pass a custom-allocated OGRPoint instance, but the iterator would have to allocate one. Plus, the OGRPoints returned by the iterator shouldn't be deleted when the iterator itself is deleted. Additionally, the OGRLineString does not store actual instances of OGRPoint that are being copied for getPoint, but simple structs storing x/y/z coordinates; all required additional information (e.g., the spatial reference) is copied in the accessor. Thus, a simple #define private public hack wouldn't help.

Is there any sane/clean way to add an iterator without modifying the original source of OGRLineString? E.g., is there a way to add features to the original class, or modify it, like Ruby's "monkey patching" feature would do? Or watch the container's life time in order to clean up on the OGRPoint instances returned by the iterator?


回答1:


This assumes that OGRPoint is copy-constructible. If not, use smart-pointers.

#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()};
}



回答2:


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);}

This "pseudo pointer" return type is used when you need code that manages the lifetime of a returned value. It can also be used to "return a mutable reference" for internal members that don't actually exist. vector<bool> uses bitfields internally instead of bool objects, but uses this same pattern to return a mutable reference from operator[].



来源:https://stackoverflow.com/questions/27969824/create-a-well-behaved-iterator-for-a-write-to-pointer-api

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!