รองรับ BOOST_FUSION_ADAPT_STRUCT สำหรับวัตถุที่มีอาร์เรย์คงที่หรือไม่

สมมติว่าฉันมีโครงสร้างที่มีลักษณะดังนี้:

struct LETTER
{
    double one;
    char[12] two;
    double three;
    char[12] four;
};

และอินพุตของฉันจะคั่นด้วยเครื่องหมายจุลภาค เช่น:

"32,CATSANDDOGS,42,WHAT"
"43,BATANDZEBRAS,23,PARROT"

ฉันพยายามปรับตัวอย่างนี้ (Spirit Qi : กฎสำหรับ char [5]) เพื่อม้วนผ่าน BOOST_FUSION_ADAPT_STRUCT แต่ไม่มีโชคเลย ฉันลองใช้ std::array ดังที่แสดงไว้ที่นี่ (http://www.boost.org/doc/libs/1_64_0/libs/spirit/example/qi/boost_array.cpp) แต่ฉันไม่สามารถทำให้มันทำงานในโครงสร้างได้ สิ่งที่ฉันพยายามทำเป็นไปได้หรือไม่? ฉันทำอะไรผิดที่นี่? ฉันคิดว่านี่จะเป็นกรณีการใช้งานที่ชัดเจนที่สุด

สิ่งที่ฉันพยายามทำเป็นไปได้หรือไม่?


person Carbon    schedule 27.11.2017    source แหล่งที่มา
comment
กรณีการใช้งานที่ชัดเจนที่สุดใช้อาร์เรย์ถ่านขนาดคงที่? นั่นชัดเจนที่สุดในยุค C ของปี 1970 เพียงใช้ std::string หากคุณต้องการกรณีการใช้งานที่ชัดเจน   -  person sehe    schedule 28.11.2017


คำตอบ (1)


ฉันจะสมมติว่าคุณต้องการเขียนโค้ด C++ สำนวน (ซึ่งเห็นได้ชัดว่าเป็นโดเมนเป้าหมายสำหรับ Spirit Qi) เพื่อให้คุณสามารถใช้ std::string:

สดบน Coliru

#include <boost/fusion/adapted.hpp>
#include <boost/fusion/include/io.hpp>
#include <boost/spirit/include/qi.hpp>
#include <iostream>

namespace qi = boost::spirit::qi;

struct Letter {
    double one;
    std::string two;
    double three;
    std::string four;
};

BOOST_FUSION_ADAPT_STRUCT(Letter, one, two, three, four)

template <typename Iterator> struct LETTERParser : qi::grammar<Iterator, Letter()> {
    LETTERParser() : LETTERParser::base_type(start) {
        using namespace qi;

        _11c = repeat(11) [char_];
        start = skip(space) [ "LETTER" >> double_ >> _11c >> double_ >> _11c ];
    }
  private:
    qi::rule<Iterator, Letter()> start;
    qi::rule<Iterator, std::string()> _11c;
};

int main() {
    const std::string input("LETTER 42 12345678901 +Inf abcdefghijk  ");
    using It = std::string::const_iterator;

    LETTERParser<It> parser;
    Letter example;

    It f = input.begin(), l = input.end();

    if (phrase_parse(f, l, parser, qi::ascii::space, example)) {
        std::cout << "parsed: " << boost::fusion::as_vector(example) << "\n";
        std::cout << " example.one: " << example.one << "\n";
        std::cout << " example.two: '" << example.two << "'\n";
        std::cout << " example.three: " << example.three << "\n";
        std::cout << " example.four: '" << example.four << "'\n";
    } else {
        std::cout << "couldn't parse '" << input << "'\n";
    }

    if (f != l)
        std::cout << "Remaining unparsed input: '" << std::string(f,l) << "'\n";
}

พิมพ์

parsed: (42 12345678901 inf abcdefghijk)
 example.one: 42
 example.two: '12345678901'
 example.three: inf
 example.four: 'abcdefghijk'
person sehe    schedule 28.11.2017
comment
โอเค เดาว่าฉันจะให้ทีมทำข้อตกลงกับ std::string สำหรับโครงสร้างข้อมูลนี้ - person Carbon; 28.11.2017