/Src/Dependencies/Boost/boost/spirit/home/phoenix/scope/dynamic.hpp

http://hadesmem.googlecode.com/ · C++ Header · 193 lines · 120 code · 36 blank · 37 comment · 1 complexity · 6fe437f94c4dce73f4b6f990fc9e9ebe MD5 · raw file

  1. /*=============================================================================
  2. Copyright (c) 2001-2007 Joel de Guzman
  3. Copyright (c) 2004 Daniel Wallin
  4. Distributed under the Boost Software License, Version 1.0. (See accompanying
  5. file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  6. ==============================================================================*/
  7. #ifndef PHOENIX_SCOPE_DYNAMIC_HPP
  8. #define PHOENIX_SCOPE_DYNAMIC_HPP
  9. #include <boost/spirit/home/phoenix/core/limits.hpp>
  10. #include <boost/fusion/include/at.hpp>
  11. #include <boost/fusion/include/vector.hpp>
  12. #include <boost/spirit/home/phoenix/core/actor.hpp>
  13. #include <boost/preprocessor/cat.hpp>
  14. #include <boost/preprocessor/punctuation/comma_if.hpp>
  15. #include <boost/preprocessor/seq/for_each_i.hpp>
  16. #include <boost/preprocessor/tuple/elem.hpp>
  17. #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
  18. #include <boost/preprocessor/repetition/enum_params.hpp>
  19. #include <boost/preprocessor/cat.hpp>
  20. #include <boost/preprocessor/inc.hpp>
  21. #include <boost/noncopyable.hpp>
  22. #include <boost/assert.hpp>
  23. #define PHOENIX_DYNAMIC_MEMBER(z, n, data) \
  24. typedef actor<dynamic_member<n, self_type> > \
  25. BOOST_PP_CAT(member, BOOST_PP_INC(n));
  26. namespace boost { namespace phoenix
  27. {
  28. template <typename DynamicScope>
  29. struct dynamic_frame : noncopyable
  30. {
  31. typedef typename DynamicScope::tuple_type tuple_type;
  32. dynamic_frame(DynamicScope const& scope)
  33. : tuple()
  34. , save(scope.frame)
  35. , scope(scope)
  36. {
  37. scope.frame = this;
  38. }
  39. template <typename Tuple>
  40. dynamic_frame(DynamicScope const& scope, Tuple const& init)
  41. : tuple(init)
  42. , save(scope.frame)
  43. , scope(scope)
  44. {
  45. scope.frame = this;
  46. }
  47. ~dynamic_frame()
  48. {
  49. scope.frame = save;
  50. }
  51. tuple_type& data() { return tuple; }
  52. tuple_type const& data() const { return tuple; }
  53. private:
  54. tuple_type tuple;
  55. dynamic_frame* save;
  56. DynamicScope const& scope;
  57. };
  58. template <int N, typename DynamicScope>
  59. struct dynamic_member
  60. {
  61. typedef mpl::false_ no_nullary;
  62. typedef typename DynamicScope::tuple_type tuple_type;
  63. dynamic_member(DynamicScope const& scope)
  64. : scope(scope) {}
  65. template <typename Env>
  66. struct result
  67. {
  68. typedef typename
  69. fusion::result_of::at_c<tuple_type, N>::type
  70. type;
  71. };
  72. template <typename Env>
  73. typename result<Env>::type
  74. eval(Env const& /*env*/) const
  75. {
  76. BOOST_ASSERT(scope.frame != 0);
  77. return fusion::at_c<N>(scope.frame->data());
  78. }
  79. private:
  80. DynamicScope const& scope;
  81. };
  82. template <BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(PHOENIX_DYNAMIC_LIMIT, typename T, void_)>
  83. struct dynamic : noncopyable
  84. {
  85. typedef fusion::vector<BOOST_PP_ENUM_PARAMS(PHOENIX_DYNAMIC_LIMIT, T)> tuple_type;
  86. typedef dynamic<BOOST_PP_ENUM_PARAMS(PHOENIX_DYNAMIC_LIMIT, T)> self_type;
  87. typedef dynamic_frame<self_type> dynamic_frame_type;
  88. dynamic()
  89. : frame(0) {}
  90. BOOST_PP_REPEAT(PHOENIX_DYNAMIC_LIMIT, PHOENIX_DYNAMIC_MEMBER, _)
  91. private:
  92. template <int N, typename DynamicScope>
  93. friend struct dynamic_member;
  94. template <typename DynamicScope>
  95. friend struct dynamic_frame;
  96. mutable dynamic_frame_type* frame;
  97. };
  98. }}
  99. #if defined(BOOST_MSVC)
  100. # pragma warning(push)
  101. # pragma warning(disable:4355)
  102. #endif
  103. /*
  104. PHOENIX_DYNAMIC macro(name, type-name sequence)
  105. Example:
  106. PHOENIX_DYNAMIC(
  107. my_dynamic,
  108. (int, num)
  109. (std::string, message)
  110. (double, real)
  111. );
  112. which expands to:
  113. struct my_dynamic : ::boost::phoenix::dynamic<int, std::string, double>
  114. {
  115. my_dynamic() : num(*this), message(*this), real(*this) {}
  116. member1 num;
  117. member2 message;
  118. member3 real;
  119. };
  120. PHOENIX_DYNAMIC takes the input (containing a binary sequence)
  121. and converts that sequence to a unary sequence of
  122. binary tuples and passes it on to PHOENIX_DYNAMIC_I.
  123. Thanks to Paul Mensonides for the PP macro help
  124. */
  125. #define PHOENIX_DYNAMIC(name, bseq) \
  126. PHOENIX_DYNAMIC_I(name, BOOST_PP_CAT(PHOENIX_DYNAMIC_X bseq, 0)) \
  127. #define PHOENIX_DYNAMIC_X(x, y) ((x, y)) PHOENIX_DYNAMIC_Y
  128. #define PHOENIX_DYNAMIC_Y(x, y) ((x, y)) PHOENIX_DYNAMIC_X
  129. #define PHOENIX_DYNAMIC_X0
  130. #define PHOENIX_DYNAMIC_Y0
  131. // PHOENIX_DYNAMIC_I generates the overarching structure and uses
  132. // SEQ_FOR_EACH_I to generate the "linear" substructures.
  133. #define PHOENIX_DYNAMIC_I(name, seq) \
  134. struct name : \
  135. ::boost::phoenix::dynamic< \
  136. BOOST_PP_SEQ_FOR_EACH_I(PHOENIX_DYNAMIC_A, ~, seq)> { \
  137. name() : BOOST_PP_SEQ_FOR_EACH_I(PHOENIX_DYNAMIC_B, ~, seq) {} \
  138. BOOST_PP_SEQ_FOR_EACH_I(PHOENIX_DYNAMIC_C, ~, seq) \
  139. } \
  140. #define PHOENIX_DYNAMIC_A(r, _, i, xy) \
  141. BOOST_PP_COMMA_IF(i) BOOST_PP_TUPLE_ELEM(2, 0, xy) \
  142. #define PHOENIX_DYNAMIC_B(r, _, i, xy) \
  143. BOOST_PP_COMMA_IF(i) BOOST_PP_TUPLE_ELEM(2, 1, xy)(*this) \
  144. #define PHOENIX_DYNAMIC_C(r, _, i, xy) \
  145. BOOST_PP_CAT(member, BOOST_PP_INC(i)) BOOST_PP_TUPLE_ELEM(2, 1, xy); \
  146. #undef PHOENIX_DYNAMIC_MEMBER
  147. #if defined(BOOST_MSVC)
  148. # pragma warning(pop)
  149. #endif
  150. #endif