Boost  v1.57.0
doxygen for www.boost.org
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
boost::proto::context::callable_context< Context, DefaultCtx > Struct Template Reference

An evaluation context adaptor that makes authoring a context a simple matter of writing function overloads, rather then writing template specializations. More...

#include <proto_fwd.hpp>

Classes

struct  eval
 A BinaryFunction that accepts an Expr and a Context, and either fans out the expression and passes it to the context, or else hands off the expression to DefaultCtx. More...
 

Detailed Description

template<typename Context, typename DefaultCtx>
struct boost::proto::context::callable_context< Context, DefaultCtx >

An evaluation context adaptor that makes authoring a context a simple matter of writing function overloads, rather then writing template specializations.

callable_context<> is a base class that implements the context protocol by passing fanned-out expression nodes to the derived context, making it easy to customize the handling of expression types by writing function overloads. Only those expression types needing special handling require explicit handling. All others are dispatched to a user-specified default context, DefaultCtx.

callable_context<> is defined simply as:

template<typename Context, typename DefaultCtx = default_context>
struct callable_context
{
template<typename Expr, typename ThisContext = Context>
struct eval
is_expr_handled_<Expr, Context> // For exposition
, callable_eval<Expr, ThisContext>
, typename DefaultCtx::template eval<Expr, Context>
{};
};

The Boolean metafunction is_expr_handled_<> uses metaprogramming tricks to determine whether Context has an overloaded function call operator that accepts the fanned-out constituents of an expression of type Expr. If so, the handling of the expression is dispatched to callable_eval<>. If not, it is dispatched to the user-specified DefaultCtx.

Below is an example of how to use callable_context<>:

// An evaluation context that increments all
// integer terminals in-place.
struct increment_ints
: callable_context<
increment_ints const // derived context
, null_context const // fall-back context
>
{
typedef void result_type;
// Handle int terminals here:
void operator()(proto::tag::terminal, int &i) const
{
++i;
}
};

With increment_ints, we can do the following:

literal<int> i = 0, j = 10;
proto::eval( i - j * 3.14, increment_ints() );
assert( i.get() == 1 && j.get() == 11 );

The documentation for this struct was generated from the following file: