Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2025-01-18 09:39:09

0001 /*=============================================================================
0002     Adaptable closures
0003 
0004     Phoenix V0.9
0005     Copyright (c) 2001-2002 Joel de Guzman
0006 
0007     Distributed under the Boost Software License, Version 1.0. (See
0008     accompanying file LICENSE_1_0.txt or copy at
0009     http://www.boost.org/LICENSE_1_0.txt)
0010 
0011     URL: http://spirit.sourceforge.net/
0012 
0013 ==============================================================================*/
0014 #ifndef BOOST_LAMBDA_CLOSURES_HPP
0015 #define BOOST_LAMBDA_CLOSURES_HPP
0016 
0017 ///////////////////////////////////////////////////////////////////////////////
0018 #include "boost/lambda/core.hpp"
0019 ///////////////////////////////////////////////////////////////////////////////
0020 namespace boost {
0021 namespace lambda {
0022 
0023 ///////////////////////////////////////////////////////////////////////////////
0024 //
0025 //  Adaptable closures
0026 //
0027 //      The framework will not be complete without some form of closures
0028 //      support. Closures encapsulate a stack frame where local
0029 //      variables are created upon entering a function and destructed
0030 //      upon exiting. Closures provide an environment for local
0031 //      variables to reside. Closures can hold heterogeneous types.
0032 //
0033 //      Phoenix closures are true hardware stack based closures. At the
0034 //      very least, closures enable true reentrancy in lambda functions.
0035 //      A closure provides access to a function stack frame where local
0036 //      variables reside. Modeled after Pascal nested stack frames,
0037 //      closures can be nested just like nested functions where code in
0038 //      inner closures may access local variables from in-scope outer
0039 //      closures (accessing inner scopes from outer scopes is an error
0040 //      and will cause a run-time assertion failure).
0041 //
0042 //      There are three (3) interacting classes:
0043 //
0044 //      1) closure:
0045 //
0046 //      At the point of declaration, a closure does not yet create a
0047 //      stack frame nor instantiate any variables. A closure declaration
0048 //      declares the types and names[note] of the local variables. The
0049 //      closure class is meant to be subclassed. It is the
0050 //      responsibility of a closure subclass to supply the names for
0051 //      each of the local variable in the closure. Example:
0052 //
0053 //          struct my_closure : closure<int, string, double> {
0054 //
0055 //              member1 num;        // names the 1st (int) local variable
0056 //              member2 message;    // names the 2nd (string) local variable
0057 //              member3 real;       // names the 3rd (double) local variable
0058 //          };
0059 //
0060 //          my_closure clos;
0061 //
0062 //      Now that we have a closure 'clos', its local variables can be
0063 //      accessed lazily using the dot notation. Each qualified local
0064 //      variable can be used just like any primitive actor (see
0065 //      primitives.hpp). Examples:
0066 //
0067 //          clos.num = 30
0068 //          clos.message = arg1
0069 //          clos.real = clos.num * 1e6
0070 //
0071 //      The examples above are lazily evaluated. As usual, these
0072 //      expressions return composite actors that will be evaluated
0073 //      through a second function call invocation (see operators.hpp).
0074 //      Each of the members (clos.xxx) is an actor. As such, applying
0075 //      the operator() will reveal its identity:
0076 //
0077 //          clos.num() // will return the current value of clos.num
0078 //
0079 //      *** [note] Acknowledgement: Juan Carlos Arevalo-Baeza (JCAB)
0080 //      introduced and initilally implemented the closure member names
0081 //      that uses the dot notation.
0082 //
0083 //      2) closure_member
0084 //
0085 //      The named local variables of closure 'clos' above are actually
0086 //      closure members. The closure_member class is an actor and
0087 //      conforms to its conceptual interface. member1..memberN are
0088 //      predefined typedefs that correspond to each of the listed types
0089 //      in the closure template parameters.
0090 //
0091 //      3) closure_frame
0092 //
0093 //      When a closure member is finally evaluated, it should refer to
0094 //      an actual instance of the variable in the hardware stack.
0095 //      Without doing so, the process is not complete and the evaluated
0096 //      member will result to an assertion failure. Remember that the
0097 //      closure is just a declaration. The local variables that a
0098 //      closure refers to must still be instantiated.
0099 //
0100 //      The closure_frame class does the actual instantiation of the
0101 //      local variables and links these variables with the closure and
0102 //      all its members. There can be multiple instances of
0103 //      closure_frames typically situated in the stack inside a
0104 //      function. Each closure_frame instance initiates a stack frame
0105 //      with a new set of closure local variables. Example:
0106 //
0107 //          void foo()
0108 //          {
0109 //              closure_frame<my_closure> frame(clos);
0110 //              /* do something */
0111 //          }
0112 //
0113 //      where 'clos' is an instance of our closure 'my_closure' above.
0114 //      Take note that the usage above precludes locally declared
0115 //      classes. If my_closure is a locally declared type, we can still
0116 //      use its self_type as a paramater to closure_frame:
0117 //
0118 //          closure_frame<my_closure::self_type> frame(clos);
0119 //
0120 //      Upon instantiation, the closure_frame links the local variables
0121 //      to the closure. The previous link to another closure_frame
0122 //      instance created before is saved. Upon destruction, the
0123 //      closure_frame unlinks itself from the closure and relinks the
0124 //      preceding closure_frame prior to this instance.
0125 //
0126 //      The local variables in the closure 'clos' above is default
0127 //      constructed in the stack inside function 'foo'. Once 'foo' is
0128 //      exited, all of these local variables are destructed. In some
0129 //      cases, default construction is not desirable and we need to
0130 //      initialize the local closure variables with some values. This
0131 //      can be done by passing in the initializers in a compatible
0132 //      tuple. A compatible tuple is one with the same number of
0133 //      elements as the destination and where each element from the
0134 //      destination can be constructed from each corresponding element
0135 //      in the source. Example:
0136 //
0137 //          tuple<int, char const*, int> init(123, "Hello", 1000);
0138 //          closure_frame<my_closure> frame(clos, init);
0139 //
0140 //      Here now, our closure_frame's variables are initialized with
0141 //      int: 123, char const*: "Hello" and int: 1000.
0142 //
0143 ///////////////////////////////////////////////////////////////////////////////
0144 
0145 
0146 
0147 ///////////////////////////////////////////////////////////////////////////////
0148 //
0149 //  closure_frame class
0150 //
0151 ///////////////////////////////////////////////////////////////////////////////
0152 template <typename ClosureT>
0153 class closure_frame : public ClosureT::tuple_t {
0154 
0155 public:
0156 
0157     closure_frame(ClosureT& clos)
0158     : ClosureT::tuple_t(), save(clos.frame), frame(clos.frame)
0159     { clos.frame = this; }
0160 
0161     template <typename TupleT>
0162     closure_frame(ClosureT& clos, TupleT const& init)
0163     : ClosureT::tuple_t(init), save(clos.frame), frame(clos.frame)
0164     { clos.frame = this; }
0165 
0166     ~closure_frame()
0167     { frame = save; }
0168 
0169 private:
0170 
0171     closure_frame(closure_frame const&);            // no copy
0172     closure_frame& operator=(closure_frame const&); // no assign
0173 
0174     closure_frame* save;
0175     closure_frame*& frame;
0176 };
0177 
0178 ///////////////////////////////////////////////////////////////////////////////
0179 //
0180 //  closure_member class
0181 //
0182 ///////////////////////////////////////////////////////////////////////////////
0183 template <int N, typename ClosureT>
0184 class closure_member {
0185 
0186 public:
0187 
0188     typedef typename ClosureT::tuple_t tuple_t;
0189 
0190     closure_member()
0191     : frame(ClosureT::closure_frame_ref()) {}
0192 
0193     template <typename TupleT>
0194     struct sig {
0195 
0196         typedef typename detail::tuple_element_as_reference<
0197             N, typename ClosureT::tuple_t
0198         >::type type;
0199     };
0200 
0201     template <class Ret, class A, class B, class C>
0202     //    typename detail::tuple_element_as_reference
0203     //        <N, typename ClosureT::tuple_t>::type
0204     Ret
0205     call(A&, B&, C&) const
0206     {
0207         assert(frame);
0208         return boost::tuples::get<N>(*frame);
0209     }
0210 
0211 
0212 private:
0213 
0214     typename ClosureT::closure_frame_t*& frame;
0215 };
0216 
0217 ///////////////////////////////////////////////////////////////////////////////
0218 //
0219 //  closure class
0220 //
0221 ///////////////////////////////////////////////////////////////////////////////
0222 template <
0223     typename T0 = null_type,
0224     typename T1 = null_type,
0225     typename T2 = null_type,
0226     typename T3 = null_type,
0227     typename T4 = null_type
0228 >
0229 class closure {
0230 
0231 public:
0232 
0233     typedef tuple<T0, T1, T2, T3, T4> tuple_t;
0234     typedef closure<T0, T1, T2, T3, T4> self_t;
0235     typedef closure_frame<self_t> closure_frame_t;
0236 
0237                             closure()
0238                             : frame(0)      { closure_frame_ref(&frame); }
0239     closure_frame_t&        context()       { assert(frame); return frame; }
0240     closure_frame_t const&  context() const { assert(frame); return frame; }
0241 
0242     typedef lambda_functor<closure_member<0, self_t> > member1;
0243     typedef lambda_functor<closure_member<1, self_t> > member2;
0244     typedef lambda_functor<closure_member<2, self_t> > member3;
0245     typedef lambda_functor<closure_member<3, self_t> > member4;
0246     typedef lambda_functor<closure_member<4, self_t> > member5;
0247 
0248 private:
0249 
0250     closure(closure const&);            // no copy
0251     closure& operator=(closure const&); // no assign
0252 
0253     template <int N, typename ClosureT>
0254     friend class closure_member;
0255 
0256     template <typename ClosureT>
0257     friend class closure_frame;
0258 
0259     static closure_frame_t*&
0260     closure_frame_ref(closure_frame_t** frame_ = 0)
0261     {
0262         static closure_frame_t** frame = 0;
0263         if (frame_ != 0)
0264             frame = frame_;
0265         return *frame;
0266     }
0267 
0268     closure_frame_t* frame;
0269 };
0270 
0271 }}
0272    //  namespace 
0273 
0274 #endif