sl@0: /*============================================================================= sl@0: Phoenix V1.2.1 sl@0: Copyright (c) 2001-2002 Joel de Guzman sl@0: MT code Copyright (c) 2002-2003 Martin Wille sl@0: sl@0: Use, modification and distribution is subject to the Boost Software sl@0: License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at sl@0: http://www.boost.org/LICENSE_1_0.txt) sl@0: ==============================================================================*/ sl@0: #ifndef PHOENIX_CLOSURES_HPP sl@0: #define PHOENIX_CLOSURES_HPP sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: #include sl@0: #include sl@0: sl@0: #ifdef PHOENIX_THREADSAFE sl@0: #include sl@0: #include sl@0: #endif sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: namespace phoenix { sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // sl@0: // Adaptable closures sl@0: // sl@0: // The framework will not be complete without some form of closures sl@0: // support. Closures encapsulate a stack frame where local sl@0: // variables are created upon entering a function and destructed sl@0: // upon exiting. Closures provide an environment for local sl@0: // variables to reside. Closures can hold heterogeneous types. sl@0: // sl@0: // Phoenix closures are true hardware stack based closures. At the sl@0: // very least, closures enable true reentrancy in lambda functions. sl@0: // A closure provides access to a function stack frame where local sl@0: // variables reside. Modeled after Pascal nested stack frames, sl@0: // closures can be nested just like nested functions where code in sl@0: // inner closures may access local variables from in-scope outer sl@0: // closures (accessing inner scopes from outer scopes is an error sl@0: // and will cause a run-time assertion failure). sl@0: // sl@0: // There are three (3) interacting classes: sl@0: // sl@0: // 1) closure: sl@0: // sl@0: // At the point of declaration, a closure does not yet create a sl@0: // stack frame nor instantiate any variables. A closure declaration sl@0: // declares the types and names[note] of the local variables. The sl@0: // closure class is meant to be subclassed. It is the sl@0: // responsibility of a closure subclass to supply the names for sl@0: // each of the local variable in the closure. Example: sl@0: // sl@0: // struct my_closure : closure { sl@0: // sl@0: // member1 num; // names the 1st (int) local variable sl@0: // member2 message; // names the 2nd (string) local variable sl@0: // member3 real; // names the 3rd (double) local variable sl@0: // }; sl@0: // sl@0: // my_closure clos; sl@0: // sl@0: // Now that we have a closure 'clos', its local variables can be sl@0: // accessed lazily using the dot notation. Each qualified local sl@0: // variable can be used just like any primitive actor (see sl@0: // primitives.hpp). Examples: sl@0: // sl@0: // clos.num = 30 sl@0: // clos.message = arg1 sl@0: // clos.real = clos.num * 1e6 sl@0: // sl@0: // The examples above are lazily evaluated. As usual, these sl@0: // expressions return composite actors that will be evaluated sl@0: // through a second function call invocation (see operators.hpp). sl@0: // Each of the members (clos.xxx) is an actor. As such, applying sl@0: // the operator() will reveal its identity: sl@0: // sl@0: // clos.num() // will return the current value of clos.num sl@0: // sl@0: // *** [note] Acknowledgement: Juan Carlos Arevalo-Baeza (JCAB) sl@0: // introduced and initilally implemented the closure member names sl@0: // that uses the dot notation. sl@0: // sl@0: // 2) closure_member sl@0: // sl@0: // The named local variables of closure 'clos' above are actually sl@0: // closure members. The closure_member class is an actor and sl@0: // conforms to its conceptual interface. member1..memberN are sl@0: // predefined typedefs that correspond to each of the listed types sl@0: // in the closure template parameters. sl@0: // sl@0: // 3) closure_frame sl@0: // sl@0: // When a closure member is finally evaluated, it should refer to sl@0: // an actual instance of the variable in the hardware stack. sl@0: // Without doing so, the process is not complete and the evaluated sl@0: // member will result to an assertion failure. Remember that the sl@0: // closure is just a declaration. The local variables that a sl@0: // closure refers to must still be instantiated. sl@0: // sl@0: // The closure_frame class does the actual instantiation of the sl@0: // local variables and links these variables with the closure and sl@0: // all its members. There can be multiple instances of sl@0: // closure_frames typically situated in the stack inside a sl@0: // function. Each closure_frame instance initiates a stack frame sl@0: // with a new set of closure local variables. Example: sl@0: // sl@0: // void foo() sl@0: // { sl@0: // closure_frame frame(clos); sl@0: // /* do something */ sl@0: // } sl@0: // sl@0: // where 'clos' is an instance of our closure 'my_closure' above. sl@0: // Take note that the usage above precludes locally declared sl@0: // classes. If my_closure is a locally declared type, we can still sl@0: // use its self_type as a paramater to closure_frame: sl@0: // sl@0: // closure_frame frame(clos); sl@0: // sl@0: // Upon instantiation, the closure_frame links the local variables sl@0: // to the closure. The previous link to another closure_frame sl@0: // instance created before is saved. Upon destruction, the sl@0: // closure_frame unlinks itself from the closure and relinks the sl@0: // preceding closure_frame prior to this instance. sl@0: // sl@0: // The local variables in the closure 'clos' above is default sl@0: // constructed in the stack inside function 'foo'. Once 'foo' is sl@0: // exited, all of these local variables are destructed. In some sl@0: // cases, default construction is not desirable and we need to sl@0: // initialize the local closure variables with some values. This sl@0: // can be done by passing in the initializers in a compatible sl@0: // tuple. A compatible tuple is one with the same number of sl@0: // elements as the destination and where each element from the sl@0: // destination can be constructed from each corresponding element sl@0: // in the source. Example: sl@0: // sl@0: // tuple init(123, "Hello", 1000); sl@0: // closure_frame frame(clos, init); sl@0: // sl@0: // Here now, our closure_frame's variables are initialized with sl@0: // int: 123, char const*: "Hello" and int: 1000. sl@0: // sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: sl@0: namespace impl sl@0: { sl@0: /////////////////////////////////////////////////////////////////////// sl@0: // closure_frame_holder is a simple class that encapsulates the sl@0: // storage for a frame pointer. It uses thread specific data in sl@0: // case when multithreading is enabled, an ordinary pointer otherwise sl@0: // sl@0: // it has get() and set() member functions. set() has to be used sl@0: // _after_ get(). get() contains intialisation code in the multi sl@0: // threading case sl@0: // sl@0: // closure_frame_holder is used by the closure<> class to store sl@0: // the pointer to the current frame. sl@0: // sl@0: #ifndef PHOENIX_THREADSAFE sl@0: template sl@0: struct closure_frame_holder sl@0: { sl@0: typedef FrameT frame_t; sl@0: typedef frame_t *frame_ptr; sl@0: sl@0: closure_frame_holder() : frame(0) {} sl@0: sl@0: frame_ptr &get() { return frame; } sl@0: void set(frame_t *f) { frame = f; } sl@0: sl@0: private: sl@0: frame_ptr frame; sl@0: sl@0: // no copies, no assignments sl@0: closure_frame_holder(closure_frame_holder const &); sl@0: closure_frame_holder &operator=(closure_frame_holder const &); sl@0: }; sl@0: #else sl@0: template sl@0: struct closure_frame_holder sl@0: { sl@0: typedef FrameT frame_t; sl@0: typedef frame_t *frame_ptr; sl@0: sl@0: closure_frame_holder() : tsp_frame() {} sl@0: sl@0: frame_ptr &get() sl@0: { sl@0: if (!tsp_frame.get()) sl@0: tsp_frame.reset(new frame_ptr(0)); sl@0: return *tsp_frame; sl@0: } sl@0: void set(frame_ptr f) sl@0: { sl@0: *tsp_frame = f; sl@0: } sl@0: sl@0: private: sl@0: boost::thread_specific_ptr tsp_frame; sl@0: sl@0: // no copies, no assignments sl@0: closure_frame_holder(closure_frame_holder const &); sl@0: closure_frame_holder &operator=(closure_frame_holder const &); sl@0: }; sl@0: #endif sl@0: } // namespace phoenix::impl sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // sl@0: // closure_frame class sl@0: // sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: template sl@0: class closure_frame : public ClosureT::tuple_t { sl@0: sl@0: public: sl@0: sl@0: closure_frame(ClosureT const& clos) sl@0: : ClosureT::tuple_t(), save(clos.frame.get()), frame(clos.frame) sl@0: { clos.frame.set(this); } sl@0: sl@0: template sl@0: closure_frame(ClosureT const& clos, TupleT const& init) sl@0: : ClosureT::tuple_t(init), save(clos.frame.get()), frame(clos.frame) sl@0: { clos.frame.set(this); } sl@0: sl@0: ~closure_frame() sl@0: { frame.set(save); } sl@0: sl@0: private: sl@0: sl@0: closure_frame(closure_frame const&); // no copy sl@0: closure_frame& operator=(closure_frame const&); // no assign sl@0: sl@0: closure_frame* save; sl@0: impl::closure_frame_holder& frame; sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // sl@0: // closure_member class sl@0: // sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: template sl@0: class closure_member { sl@0: sl@0: public: sl@0: sl@0: typedef typename ClosureT::tuple_t tuple_t; sl@0: sl@0: closure_member() sl@0: : frame(ClosureT::closure_frame_holder_ref()) {} sl@0: sl@0: template sl@0: struct result { sl@0: sl@0: typedef typename tuple_element< sl@0: N, typename ClosureT::tuple_t sl@0: >::rtype type; sl@0: }; sl@0: sl@0: template sl@0: typename tuple_element::rtype sl@0: eval(TupleT const& /*args*/) const sl@0: { sl@0: using namespace std; sl@0: assert(frame.get() != 0); sl@0: return (*frame.get())[tuple_index()]; sl@0: } sl@0: sl@0: private: sl@0: impl::closure_frame_holder &frame; sl@0: }; sl@0: sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: // sl@0: // closure class sl@0: // sl@0: /////////////////////////////////////////////////////////////////////////////// sl@0: template < sl@0: typename T0 = nil_t sl@0: , typename T1 = nil_t sl@0: , typename T2 = nil_t sl@0: sl@0: #if PHOENIX_LIMIT > 3 sl@0: , typename T3 = nil_t sl@0: , typename T4 = nil_t sl@0: , typename T5 = nil_t sl@0: sl@0: #if PHOENIX_LIMIT > 6 sl@0: , typename T6 = nil_t sl@0: , typename T7 = nil_t sl@0: , typename T8 = nil_t sl@0: sl@0: #if PHOENIX_LIMIT > 9 sl@0: , typename T9 = nil_t sl@0: , typename T10 = nil_t sl@0: , typename T11 = nil_t sl@0: sl@0: #if PHOENIX_LIMIT > 12 sl@0: , typename T12 = nil_t sl@0: , typename T13 = nil_t sl@0: , typename T14 = nil_t sl@0: sl@0: #endif sl@0: #endif sl@0: #endif sl@0: #endif sl@0: > sl@0: class closure { sl@0: sl@0: public: sl@0: sl@0: typedef tuple< sl@0: T0, T1, T2 sl@0: #if PHOENIX_LIMIT > 3 sl@0: , T3, T4, T5 sl@0: #if PHOENIX_LIMIT > 6 sl@0: , T6, T7, T8 sl@0: #if PHOENIX_LIMIT > 9 sl@0: , T9, T10, T11 sl@0: #if PHOENIX_LIMIT > 12 sl@0: , T12, T13, T14 sl@0: #endif sl@0: #endif sl@0: #endif sl@0: #endif sl@0: > tuple_t; sl@0: sl@0: typedef closure< sl@0: T0, T1, T2 sl@0: #if PHOENIX_LIMIT > 3 sl@0: , T3, T4, T5 sl@0: #if PHOENIX_LIMIT > 6 sl@0: , T6, T7, T8 sl@0: #if PHOENIX_LIMIT > 9 sl@0: , T9, T10, T11 sl@0: #if PHOENIX_LIMIT > 12 sl@0: , T12, T13, T14 sl@0: #endif sl@0: #endif sl@0: #endif sl@0: #endif sl@0: > self_t; sl@0: sl@0: typedef closure_frame closure_frame_t; sl@0: sl@0: closure() sl@0: : frame() { closure_frame_holder_ref(&frame); } sl@0: closure_frame_t& context() { assert(frame!=0); return frame.get(); } sl@0: closure_frame_t const& context() const { assert(frame!=0); return frame.get(); } sl@0: sl@0: typedef actor > member1; sl@0: typedef actor > member2; sl@0: typedef actor > member3; sl@0: sl@0: #if PHOENIX_LIMIT > 3 sl@0: typedef actor > member4; sl@0: typedef actor > member5; sl@0: typedef actor > member6; sl@0: sl@0: #if PHOENIX_LIMIT > 6 sl@0: typedef actor > member7; sl@0: typedef actor > member8; sl@0: typedef actor > member9; sl@0: sl@0: #if PHOENIX_LIMIT > 9 sl@0: typedef actor > member10; sl@0: typedef actor > member11; sl@0: typedef actor > member12; sl@0: sl@0: #if PHOENIX_LIMIT > 12 sl@0: typedef actor > member13; sl@0: typedef actor > member14; sl@0: typedef actor > member15; sl@0: sl@0: #endif sl@0: #endif sl@0: #endif sl@0: #endif sl@0: sl@0: #if !defined(__MWERKS__) || (__MWERKS__ > 0x3002) sl@0: private: sl@0: #endif sl@0: sl@0: closure(closure const&); // no copy sl@0: closure& operator=(closure const&); // no assign sl@0: sl@0: #if !defined(__MWERKS__) || (__MWERKS__ > 0x3002) sl@0: template sl@0: friend class closure_member; sl@0: sl@0: template sl@0: friend class closure_frame; sl@0: #endif sl@0: sl@0: typedef impl::closure_frame_holder holder_t; sl@0: sl@0: #ifdef PHOENIX_THREADSAFE sl@0: static boost::thread_specific_ptr & sl@0: tsp_frame_instance() sl@0: { sl@0: static boost::thread_specific_ptr the_instance; sl@0: return the_instance; sl@0: } sl@0: sl@0: static void sl@0: tsp_frame_instance_init() sl@0: { sl@0: tsp_frame_instance(); sl@0: } sl@0: #endif sl@0: sl@0: static holder_t & sl@0: closure_frame_holder_ref(holder_t* holder_ = 0) sl@0: { sl@0: #ifdef PHOENIX_THREADSAFE sl@0: static boost::once_flag been_here = BOOST_ONCE_INIT; sl@0: boost::call_once(tsp_frame_instance_init, been_here); sl@0: boost::thread_specific_ptr &tsp_frame = tsp_frame_instance(); sl@0: if (!tsp_frame.get()) sl@0: tsp_frame.reset(new holder_t *(0)); sl@0: holder_t *& holder = *tsp_frame; sl@0: #else sl@0: static holder_t* holder = 0; sl@0: #endif sl@0: if (holder_ != 0) sl@0: holder = holder_; sl@0: return *holder; sl@0: } sl@0: sl@0: mutable holder_t frame; sl@0: }; sl@0: sl@0: } sl@0: // namespace phoenix sl@0: sl@0: #endif