⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 if_p_int_as_condition_test.cpp

📁 C++的一个好库。。。现在很流行
💻 CPP
字号:
/*=============================================================================
    Copyright (c) 2004 Martin Wille
    http://spirit.sourceforge.net/

    Use, modification and distribution is subject to the Boost Software
    License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
    http://www.boost.org/LICENSE_1_0.txt)
=============================================================================*/
#include <boost/spirit/core.hpp>
#include <boost/spirit/dynamic.hpp>
#include <boost/spirit/tree/ast.hpp>
#include <iostream>
#include <boost/detail/lightweight_test.hpp>

using namespace boost::spirit;
int the_var_to_be_tested = 0;

namespace local
{
    template <typename T>
    struct var_wrapper : public ::boost::reference_wrapper<T>
    {
        typedef ::boost::reference_wrapper<T> parent;

        explicit inline var_wrapper(T& t) : parent(t) {}

        inline T& operator()() const { return parent::get(); }
    };

    template <typename T>
    inline var_wrapper<T>
    var(T& t)
    {
        return var_wrapper<T>(t);
    }
}

struct test_grammar : public grammar <test_grammar>
{
    template <typename ScannerT>
    
    struct definition
    {
        rule <ScannerT, parser_tag <0> > test;
        
        definition(const test_grammar& self)
        {
            test
                = if_p(local::var(the_var_to_be_tested))
                    [
                        real_p
                    ];

        }
        const rule <ScannerT, parser_tag<0> >& start() const {return test;}
    };
};

int main()
{
    test_grammar gram;
    tree_parse_info <const char*> result;

    //predictably fails
    the_var_to_be_tested = 0;
    result = ast_parse("1.50", gram, space_p);
    std::cout << "Testing if_p against: " << the_var_to_be_tested << std::endl;
    std::cout << "success: " << result.full << std::endl;
    BOOST_TEST(!result.full);

    //predicatably succeeds
    the_var_to_be_tested = 1;
    result = ast_parse("1.50", gram, space_p);
    std::cout << "Testing if_p against: " << the_var_to_be_tested << std::endl;
    std::cout << "success: " << result.full << std::endl;
    BOOST_TEST(result.full);

    //should succeed
    the_var_to_be_tested = 2;
    result = ast_parse("1.50", gram, space_p);
    std::cout << "Testing if_p against: " << the_var_to_be_tested << std::endl;
    std::cout << "success: " << result.full << std::endl;
    BOOST_TEST(result.full);

    return boost::report_errors();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -