#include #include #include #include #include #include #include #include using namespace std; typedef boost::any attribute_value; typedef map< string, attribute_value > attribute_values_view; namespace tag { template struct attr { typedef T value_type; char const* const name; }; } struct get_attr { template struct result; template struct result { typedef boost::optional type; }; template boost::optional operator()(tag::attr const& a, Env const& env) const { attribute_values_view const& view = ::boost::fusion::at_c<0>(env.args()); attribute_values_view::const_iterator it(view.find(a.name)); if (it != view.end()) if (T const* p = boost::any_cast(&it->second)) return *p; return boost::optional(); } }; BOOST_PHOENIX_DEFINE_CUSTOM_TERMINAL( template , ::tag::attr , mpl::false_ , get_attr(proto::_, functional::env(proto::_state)) ) struct get_value_or_impl { template struct result; template struct result, A)> { typedef T const& type; }; template T const& operator()(boost::optional const& o, A const& val) const { return o.get_value_or(val); } }; template< typename DescriptorT > struct attribute_keyword : ::boost::phoenix::actor { typedef attribute_keyword this_type; template typename boost::phoenix::detail::expression::function_eval ::type or_default(T const& val) const { return boost::phoenix::detail::expression::function_eval ::make( get_value_or_impl(), *this, val); } }; #define BOOST_LOG_REGISTER_ATTRIBUTE( string, name, type ) \ ::boost::phoenix::actor< ::tag::attr > name##_base = {{string}};\ attribute_keyword< ::tag::attr >& name\ = static_cast >&>(name##_base); typedef boost::function< bool (attribute_values_view const&) > filter; enum my_severity { info, warning, error }; BOOST_LOG_REGISTER_ATTRIBUTE("Severity", severity, my_severity) int main(int argc, char *argv[]) { { filter flt = severity >= warning; attribute_values_view a; a["Severity"] = error; cout << flt(a) << endl; } cout << "-----------------\n"; { attribute_values_view const a; cout << severity.or_default(info)(a) << endl; } return EXIT_SUCCESS; }