summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/serialization/performance/xml/macro.hpp
blob: 6f8cbf963e2f44aee0d9d921d6b3eda1ab4ea43d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
/* /libs/serialization/xml_performance/macro.hpp *******************************

(C) Copyright 2010 Bryce Lelbach

Use, modification and distribution is subject to the Boost Software License,
Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at 
http://www.boost.org/LICENSE_1_0.txt)

*******************************************************************************/

#if !defined(BOOST_SERIALIZATION_XML_PERFORMANCE_MACRO_HPP)
#define BOOST_SERIALIZATION_XML_PERFORMANCE_MACRO_HPP

#if defined(_MSC_VER)
  #pragma once
#endif

#include <boost/preprocessor.hpp>

#if !defined(BSL_NODE_MAX)
  #define BSL_NODE_MAX 4
#endif

#if !defined(BSL_DEPTH)
  #define BSL_DEPTH 2
#endif

#if !defined(BSL_ROUNDS)
  #define BSL_ROUNDS 256
#endif

#if !defined(BSL_TYPE)
  #define BSL_TYPE int
#endif

#if !defined(BSL_SAVE_TMPFILE)
  #define BSL_SAVE_TMPFILE 0
#endif

#if !defined(BSL_RESULTS_FILE)
  #define BSL_RESULTS_FILE                                \
    BOOST_PP_STRINGIZE(BSL_TYPE)                          \
    BOOST_PP_STRINGIZE(BSL_EXP(BSL_NODE_MAX, BSL_DEPTH))  \
    "_results.xml"                                        \
  /**/
#endif

// utility print macro

#define BSL_PRINT(Z, N, T) T

// preprocessor power function, BSL_EXP

#define BSL_EXP_PRED(B, D) BOOST_PP_TUPLE_ELEM(3, 0, D)

#define BSL_EXP_OP(B, D)                                                       \
   (                                                                           \
      BOOST_PP_DEC(BOOST_PP_TUPLE_ELEM(3, 0, D)),                              \
      BOOST_PP_TUPLE_ELEM(3, 1, D),                                            \
      BOOST_PP_MUL_D(                                                          \
         B,                                                                    \
         BOOST_PP_TUPLE_ELEM(3, 2, D),                                         \
         BOOST_PP_TUPLE_ELEM(3, 1, D)                                          \
      )                                                                        \
   )                                                                           \
   /**/

#define BSL_EXP(X, N)                                                          \
  BOOST_PP_TUPLE_ELEM(                                                         \
    3, 2, BOOST_PP_WHILE(BSL_EXP_PRED, BSL_EXP_OP, (N, X, 1))                  \
  )                                                                            \
  /**/

// boost::archive::xml::node macros

#define BSL_NODE_DECL_MEMBER(Z, N, _)   T ## N  element ## N  ;
#define BSL_NODE_DECL_NONE(Z, N, _)     unused_type  element ## N  ;
#define BSL_NODE_xDECL_CTOR()           node (void) { }

#define BSL_NODE_DECL_CTOR(P)                                                  \
  BOOST_PP_IF(P,                                                               \
    BSL_NODE_xDECL_CTOR,                                                       \
    BOOST_PP_EMPTY                                                             \
  )()                                                                          \
  /**/

#define BSL_NODE_SERIALIZE(Z, N, _)                                            \
  & BOOST_SERIALIZATION_NVP(BOOST_PP_CAT(element, N))                          \
  /**/

#define BSL_NODE_INIT_LIST(Z, N, _)                                            \
  BOOST_PP_COMMA_IF(N)  BOOST_PP_CAT(element, N)                               \
  BOOST_PP_LPAREN() BOOST_PP_CAT(p, N) BOOST_PP_RPAREN()                       \
  /**/

#define BSL_NODE_DECL(Z, N, _)                                                 \
  template<BOOST_PP_ENUM_PARAMS_Z(Z, N, typename T)>                           \
  struct node<                                                                 \
    BOOST_PP_ENUM_PARAMS_Z(Z, N, T)                                            \
    BOOST_PP_COMMA_IF(N)                                                       \
    BOOST_PP_ENUM_ ## Z(BOOST_PP_SUB(BSL_NODE_MAX, N), BSL_PRINT, unused_type) \
  > {                                                                          \
    BOOST_PP_REPEAT_ ## Z(N, BSL_NODE_DECL_MEMBER, _)                          \
                                                                               \
    BOOST_PP_REPEAT_FROM_TO_ ## Z(N, BSL_NODE_MAX, BSL_NODE_DECL_NONE, _)      \
                                                                               \
    template<class ARC>                                                        \
    void serialize (ARC& ar, const unsigned int) {                             \
      ar BOOST_PP_REPEAT_ ## Z(N, BSL_NODE_SERIALIZE, _);                      \
    }                                                                          \
                                                                               \
    BSL_NODE_DECL_CTOR(N)                                                      \
                                                                               \
    node (BOOST_PP_ENUM_BINARY_PARAMS_Z(Z, N, T, p)):                          \
      BOOST_PP_REPEAT_ ## Z(N, BSL_NODE_INIT_LIST, _) { }                      \
  };                                                                           \
  /**/

// instantiation macros

