Chris@16: /////////////////////////////////////////////////////////////////////////////// Chris@16: /// \file generate.hpp Chris@16: /// Contains definition of generate\<\> class template, which end users can Chris@16: /// specialize for generating domain-specific expression wrappers. Chris@16: // Chris@16: // Copyright 2008 Eric Niebler. Distributed under the Boost Chris@16: // Software License, Version 1.0. (See accompanying file Chris@16: // LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) Chris@16: Chris@16: #ifndef BOOST_PROTO_GENERATE_HPP_EAN_02_13_2007 Chris@16: #define BOOST_PROTO_GENERATE_HPP_EAN_02_13_2007 Chris@16: Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: #include Chris@16: Chris@101: #if defined(_MSC_VER) Chris@16: # pragma warning(push) Chris@16: # pragma warning(disable : 4714) // function 'xxx' marked as __forceinline not inlined Chris@16: #endif Chris@16: Chris@16: namespace boost { namespace proto Chris@16: { Chris@16: Chris@16: namespace detail Chris@16: { Chris@16: template Chris@16: struct by_value_generator_; Chris@16: Chris@16: template Chris@16: struct by_value_generator_, 0> > Chris@16: { Chris@16: typedef Chris@16: proto::expr< Chris@16: Tag Chris@16: , term::value_type> Chris@16: , 0 Chris@16: > Chris@16: type; Chris@16: Chris@16: BOOST_FORCEINLINE Chris@16: static type const call(proto::expr, 0> const &e) Chris@16: { Chris@16: type that = {e.child0}; Chris@16: return that; Chris@16: } Chris@16: }; Chris@16: Chris@16: template Chris@16: struct by_value_generator_, 0> > Chris@16: { Chris@16: typedef Chris@16: proto::basic_expr< Chris@16: Tag Chris@16: , term::value_type> Chris@16: , 0 Chris@16: > Chris@16: type; Chris@16: Chris@16: BOOST_FORCEINLINE Chris@16: static type const call(proto::basic_expr, 0> const &e) Chris@16: { Chris@16: type that = {e.child0}; Chris@16: return that; Chris@16: } Chris@16: }; Chris@16: Chris@16: // Include the other specializations of by_value_generator_ Chris@16: #include Chris@16: } Chris@16: Chris@16: /// \brief Annotate a generator to indicate that it would Chris@16: /// prefer to be passed instances of \c proto::basic_expr\<\> rather Chris@16: /// than \c proto::expr\<\>. use_basic_expr\ is Chris@16: /// itself a generator. Chris@16: /// Chris@16: template Chris@16: struct use_basic_expr Chris@16: : Generator Chris@16: { Chris@16: BOOST_PROTO_USE_BASIC_EXPR() Chris@16: }; Chris@16: Chris@16: /// \brief A simple generator that passes an expression Chris@16: /// through unchanged. Chris@16: /// Chris@16: /// Generators are intended for use as the first template parameter Chris@16: /// to the \c domain\<\> class template and control if and how Chris@16: /// expressions within that domain are to be customized. Chris@16: /// The \c default_generator makes no modifications to the expressions Chris@16: /// passed to it. Chris@16: struct default_generator Chris@16: { Chris@16: BOOST_PROTO_CALLABLE() Chris@16: Chris@16: template Chris@16: struct result; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Expr type; Chris@16: }; Chris@16: Chris@16: /// \param expr A Proto expression Chris@16: /// \return expr Chris@16: template Chris@16: BOOST_FORCEINLINE Chris@16: BOOST_PROTO_RETURN_TYPE_STRICT_LOOSE(Expr, Expr const &) Chris@16: operator ()(Expr const &e) const Chris@16: { Chris@16: return e; Chris@16: } Chris@16: }; Chris@16: Chris@16: /// \brief A simple generator that passes an expression Chris@16: /// through unchanged and specifies a preference for Chris@16: /// \c proto::basic_expr\<\> over \c proto::expr\<\>. Chris@16: /// Chris@16: /// Generators are intended for use as the first template parameter Chris@16: /// to the \c domain\<\> class template and control if and how Chris@16: /// expressions within that domain are to be customized. Chris@16: /// The \c default_generator makes no modifications to the expressions Chris@16: /// passed to it. Chris@16: struct basic_default_generator Chris@16: : proto::use_basic_expr Chris@16: {}; Chris@16: Chris@16: /// \brief A generator that wraps expressions passed Chris@16: /// to it in the specified extension wrapper. Chris@16: /// Chris@16: /// Generators are intended for use as the first template parameter Chris@16: /// to the \c domain\<\> class template and control if and how Chris@16: /// expressions within that domain are to be customized. Chris@16: /// \c generator\<\> wraps each expression passed to it in Chris@16: /// the \c Extends\<\> wrapper. Chris@16: template class Extends> Chris@16: struct generator Chris@16: { Chris@16: BOOST_PROTO_CALLABLE() Chris@16: BOOST_PROTO_USE_BASIC_EXPR() Chris@16: Chris@16: template Chris@16: struct result; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Extends type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Extends type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Extends type; Chris@16: }; Chris@16: Chris@16: /// \param expr A Proto expression Chris@16: /// \return Extends(expr) Chris@16: template Chris@16: BOOST_FORCEINLINE Chris@16: Extends operator ()(Expr const &e) const Chris@16: { Chris@16: return Extends(e); Chris@16: } Chris@16: }; Chris@16: Chris@16: /// \brief A generator that wraps expressions passed Chris@16: /// to it in the specified extension wrapper and uses Chris@16: /// aggregate initialization for the wrapper. Chris@16: /// Chris@16: /// Generators are intended for use as the first template parameter Chris@16: /// to the \c domain\<\> class template and control if and how Chris@16: /// expressions within that domain are to be customized. Chris@16: /// \c pod_generator\<\> wraps each expression passed to it in Chris@16: /// the \c Extends\<\> wrapper, and uses aggregate initialzation Chris@16: /// for the wrapped object. Chris@16: template class Extends> Chris@16: struct pod_generator Chris@16: { Chris@16: BOOST_PROTO_CALLABLE() Chris@16: BOOST_PROTO_USE_BASIC_EXPR() Chris@16: Chris@16: template Chris@16: struct result; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Extends type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Extends type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Extends type; Chris@16: }; Chris@16: Chris@16: /// \param expr The expression to wrap Chris@16: /// \return Extends\ that = {expr}; return that; Chris@16: template Chris@16: BOOST_FORCEINLINE Chris@16: Extends operator ()(Expr const &e) const Chris@16: { Chris@16: Extends that = {e}; Chris@16: return that; Chris@16: } Chris@16: Chris@16: // Work-around for: Chris@16: // https://connect.microsoft.com/VisualStudio/feedback/details/765449/codegen-stack-corruption-using-runtime-checks-when-aggregate-initializing-struct Chris@16: #if BOOST_WORKAROUND(BOOST_MSVC, BOOST_TESTED_AT(1700)) Chris@16: template Chris@16: BOOST_FORCEINLINE Chris@16: Extends > > operator ()(expr > const &e) const Chris@16: { Chris@16: Extends > > that; Chris@16: proto::value(that.proto_expr_) = proto::value(e); Chris@16: return that; Chris@16: } Chris@16: Chris@16: template Chris@16: BOOST_FORCEINLINE Chris@16: Extends > > operator ()(basic_expr > const &e) const Chris@16: { Chris@16: Extends > > that; Chris@16: proto::value(that.proto_expr_) = proto::value(e); Chris@16: return that; Chris@16: } Chris@16: #endif Chris@16: }; Chris@16: Chris@16: /// \brief A generator that replaces child nodes held by Chris@16: /// reference with ones held by value. Use with Chris@16: /// \c compose_generators to forward that result to another Chris@16: /// generator. Chris@16: /// Chris@16: /// Generators are intended for use as the first template parameter Chris@16: /// to the \c domain\<\> class template and control if and how Chris@16: /// expressions within that domain are to be customized. Chris@16: /// \c by_value_generator ensures all child nodes are Chris@16: /// held by value. This generator is typically composed with a Chris@16: /// second generator for further processing, as Chris@16: /// compose_generators\. Chris@16: struct by_value_generator Chris@16: { Chris@16: BOOST_PROTO_CALLABLE() Chris@16: Chris@16: template Chris@16: struct result; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Chris@16: typename detail::by_value_generator_::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Chris@16: typename detail::by_value_generator_::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Chris@16: typename detail::by_value_generator_::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: /// \param expr The expression to modify. Chris@16: /// \return deep_copy(expr) Chris@16: template Chris@16: BOOST_FORCEINLINE Chris@16: typename result::type operator ()(Expr const &e) const Chris@16: { Chris@16: return detail::by_value_generator_::call(e); Chris@16: } Chris@16: }; Chris@16: Chris@16: /// \brief A composite generator that first applies one Chris@16: /// transform to an expression and then forwards the result Chris@16: /// on to another generator for further transformation. Chris@16: /// Chris@16: /// Generators are intended for use as the first template parameter Chris@16: /// to the \c domain\<\> class template and control if and how Chris@16: /// expressions within that domain are to be customized. Chris@16: /// \c compose_generators\<\> is a composite generator that first Chris@16: /// applies one transform to an expression and then forwards the Chris@16: /// result on to another generator for further transformation. Chris@16: template Chris@16: struct compose_generators Chris@16: { Chris@16: BOOST_PROTO_CALLABLE() Chris@16: Chris@16: template Chris@16: struct result; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Chris@16: typename Second::template result< Chris@16: Second(typename First::template result::type) Chris@16: >::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Chris@16: typename Second::template result< Chris@16: Second(typename First::template result::type) Chris@16: >::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct result Chris@16: { Chris@16: typedef Chris@16: typename Second::template result< Chris@16: Second(typename First::template result::type) Chris@16: >::type Chris@16: type; Chris@16: }; Chris@16: Chris@16: /// \param expr The expression to modify. Chris@16: /// \return Second()(First()(expr)) Chris@16: template Chris@16: BOOST_FORCEINLINE Chris@16: typename result::type operator ()(Expr const &e) const Chris@16: { Chris@16: return Second()(First()(e)); Chris@16: } Chris@16: }; Chris@16: Chris@16: /// \brief Tests a generator to see whether it would prefer Chris@16: /// to be passed instances of \c proto::basic_expr\<\> rather than Chris@16: /// \c proto::expr\<\>. Chris@16: /// Chris@16: template Chris@16: struct wants_basic_expr Chris@16: : mpl::false_ Chris@16: {}; Chris@16: Chris@16: template Chris@16: struct wants_basic_expr Chris@16: : mpl::true_ Chris@16: {}; Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: template<> Chris@16: struct is_callable Chris@16: : mpl::true_ Chris@16: {}; Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: template class Extends> Chris@16: struct is_callable > Chris@16: : mpl::true_ Chris@16: {}; Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: template class Extends> Chris@16: struct is_callable > Chris@16: : mpl::true_ Chris@16: {}; Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: template<> Chris@16: struct is_callable Chris@16: : mpl::true_ Chris@16: {}; Chris@16: Chris@16: /// INTERNAL ONLY Chris@16: template Chris@16: struct is_callable > Chris@16: : mpl::true_ Chris@16: {}; Chris@16: Chris@16: }} Chris@16: Chris@16: // Specializations of boost::result_of and boost::tr1_result_of to eliminate Chris@16: // some unnecessary template instantiations Chris@16: namespace boost Chris@16: { Chris@16: template Chris@16: struct result_of Chris@16: { Chris@16: typedef Expr type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct result_of Chris@16: { Chris@16: typedef Expr type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct result_of Chris@16: { Chris@16: typedef Expr type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct result_of Chris@16: { Chris@16: typedef Expr type; Chris@16: }; Chris@16: Chris@16: #if BOOST_VERSION >= 104400 Chris@16: template Chris@16: struct tr1_result_of Chris@16: { Chris@16: typedef Expr type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct tr1_result_of Chris@16: { Chris@16: typedef Expr type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct tr1_result_of Chris@16: { Chris@16: typedef Expr type; Chris@16: }; Chris@16: Chris@16: template Chris@16: struct tr1_result_of Chris@16: { Chris@16: typedef Expr type; Chris@16: }; Chris@16: #endif Chris@16: } Chris@16: Chris@101: #if defined(_MSC_VER) Chris@16: # pragma warning(pop) Chris@16: #endif Chris@16: Chris@16: #endif // BOOST_PROTO_GENERATE_HPP_EAN_02_13_2007