从Boost.Spirit.Qi制作shared_ptr

时间:2015-05-26 16:46:06

标签: c++ parsing boost shared-ptr boost-spirit-qi

我想使用Spirit Qi语法将文本解析为字符串的共享指针。实际上,我想将多元多项式解析成它们的系统,在多项式中出现各种先前遇到的符号,但是现在,让我们从文本解析为std::shared_ptr<std::string>

我已经挣扎了很长时间了,并在头文件中提取了这个小测试语法:

#define BOOST_SPIRIT_USE_PHOENIX_V3 1

#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/bind.hpp>


namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;

template<typename Iterator>
struct SharedPtrParser :  qi::grammar<Iterator, std::shared_ptr<std::string>(), boost::spirit::ascii::space_type>
{
    SharedPtrParser() : SharedPtrParser(startrule)
    {
        startrule = qi::eps[ qi::_val = std::make_shared<std::string>()];
    }

    qi::rule<Iterator,std::shared_ptr<std::string>()> startrule;
};

以及它在main中的用法:

#include "shared_ptr_test.h"

int main()
{



    SharedPtrParser<std::string::const_iterator> C;

    std::string text = "thisissometext";

    auto iter = text.begin();
    auto end = text.end();


    std::shared_ptr<std::string> S;

    bool s = phrase_parse(iter, end, C,boost::spirit::ascii::space, S);


    return 0;
}

我知道我的语法并没有蹲下。我只是想立即从Qi做出共享指针!为此,我发现了一些关于将智能指针与Qi结合使用的提示,但没有一些语法可以作为它们的工厂。我觉得我已经很亲密了,特别是在精神综合论坛上发现了this的帖子。但是,我还没有把它放在一起。

编译时尝试产生两个错误,如写入ATM。

之一:

./shared_ptr_test.h:16:22: error: no matching constructor for initialization of 'SharedPtrParser<std::__1::__wrap_iter<const char *> >'
        SharedPtrParser() : SharedPtrParser(startrule)
                            ^               ~~~~~~~~~
shared_ptr_test.cpp:8:47: note: in instantiation of member function 'SharedPtrParser<std::__1::__wrap_iter<const char *> >::SharedPtrParser' requested here
        SharedPtrParser<std::string::const_iterator> C;
                                                     ^
./shared_ptr_test.h:14:8: note: candidate constructor (the implicit copy constructor) not viable: no known conversion from 'qi::rule<__wrap_iter<const char *>,
      std::shared_ptr<std::string> ()>' to 'const SharedPtrParser<std::__1::__wrap_iter<const char *> >' for 1st argument
struct SharedPtrParser :  qi::grammar<Iterator, std::shared_ptr<std::string>(), boost::spirit::ascii::space_type>
       ^
./shared_ptr_test.h:16:2: note: candidate constructor not viable: requires 0 arguments, but 1 was provided
        SharedPtrParser() : SharedPtrParser(startrule)
        ^

2:

In file included from shared_ptr_test.cpp:1:
In file included from ./shared_ptr_test.h:4:
In file included from /usr/local/include/boost/spirit/include/qi.hpp:16:
In file included from /usr/local/include/boost/spirit/home/qi.hpp:21:
In file included from /usr/local/include/boost/spirit/home/qi/nonterminal.hpp:14:
In file included from /usr/local/include/boost/spirit/home/qi/nonterminal/rule.hpp:35:
/usr/local/include/boost/spirit/home/qi/reference.hpp:43:30: error: no matching member function for call to 'parse'
            return ref.get().parse(first, last, context, skipper, attr_);
                   ~~~~~~~~~~^~~~~
