Iterate Over Struct; Easily Display Struct Fields And Values In a RichEdit Box

后端 未结 9 1846
离开以前
离开以前 2020-12-13 22:10

Is there an easier way to display the struct fields and their corresponding values in RichEdit control?

This is what I am doing now:

<
9条回答
  •  醉梦人生
    2020-12-13 22:39

    BOOST_FUSION_ADAPT_STRUCT seems to fit well here. For example:

    // Your existing struct
    struct Foo
    {
        int i;
        bool j;
        char k[100];
    };
    
    // Generate an adapter allowing to view "Foo" as a Boost.Fusion sequence
    BOOST_FUSION_ADAPT_STRUCT(
        Foo,
        (int, i)
        (bool, j)
        (char, k[100])
    )
    
    // The action we will call on each member of Foo
    struct AppendToTextBox
    {
        AppendToTextBox(RichEditControl& Ctrl) : m_Ctrl(Ctrl){}
    
        template
        void operator()(T& t)const
        {
    
            m_Ctrl.Lines.Append(boost::lexical_cast(t));
        }
    
        RichEditControl& m_Ctrl;
    
    };
    
    // Usage:
    void FillTextBox(Foo& F, RichEditControl& Ctrl)
    {
        boost::fusion::for_each(F, AppendToTextBox(Ctrl));
    }
    

提交回复
热议问题