使用boost :: range

时间:2015-07-27 18:52:41

标签: c++ algorithm boost gtkmm boost-range

我正在为文本编辑器创建一个unindent算法。我设法获得了操作范围,但是当我想要进行Gtk :: TextBuffer :: erase时,它失败了:

void unindentSelection(const Glib::RefPtr<Gtk::TextBuffer> &buffer)
{
    Gtk::TextBuffer::iterator start, end;

    buffer->get_selection_bounds(start, end);

    auto selRange = boost::make_iterator_range(start, end);
    auto buffRange = boost::make_iterator_range(buffer->begin(), buffer->end());

    auto prevRangeRev = boost::make_iterator_range(buffRange.begin(), selRange.begin()) | boost::adaptors::reversed;
    auto prevRangeLineRev = boost::find<boost::return_begin_found>(prevRangeRev, '\n');
    auto prevRangeLine = prevRangeLineRev | boost::adaptors::reversed;

    auto afterRange = boost::make_iterator_range(selRange.end(), buffRange.end());
    auto afterRangeLine = boost::find<boost::return_begin_found>(afterRange, '\n');

    auto exSelRangeAux = boost::join(prevRangeLine, selRange);
    auto exSelRange = boost::join(exSelRangeAux, afterRangeLine);

    show_range(exSelRange);

    while (true)
    {
        auto spaceRange = boost::find_if<boost::return_begin_found>(exSelRange, findNonspaceNL);

        if (boost::distance(spaceRange))
        {
            buffer->erase(spaceRange.begin(), spaceRange.end());
        }
    }
}

TextEditor.cpp:501:31:错误:来自&#39; boost :: range_detail :: join_iterator&gt;,Gtk :: TextIter,unsigned int,unsigned int,boost :: iterators :: bidirectional_traversal_tag&gt; ,Gtk :: TextIter,unsigned int,unsigned int,boost :: iterators :: bidirectional_traversal_tag&gt;&#39;到#const迭代器&#39; (又名&#39; const Gtk :: TextIter&#39;)                 buffer-&gt; erase(spaceRange.begin(),afterRangeLine.end());                               ^ ~~~~~~~~~~~~~~~~~ /usr/include/gtkmm-3.0/gtkmm/textiter.h:145:7:注意:候选构造函数(隐式复制构造函数)不可行:没有来自&#39; boost :: range_detail :: join_iterator&gt;的已知转换, Gtk :: TextIter,unsigned int,unsigned int,boost :: iterators :: bidirectional_traversal_tag&gt;,Gtk :: TextIter,unsigned int,unsigned int,boost :: iterators :: bidirectional_traversal_tag&gt;&#39; to&#39; const Gtk :: TextIter&amp;&#39;第一个论点 class TextIter       ^ /usr/include/gtkmm-3.0/gtkmm/textiter.h:145:7:注意:候选构造函数(隐式移动构造函数)不可行:来自&#39; boost :: range_detail :: join_iterator&gt;的已知转换, Gtk :: TextIter,unsigned int,unsigned int,boost :: iterators :: bidirectional_traversal_tag&gt;,Gtk :: TextIter,unsigned int,unsigned int,boost :: iterators :: bidirectional_traversal_tag&gt;&#39;到&#39; Gtk :: TextIter&amp;&amp;&#39;第一个论点 class TextIter       ^ /usr/include/gtkmm-3.0/gtkmm/textbuffer.h:378:34:注意:将参数传递给参数&#39; range_begin&#39;这里   迭代器擦除(const iterator&amp; range_begin,const iterator&amp; range_end);

有什么想法吗?

1 个答案:

答案 0 :(得分:1)

我必须创建一个迭代器类型,其中包含一个模板化的迭代器&#39;在我的情况下(在我的例子中是Gtk :: TextIter),使用&#39; boost :: iterator_facade&#39;。对迭代器的所有操作都转发到&#39;它除了运算符*()之外,它只给出了普通的迭代器。所以擦除就是这样做的:

buffer->erase(*spaceRange.begin(), *spaceRange.end());

这种新类型的迭代器对于操作范围非常有用。不确定这是最好的解决方案,但有效。