Boost logo

Boost Users :

Subject: Re: [Boost-users] Parsing statements from ascii files in arbitrary order into hierarchies of structs using boost spirit
From: mark pashley (mark.pashley_at_[hidden])
Date: 2011-12-16 04:02:52


I have a second example here , this uses BOOST_FUSION_ADAPT_STRUCT:

http://www.softobjects.co.uk/mediawiki-1.16.0/index.php/Boost::Spirit_JSON_File_Parser

The key bit of interest to you is:

struct JsonMember; ///< Forward declaration of JsonObject

/**
 * Declare JsonValue as a bopost variant.
 *
 * Note: boost::recursive_wrapper is used to allow the JsonValue to
 * include a vector of JsonMember which has been forward
 * declared.
 *
 * Note: boost::make_recursive_variant is used to allow the JsonValue to
 * include a vector of JsonValue.
 *
 */
typedef boost::make_recursive_variant< bool
                      , double
                      , std::string
                      , boost::recursive_wrapper< std::vector< JsonMember >
>
                      , std::vector< boost::recursive_variant_ >
>::type JsonValue;

/**
 * Structure for parse Json members.
 */
struct JsonMember
{
   std::string name; ///< the name of the member
   JsonValue value; ///< the value of the member

   /** constructor */
   JsonMember() : name()
                , value()
   {}

   /**
    * Convenience constructor.
    *
    * \param nm the name of the member.
    * \param val the value for the member.
    */
   JsonMember( const std::string& nm, const JsonValue& val )
       : name(nm)
       , value(val)
    {}
};

/**
 * Adapt the JsonMember structure using boost::fusion so that it can
 * be used with boost::spirit::qi.
 */
BOOST_FUSION_ADAPT_STRUCT( JsonMember, ( std::string, name )
                                                        ( JsonValue, value )
)

in this case there is no inheritance hierarchy, but I believe you should be
able to simply us BOOST_FUSION_ADAPT_STRUCT in the following way:

BOOST_FUSION_ADAPT_STRUCT( DerivedStatement0,
                                                        ( Type_of_member1,
name_of_member1 )
                                                        // ....
                                                        ( Type_of_memberN,
name_of_memberN ) )

--
View this message in context: http://boost.2283326.n4.nabble.com/Parsing-statements-from-ascii-files-in-arbitrary-order-into-hierarchies-of-structs-using-boost-spirit-tp4199965p4203822.html
Sent from the Boost - Users mailing list archive at Nabble.com.

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net