symbols.hpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  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_KARMA_SYMBOLS_NOV_23_2009_1251PM)
  6. #define BOOST_SPIRIT_KARMA_SYMBOLS_NOV_23_2009_1251PM
  7. #include <boost/spirit/home/support/common_terminals.hpp>
  8. #include <boost/spirit/home/support/info.hpp>
  9. #include <boost/spirit/home/support/unused.hpp>
  10. #include <boost/spirit/home/support/attributes_fwd.hpp>
  11. #include <boost/spirit/home/support/detail/get_encoding.hpp>
  12. #include <boost/spirit/home/karma/detail/attributes.hpp>
  13. #include <boost/spirit/home/karma/detail/extract_from.hpp>
  14. #include <boost/spirit/home/karma/domain.hpp>
  15. #include <boost/spirit/home/karma/meta_compiler.hpp>
  16. #include <boost/spirit/home/karma/reference.hpp>
  17. #include <boost/spirit/home/karma/generate.hpp>
  18. #include <boost/spirit/home/karma/delimit_out.hpp>
  19. #include <boost/spirit/home/karma/detail/get_casetag.hpp>
  20. #include <boost/spirit/home/karma/detail/string_generate.hpp>
  21. #include <boost/config.hpp>
  22. #include <boost/shared_ptr.hpp>
  23. #include <boost/mpl/if.hpp>
  24. #include <map>
  25. #include <set>
  26. #if defined(BOOST_MSVC)
  27. # pragma warning(push)
  28. # pragma warning(disable: 4355) // 'this' : used in base member initializer list warning
  29. #endif
  30. ///////////////////////////////////////////////////////////////////////////////
  31. namespace boost { namespace spirit { namespace traits
  32. {
  33. template <typename T, typename Attribute, typename Enable>
  34. struct symbols_lookup
  35. {
  36. typedef
  37. mpl::eval_if<fusion::traits::is_sequence<T>
  38. , traits::detail::value_at_c<T, 0>
  39. , detail::add_const_ref<T> > sequence_type;
  40. typedef typename
  41. mpl::eval_if<traits::is_container<T>
  42. , traits::container_value<T>
  43. , sequence_type>::type type;
  44. // fusion sequence
  45. template <typename T_>
  46. static type call(T_ const& t, mpl::false_, mpl::true_)
  47. {
  48. return fusion::at_c<0>(t);
  49. }
  50. // container
  51. template <typename T_, typename IsSequence>
  52. static type call(T_ const& t, mpl::true_, IsSequence)
  53. {
  54. return t[0];
  55. }
  56. // not a container and not a fusion sequence
  57. template <typename T_>
  58. static type call(T_ const& t, mpl::false_, mpl::false_)
  59. {
  60. return t;
  61. }
  62. static type call(T const& t)
  63. {
  64. typedef typename traits::is_container<T>::type is_container;
  65. typedef typename fusion::traits::is_sequence<T>::type is_sequence;
  66. return call(t, is_container(), is_sequence());
  67. }
  68. };
  69. template <typename Attribute>
  70. struct symbols_lookup<Attribute, Attribute>
  71. {
  72. typedef Attribute const& type;
  73. static type call(Attribute const& t)
  74. {
  75. return t;
  76. }
  77. };
  78. template <typename Attribute, typename T, typename Enable>
  79. struct symbols_value
  80. {
  81. typedef
  82. mpl::eval_if<fusion::traits::is_sequence<T>
  83. , traits::detail::value_at_c<T, 1>
  84. , mpl::identity<unused_type> > sequence_type;
  85. typedef typename
  86. mpl::eval_if<traits::is_container<T>
  87. , traits::container_value<T>
  88. , sequence_type>::type type;
  89. // fusion sequence
  90. template <typename T_>
  91. static type call(T_ const& t, mpl::false_, mpl::true_)
  92. {
  93. return fusion::at_c<1>(t);
  94. }
  95. // container
  96. template <typename T_, typename IsSequence>
  97. static type call(T_ const& t, mpl::true_, IsSequence)
  98. {
  99. return t[1];
  100. }
  101. // not a container nor a fusion sequence
  102. template <typename T_>
  103. static type call(T_ const&, mpl::false_, mpl::false_)
  104. {
  105. return unused;
  106. }
  107. static type call(T const& t)
  108. {
  109. typedef typename traits::is_container<T>::type is_container;
  110. typedef typename fusion::traits::is_sequence<T>::type is_sequence;
  111. return call(t, is_container(), is_sequence());
  112. }
  113. };
  114. template <typename Attribute>
  115. struct symbols_value<Attribute, Attribute>
  116. {
  117. typedef unused_type type;
  118. static type call(Attribute const&)
  119. {
  120. return unused;
  121. }
  122. };
  123. }}}
  124. ///////////////////////////////////////////////////////////////////////////////
  125. namespace boost { namespace spirit { namespace karma
  126. {
  127. ///////////////////////////////////////////////////////////////////////////
  128. template <typename T, typename Attribute>
  129. struct symbols_lookup
  130. : mpl::if_<
  131. traits::not_is_unused<T>
  132. , std::map<Attribute, T>
  133. , std::set<Attribute>
  134. >
  135. {};
  136. ///////////////////////////////////////////////////////////////////////////
  137. namespace detail
  138. {
  139. ///////////////////////////////////////////////////////////////////////
  140. template <typename CharEncoding, typename Tag>
  141. struct generate_encoded
  142. {
  143. typedef typename
  144. proto::terminal<tag::char_code<Tag, CharEncoding> >::type
  145. encoding_type;
  146. template <typename OutputIterator, typename Expr, typename Attribute>
  147. static bool call(OutputIterator& sink, Expr const& expr
  148. , Attribute const& attr)
  149. {
  150. encoding_type const encoding = encoding_type();
  151. return karma::generate(sink, encoding[expr], attr);
  152. }
  153. };
  154. template <>
  155. struct generate_encoded<unused_type, unused_type>
  156. {
  157. template <typename OutputIterator, typename Expr, typename Attribute>
  158. static bool call(OutputIterator& sink, Expr const& expr
  159. , Attribute const& attr)
  160. {
  161. return karma::generate(sink, expr, attr);
  162. }
  163. };
  164. }
  165. template <
  166. typename Attribute = char, typename T = unused_type
  167. , typename Lookup = typename symbols_lookup<T, Attribute>::type
  168. , typename CharEncoding = unused_type, typename Tag = unused_type>
  169. struct symbols
  170. : proto::extends<
  171. typename proto::terminal<
  172. reference<symbols<Attribute, T, Lookup, CharEncoding, Tag> >
  173. >::type
  174. , symbols<Attribute, T, Lookup, CharEncoding, Tag> >
  175. , primitive_generator<
  176. symbols<Attribute, T, Lookup, CharEncoding, Tag> >
  177. {
  178. typedef T value_type; // the value associated with each entry
  179. typedef reference<symbols> reference_;
  180. typedef typename proto::terminal<reference_>::type terminal;
  181. typedef proto::extends<terminal, symbols> base_type;
  182. template <typename Context, typename Unused>
  183. struct attribute
  184. {
  185. typedef Attribute type;
  186. };
  187. symbols(std::string const& name = "symbols")
  188. : base_type(terminal::make(reference_(*this)))
  189. , add(*this)
  190. , remove(*this)
  191. , lookup(new Lookup())
  192. , name_(name)
  193. {}
  194. symbols(symbols const& syms)
  195. : base_type(terminal::make(reference_(*this)))
  196. , add(*this)
  197. , remove(*this)
  198. , lookup(syms.lookup)
  199. , name_(syms.name_)
  200. {}
  201. template <typename CharEncoding_, typename Tag_>
  202. symbols(symbols<Attribute, T, Lookup, CharEncoding_, Tag_> const& syms)
  203. : base_type(terminal::make(reference_(*this)))
  204. , add(*this)
  205. , remove(*this)
  206. , lookup(syms.lookup)
  207. , name_(syms.name_)
  208. {}
  209. template <typename Symbols, typename Data>
  210. symbols(Symbols const& syms, Data const& data
  211. , std::string const& name = "symbols")
  212. : base_type(terminal::make(reference_(*this)))
  213. , add(*this)
  214. , remove(*this)
  215. , lookup(new Lookup())
  216. , name_(name)
  217. {
  218. typename range_const_iterator<Symbols>::type si = boost::begin(syms);
  219. typename range_const_iterator<Data>::type di = boost::begin(data);
  220. while (si != boost::end(syms))
  221. add(*si++, *di++);
  222. }
  223. symbols&
  224. operator=(symbols const& rhs)
  225. {
  226. *lookup = *rhs.lookup;
  227. name_ = rhs.name_;
  228. return *this;
  229. }
  230. template <typename CharEncoding_, typename Tag_>
  231. symbols&
  232. operator=(symbols<Attribute, T, Lookup, CharEncoding_, Tag_> const& rhs)
  233. {
  234. *lookup = *rhs.lookup;
  235. name_ = rhs.name_;
  236. return *this;
  237. }
  238. void clear()
  239. {
  240. lookup->clear();
  241. }
  242. struct adder;
  243. struct remover;
  244. template <typename Attr, typename T_>
  245. adder const&
  246. operator=(std::pair<Attr, T_> const& p)
  247. {
  248. lookup->clear();
  249. return add(p.first, p.second);
  250. }
  251. template <typename Attr, typename T_>
  252. friend adder const&
  253. operator+= (symbols& sym, std::pair<Attr, T_> const& p)
  254. {
  255. return sym.add(p.first, p.second);
  256. }
  257. template <typename Attr>
  258. friend remover const&
  259. operator-= (symbols& sym, Attr const& attr)
  260. {
  261. return sym.remove(attr);
  262. }
  263. #if defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  264. // non-const version needed to suppress proto's += kicking in
  265. template <typename Attr, typename T_>
  266. friend adder const&
  267. operator+= (symbols& sym, std::pair<Attr, T_>& p)
  268. {
  269. return sym.add(p.first, p.second);
  270. }
  271. // non-const version needed to suppress proto's -= kicking in
  272. template <typename Attr>
  273. friend remover const&
  274. operator-= (symbols& sym, Attr& attr)
  275. {
  276. return sym.remove(attr);
  277. }
  278. #else
  279. // for rvalue references
  280. template <typename Attr, typename T_>
  281. friend adder const&
  282. operator+= (symbols& sym, std::pair<Attr, T_>&& p)
  283. {
  284. return sym.add(p.first, p.second);
  285. }
  286. // for rvalue references
  287. template <typename Attr>
  288. friend remover const&
  289. operator-= (symbols& sym, Attr&& attr)
  290. {
  291. return sym.remove(attr);
  292. }
  293. #endif
  294. template <typename F>
  295. void for_each(F f) const
  296. {
  297. std::for_each(lookup->begin(), lookup->end(), f);
  298. }
  299. template <typename Attr>
  300. value_type* find(Attr const& attr)
  301. {
  302. typename Lookup::iterator it = lookup->find(attr);
  303. return (it != lookup->end()) ? &(*it).second : 0;
  304. }
  305. template <typename Attr>
  306. value_type& at(Attr const& attr)
  307. {
  308. return (*lookup)[attr];
  309. }
  310. ///////////////////////////////////////////////////////////////////////
  311. template <typename OutputIterator, typename Context, typename Delimiter
  312. , typename Attr>
  313. bool generate(OutputIterator& sink, Context&, Delimiter const& d
  314. , Attr const& attr) const
  315. {
  316. typename Lookup::iterator it = lookup->find(
  317. traits::symbols_lookup<Attr, Attribute>::call(attr));
  318. if (it == lookup->end())
  319. return false;
  320. return karma::detail::generate_encoded<CharEncoding, Tag>::call(
  321. sink, (*it).second
  322. , traits::symbols_value<Attribute, Attr>::call(attr)) &&
  323. karma::delimit_out(sink, d);
  324. }
  325. template <typename Context>
  326. info what(Context&) const
  327. {
  328. return info(name_);
  329. }
  330. void name(std::string const &str)
  331. {
  332. name_ = str;
  333. }
  334. std::string const &name() const
  335. {
  336. return name_;
  337. }
  338. ///////////////////////////////////////////////////////////////////////
  339. struct adder
  340. {
  341. template <typename, typename = unused_type>
  342. struct result { typedef adder const& type; };
  343. adder(symbols& sym)
  344. : sym(sym)
  345. {
  346. }
  347. template <typename Attr>
  348. adder const&
  349. operator()(Attr const& attr, T const& val = T()) const
  350. {
  351. sym.lookup->insert(typename Lookup::value_type(attr, val));
  352. return *this;
  353. }
  354. template <typename Attr>
  355. adder const&
  356. operator, (Attr const& attr) const
  357. {
  358. sym.lookup->insert(typename Lookup::value_type(attr, T()));
  359. return *this;
  360. }
  361. symbols& sym;
  362. private:
  363. // silence MSVC warning C4512: assignment operator could not be generated
  364. adder& operator= (adder const&);
  365. };
  366. struct remover
  367. {
  368. template <typename>
  369. struct result { typedef remover const& type; };
  370. remover(symbols& sym)
  371. : sym(sym)
  372. {
  373. }
  374. template <typename Attr>
  375. remover const&
  376. operator()(Attr const& attr) const
  377. {
  378. sym.lookup->erase(attr);
  379. return *this;
  380. }
  381. template <typename Attr>
  382. remover const&
  383. operator, (Attr const& attr) const
  384. {
  385. sym.lookup->erase(attr);
  386. return *this;
  387. }
  388. symbols& sym;
  389. private:
  390. // silence MSVC warning C4512: assignment operator could not be generated
  391. remover& operator= (remover const&);
  392. };
  393. adder add;
  394. remover remove;
  395. shared_ptr<Lookup> lookup;
  396. std::string name_;
  397. };
  398. ///////////////////////////////////////////////////////////////////////////
  399. // specialization for unused stored type
  400. template <
  401. typename Attribute, typename Lookup
  402. , typename CharEncoding, typename Tag>
  403. struct symbols<Attribute, unused_type, Lookup, CharEncoding, Tag>
  404. : proto::extends<
  405. typename proto::terminal<
  406. spirit::karma::reference<
  407. symbols<Attribute, unused_type, Lookup, CharEncoding, Tag> >
  408. >::type
  409. , symbols<Attribute, unused_type, Lookup, CharEncoding, Tag>
  410. >
  411. , spirit::karma::generator<
  412. symbols<Attribute, unused_type, Lookup, CharEncoding, Tag> >
  413. {
  414. typedef unused_type value_type; // the value associated with each entry
  415. typedef spirit::karma::reference<symbols> reference_;
  416. typedef typename proto::terminal<reference_>::type terminal;
  417. typedef proto::extends<terminal, symbols> base_type;
  418. template <typename Context, typename Unused>
  419. struct attribute
  420. {
  421. typedef Attribute type;
  422. };
  423. symbols(std::string const& name = "symbols")
  424. : base_type(terminal::make(reference_(*this)))
  425. , add(*this)
  426. , remove(*this)
  427. , lookup(new Lookup())
  428. , name_(name)
  429. {}
  430. symbols(symbols const& syms)
  431. : base_type(terminal::make(reference_(*this)))
  432. , add(*this)
  433. , remove(*this)
  434. , lookup(syms.lookup)
  435. , name_(syms.name_)
  436. {}
  437. template <typename CharEncoding_, typename Tag_>
  438. symbols(symbols<Attribute, unused_type, Lookup, CharEncoding_, Tag_> const& syms)
  439. : base_type(terminal::make(reference_(*this)))
  440. , add(*this)
  441. , remove(*this)
  442. , lookup(syms.lookup)
  443. , name_(syms.name_)
  444. {}
  445. template <typename Symbols, typename Data>
  446. symbols(Symbols const& syms, Data const& data
  447. , std::string const& name = "symbols")
  448. : base_type(terminal::make(reference_(*this)))
  449. , add(*this)
  450. , remove(*this)
  451. , lookup(new Lookup())
  452. , name_(name)
  453. {
  454. typename range_const_iterator<Symbols>::type si = boost::begin(syms);
  455. typename range_const_iterator<Data>::type di = boost::begin(data);
  456. while (si != boost::end(syms))
  457. add(*si++, *di++);
  458. }
  459. symbols&
  460. operator=(symbols const& rhs)
  461. {
  462. *lookup = *rhs.lookup;
  463. name_ = rhs.name_;
  464. return *this;
  465. }
  466. template <typename CharEncoding_, typename Tag_>
  467. symbols&
  468. operator=(symbols<Attribute, unused_type, Lookup, CharEncoding_, Tag_> const& rhs)
  469. {
  470. *lookup = *rhs.lookup;
  471. name_ = rhs.name_;
  472. return *this;
  473. }
  474. void clear()
  475. {
  476. lookup->clear();
  477. }
  478. struct adder;
  479. struct remover;
  480. template <typename Attr>
  481. adder const&
  482. operator=(Attr const& attr)
  483. {
  484. lookup->clear();
  485. return add(attr);
  486. }
  487. template <typename Attr>
  488. friend adder const&
  489. operator+= (symbols& sym, Attr const& attr)
  490. {
  491. return sym.add(attr);
  492. }
  493. template <typename Attr>
  494. friend remover const&
  495. operator-= (symbols& sym, Attr const& attr)
  496. {
  497. return sym.remove(attr);
  498. }
  499. // non-const version needed to suppress proto's += kicking in
  500. template <typename Attr>
  501. friend adder const&
  502. operator+= (symbols& sym, Attr& attr)
  503. {
  504. return sym.add(attr);
  505. }
  506. // non-const version needed to suppress proto's -= kicking in
  507. template <typename Attr>
  508. friend remover const&
  509. operator-= (symbols& sym, Attr& attr)
  510. {
  511. return sym.remove(attr);
  512. }
  513. template <typename F>
  514. void for_each(F f) const
  515. {
  516. std::for_each(lookup->begin(), lookup->end(), f);
  517. }
  518. template <typename Attr>
  519. value_type const* find(Attr const& attr)
  520. {
  521. typename Lookup::iterator it = lookup->find(attr);
  522. return (it != lookup->end()) ? &unused : 0;
  523. }
  524. template <typename Attr>
  525. value_type at(Attr const& attr)
  526. {
  527. typename Lookup::iterator it = lookup->find(attr);
  528. if (it == lookup->end())
  529. add(attr);
  530. return unused;
  531. }
  532. ///////////////////////////////////////////////////////////////////////
  533. template <typename OutputIterator, typename Context, typename Delimiter
  534. , typename Attr>
  535. bool generate(OutputIterator& sink, Context&, Delimiter const& d
  536. , Attr const& attr) const
  537. {
  538. typename Lookup::iterator it = lookup->find(
  539. traits::symbols_lookup<Attr, Attribute>::call(attr));
  540. if (it == lookup->end())
  541. return false;
  542. return karma::detail::generate_encoded<CharEncoding, Tag>::
  543. call(sink
  544. , traits::symbols_lookup<Attr, Attribute>::call(attr)
  545. , unused) &&
  546. karma::delimit_out(sink, d);
  547. }
  548. template <typename Context>
  549. info what(Context&) const
  550. {
  551. return info(name_);
  552. }
  553. void name(std::string const &str)
  554. {
  555. name_ = str;
  556. }
  557. std::string const &name() const
  558. {
  559. return name_;
  560. }
  561. ///////////////////////////////////////////////////////////////////////
  562. struct adder
  563. {
  564. template <typename, typename = unused_type>
  565. struct result { typedef adder const& type; };
  566. adder(symbols& sym)
  567. : sym(sym)
  568. {
  569. }
  570. template <typename Attr>
  571. adder const&
  572. operator()(Attr const& attr) const
  573. {
  574. sym.lookup->insert(attr);
  575. return *this;
  576. }
  577. template <typename Attr>
  578. adder const&
  579. operator, (Attr const& attr) const
  580. {
  581. sym.lookup->insert(attr);
  582. return *this;
  583. }
  584. symbols& sym;
  585. private:
  586. // silence MSVC warning C4512: assignment operator could not be generated
  587. adder& operator= (adder const&);
  588. };
  589. struct remover
  590. {
  591. template <typename>
  592. struct result { typedef remover const& type; };
  593. remover(symbols& sym)
  594. : sym(sym)
  595. {
  596. }
  597. template <typename Attr>
  598. remover const&
  599. operator()(Attr const& attr) const
  600. {
  601. sym.lookup->erase(attr);
  602. return *this;
  603. }
  604. template <typename Attr>
  605. remover const&
  606. operator, (Attr const& attr) const
  607. {
  608. sym.lookup->erase(attr);
  609. return *this;
  610. }
  611. symbols& sym;
  612. private:
  613. // silence MSVC warning C4512: assignment operator could not be generated
  614. remover& operator= (remover const&);
  615. };
  616. adder add;
  617. remover remove;
  618. shared_ptr<Lookup> lookup;
  619. std::string name_;
  620. };
  621. ///////////////////////////////////////////////////////////////////////////
  622. // Generator generators: make_xxx function (objects)
  623. ///////////////////////////////////////////////////////////////////////////
  624. template <typename Attribute, typename T, typename Lookup
  625. , typename CharEnconding, typename Tag, typename Modifiers>
  626. struct make_primitive<
  627. reference<symbols<Attribute, T, Lookup, CharEnconding, Tag> >
  628. , Modifiers>
  629. {
  630. static bool const lower =
  631. has_modifier<Modifiers, tag::char_code_base<tag::lower> >::value;
  632. static bool const upper =
  633. has_modifier<Modifiers, tag::char_code_base<tag::upper> >::value;
  634. typedef reference<
  635. symbols<Attribute, T, Lookup, CharEnconding, Tag>
  636. > reference_;
  637. typedef typename mpl::if_c<
  638. lower || upper
  639. , symbols<
  640. Attribute, T, Lookup
  641. , typename spirit::detail::get_encoding_with_case<
  642. Modifiers, unused_type, lower || upper>::type
  643. , typename detail::get_casetag<Modifiers, lower || upper>::type>
  644. , reference_>::type
  645. result_type;
  646. result_type operator()(reference_ ref, unused_type) const
  647. {
  648. return result_type(ref.ref.get());
  649. }
  650. };
  651. }}}
  652. namespace boost { namespace spirit { namespace traits
  653. {
  654. ///////////////////////////////////////////////////////////////////////////
  655. template <typename Attribute, typename T, typename Lookup
  656. , typename CharEncoding, typename Tag
  657. , typename Attr, typename Context, typename Iterator>
  658. struct handles_container<karma::symbols<Attribute, T, Lookup, CharEncoding, Tag>
  659. , Attr, Context, Iterator>
  660. : traits::is_container<Attr> {};
  661. }}}
  662. #if defined(BOOST_MSVC)
  663. # pragma warning(pop)
  664. #endif
  665. #endif