macro.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /* /libs/serialization/xml_performance/macro.hpp *******************************
  2. (C) Copyright 2010 Bryce Lelbach
  3. Use, modification and distribution is subject to the Boost Software License,
  4. Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  5. http://www.boost.org/LICENSE_1_0.txt)
  6. *******************************************************************************/
  7. #if !defined(BOOST_SERIALIZATION_XML_PERFORMANCE_MACRO_HPP)
  8. #define BOOST_SERIALIZATION_XML_PERFORMANCE_MACRO_HPP
  9. #if defined(_MSC_VER)
  10. #pragma once
  11. #endif
  12. #include <boost/preprocessor.hpp>
  13. #if !defined(BSL_NODE_MAX)
  14. #define BSL_NODE_MAX 4
  15. #endif
  16. #if !defined(BSL_DEPTH)
  17. #define BSL_DEPTH 2
  18. #endif
  19. #if !defined(BSL_ROUNDS)
  20. #define BSL_ROUNDS 256
  21. #endif
  22. #if !defined(BSL_TYPE)
  23. #define BSL_TYPE int
  24. #endif
  25. #if !defined(BSL_SAVE_TMPFILE)
  26. #define BSL_SAVE_TMPFILE 0
  27. #endif
  28. #if !defined(BSL_RESULTS_FILE)
  29. #define BSL_RESULTS_FILE \
  30. BOOST_PP_STRINGIZE(BSL_TYPE) \
  31. BOOST_PP_STRINGIZE(BSL_EXP(BSL_NODE_MAX, BSL_DEPTH)) \
  32. "_results.xml" \
  33. /**/
  34. #endif
  35. // utility print macro
  36. #define BSL_PRINT(Z, N, T) T
  37. // preprocessor power function, BSL_EXP
  38. #define BSL_EXP_PRED(B, D) BOOST_PP_TUPLE_ELEM(3, 0, D)
  39. #define BSL_EXP_OP(B, D) \
  40. ( \
  41. BOOST_PP_DEC(BOOST_PP_TUPLE_ELEM(3, 0, D)), \
  42. BOOST_PP_TUPLE_ELEM(3, 1, D), \
  43. BOOST_PP_MUL_D( \
  44. B, \
  45. BOOST_PP_TUPLE_ELEM(3, 2, D), \
  46. BOOST_PP_TUPLE_ELEM(3, 1, D) \
  47. ) \
  48. ) \
  49. /**/
  50. #define BSL_EXP(X, N) \
  51. BOOST_PP_TUPLE_ELEM( \
  52. 3, 2, BOOST_PP_WHILE(BSL_EXP_PRED, BSL_EXP_OP, (N, X, 1)) \
  53. ) \
  54. /**/
  55. // boost::archive::xml::node macros
  56. #define BSL_NODE_DECL_MEMBER(Z, N, _) T ## N element ## N ;
  57. #define BSL_NODE_DECL_NONE(Z, N, _) unused_type element ## N ;
  58. #define BSL_NODE_xDECL_CTOR() node (void) { }
  59. #define BSL_NODE_DECL_CTOR(P) \
  60. BOOST_PP_IF(P, \
  61. BSL_NODE_xDECL_CTOR, \
  62. BOOST_PP_EMPTY \
  63. )() \
  64. /**/
  65. #define BSL_NODE_SERIALIZE(Z, N, _) \
  66. & BOOST_SERIALIZATION_NVP(BOOST_PP_CAT(element, N)) \
  67. /**/
  68. #define BSL_NODE_INIT_LIST(Z, N, _) \
  69. BOOST_PP_COMMA_IF(N) BOOST_PP_CAT(element, N) \
  70. BOOST_PP_LPAREN() BOOST_PP_CAT(p, N) BOOST_PP_RPAREN() \
  71. /**/
  72. #define BSL_NODE_DECL(Z, N, _) \
  73. template<BOOST_PP_ENUM_PARAMS_Z(Z, N, typename T)> \
  74. struct node< \
  75. BOOST_PP_ENUM_PARAMS_Z(Z, N, T) \
  76. BOOST_PP_COMMA_IF(N) \
  77. BOOST_PP_ENUM_ ## Z(BOOST_PP_SUB(BSL_NODE_MAX, N), BSL_PRINT, unused_type) \
  78. > { \
  79. BOOST_PP_REPEAT_ ## Z(N, BSL_NODE_DECL_MEMBER, _) \
  80. \
  81. BOOST_PP_REPEAT_FROM_TO_ ## Z(N, BSL_NODE_MAX, BSL_NODE_DECL_NONE, _) \
  82. \
  83. template<class ARC> \
  84. void serialize (ARC& ar, const unsigned int) { \
  85. ar BOOST_PP_REPEAT_ ## Z(N, BSL_NODE_SERIALIZE, _); \
  86. } \
  87. \
  88. BSL_NODE_DECL_CTOR(N) \
  89. \
  90. node (BOOST_PP_ENUM_BINARY_PARAMS_Z(Z, N, T, p)): \
  91. BOOST_PP_REPEAT_ ## Z(N, BSL_NODE_INIT_LIST, _) { } \
  92. }; \
  93. /**/
  94. // instantiation macros
  95. #define BSL_INST_BASE(Z, N, L) \
  96. T0 T0 ## _ ## N(BOOST_PP_ENUM_ ## Z( \
  97. BSL_NODE_MAX, BSL_PRINT, \
  98. boost::archive::xml::random<BSL_TYPE> BOOST_PP_LPAREN() BOOST_PP_RPAREN() \
  99. )); \
  100. /**/
  101. #define BSL_INST_yNODES(Z, N, L) \
  102. BOOST_PP_COMMA_IF(N) \
  103. BOOST_PP_CAT(T, \
  104. BOOST_PP_CAT(BOOST_PP_LIST_AT(L, 1), \
  105. BOOST_PP_CAT(_, \
  106. BOOST_PP_ADD(N, \
  107. BOOST_PP_LIST_AT(L, 0) \
  108. ) \
  109. ) \
  110. ) \
  111. ) \
  112. /**/
  113. #define BSL_INST_xNODES(Z, N, L) \
  114. T ## L T ## L ## _ ## N( \
  115. BOOST_PP_REPEAT_ ## Z( \
  116. BSL_NODE_MAX, BSL_INST_yNODES, \
  117. (BOOST_PP_MUL(N, BSL_NODE_MAX), (BOOST_PP_SUB(L, 1), BOOST_PP_NIL)) \
  118. ) \
  119. ); \
  120. /**/
  121. #define BSL_INST_NODES(Z, N, L) \
  122. BOOST_PP_REPEAT_ ## Z( \
  123. BSL_EXP(BSL_NODE_MAX, BOOST_PP_SUB(BSL_DEPTH, N)), \
  124. BSL_INST_xNODES, N \
  125. ) \
  126. /**/
  127. #define BSL_TYPEDEF_NODES(Z, N, L) \
  128. typedef boost::archive::xml::node< \
  129. BOOST_PP_ENUM_ ## Z( \
  130. BSL_NODE_MAX, BSL_PRINT, BOOST_PP_CAT(T, BOOST_PP_SUB(N, 1)) \
  131. ) \
  132. > T ## N; \
  133. /**/
  134. // main macro
  135. #define BSL_MAIN \
  136. int main (void) { \
  137. using namespace boost::archive; \
  138. using namespace boost::archive::xml; \
  139. \
  140. typedef node<BOOST_PP_ENUM(BSL_NODE_MAX, BSL_PRINT, BSL_TYPE)> T0; \
  141. \
  142. BOOST_PP_REPEAT_FROM_TO(1, BSL_DEPTH, BSL_TYPEDEF_NODES, _) \
  143. \
  144. typedef node<BOOST_PP_ENUM( \
  145. BSL_NODE_MAX, BSL_PRINT, \
  146. BOOST_PP_CAT(T, BOOST_PP_SUB(BSL_DEPTH, 1)) \
  147. )> HEAD; \
  148. \
  149. result_set results; \
  150. std::size_t rounds = BSL_ROUNDS; \
  151. \
  152. while (rounds --> 0) { \
  153. BOOST_PP_REPEAT(BSL_EXP(BSL_NODE_MAX, BSL_DEPTH), BSL_INST_BASE, _) \
  154. \
  155. BOOST_PP_REPEAT_FROM_TO(1, BSL_DEPTH, BSL_INST_NODES, _) \
  156. \
  157. HEAD h(BOOST_PP_ENUM_PARAMS( \
  158. BSL_NODE_MAX, \
  159. BOOST_PP_CAT(T, BOOST_PP_CAT(BOOST_PP_SUB(BSL_DEPTH, 1), _)) \
  160. )); \
  161. \
  162. std::string fn = save_archive(h); \
  163. \
  164. std::pair<double, HEAD> r = restore_archive<HEAD>(fn); \
  165. \
  166. std::cout << "round " \
  167. << ((BSL_ROUNDS - 1) - rounds) \
  168. << " -> " << fn << "\n"; \
  169. \
  170. BOOST_PP_IF(BSL_SAVE_TMPFILE, \
  171. BOOST_PP_EMPTY(), \
  172. std::remove(fn.c_str()); \
  173. ) \
  174. \
  175. results.entries.push_back(entry( \
  176. BOOST_PP_STRINGIZE(BSL_TYPE), \
  177. BSL_EXP(BSL_NODE_MAX, BSL_DEPTH), r.first \
  178. )); \
  179. } \
  180. \
  181. std::fstream fs(BSL_RESULTS_FILE, std::fstream::in); \
  182. \
  183. if (fs.good()) { \
  184. xml_iarchive ia(fs); \
  185. ia >> BOOST_SERIALIZATION_NVP(results); \
  186. fs.close(); \
  187. } \
  188. \
  189. fs.open(BSL_RESULTS_FILE, std::fstream::out | std::fstream::trunc); \
  190. xml_oarchive oa(fs); \
  191. oa << BOOST_SERIALIZATION_NVP(results); \
  192. \
  193. fs.close(); \
  194. } \
  195. /**/
  196. #endif // BOOST_SERIALIZATION_XML_PERFORMANCE_MACRO_HPP