搜索/迭代的boost ::精神::齐::符号 [英] Searching/Iterating boost::spirit::qi::symbols

查看:130
本文介绍了搜索/迭代的boost ::精神::齐::符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有一个符号表:

struct MySymbols : symbols<char, MyEnum::Fruits>
{
    MySymbols ()
        : symbols<char, MyEnum::Fruits>(std::string("MySymbols"))
    {
        add("apple", MyEnum::Apple)
           ("orange", MyEnum::Orange);
    }
};

我要遍历表,以搜索由数据值的符号。我无法使用拉姆达前pressions所以我实现了一个简单的类:

I want to iterate over the table in order to search for a symbol by data value. I cannot use lambda expressions so I implemented a simple class:

template<typename T>
struct SymbolSearcher
{
    SymbolSearcher::SymbolSearcher(T searchFor)
        : _sought(searchFor)
    {
        // do nothing
    }

    void operator() (std::basic_string<char> s, T ct)
    {
        if (_sought == ct)
        {
            _found = s;
        }
    }

    std::string found() const { return _found; }

private:
    T _sought;
    std::string _found;
};

和我如下使用它:

SymbolSearcher<MyEnum::Fruits> search(ct);
MySymbols symbols;

symbols.for_each(search);
std::string symbolStr = search.found();

如果我设置 _found = S 断点我可以证实,_found是越来越设置,但是search.found()总是返回一个空字符串。我猜它是与仿函数被调用内部的for_each的方式,但我不知道。

If I set a breakpoint on _found = s I can confirm that _found is getting set, however search.found() always returns an empty string. I'm guessing it has something to do with the way the functor is being called inside for_each but I don't know.

我是什么做错了吗?

推荐答案

这可能是


  • 字符串的实际值是空字符串(不太可能)

  • the actual value of the string is the empty string (unlikely)

仿函数被按值传递,使得状态仿函数无用(如原来的状态实际上不会传递)。

the functor is being passed by value, making the stateful functor useless (as the original state will not actually be passed).

您可以使 _found 字段的引用(需要您确保遵守规则 - - 三,使其工作)。

You could make the _found field a reference (requiring you to make sure to respect the Rule-Of-Three to make it work).

下面是由VIA SymbolSearcher 断言往返的结果表明,该原理的演示: http://liveworkspace.org/$c$c/4qupWC$1

Here's a demonstration that shows the principle by asserting the result of a roundtrip via SymbolSearcher: http://liveworkspace.org/code/4qupWC$1

#include <boost/spirit/include/qi.hpp>

namespace qi     = boost::spirit::qi;

template<typename T>
struct SymbolSearcher
{
    SymbolSearcher(T searchFor, std::string& result) : _sought(searchFor), _found(result)
    {
    }

    void operator() (std::basic_string<char> s, T ct)
    {
        if (_sought == ct)
        {
            _found = s;
        }
    }

    std::string found() const { return _found; }

private:
    T _sought;
    std::string& _found;
};

int main()
{
    const std::string str("mies");

    typedef std::string::const_iterator It;
    It begin = str.cbegin();
    It end   = str.cend();

    qi::symbols<char, int> symbols;
    symbols.add("aap", 1)("noot", 2)("mies", 3);

    int out;
    bool ok = qi::parse(begin, end, symbols, out);
    assert(ok);

    std::string found;
    SymbolSearcher<int> sf(out, found);
    symbols.for_each(sf);

    assert(str == sf.found());
}

这篇关于搜索/迭代的boost ::精神::齐::符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