user_macros_snippets.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376
  1. // Copyright (C) 2016-2018 T. Zachary Laine
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See
  4. // accompanying file LICENSE_1_0.txt or copy at
  5. // http://www.boost.org/LICENSE_1_0.txt)
  6. #include <boost/yap/expression.hpp>
  7. #include <string>
  8. #include <vector>
  9. #define user_expr user_expr_1
  10. /// [USER_UNARY_OPERATOR]
  11. template <boost::yap::expr_kind Kind, typename Tuple>
  12. struct user_expr
  13. {
  14. static const boost::yap::expr_kind kind = Kind;
  15. Tuple elements;
  16. };
  17. // Operator overloads for operator!().
  18. BOOST_YAP_USER_UNARY_OPERATOR(logical_not, user_expr, user_expr)
  19. /// [USER_UNARY_OPERATOR]
  20. struct lazy_vector_1 :
  21. user_expr<
  22. boost::yap::expr_kind::terminal,
  23. boost::hana::tuple<std::vector<double>>
  24. >
  25. {};
  26. #undef user_expr
  27. #define user_expr user_expr_2
  28. /// [USER_BINARY_OPERATOR]
  29. template <boost::yap::expr_kind Kind, typename Tuple>
  30. struct user_expr
  31. {
  32. static const boost::yap::expr_kind kind = Kind;
  33. Tuple elements;
  34. };
  35. // Operator overloads for operator&&()
  36. BOOST_YAP_USER_BINARY_OPERATOR(logical_and, user_expr, user_expr)
  37. /// [USER_BINARY_OPERATOR]
  38. struct lazy_vector_2 :
  39. user_expr<
  40. boost::yap::expr_kind::terminal,
  41. boost::hana::tuple<std::vector<double>>
  42. >
  43. {};
  44. #undef user_expr
  45. #define user_expr user_expr_3
  46. /// [USER_CALL_OPERATOR]
  47. template <boost::yap::expr_kind Kind, typename Tuple>
  48. struct user_expr
  49. {
  50. static const boost::yap::expr_kind kind = Kind;
  51. Tuple elements;
  52. // Member operator overloads for operator()(). These will match any
  53. // number of parameters. Each one can be any type, even another
  54. // expression.
  55. BOOST_YAP_USER_CALL_OPERATOR(::user_expr)
  56. };
  57. /// [USER_CALL_OPERATOR]
  58. struct lazy_vector_3 :
  59. user_expr<
  60. boost::yap::expr_kind::terminal,
  61. boost::hana::tuple<std::vector<double>>
  62. >
  63. {};
  64. #undef user_expr
  65. #define user_expr user_expr_4
  66. /// [USER_SUBSCRIPT_OPERATOR]
  67. template <boost::yap::expr_kind Kind, typename Tuple>
  68. struct user_expr
  69. {
  70. static const boost::yap::expr_kind kind = Kind;
  71. Tuple elements;
  72. // Member operator overloads for operator[](). These will match any value
  73. // on the right-hand side, even another expression.
  74. BOOST_YAP_USER_SUBSCRIPT_OPERATOR(::user_expr)
  75. };
  76. /// [USER_SUBSCRIPT_OPERATOR]
  77. struct lazy_vector_4 :
  78. user_expr<
  79. boost::yap::expr_kind::terminal,
  80. boost::hana::tuple<std::vector<double>>
  81. >
  82. {};
  83. #undef user_expr
  84. #define user_expr user_expr_5
  85. /// [USER_EXPR_IF_ELSE]
  86. template <boost::yap::expr_kind Kind, typename Tuple>
  87. struct user_expr
  88. {
  89. static const boost::yap::expr_kind kind = Kind;
  90. Tuple elements;
  91. };
  92. // Defines an overload of if_else() that returns expressions instantiated from
  93. // user_expr.
  94. BOOST_YAP_USER_EXPR_IF_ELSE(::user_expr)
  95. /// [USER_EXPR_IF_ELSE]
  96. struct lazy_vector_5 :
  97. user_expr<
  98. boost::yap::expr_kind::terminal,
  99. boost::hana::tuple<std::vector<double>>
  100. >
  101. {};
  102. #undef user_expr
  103. #define user_expr user_expr_6
  104. #define is_vector is_vector_1
  105. /// [USER_UDT_ANY_IF_ELSE]
  106. template <boost::yap::expr_kind Kind, typename Tuple>
  107. struct user_expr
  108. {
  109. static const boost::yap::expr_kind kind = Kind;
  110. Tuple elements;
  111. };
  112. template <typename T>
  113. struct is_vector : std::false_type {};
  114. template <typename T, typename A>
  115. struct is_vector<std::vector<T, A>> : std::true_type {};
  116. // Defines an overload of if_else() that returns expressions instantiated from
  117. // user_expr.
  118. BOOST_YAP_USER_UDT_ANY_IF_ELSE(::user_expr, is_vector)
  119. /// [USER_UDT_ANY_IF_ELSE]
  120. struct lazy_vector_6 :
  121. user_expr<
  122. boost::yap::expr_kind::terminal,
  123. boost::hana::tuple<std::vector<double>>
  124. >
  125. {};
  126. #undef is_vector
  127. #undef user_expr
  128. #define user_expr user_expr_7
  129. #define is_vector is_vector_2
  130. /// [USER_UDT_UNARY_OPERATOR]
  131. template <boost::yap::expr_kind Kind, typename Tuple>
  132. struct user_expr
  133. {
  134. static const boost::yap::expr_kind kind = Kind;
  135. Tuple elements;
  136. };
  137. template <typename T>
  138. struct is_vector : std::false_type {};
  139. template <typename T, typename A>
  140. struct is_vector<std::vector<T, A>> : std::true_type {};
  141. // Defines an overload of operator!() that applies to vectors and returns
  142. // expressions instantiated from user_expr.
  143. BOOST_YAP_USER_UDT_UNARY_OPERATOR(logical_not, ::user_expr, is_vector)
  144. /// [USER_UDT_UNARY_OPERATOR]
  145. struct lazy_vector_7 :
  146. user_expr<
  147. boost::yap::expr_kind::terminal,
  148. boost::hana::tuple<std::vector<double>>
  149. >
  150. {};
  151. #undef is_vector
  152. #undef user_expr
  153. #define user_expr user_expr_8
  154. #define is_vector is_vector_3
  155. /// [USER_UDT_UDT_BINARY_OPERATOR]
  156. template <boost::yap::expr_kind Kind, typename Tuple>
  157. struct user_expr
  158. {
  159. static const boost::yap::expr_kind kind = Kind;
  160. Tuple elements;
  161. };
  162. template <typename T>
  163. struct is_vector : std::false_type {};
  164. template <typename T, typename A>
  165. struct is_vector<std::vector<T, A>> : std::true_type {};
  166. template <typename T>
  167. struct is_string : std::false_type {};
  168. template <>
  169. struct is_string<std::string> : std::true_type {};
  170. // Defines an overload of operator||() that applies to vectors on the left and
  171. // strings on the right, and returns expressions instantiated from user_expr.
  172. BOOST_YAP_USER_UDT_UDT_BINARY_OPERATOR(logical_or, ::user_expr, is_vector, is_string)
  173. // Defines an overload of operator&&() that applies to strings and returns
  174. // expressions instantiated from user_expr.
  175. BOOST_YAP_USER_UDT_UDT_BINARY_OPERATOR(logical_and, ::user_expr, is_string, is_string)
  176. /// [USER_UDT_UDT_BINARY_OPERATOR]
  177. struct lazy_vector_8 :
  178. user_expr<
  179. boost::yap::expr_kind::terminal,
  180. boost::hana::tuple<std::vector<double>>
  181. >
  182. {};
  183. #undef is_vector
  184. #undef user_expr
  185. #define user_expr user_expr_9
  186. #define is_vector is_vector_4
  187. /// [USER_UDT_ANY_BINARY_OPERATOR]
  188. template <boost::yap::expr_kind Kind, typename Tuple>
  189. struct user_expr
  190. {
  191. static const boost::yap::expr_kind kind = Kind;
  192. Tuple elements;
  193. };
  194. template <typename T>
  195. struct is_vector : std::false_type {};
  196. template <typename T, typename A>
  197. struct is_vector<std::vector<T, A>> : std::true_type {};
  198. // Defines an overload of operator&&() that matches a vector on either side,
  199. // and any type (possibly a vector) on the other.
  200. BOOST_YAP_USER_UDT_ANY_BINARY_OPERATOR(logical_and, ::user_expr, is_vector)
  201. /// [USER_UDT_ANY_BINARY_OPERATOR]
  202. struct lazy_vector_9 :
  203. user_expr<
  204. boost::yap::expr_kind::terminal,
  205. boost::hana::tuple<std::vector<double>>
  206. >
  207. {};
  208. #undef is_vector
  209. #undef user_expr
  210. #define user_expr user_expr_10
  211. /// [USER_LITERAL_PLACEHOLDER_OPERATOR]
  212. template <boost::yap::expr_kind Kind, typename Tuple>
  213. struct user_expr
  214. {
  215. static const boost::yap::expr_kind kind = Kind;
  216. Tuple elements;
  217. };
  218. namespace literals {
  219. // Defines a user literal operator that makes placeholders; 2_p will be a
  220. // 2-placeholder instantiated from the user_expr template.
  221. BOOST_YAP_USER_LITERAL_PLACEHOLDER_OPERATOR(user_expr)
  222. }
  223. /// [USER_LITERAL_PLACEHOLDER_OPERATOR]
  224. struct lazy_vector_10 :
  225. user_expr<
  226. boost::yap::expr_kind::terminal,
  227. boost::hana::tuple<std::vector<double>>
  228. >
  229. {};
  230. #undef user_expr
  231. #define user_expr user_expr_11
  232. /// [USER_ASSIGN_OPERATOR]
  233. template <boost::yap::expr_kind Kind, typename Tuple>
  234. struct user_expr
  235. {
  236. static const boost::yap::expr_kind kind = Kind;
  237. Tuple elements;
  238. // Member operator overloads for operator=(). These will match any value
  239. // on the right-hand side, even another expression, except that it will
  240. // not conflict with the asignment or move assignment operators.
  241. BOOST_YAP_USER_ASSIGN_OPERATOR(user_expr, ::user_expr)
  242. };
  243. /// [USER_ASSIGN_OPERATOR]
  244. struct lazy_vector_11 :
  245. user_expr<
  246. boost::yap::expr_kind::terminal,
  247. boost::hana::tuple<std::vector<double>>
  248. >
  249. {};
  250. #undef user_expr
  251. #undef user_expr
  252. #define user_expr user_expr_12
  253. /// [USER_CALL_OPERATOR]
  254. template <boost::yap::expr_kind Kind, typename Tuple>
  255. struct user_expr
  256. {
  257. static const boost::yap::expr_kind kind = Kind;
  258. Tuple elements;
  259. // Member operator overloads for operator()(). These will take exactly N
  260. // parameters. Each one can be any type, even another expression.
  261. BOOST_YAP_USER_CALL_OPERATOR(::user_expr)
  262. };
  263. /// [USER_CALL_OPERATOR]
  264. struct lazy_vector_12 :
  265. user_expr<
  266. boost::yap::expr_kind::terminal,
  267. boost::hana::tuple<std::vector<double>>
  268. >
  269. {};
  270. int main ()
  271. {
  272. lazy_vector_1 v1;
  273. lazy_vector_2 v2;
  274. lazy_vector_3 v3;
  275. lazy_vector_4 v4;
  276. lazy_vector_5 v5;
  277. lazy_vector_6 v6;
  278. lazy_vector_7 v7;
  279. lazy_vector_8 v8;
  280. lazy_vector_9 v9;
  281. lazy_vector_10 v10;
  282. lazy_vector_11 v11;
  283. lazy_vector_12 v12;
  284. return 0;
  285. }