closures.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450
  1. /*=============================================================================
  2. Phoenix V1.2.1
  3. Copyright (c) 2001-2002 Joel de Guzman
  4. MT code Copyright (c) 2002-2003 Martin Wille
  5. Distributed under the Boost Software License, Version 1.0. (See accompanying
  6. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  7. ==============================================================================*/
  8. #ifndef CLASSIC_PHOENIX_CLOSURES_HPP
  9. #define CLASSIC_PHOENIX_CLOSURES_HPP
  10. ///////////////////////////////////////////////////////////////////////////////
  11. #include <boost/spirit/home/classic/phoenix/actor.hpp>
  12. #include <boost/assert.hpp>
  13. #ifdef PHOENIX_THREADSAFE
  14. #include <boost/thread/tss.hpp>
  15. #include <boost/thread/once.hpp>
  16. #endif
  17. ///////////////////////////////////////////////////////////////////////////////
  18. namespace phoenix {
  19. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  20. #pragma warning(push)
  21. #pragma warning(disable:4512) //assignment operator could not be generated
  22. #endif
  23. ///////////////////////////////////////////////////////////////////////////////
  24. //
  25. // Adaptable closures
  26. //
  27. // The framework will not be complete without some form of closures
  28. // support. Closures encapsulate a stack frame where local
  29. // variables are created upon entering a function and destructed
  30. // upon exiting. Closures provide an environment for local
  31. // variables to reside. Closures can hold heterogeneous types.
  32. //
  33. // Phoenix closures are true hardware stack based closures. At the
  34. // very least, closures enable true reentrancy in lambda functions.
  35. // A closure provides access to a function stack frame where local
  36. // variables reside. Modeled after Pascal nested stack frames,
  37. // closures can be nested just like nested functions where code in
  38. // inner closures may access local variables from in-scope outer
  39. // closures (accessing inner scopes from outer scopes is an error
  40. // and will cause a run-time assertion failure).
  41. //
  42. // There are three (3) interacting classes:
  43. //
  44. // 1) closure:
  45. //
  46. // At the point of declaration, a closure does not yet create a
  47. // stack frame nor instantiate any variables. A closure declaration
  48. // declares the types and names[note] of the local variables. The
  49. // closure class is meant to be subclassed. It is the
  50. // responsibility of a closure subclass to supply the names for
  51. // each of the local variable in the closure. Example:
  52. //
  53. // struct my_closure : closure<int, string, double> {
  54. //
  55. // member1 num; // names the 1st (int) local variable
  56. // member2 message; // names the 2nd (string) local variable
  57. // member3 real; // names the 3rd (double) local variable
  58. // };
  59. //
  60. // my_closure clos;
  61. //
  62. // Now that we have a closure 'clos', its local variables can be
  63. // accessed lazily using the dot notation. Each qualified local
  64. // variable can be used just like any primitive actor (see
  65. // primitives.hpp). Examples:
  66. //
  67. // clos.num = 30
  68. // clos.message = arg1
  69. // clos.real = clos.num * 1e6
  70. //
  71. // The examples above are lazily evaluated. As usual, these
  72. // expressions return composite actors that will be evaluated
  73. // through a second function call invocation (see operators.hpp).
  74. // Each of the members (clos.xxx) is an actor. As such, applying
  75. // the operator() will reveal its identity:
  76. //
  77. // clos.num() // will return the current value of clos.num
  78. //
  79. // *** [note] Acknowledgement: Juan Carlos Arevalo-Baeza (JCAB)
  80. // introduced and initilally implemented the closure member names
  81. // that uses the dot notation.
  82. //
  83. // 2) closure_member
  84. //
  85. // The named local variables of closure 'clos' above are actually
  86. // closure members. The closure_member class is an actor and
  87. // conforms to its conceptual interface. member1..memberN are
  88. // predefined typedefs that correspond to each of the listed types
  89. // in the closure template parameters.
  90. //
  91. // 3) closure_frame
  92. //
  93. // When a closure member is finally evaluated, it should refer to
  94. // an actual instance of the variable in the hardware stack.
  95. // Without doing so, the process is not complete and the evaluated
  96. // member will result to an assertion failure. Remember that the
  97. // closure is just a declaration. The local variables that a
  98. // closure refers to must still be instantiated.
  99. //
  100. // The closure_frame class does the actual instantiation of the
  101. // local variables and links these variables with the closure and
  102. // all its members. There can be multiple instances of
  103. // closure_frames typically situated in the stack inside a
  104. // function. Each closure_frame instance initiates a stack frame
  105. // with a new set of closure local variables. Example:
  106. //
  107. // void foo()
  108. // {
  109. // closure_frame<my_closure> frame(clos);
  110. // /* do something */
  111. // }
  112. //
  113. // where 'clos' is an instance of our closure 'my_closure' above.
  114. // Take note that the usage above precludes locally declared
  115. // classes. If my_closure is a locally declared type, we can still
  116. // use its self_type as a paramater to closure_frame:
  117. //
  118. // closure_frame<my_closure::self_type> frame(clos);
  119. //
  120. // Upon instantiation, the closure_frame links the local variables
  121. // to the closure. The previous link to another closure_frame
  122. // instance created before is saved. Upon destruction, the
  123. // closure_frame unlinks itself from the closure and relinks the
  124. // preceding closure_frame prior to this instance.
  125. //
  126. // The local variables in the closure 'clos' above is default
  127. // constructed in the stack inside function 'foo'. Once 'foo' is
  128. // exited, all of these local variables are destructed. In some
  129. // cases, default construction is not desirable and we need to
  130. // initialize the local closure variables with some values. This
  131. // can be done by passing in the initializers in a compatible
  132. // tuple. A compatible tuple is one with the same number of
  133. // elements as the destination and where each element from the
  134. // destination can be constructed from each corresponding element
  135. // in the source. Example:
  136. //
  137. // tuple<int, char const*, int> init(123, "Hello", 1000);
  138. // closure_frame<my_closure> frame(clos, init);
  139. //
  140. // Here now, our closure_frame's variables are initialized with
  141. // int: 123, char const*: "Hello" and int: 1000.
  142. //
  143. ///////////////////////////////////////////////////////////////////////////////
  144. namespace impl
  145. {
  146. ///////////////////////////////////////////////////////////////////////
  147. // closure_frame_holder is a simple class that encapsulates the
  148. // storage for a frame pointer. It uses thread specific data in
  149. // case when multithreading is enabled, an ordinary pointer otherwise
  150. //
  151. // it has get() and set() member functions. set() has to be used
  152. // _after_ get(). get() contains intialisation code in the multi
  153. // threading case
  154. //
  155. // closure_frame_holder is used by the closure<> class to store
  156. // the pointer to the current frame.
  157. //
  158. #ifndef PHOENIX_THREADSAFE
  159. template <typename FrameT>
  160. struct closure_frame_holder
  161. {
  162. typedef FrameT frame_t;
  163. typedef frame_t *frame_ptr;
  164. closure_frame_holder() : frame(0) {}
  165. frame_ptr &get() { return frame; }
  166. void set(frame_t *f) { frame = f; }
  167. private:
  168. frame_ptr frame;
  169. // no copies, no assignments
  170. closure_frame_holder(closure_frame_holder const &);
  171. closure_frame_holder &operator=(closure_frame_holder const &);
  172. };
  173. #else
  174. template <typename FrameT>
  175. struct closure_frame_holder
  176. {
  177. typedef FrameT frame_t;
  178. typedef frame_t *frame_ptr;
  179. closure_frame_holder() : tsp_frame() {}
  180. frame_ptr &get()
  181. {
  182. if (!tsp_frame.get())
  183. tsp_frame.reset(new frame_ptr(0));
  184. return *tsp_frame;
  185. }
  186. void set(frame_ptr f)
  187. {
  188. *tsp_frame = f;
  189. }
  190. private:
  191. boost::thread_specific_ptr<frame_ptr> tsp_frame;
  192. // no copies, no assignments
  193. closure_frame_holder(closure_frame_holder const &);
  194. closure_frame_holder &operator=(closure_frame_holder const &);
  195. };
  196. #endif
  197. } // namespace phoenix::impl
  198. ///////////////////////////////////////////////////////////////////////////////
  199. //
  200. // closure_frame class
  201. //
  202. ///////////////////////////////////////////////////////////////////////////////
  203. template <typename ClosureT>
  204. class closure_frame : public ClosureT::tuple_t {
  205. public:
  206. closure_frame(ClosureT const& clos)
  207. : ClosureT::tuple_t(), save(clos.frame.get()), frame(clos.frame)
  208. { clos.frame.set(this); }
  209. template <typename TupleT>
  210. closure_frame(ClosureT const& clos, TupleT const& init)
  211. : ClosureT::tuple_t(init), save(clos.frame.get()), frame(clos.frame)
  212. { clos.frame.set(this); }
  213. ~closure_frame()
  214. { frame.set(save); }
  215. private:
  216. closure_frame(closure_frame const&); // no copy
  217. closure_frame& operator=(closure_frame const&); // no assign
  218. closure_frame* save;
  219. impl::closure_frame_holder<closure_frame>& frame;
  220. };
  221. ///////////////////////////////////////////////////////////////////////////////
  222. //
  223. // closure_member class
  224. //
  225. ///////////////////////////////////////////////////////////////////////////////
  226. template <int N, typename ClosureT>
  227. class closure_member {
  228. public:
  229. typedef typename ClosureT::tuple_t tuple_t;
  230. closure_member()
  231. : frame(ClosureT::closure_frame_holder_ref()) {}
  232. template <typename TupleT>
  233. struct result {
  234. typedef typename tuple_element<
  235. N, typename ClosureT::tuple_t
  236. >::rtype type;
  237. };
  238. template <typename TupleT>
  239. typename tuple_element<N, typename ClosureT::tuple_t>::rtype
  240. eval(TupleT const& /*args*/) const
  241. {
  242. using namespace std;
  243. BOOST_ASSERT(frame.get() != 0);
  244. return (*frame.get())[tuple_index<N>()];
  245. }
  246. private:
  247. impl::closure_frame_holder<typename ClosureT::closure_frame_t> &frame;
  248. };
  249. ///////////////////////////////////////////////////////////////////////////////
  250. //
  251. // closure class
  252. //
  253. ///////////////////////////////////////////////////////////////////////////////
  254. template <
  255. typename T0 = nil_t
  256. , typename T1 = nil_t
  257. , typename T2 = nil_t
  258. #if PHOENIX_LIMIT > 3
  259. , typename T3 = nil_t
  260. , typename T4 = nil_t
  261. , typename T5 = nil_t
  262. #if PHOENIX_LIMIT > 6
  263. , typename T6 = nil_t
  264. , typename T7 = nil_t
  265. , typename T8 = nil_t
  266. #if PHOENIX_LIMIT > 9
  267. , typename T9 = nil_t
  268. , typename T10 = nil_t
  269. , typename T11 = nil_t
  270. #if PHOENIX_LIMIT > 12
  271. , typename T12 = nil_t
  272. , typename T13 = nil_t
  273. , typename T14 = nil_t
  274. #endif
  275. #endif
  276. #endif
  277. #endif
  278. >
  279. class closure {
  280. public:
  281. typedef tuple<
  282. T0, T1, T2
  283. #if PHOENIX_LIMIT > 3
  284. , T3, T4, T5
  285. #if PHOENIX_LIMIT > 6
  286. , T6, T7, T8
  287. #if PHOENIX_LIMIT > 9
  288. , T9, T10, T11
  289. #if PHOENIX_LIMIT > 12
  290. , T12, T13, T14
  291. #endif
  292. #endif
  293. #endif
  294. #endif
  295. > tuple_t;
  296. typedef closure<
  297. T0, T1, T2
  298. #if PHOENIX_LIMIT > 3
  299. , T3, T4, T5
  300. #if PHOENIX_LIMIT > 6
  301. , T6, T7, T8
  302. #if PHOENIX_LIMIT > 9
  303. , T9, T10, T11
  304. #if PHOENIX_LIMIT > 12
  305. , T12, T13, T14
  306. #endif
  307. #endif
  308. #endif
  309. #endif
  310. > self_t;
  311. typedef closure_frame<self_t> closure_frame_t;
  312. closure()
  313. : frame() { closure_frame_holder_ref(&frame); }
  314. typedef actor<closure_member<0, self_t> > member1;
  315. typedef actor<closure_member<1, self_t> > member2;
  316. typedef actor<closure_member<2, self_t> > member3;
  317. #if PHOENIX_LIMIT > 3
  318. typedef actor<closure_member<3, self_t> > member4;
  319. typedef actor<closure_member<4, self_t> > member5;
  320. typedef actor<closure_member<5, self_t> > member6;
  321. #if PHOENIX_LIMIT > 6
  322. typedef actor<closure_member<6, self_t> > member7;
  323. typedef actor<closure_member<7, self_t> > member8;
  324. typedef actor<closure_member<8, self_t> > member9;
  325. #if PHOENIX_LIMIT > 9
  326. typedef actor<closure_member<9, self_t> > member10;
  327. typedef actor<closure_member<10, self_t> > member11;
  328. typedef actor<closure_member<11, self_t> > member12;
  329. #if PHOENIX_LIMIT > 12
  330. typedef actor<closure_member<12, self_t> > member13;
  331. typedef actor<closure_member<13, self_t> > member14;
  332. typedef actor<closure_member<14, self_t> > member15;
  333. #endif
  334. #endif
  335. #endif
  336. #endif
  337. #if !defined(__MWERKS__) || (__MWERKS__ > 0x3002)
  338. private:
  339. #endif
  340. closure(closure const&); // no copy
  341. closure& operator=(closure const&); // no assign
  342. #if !defined(__MWERKS__) || (__MWERKS__ > 0x3002)
  343. template <int N, typename ClosureT>
  344. friend class closure_member;
  345. template <typename ClosureT>
  346. friend class closure_frame;
  347. #endif
  348. typedef impl::closure_frame_holder<closure_frame_t> holder_t;
  349. #ifdef PHOENIX_THREADSAFE
  350. static boost::thread_specific_ptr<holder_t*> &
  351. tsp_frame_instance()
  352. {
  353. static boost::thread_specific_ptr<holder_t*> the_instance;
  354. return the_instance;
  355. }
  356. static void
  357. tsp_frame_instance_init()
  358. {
  359. tsp_frame_instance();
  360. }
  361. #endif
  362. static holder_t &
  363. closure_frame_holder_ref(holder_t* holder_ = 0)
  364. {
  365. #ifdef PHOENIX_THREADSAFE
  366. #ifndef BOOST_THREAD_PROVIDES_ONCE_CXX11
  367. static boost::once_flag been_here = BOOST_ONCE_INIT;
  368. #else
  369. static boost::once_flag been_here;
  370. #endif
  371. boost::call_once(been_here, tsp_frame_instance_init);
  372. boost::thread_specific_ptr<holder_t*> &tsp_frame = tsp_frame_instance();
  373. if (!tsp_frame.get())
  374. tsp_frame.reset(new holder_t *(0));
  375. holder_t *& holder = *tsp_frame;
  376. #else
  377. static holder_t* holder = 0;
  378. #endif
  379. if (holder_ != 0)
  380. holder = holder_;
  381. return *holder;
  382. }
  383. mutable holder_t frame;
  384. };
  385. #if BOOST_WORKAROUND(BOOST_MSVC, >= 1400)
  386. #pragma warning(pop)
  387. #endif
  388. }
  389. // namespace phoenix
  390. #endif