Разбор структуры с массивами с помощью Boost Spirit
Прежде чем я начну, да, я знаю, что есть много постов, связанных с этой темой, я провожу больше дня, читая, но мои знания о Boost::spirit ограничены, поэтому я могу понять, как решить мою проблему.
Я беру пример с Boost Employee и модифицирую его, позвольте мне показать вам
#include <boost/config/warning_disable.hpp>
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix_core.hpp>
#include <boost/spirit/include/phoenix_operator.hpp>
#include <boost/spirit/include/phoenix_object.hpp>
#include <boost/fusion/include/adapt_struct.hpp>
#include <boost/fusion/include/io.hpp>
#include <iostream>
#include <string>
#include <complex>
namespace client
{
namespace qi = boost::spirit::qi;
namespace ascii = boost::spirit::ascii;
///////////////////////////////////////////////////////////////////////////
// Our employee struct
///////////////////////////////////////////////////////////////////////////
//[tutorial_employee_struct
struct employee
{
char user[31];
char pass[34];
uint16_t page; // Pagina de usuarios
uint16_t port; // Puerto por donde es realiza la comunicación
uint8_t ip[4]; // Direccion del socket de juego
uint16_t amount;
uint16_t cents;
char nick[9];
uint8_t already_logged;
uint8_t version;
uint8_t extension;
};
}
// We need to tell fusion about our employee struct
// to make it a first-class fusion citizen. This has to
// be in global scope.
//[tutorial_employee_adapt_struct
BOOST_FUSION_ADAPT_STRUCT(
client::employee,
(char, user[31])
(char, pass[34])
(uint16_t, page)
(uint16_t, port)
(uint8_t, ip[4])
(uint16_t, amount)
(uint16_t, cents)
(char, nick[9])
(uint8_t, already_logged)
(uint8_t, version)
(uint8_t, extension)
)
//]
namespace client
{
///////////////////////////////////////////////////////////////////////////////
// Our employee parser
///////////////////////////////////////////////////////////////////////////////
//[tutorial_employee_parser
template <typename Iterator>
struct employee_parser : qi::grammar<Iterator, employee(), ascii::space_type>
{
employee_parser() : employee_parser::base_type(start)
{
using boost::spirit::qi::ushort_;
using ascii::char_;
start %=
char_ >> ','
>> char_ >> ','
>> ushort_ >> ','
;
}
qi::rule<Iterator, employee(), ascii::space_type> start;
};
//]
}
////////////////////////////////////////////////////////////////////////////
// Main program
////////////////////////////////////////////////////////////////////////////
int
main()
{
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout << "\t\tAn employee parser for Spirit...\n\n";
std::cout << "/////////////////////////////////////////////////////////\n\n";
std::cout
<< "Give me an employee of the form :"
<< "employee{age, \"surname\", \"forename\", salary } \n";
std::cout << "Type [q or Q] to quit\n\n";
using boost::spirit::ascii::space;
typedef std::string::const_iterator iterator_type;
typedef client::employee_parser<iterator_type> employee_parser;
employee_parser g; // Our grammar
std::string str;
while (getline(std::cin, str))
{
if (str.empty() || str[0] == 'q' || str[0] == 'Q')
break;
client::employee emp;
std::string::const_iterator iter = str.begin();
std::string::const_iterator end = str.end();
bool r = phrase_parse(iter, end, g, space, emp);
if (r && iter == end)
{
std::cout << boost::fusion::tuple_open('[');
std::cout << boost::fusion::tuple_close(']');
std::cout << boost::fusion::tuple_delimiter(", ");
std::cout << "-------------------------\n";
std::cout << "Parsing succeeded\n";
std::cout << "got: " << boost::fusion::as_vector(emp) << std::endl;
std::cout << "\n-------------------------\n";
}
else
{
std::cout << "-------------------------\n";
std::cout << "Parsing failed\n";
std::cout << "-------------------------\n";
}
}
std::cout << "Bye... :-) \n\n";
return 0;
}
Но я получаю ошибки компиляции, и я не знаю, как ее решить или как написать парсер
In file included from /usr/include/boost/fusion/include/as_vector.hpp:11:0,
from /usr/include/boost/spirit/home/support/attributes.hpp:24,
from /usr/include/boost/spirit/home/qi/detail/attributes.hpp:12,
from /usr/include/boost/spirit/home/qi/action/action.hpp:16,
from /usr/include/boost/spirit/home/qi/action.hpp:14,
from /usr/include/boost/spirit/home/qi.hpp:14,
from /usr/include/boost/spirit/include/qi.hpp:16,
from user.cpp:17:
/usr/include/boost/fusion/container/vector/convert.hpp: In instantiation of ‘struct boost::fusion::result_of::as_vector<const client::employee>’:
/usr/include/boost/fusion/container/vector/convert.hpp:43:5: required by substitution of ‘template<class Sequence> typename boost::fusion::result_of::as_vector<const Sequence>::type boost::fusion::as_vector(const Sequence&) [with Sequence = client::employee]’
user.cpp:140:65: required from here
/usr/include/boost/fusion/container/vector/convert.hpp:27:13: error: invalid use of incomplete type ‘struct boost::fusion::detail::as_vector<11>’
type;
^
In file included from /usr/include/boost/fusion/container/vector/convert.hpp:11:0,
from /usr/include/boost/fusion/include/as_vector.hpp:11,
from /usr/include/boost/spirit/home/support/attributes.hpp:24,
from /usr/include/boost/spirit/home/qi/detail/attributes.hpp:12,
from /usr/include/boost/spirit/home/qi/action/action.hpp:16,
from /usr/include/boost/spirit/home/qi/action.hpp:14,
from /usr/include/boost/spirit/home/qi.hpp:14,
from /usr/include/boost/spirit/include/qi.hpp:16,
from user.cpp:17:
/usr/include/boost/fusion/container/vector/detail/as_vector.hpp:26:12: error: declaration of ‘struct boost::fusion::detail::as_vector<11>’
struct as_vector;
^
In file included from /usr/include/boost/fusion/include/as_vector.hpp:11:0,
from /usr/include/boost/spirit/home/support/attributes.hpp:24,
from /usr/include/boost/spirit/home/qi/detail/attributes.hpp:12,
from /usr/include/boost/spirit/home/qi/action/action.hpp:16,
from /usr/include/boost/spirit/home/qi/action.hpp:14,
from /usr/include/boost/spirit/home/qi.hpp:14,
from /usr/include/boost/spirit/include/qi.hpp:16,
from user.cpp:17:
/usr/include/boost/fusion/container/vector/convert.hpp: In instantiation of ‘struct boost::fusion::result_of::as_vector<client::employee>’:
/usr/include/boost/fusion/container/vector/convert.hpp:34:5: required by substitution of ‘template<class Sequence> typename boost::fusion::result_of::as_vector<Sequence>::type boost::fusion::as_vector(Sequence&) [with Sequence = client::employee]’
user.cpp:140:65: required from here
/usr/include/boost/fusion/container/vector/convert.hpp:27:13: error: invalid use of incomplete type ‘struct boost::fusion::detail::as_vector<11>’
type;
^
In file included from /usr/include/boost/fusion/container/vector/convert.hpp:11:0,
from /usr/include/boost/fusion/include/as_vector.hpp:11,
from /usr/include/boost/spirit/home/support/attributes.hpp:24,
from /usr/include/boost/spirit/home/qi/detail/attributes.hpp:12,
from /usr/include/boost/spirit/home/qi/action/action.hpp:16,
from /usr/include/boost/spirit/home/qi/action.hpp:14,
from /usr/include/boost/spirit/home/qi.hpp:14,
from /usr/include/boost/spirit/include/qi.hpp:16,
from user.cpp:17:
/usr/include/boost/fusion/container/vector/detail/as_vector.hpp:26:12: error: declaration of ‘struct boost::fusion::detail::as_vector<11>’
struct as_vector;
^
user.cpp: In function ‘int main()’:
user.cpp:140:65: error: no matching function for call to ‘as_vector(client::employee&)’
std::cout << "got: " << boost::fusion::as_vector(emp) << std::endl;
^
user.cpp:140:65: note: candidates are:
In file included from /usr/include/boost/fusion/include/as_vector.hpp:11:0,
from /usr/include/boost/spirit/home/support/attributes.hpp:24,
from /usr/include/boost/spirit/home/qi/detail/attributes.hpp:12,
from /usr/include/boost/spirit/home/qi/action/action.hpp:16,
from /usr/include/boost/spirit/home/qi/action.hpp:14,
from /usr/include/boost/spirit/home/qi.hpp:14,
from /usr/include/boost/spirit/include/qi.hpp:16,
from user.cpp:17:
/usr/include/boost/fusion/container/vector/convert.hpp:34:5: note: template<class Sequence> typename boost::fusion::result_of::as_vector<Sequence>::type boost::fusion::as_vector(Sequence&)
as_vector(Sequence& seq)
^
/usr/include/boost/fusion/container/vector/convert.hpp:34:5: note: substitution of deduced template arguments resulted in errors seen above
/usr/include/boost/fusion/container/vector/convert.hpp:43:5: note: template<class Sequence> typename boost::fusion::result_of::as_vector<const Sequence>::type boost::fusion::as_vector(const Sequence&)
as_vector(Sequence const& seq)
^
/usr/include/boost/fusion/container/vector/convert.hpp:43:5: note: substitution of deduced template arguments resulted in errors seen above
Моя единственная потребность - иметь структуру фиксированного размера, поэтому я должен использовать символ XXX[31], но я могу изменить это для любого объявления с фиксированным размером.
заранее спасибо
1 ответ
У вас есть более 10 полей. Вам нужно поднять предел размера последовательности по умолчанию в fusion/container/vector/limits.hpp
,
Включите это перед включением любого заголовка Boost Fusion (или любого заголовка Boost, потому что многие библиотеки могут неявно включать его):
#define FUSION_MAX_VECTOR_SIZE 15