#define BSL_INST_BASE(Z, N, L)                                                 \
  T0 T0 ## _ ## N(BOOST_PP_ENUM_ ## Z(                                         \
    BSL_NODE_MAX, BSL_PRINT,                                                   \
    boost::archive::xml::random<BSL_TYPE> BOOST_PP_LPAREN() BOOST_PP_RPAREN()  \
  ));                                                                          \
  /**/

#define BSL_INST_yNODES(Z, N, L)                                               \
  BOOST_PP_COMMA_IF(N)                                                         \
  BOOST_PP_CAT(T,                                                              \
    BOOST_PP_CAT(BOOST_PP_LIST_AT(L, 1),                                       \
      BOOST_PP_CAT(_,                                                          \
        BOOST_PP_ADD(N,                                                        \
          BOOST_PP_LIST_AT(L, 0)                                               \
        )                                                                      \
      )                                                                        \
    )                                                                          \
  )                                                                            \
  /**/

#define BSL_INST_xNODES(Z, N, L)                                               \
  T ## L T ## L ## _ ## N(                                                     \
    BOOST_PP_REPEAT_ ## Z(                                                     \
      BSL_NODE_MAX, BSL_INST_yNODES,                                           \
      (BOOST_PP_MUL(N, BSL_NODE_MAX), (BOOST_PP_SUB(L, 1), BOOST_PP_NIL))      \
    )                                                                          \
  );                                                                           \
  /**/

#define BSL_INST_NODES(Z, N, L)                                                \
  BOOST_PP_REPEAT_ ## Z(                                                       \
    BSL_EXP(BSL_NODE_MAX, BOOST_PP_SUB(BSL_DEPTH, N)),                         \
    BSL_INST_xNODES, N                                                         \
  )                                                                            \
  /**/

#define BSL_TYPEDEF_NODES(Z, N, L)                                             \
  typedef boost::archive::xml::node<                                           \
    BOOST_PP_ENUM_ ## Z(                                                       \
      BSL_NODE_MAX, BSL_PRINT, BOOST_PP_CAT(T, BOOST_PP_SUB(N, 1))             \
    )                                                                          \
  > T ## N;                                                                    \
  /**/

// main macro

#define BSL_MAIN                                                               \
  int main (void) {                                                            \
    using namespace boost::archive;                                            \
    using namespace boost::archive::xml;                                       \
                                                                               \
    typedef node<BOOST_PP_ENUM(BSL_NODE_MAX, BSL_PRINT, BSL_TYPE)> T0;         \
                                                                               \
    BOOST_PP_REPEAT_FROM_TO(1, BSL_DEPTH, BSL_TYPEDEF_NODES, _)                \
                                                                               \
    typedef node<BOOST_PP_ENUM(                                                \
      BSL_NODE_MAX, BSL_PRINT,                                                 \
      BOOST_PP_CAT(T, BOOST_PP_SUB(BSL_DEPTH, 1))                              \
    )> HEAD;                                                                   \
                                                                               \
    result_set results;                                                        \
    std::size_t rounds = BSL_ROUNDS;                                           \
                                                                               \
    while (rounds --> 0) {                                                     \
      BOOST_PP_REPEAT(BSL_EXP(BSL_NODE_MAX, BSL_DEPTH), BSL_INST_BASE, _)      \
                                                                               \
      BOOST_PP_REPEAT_FROM_TO(1, BSL_DEPTH, BSL_INST_NODES, _)                 \
                                                                               \
      HEAD h(BOOST_PP_ENUM_PARAMS(                                             \
        BSL_NODE_MAX,                                                          \
        BOOST_PP_CAT(T, BOOST_PP_CAT(BOOST_PP_SUB(BSL_DEPTH, 1), _))           \
      ));                                                                      \
                                                                               \
      std::string fn = save_archive(h);                                        \
                                                                               \
      std::pair<double, HEAD> r = restore_archive<HEAD>(fn);                   \
                                                                               \
      std::cout << "round "                                                    \
                << ((BSL_ROUNDS - 1) - rounds)                                 \
                << " -> " << fn << "\n";                                       \
                                                                               \
      BOOST_PP_IF(BSL_SAVE_TMPFILE,                                            \
        BOOST_PP_EMPTY(),                                                      \
        std::remove(fn.c_str());                                               \
      )                                                                        \
                                                                               \
      results.entries.push_back(entry(                                         \
         BOOST_PP_STRINGIZE(BSL_TYPE),                                         \
         BSL_EXP(BSL_NODE_MAX, BSL_DEPTH), r.first                             \
      ));                                                                      \
    }                                                                          \
                                                                               \
    std::fstream fs(BSL_RESULTS_FILE, std::fstream::in);                       \
                                                                               \
    if (fs.good()) {                                                           \
      xml_iarchive ia(fs);                                                     \
      ia >> BOOST_SERIALIZATION_NVP(results);                                  \
      fs.close();                                                              \
    }                                                                          \
                                                                               \
    fs.open(BSL_RESULTS_FILE, std::fstream::out | std::fstream::trunc);        \
    xml_oarchive oa(fs);                                                       \
    oa << BOOST_SERIALIZATION_NVP(results);                                    \
                                                                               \
    fs.close();                                                                \
  }                                                                            \
  /**/

#endif // BOOST_SERIALIZATION_XML_PERFORMANCE_MACRO_HPP