adapt_adt_attributes.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. // Copyright (c) 2001-2011 Hartmut Kaiser
  2. //
  3. // Distributed under the Boost Software License, Version 1.0. (See accompanying
  4. // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  5. #if !defined(BOOST_SPIRIT_ADAPT_ADT_ATTRIBUTES_SEP_15_2010_1219PM)
  6. #define BOOST_SPIRIT_ADAPT_ADT_ATTRIBUTES_SEP_15_2010_1219PM
  7. #if defined(_MSC_VER)
  8. #pragma once
  9. #endif
  10. #include <boost/spirit/home/support/attributes.hpp>
  11. #include <boost/spirit/home/support/container.hpp>
  12. #include <boost/spirit/home/support/numeric_traits.hpp>
  13. #include <boost/fusion/include/adapt_adt.hpp>
  14. #include <boost/utility/enable_if.hpp>
  15. ///////////////////////////////////////////////////////////////////////////////
  16. // customization points allowing to use adapted classes with spirit
  17. namespace boost { namespace spirit { namespace traits
  18. {
  19. ///////////////////////////////////////////////////////////////////////////
  20. template <typename T, int N, bool Const, typename Domain>
  21. struct not_is_variant<
  22. fusion::extension::adt_attribute_proxy<T, N, Const>, Domain>
  23. : not_is_variant<
  24. typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
  25. , Domain>
  26. {};
  27. template <typename T, int N, bool Const, typename Domain>
  28. struct not_is_optional<
  29. fusion::extension::adt_attribute_proxy<T, N, Const>, Domain>
  30. : not_is_optional<
  31. typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
  32. , Domain>
  33. {};
  34. ///////////////////////////////////////////////////////////////////////////
  35. template <typename T, int N, bool Const>
  36. struct is_container<fusion::extension::adt_attribute_proxy<T, N, Const> >
  37. : is_container<
  38. typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
  39. >
  40. {};
  41. template <typename T, int N, bool Const>
  42. struct container_value<fusion::extension::adt_attribute_proxy<T, N, Const> >
  43. : container_value<
  44. typename remove_reference<
  45. typename fusion::extension::adt_attribute_proxy<
  46. T, N, Const
  47. >::type
  48. >::type
  49. >
  50. {};
  51. template <typename T, int N, bool Const>
  52. struct container_value<
  53. fusion::extension::adt_attribute_proxy<T, N, Const> const>
  54. : container_value<
  55. typename add_const<
  56. typename remove_reference<
  57. typename fusion::extension::adt_attribute_proxy<
  58. T, N, Const
  59. >::type
  60. >::type
  61. >::type
  62. >
  63. {};
  64. template <typename T, int N, typename Val>
  65. struct push_back_container<
  66. fusion::extension::adt_attribute_proxy<T, N, false>
  67. , Val
  68. , typename enable_if<is_reference<
  69. typename fusion::extension::adt_attribute_proxy<T, N, false>::type
  70. > >::type>
  71. {
  72. static bool call(
  73. fusion::extension::adt_attribute_proxy<T, N, false>& p
  74. , Val const& val)
  75. {
  76. typedef typename
  77. fusion::extension::adt_attribute_proxy<T, N, false>::type
  78. type;
  79. return push_back(type(p), val);
  80. }
  81. };
  82. template <typename T, int N, bool Const>
  83. struct container_iterator<
  84. fusion::extension::adt_attribute_proxy<T, N, Const> >
  85. : container_iterator<
  86. typename remove_reference<
  87. typename fusion::extension::adt_attribute_proxy<
  88. T, N, Const
  89. >::type
  90. >::type
  91. >
  92. {};
  93. template <typename T, int N, bool Const>
  94. struct container_iterator<
  95. fusion::extension::adt_attribute_proxy<T, N, Const> const>
  96. : container_iterator<
  97. typename add_const<
  98. typename remove_reference<
  99. typename fusion::extension::adt_attribute_proxy<
  100. T, N, Const
  101. >::type
  102. >::type
  103. >::type
  104. >
  105. {};
  106. template <typename T, int N, bool Const>
  107. struct begin_container<fusion::extension::adt_attribute_proxy<T, N, Const> >
  108. {
  109. typedef typename remove_reference<
  110. typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
  111. >::type container_type;
  112. static typename container_iterator<container_type>::type
  113. call(fusion::extension::adt_attribute_proxy<T, N, Const>& c)
  114. {
  115. return c.get().begin();
  116. }
  117. };
  118. template <typename T, int N, bool Const>
  119. struct begin_container<fusion::extension::adt_attribute_proxy<T, N, Const> const>
  120. {
  121. typedef typename add_const<
  122. typename remove_reference<
  123. typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
  124. >::type
  125. >::type container_type;
  126. static typename container_iterator<container_type>::type
  127. call(fusion::extension::adt_attribute_proxy<T, N, Const> const& c)
  128. {
  129. return c.get().begin();
  130. }
  131. };
  132. template <typename T, int N, bool Const>
  133. struct end_container<fusion::extension::adt_attribute_proxy<T, N, Const> >
  134. {
  135. typedef typename remove_reference<
  136. typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
  137. >::type container_type;
  138. static typename container_iterator<container_type>::type
  139. call(fusion::extension::adt_attribute_proxy<T, N, Const>& c)
  140. {
  141. return c.get().end();
  142. }
  143. };
  144. template <typename T, int N, bool Const>
  145. struct end_container<fusion::extension::adt_attribute_proxy<T, N, Const> const>
  146. {
  147. typedef typename add_const<
  148. typename remove_reference<
  149. typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
  150. >::type
  151. >::type container_type;
  152. static typename container_iterator<container_type>::type
  153. call(fusion::extension::adt_attribute_proxy<T, N, Const> const& c)
  154. {
  155. return c.get().end();
  156. }
  157. };
  158. ///////////////////////////////////////////////////////////////////////////
  159. template <typename T, int N, typename Val>
  160. struct assign_to_attribute_from_value<
  161. fusion::extension::adt_attribute_proxy<T, N, false>
  162. , Val>
  163. {
  164. static void
  165. call(Val const& val
  166. , fusion::extension::adt_attribute_proxy<T, N, false>& attr)
  167. {
  168. attr = val;
  169. }
  170. };
  171. template <typename T, int N, bool Const, typename Exposed>
  172. struct extract_from_attribute<
  173. fusion::extension::adt_attribute_proxy<T, N, Const>, Exposed>
  174. {
  175. typedef
  176. typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
  177. get_return_type;
  178. typedef typename remove_const<
  179. typename remove_reference<
  180. get_return_type
  181. >::type
  182. >::type embedded_type;
  183. typedef
  184. typename spirit::result_of::extract_from<Exposed, embedded_type>::type
  185. extracted_type;
  186. // If adt_attribute_proxy returned a value we must pass the attribute
  187. // by value, otherwise we will end up with a reference to a temporary
  188. // that will expire out of scope of the function call.
  189. typedef typename mpl::if_c<is_reference<get_return_type>::value
  190. , extracted_type
  191. , typename remove_reference<extracted_type>::type
  192. >::type type;
  193. template <typename Context>
  194. static type
  195. call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val, Context& ctx)
  196. {
  197. return extract_from<Exposed>(val.get(), ctx);
  198. }
  199. };
  200. ///////////////////////////////////////////////////////////////////////////
  201. template <typename T, int N, bool Const>
  202. struct attribute_type<fusion::extension::adt_attribute_proxy<T, N, Const> >
  203. : fusion::extension::adt_attribute_proxy<T, N, Const>
  204. {};
  205. ///////////////////////////////////////////////////////////////////////////
  206. template <typename T, int N, bool Const>
  207. struct optional_attribute<
  208. fusion::extension::adt_attribute_proxy<T, N, Const> >
  209. {
  210. typedef typename result_of::optional_value<
  211. typename remove_reference<
  212. typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
  213. >::type
  214. >::type type;
  215. static type
  216. call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
  217. {
  218. return optional_value(val.get());
  219. }
  220. static bool
  221. is_valid(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
  222. {
  223. return has_optional_value(val.get());
  224. }
  225. };
  226. ///////////////////////////////////////////////////////////////////////////
  227. template <typename T, int N, typename Attribute, typename Domain>
  228. struct transform_attribute<
  229. fusion::extension::adt_attribute_proxy<T, N, false>
  230. , Attribute
  231. , Domain
  232. , typename disable_if<is_reference<
  233. typename fusion::extension::adt_attribute_proxy<T, N, false>::type
  234. > >::type>
  235. {
  236. typedef Attribute type;
  237. static Attribute
  238. pre(fusion::extension::adt_attribute_proxy<T, N, false>& val)
  239. {
  240. return val;
  241. }
  242. static void
  243. post(
  244. fusion::extension::adt_attribute_proxy<T, N, false>& val
  245. , Attribute const& attr)
  246. {
  247. val = attr;
  248. }
  249. static void
  250. fail(fusion::extension::adt_attribute_proxy<T, N, false>&)
  251. {
  252. }
  253. };
  254. template <
  255. typename T, int N, bool Const, typename Attribute, typename Domain>
  256. struct transform_attribute<
  257. fusion::extension::adt_attribute_proxy<T, N, Const>
  258. , Attribute
  259. , Domain
  260. , typename enable_if<is_reference<
  261. typename fusion::extension::adt_attribute_proxy<
  262. T, N, Const
  263. >::type
  264. > >::type>
  265. {
  266. typedef Attribute& type;
  267. static Attribute&
  268. pre(fusion::extension::adt_attribute_proxy<T, N, Const>& val)
  269. {
  270. return val;
  271. }
  272. static void
  273. post(
  274. fusion::extension::adt_attribute_proxy<T, N, Const>&
  275. , Attribute const&)
  276. {
  277. }
  278. static void
  279. fail(fusion::extension::adt_attribute_proxy<T, N, Const>&)
  280. {
  281. }
  282. };
  283. template <typename T, int N, bool Const>
  284. struct clear_value<fusion::extension::adt_attribute_proxy<T, N, Const> >
  285. {
  286. static void call(
  287. fusion::extension::adt_attribute_proxy<T, N, Const>& val)
  288. {
  289. typedef typename
  290. fusion::extension::adt_attribute_proxy<T, N, Const>::type
  291. type;
  292. clear(type(val));
  293. }
  294. };
  295. template <typename T, int N, bool Const>
  296. struct attribute_size<fusion::extension::adt_attribute_proxy<T, N, Const> >
  297. {
  298. typedef typename remove_const<
  299. typename remove_reference<
  300. typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
  301. >::type
  302. >::type embedded_type;
  303. typedef typename attribute_size<embedded_type>::type type;
  304. static type
  305. call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
  306. {
  307. return attribute_size<embedded_type>::call(val.get());
  308. }
  309. };
  310. ///////////////////////////////////////////////////////////////////////////
  311. // customization point specializations for numeric generators
  312. template <typename T, int N, bool Const>
  313. struct absolute_value<fusion::extension::adt_attribute_proxy<T, N, Const> >
  314. {
  315. typedef typename
  316. fusion::extension::adt_attribute_proxy<T, N, Const>::type
  317. type;
  318. static type
  319. call (fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
  320. {
  321. return get_absolute_value(val.get());
  322. }
  323. };
  324. template <typename T, int N, bool Const>
  325. struct is_negative<fusion::extension::adt_attribute_proxy<T, N, Const> >
  326. {
  327. static bool
  328. call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
  329. {
  330. return test_negative(val.get());
  331. }
  332. };
  333. template <typename T, int N, bool Const>
  334. struct is_zero<fusion::extension::adt_attribute_proxy<T, N, Const> >
  335. {
  336. static bool
  337. call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
  338. {
  339. return test_zero(val.get());
  340. }
  341. };
  342. template <typename T, int N, bool Const>
  343. struct is_nan<fusion::extension::adt_attribute_proxy<T, N, Const> >
  344. {
  345. static bool
  346. call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
  347. {
  348. return test_nan(val.get());
  349. }
  350. };
  351. template <typename T, int N, bool Const>
  352. struct is_infinite<fusion::extension::adt_attribute_proxy<T, N, Const> >
  353. {
  354. static bool
  355. call(fusion::extension::adt_attribute_proxy<T, N, Const> const& val)
  356. {
  357. return test_infinite(val.get());
  358. }
  359. };
  360. }}}
  361. ///////////////////////////////////////////////////////////////////////////////
  362. namespace boost { namespace spirit { namespace result_of
  363. {
  364. template <typename T, int N, bool Const>
  365. struct optional_value<fusion::extension::adt_attribute_proxy<T, N, Const> >
  366. : result_of::optional_value<
  367. typename remove_const<
  368. typename remove_reference<
  369. typename fusion::extension::adt_attribute_proxy<T, N, Const>::type
  370. >::type
  371. >::type>
  372. {};
  373. }}}
  374. #endif