#include #include #include #include #include #include #include #include struct deoptionalize_item { typedef deoptionalize_item Self; template struct result; template struct result& )> { typedef const T& type; }; template const T& operator()( const boost::optional& rhs ) const { return *rhs; } }; template struct range { T min; T max; }; /// /// Represents a range of values [min,max] with an increment of incr /// Helps generate a loop /// for (min;max;incr) /// Dummy is used to normalize range and range_incr with std::vector<> /// for the purposes of template specialization of spirit rules /// template struct range_incr { T min; T max; T incr; }; template struct make_one_or_many { typedef boost::variant< std::vector, range_incr, range, T > type; }; template struct deoptionalize { typedef typename boost::fusion::result_of::transform::type type; }; struct make_1_n_unary_metafct { template struct result; // primary template template struct result // template spec { typedef typename make_one_or_many::type type; }; template typename result::type operator()(Arg); }; template struct make_1_n { typedef typename boost::fusion::result_of::transform< parameters_t, make_1_n_unary_metafct >::type seq_t; typedef typename boost::fusion::result_of::as_vector::type type; }; template struct one_or_many_maybe { typedef boost::optional< typename make_one_or_many::type > type; }; struct make_1_n_maybe_unary_metafct { template struct result; // primary template template struct result // template spec { typedef typename one_or_many_maybe::type type; }; template typename result::type operator()(Arg); }; /// /// Apply the unary metafunction on the struct /// This yields vector< optional>, optional>, ... , optional< variant<> > > /// template struct make_1_n_maybe { typedef typename boost::fusion::result_of::transform< parameters_t, make_1_n_maybe_unary_metafct >::type seq_t; typedef typename boost::fusion::result_of::as_vector::type type; }; namespace my_ns { namespace systems { /// base aggregate for all system parameters struct parameters { double skid; /// fraction [0. : 1.] of loss from trade entry virtual parameters* clone() const; // the caller takes ownership virtual bool compareto(const parameters& rhs) const; virtual ~parameters() {} }; typedef boost::shared_ptr parameters_ptr; typedef boost::shared_ptr parameters_const_ptr; inline parameters* parameters::clone() const { return new parameters(*this); } inline bool parameters::compareto(const parameters& rhs) const { return skid==rhs.skid; } /// == operator inline bool operator==(const parameters& lhs, const parameters& rhs) { return lhs.compareto(rhs); } inline bool operator==(parameters& lhs, parameters& rhs) { return lhs.compareto(rhs); } inline bool operator==(const parameters& lhs, parameters& rhs) { return lhs.compareto(rhs); } inline bool operator==(parameters& lhs, const parameters& rhs) { return lhs.compareto(rhs); } } } namespace my_ns { namespace systems { namespace ns1 { struct parameters : public systems::parameters { ns1::parameters* clone() const; }; inline ns1::parameters* ns1::parameters::clone() const { return new ns1::parameters(*this); } } } } BOOST_FUSION_ADAPT_STRUCT( my_ns::systems::ns1::parameters, (double, skid) ) typedef make_1_n_maybe::type parameters_1_n_maybe_t; typedef deoptionalize::type parameters_1_n_seq_t; int main() { const parameters_1_n_maybe_t y; const parameters_1_n_seq_t z = boost::fusion::transform(y, deoptionalize_item()); const boost::fusion::vector1< boost::variant,range_incr,range,double> >& x = z; return 0; }