/*============================================================================= Copyright (c) 2001-2007 Joel de Guzman Distributed under 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 #include #include namespace proto = boost::proto; namespace phoenix = boost::phoenix; template struct identity_transform; struct identity_actions { template struct when : phoenix::call > {}; }; template <> struct identity_actions::when : proto::call< identity_transform(proto::_value, phoenix::_context) > {}; template <> struct identity_actions::when : proto::call< identity_transform(proto::_value, phoenix::_context) > {}; template <> struct identity_actions::when : proto::lazy< identity_transform(proto::_value, phoenix::_context) > {}; template <> struct identity_transform { typedef std::string result_type; template std::string operator()(Terminal const & terminal, Context) const { std::stringstream ss; ss << "val(" << terminal << ")"; return ss.str(); } template std::string operator()(char const * terminal, Context) const { std::stringstream ss; ss << "val(\"" << terminal << "\")"; return ss.str(); } }; template struct identity_transform > { typedef std::string result_type; template std::string operator()(Terminal const & terminal, Context) const { std::stringstream ss; ss << "ref(" << terminal << ")"; return ss.str(); } template std::string operator()(boost::reference_wrapper terminal, Context) const { std::stringstream ss; ss << "ref(\"" << terminal << "\")"; return ss.str(); } template std::string operator()(boost::reference_wrapper terminal, Context) const { std::stringstream ss; ss << "ref(\"" << terminal << "\")"; return ss.str(); } }; template <> struct identity_transform { typedef std::string result_type; template std::string operator()(N, Context) const { std::stringstream ss; ss << "_" << N::value; return ss.str(); } }; template void identity(Expr const & expr) { std::cout << phoenix::eval(expr, phoenix::context(int(), identity_actions())) << "\n"; } int main() { identity(phoenix::val(8)); identity(phoenix::val("8")); identity(phoenix::ref("blubb")); identity(phoenix::arg_names::_1); }