First public contribution.
1 /*=============================================================================
3 Copyright (c) 2001-2002 Joel de Guzman
5 Use, modification and distribution is subject to the Boost Software
6 License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
7 http://www.boost.org/LICENSE_1_0.txt)
8 ==============================================================================*/
9 #ifndef PHOENIX_PRIMITIVES_HPP
10 #define PHOENIX_PRIMITIVES_HPP
12 ///////////////////////////////////////////////////////////////////////////////
13 #include <boost/spirit/phoenix/actor.hpp>
15 ///////////////////////////////////////////////////////////////////////////////
18 ///////////////////////////////////////////////////////////////////////////////
24 // An actor base class that extracts and returns the Nth argument
25 // from the argument list passed in the 'args' tuple in the eval
26 // member function (see actor.hpp). There are some predefined
27 // argument constants that can be used as actors (arg1..argN).
29 // The argument actor is a place-holder for the actual arguments
30 // passed by the client. For example, wherever arg1 is seen placed
31 // in a lazy function (see functions.hpp) or lazy operator (see
32 // operators.hpp), this will be replaced by the actual first
33 // argument in the actual function evaluation. Argument actors are
34 // essentially lazy arguments. A lazy argument is a full actor in
35 // its own right and can be evaluated through the actor's operator().
41 // const char* s = "Hello World";
43 // cout << arg1(c) << ' ';
44 // cout << arg1(i, s) << ' ';
45 // cout << arg2(i, s) << ' ';
47 // will print out "A 123 Hello World"
49 ///////////////////////////////////////////////////////////////////////////////
53 template <typename TupleT>
54 struct result { typedef typename tuple_element<N, TupleT>::type type; };
56 template <typename TupleT>
57 typename tuple_element<N, TupleT>::type
58 eval(TupleT const& args) const
60 return args[tuple_index<N>()];
64 //////////////////////////////////
65 actor<argument<0> > const arg1 = argument<0>();
66 actor<argument<1> > const arg2 = argument<1>();
67 actor<argument<2> > const arg3 = argument<2>();
70 actor<argument<3> > const arg4 = argument<3>();
71 actor<argument<4> > const arg5 = argument<4>();
72 actor<argument<5> > const arg6 = argument<5>();
75 actor<argument<6> > const arg7 = argument<6>();
76 actor<argument<7> > const arg8 = argument<7>();
77 actor<argument<8> > const arg9 = argument<8>();
80 actor<argument<9> > const arg10 = argument<9>();
81 actor<argument<10> > const arg11 = argument<10>();
82 actor<argument<11> > const arg12 = argument<11>();
84 #if PHOENIX_LIMIT > 12
85 actor<argument<12> > const arg13 = argument<12>();
86 actor<argument<13> > const arg14 = argument<13>();
87 actor<argument<14> > const arg15 = argument<14>();
93 ///////////////////////////////////////////////////////////////////////////////
99 // A bound actual parameter is kept in a value class for deferred
100 // access later when needed. A value object is immutable. Value
101 // objects are typically created through the val(x) free function
102 // which returns a value<T> with T deduced from the type of x. x is
103 // held in the value<T> object by value.
105 // Lazy values are actors. As such, lazy values can be evaluated
106 // through the actor's operator(). Such invocation gives the value's
107 // identity. Example:
109 // cout << val(3)() << val("Hello World")();
111 // prints out "3 Hello World"
113 ///////////////////////////////////////////////////////////////////////////////
114 template <typename T>
117 typedef typename boost::remove_reference<T>::type plain_t;
119 template <typename TupleT>
120 struct result { typedef plain_t const type; };
125 template <typename TupleT>
127 eval(TupleT const& /*args*/) const
135 //////////////////////////////////
136 template <typename T>
137 inline actor<value<T> > const
143 //////////////////////////////////
144 template <typename BaseT>
146 val(actor<BaseT> const& v); // This is undefined and not allowed.
148 ///////////////////////////////////////////////////////////////////////////
150 // Arbitrary types T are typically converted to a actor<value<T> >
151 // (see as_actor<T> in actor.hpp). A specialization is also provided
152 // for arrays. T[N] arrays are converted to actor<value<T const*> >.
154 ///////////////////////////////////////////////////////////////////////////
155 template <typename T>
158 typedef actor<value<T> > type;
159 static type convert(T const& x)
160 { return value<T>(x); }
163 //////////////////////////////////
164 template <typename T, int N>
165 struct as_actor<T[N]> {
167 typedef actor<value<T const*> > type;
168 static type convert(T const x[N])
169 { return value<T const*>(x); }
172 ///////////////////////////////////////////////////////////////////////////////
178 // A bound actual parameter may also be held by non-const reference
179 // in a variable class for deferred access later when needed. A
180 // variable object is mutable, i.e. its referenced variable can be
181 // modified. Variable objects are typically created through the
182 // var(x) free function which returns a variable<T> with T deduced
183 // from the type of x. x is held in the value<T> object by
186 // Lazy variables are actors. As such, lazy variables can be
187 // evaluated through the actor's operator(). Such invocation gives
188 // the variables's identity. Example:
191 // char const* s = "Hello World";
192 // cout << var(i)() << var(s)();
194 // prints out "3 Hello World"
196 // Another free function const_(x) may also be used. const_(x) creates
197 // a variable<T const&> object using a constant reference.
199 ///////////////////////////////////////////////////////////////////////////////
200 template <typename T>
203 template <typename TupleT>
204 struct result { typedef T& type; };
209 template <typename TupleT>
211 eval(TupleT const& /*args*/) const
219 //////////////////////////////////
220 template <typename T>
221 inline actor<variable<T> > const
224 return variable<T>(v);
227 //////////////////////////////////
228 template <typename T>
229 inline actor<variable<T const> > const
232 return variable<T const>(v);
235 //////////////////////////////////
236 template <typename BaseT>
238 var(actor<BaseT> const& v); // This is undefined and not allowed.
240 //////////////////////////////////
241 template <typename BaseT>
243 const_(actor<BaseT> const& v); // This is undefined and not allowed.
245 ///////////////////////////////////////////////////////////////////////////////
246 } // namespace phoenix