/usr/local/include/boost/spirit/home/qi/parse.hpp:164:40: note: in instantiation of function template specialization 'boost::spirit::qi::reference<const
      boost::spirit::qi::rule<std::__1::__wrap_iter<const char *>, std::__1::shared_ptr<std::__1::basic_string<char> > (),
      boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal, boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space,
      boost::spirit::char_encoding::ascii> >, 0>, boost::spirit::unused_type, boost::spirit::unused_type> >::parse<std::__1::__wrap_iter<char *>,
      boost::spirit::context<boost::fusion::cons<std::__1::shared_ptr<std::__1::basic_string<char> > &, boost::fusion::nil_>, boost::spirit::locals<mpl_::na, mpl_::na, mpl_::na,
      mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na> >, boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space,
      boost::spirit::char_encoding::ascii> >, std::__1::shared_ptr<std::__1::basic_string<char> > >' requested here
        if (!compile<qi::domain>(expr).parse(
                                       ^
/usr/local/include/boost/spirit/home/qi/parse.hpp:197:20: note: in instantiation of function template specialization 'boost::spirit::qi::phrase_parse<std::__1::__wrap_iter<char *>,
      SharedPtrParser<std::__1::__wrap_iter<const char *> >, boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal,
      boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::ascii> >, 0>, std::__1::shared_ptr<std::__1::basic_string<char>
      > >' requested here
        return qi::phrase_parse(first, last, expr, skipper, skip_flag::postskip, attr);
                   ^
shared_ptr_test.cpp:18:11: note: in instantiation of function template specialization 'boost::spirit::qi::phrase_parse<std::__1::__wrap_iter<char *>,
      SharedPtrParser<std::__1::__wrap_iter<const char *> >, boost::proto::exprns_::expr<boost::proto::tagns_::tag::terminal,
      boost::proto::argsns_::term<boost::spirit::tag::char_code<boost::spirit::tag::space, boost::spirit::char_encoding::ascii> >, 0>, std::__1::shared_ptr<std::__1::basic_string<char>
      > >' requested here
        bool s = phrase_parse(iter, end, C,boost::spirit::ascii::space, S);
                 ^
/usr/local/include/boost/spirit/home/qi/nonterminal/rule.hpp:274:14: note: candidate function [with Context =
      boost::spirit::context<boost::fusion::cons<std::__1::shared_ptr<std::__1::basic_string<char> > &, boost::fusion::nil_>, boost::spirit::locals<mpl_::na, mpl_::na, mpl_::na,
      mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na, mpl_::na> >, Skipper = boost::spirit::qi::char_class<boost::spirit::tag::char_code<boost::spirit::tag::space,
      boost::spirit::char_encoding::ascii> >, Attribute = std::__1::shared_ptr<std::__1::basic_string<char> >] not viable: no known conversion from 'std::__1::__wrap_iter<char *>' to
      'std::__1::__wrap_iter<const char *> &' for 1st argument
        bool parse(Iterator& first, Iterator const& last
             ^
/usr/local/include/boost/spirit/home/qi/nonterminal/rule.hpp:320:14: note: candidate function template not viable: requires 6 arguments, but 5 were provided
        bool parse(Iterator& first, Iterator const& last

一点帮助?

1 个答案:

答案 0 :(得分:2)

我的问题在这里是微不足道的。我没有为构造函数class Datasource(models.Model): objects = GChartsManager() idestacao_meteo = models.IntegerField(db_column='idDATASOURCE', primary_key=True, editable=False) # Field name made lowercase. nome_estacao = models.CharField(db_column='NAME', max_length=45) # Field name made lowercase. fabricante = models.CharField(db_column='MANUFACTURER', max_length=45, blank=True) # Field name made lowercase. modelo = models.CharField(db_column='MODEL', max_length=45, blank=True) # Field name made lowercase. node_idnode = models.ForeignKey('Node', db_column='site_idSITE', blank=True, null=True) # Field name made lowercase. class Meta: managed = False db_table = 'datasource' class Node(models.Model): objects = GChartsManager() idnode = models.IntegerField(db_column='idSITE', primary_key=True, editable=False) # Field name made lowercase. nome = models.CharField(db_column='NAME', max_length=45) # Field name made lowercase. informacoes = models.CharField(db_column='DESCRIPTION', max_length=45, blank=True) # Field name made lowercase. class Meta: managed = False db_table = 'site' class Radio(models.Model): objects = GChartsManager() idradio = models.IntegerField(db_column='idRADIO', primary_key=True, editable=False) endreal = models.CharField(db_column='ENDREAL', max_length=64, blank=True) # Field name made lowercase. radiotipo_idradiotipo = models.ForeignKey('Radioinfo', db_column='radioInfo_idRADIOINFO') # Field name made lowercase. datasource_idestacao_meteo = models.ForeignKey(Datasource, db_column='datasource_idDATASOURCE', blank=True, null=True) # Field name made lowercase. class Meta: managed = False db_table = 'radio' class Radiotipo(models.Model): objects = GChartsManager() idradiotipo = models.IntegerField(db_column='idRADIOINFO', primary_key=True, editable=False) # Field name made lowercase. nome = models.CharField(db_column='NAME', max_length=45) descricao = models.CharField(db_column='DESCRIPTION', max_length=300, blank=True) class Meta: managed = False db_table = 'radioinfo' class Snapshot(models.Model): objects = GChartsManager() idsnap = models.IntegerField(db_column='idSNAP', primary_key=True, editable=False) # Field name made lowercase. valor = models.FloatField(db_column='VALUE', blank=True, null=True) # Field name made lowercase. snapshot = models.DateTimeField(db_column='SNAPSHOT', blank=True, null=True) # Field name made lowercase. tag_idtag = models.ForeignKey('Tag', db_column='tag_idTAG') # Field name made lowercase. class Meta: managed = False db_table = 'snapshot' class Tag(models.Model): objects = GChartsManager() idtag = models.IntegerField(db_column='idTAG', primary_key=True, editable=False) # Field name made lowercase. desvio = models.FloatField(db_column='DEVIATION', blank=True, null=True) # Field name made lowercase. tempo_max = models.IntegerField(db_column='TIME_MAX', blank=True, null=True) # Field name made lowercase. conv_rate = models.IntegerField(db_column='CONV_RATE', blank=True, null=True) # Field name made lowercase. taginfo_idtaginfo1 = models.ForeignKey('Taginfo', db_column='tagInfo_idTAGINFO') # Field name made lowercase. datasource_idestacao_meteo = models.ForeignKey(Datasource, db_column='datasource_idDATASOURCE', blank=True, null=True) # Field name made lowercase. class Meta: managed = False db_table = 'tag' class Taginfo(models.Model): objects = GChartsManager() idtaginfo = models.IntegerField(db_column='idTAGINFO', primary_key=True, editable=False) # Field name made lowercase. nome = models.CharField(db_column='NAME', max_length=45) # Field name made lowercase. descricao = models.CharField(db_column='DESCRIPTION', max_length=255, blank=True) # Field name made lowercase. class Meta: managed = False db_table = 'taginfo' class Valores(models.Model): objects = GChartsManager() idvalores = models.IntegerField(db_column='idVALUES', primary_key=True, editable=False) # Field name made lowercase. valor = models.FloatField(db_column='VALUE') # Field name made lowercase. datahora = models.DateTimeField(db_column='DATETIME') # Field name made lowercase. tag_idtag = models.ForeignKey(Tag, db_column='tag_idTAG') # Field name made lowercase. class Meta: managed = False db_table = 'values' 指定。也就是说,::base_type的行应为SharedPtrParser() : SharedPtrParser(startrule)