bootstrap_stl.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792
  1. // This file is distributed under the BSD License.
  2. // See "license.txt" for details.
  3. // Copyright 2009-2012, Jonathan Turner ([email protected])
  4. // Copyright 2009-2017, Jason Turner ([email protected])
  5. // http://www.chaiscript.com
  6. // This is an open source non-commercial project. Dear PVS-Studio, please check it.
  7. // PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
  8. /// \file
  9. /// This file contains utility functions for registration of STL container
  10. /// classes. The methodology used is based on the SGI STL concepts.
  11. /// http://www.sgi.com/tech/stl/table_of_contents.html
  12. #ifndef CHAISCRIPT_BOOTSTRAP_STL_HPP_
  13. #define CHAISCRIPT_BOOTSTRAP_STL_HPP_
  14. #include <functional>
  15. #include <memory>
  16. #include <stdexcept>
  17. #include <typeinfo>
  18. #include <vector>
  19. #include <algorithm>
  20. #include "bootstrap.hpp"
  21. #include "boxed_value.hpp"
  22. #include "dispatchkit.hpp"
  23. #include "operators.hpp"
  24. #include "proxy_constructors.hpp"
  25. #include "register_function.hpp"
  26. #include "type_info.hpp"
  27. namespace chaiscript
  28. {
  29. namespace bootstrap
  30. {
  31. namespace standard_library
  32. {
  33. /// Bidir_Range, based on the D concept of ranges.
  34. /// \todo Update the Range code to base its capabilities on
  35. /// the user_typetraits of the iterator passed in
  36. template<typename Container, typename IterType>
  37. struct Bidir_Range
  38. {
  39. typedef Container container_type;
  40. Bidir_Range(Container &c)
  41. : m_begin(c.begin()), m_end(c.end())
  42. {
  43. }
  44. bool empty() const
  45. {
  46. return m_begin == m_end;
  47. }
  48. void pop_front()
  49. {
  50. if (empty())
  51. {
  52. throw std::range_error("Range empty");
  53. }
  54. ++m_begin;
  55. }
  56. void pop_back()
  57. {
  58. if (empty())
  59. {
  60. throw std::range_error("Range empty");
  61. }
  62. --m_end;
  63. }
  64. decltype(auto) front() const
  65. {
  66. if (empty())
  67. {
  68. throw std::range_error("Range empty");
  69. }
  70. return (*m_begin);
  71. }
  72. decltype(auto) back() const
  73. {
  74. if (empty())
  75. {
  76. throw std::range_error("Range empty");
  77. }
  78. auto pos = m_end;
  79. --pos;
  80. return (*(pos));
  81. }
  82. IterType m_begin;
  83. IterType m_end;
  84. };
  85. namespace detail {
  86. template<typename T>
  87. size_t count(const T &t_target, const typename T::key_type &t_key)
  88. {
  89. return t_target.count(t_key);
  90. }
  91. template<typename T>
  92. void insert(T &t_target, const T &t_other)
  93. {
  94. t_target.insert(t_other.begin(), t_other.end());
  95. }
  96. template<typename T>
  97. void insert_ref(T &t_target, const typename T::value_type &t_val)
  98. {
  99. t_target.insert(t_val);
  100. }
  101. /// Add Bidir_Range support for the given ContainerType
  102. template<typename Bidir_Type>
  103. void input_range_type_impl(const std::string &type, Module& m)
  104. {
  105. m.add(user_type<Bidir_Type>(), type + "_Range");
  106. copy_constructor<Bidir_Type>(type + "_Range", m);
  107. m.add(constructor<Bidir_Type (typename Bidir_Type::container_type &)>(), "range_internal");
  108. m.add(fun(&Bidir_Type::empty), "empty");
  109. m.add(fun(&Bidir_Type::pop_front), "pop_front");
  110. m.add(fun(&Bidir_Type::front), "front");
  111. m.add(fun(&Bidir_Type::pop_back), "pop_back");
  112. m.add(fun(&Bidir_Type::back), "back");
  113. }
  114. /// Algorithm for inserting at a specific position into a container
  115. template<typename Type>
  116. void insert_at(Type &container, int pos, const typename Type::value_type &v)
  117. {
  118. auto itr = container.begin();
  119. auto end = container.end();
  120. if (pos < 0 || std::distance(itr, end) < pos)
  121. {
  122. throw std::range_error("Cannot insert past end of range");
  123. }
  124. std::advance(itr, pos);
  125. container.insert(itr, v);
  126. }
  127. /// Algorithm for erasing a specific position from a container
  128. template<typename Type>
  129. void erase_at(Type &container, int pos)
  130. {
  131. auto itr = container.begin();
  132. auto end = container.end();
  133. if (pos < 0 || std::distance(itr, end) < (pos-1))
  134. {
  135. throw std::range_error("Cannot erase past end of range");
  136. }
  137. std::advance(itr, pos);
  138. container.erase(itr);
  139. }
  140. }
  141. template<typename ContainerType>
  142. void input_range_type(const std::string &type, Module& m)
  143. {
  144. detail::input_range_type_impl<Bidir_Range<ContainerType, typename ContainerType::iterator> >(type,m);
  145. detail::input_range_type_impl<Bidir_Range<const ContainerType, typename ContainerType::const_iterator> >("Const_" + type,m);
  146. }
  147. template<typename ContainerType>
  148. ModulePtr input_range_type(const std::string &type)
  149. {
  150. auto m = std::make_shared<Module>();
  151. input_range_type<ContainerType>(type, *m);
  152. return m;
  153. }
  154. /// Add random_access_container concept to the given ContainerType
  155. /// http://www.sgi.com/tech/stl/RandomAccessContainer.html
  156. template<typename ContainerType>
  157. void random_access_container_type(const std::string &/*type*/, Module& m)
  158. {
  159. //In the interest of runtime safety for the m, we prefer the at() method for [] access,
  160. //to throw an exception in an out of bounds condition.
  161. m.add(
  162. fun(
  163. [](ContainerType &c, int index) -> typename ContainerType::reference {
  164. /// \todo we are prefering to keep the key as 'int' to avoid runtime conversions
  165. /// during dispatch. reevaluate
  166. return c.at(static_cast<typename ContainerType::size_type>(index));
  167. }), "[]");
  168. m.add(
  169. fun(
  170. [](const ContainerType &c, int index) -> typename ContainerType::const_reference {
  171. /// \todo we are prefering to keep the key as 'int' to avoid runtime conversions
  172. /// during dispatch. reevaluate
  173. return c.at(static_cast<typename ContainerType::size_type>(index));
  174. }), "[]");
  175. }
  176. template<typename ContainerType>
  177. ModulePtr random_access_container_type(const std::string &type)
  178. {
  179. auto m = std::make_shared<Module>();
  180. random_access_container_type<ContainerType>(type, *m);
  181. return m;
  182. }
  183. /// Add assignable concept to the given ContainerType
  184. /// http://www.sgi.com/tech/stl/Assignable.html
  185. template<typename ContainerType>
  186. void assignable_type(const std::string &type, Module& m)
  187. {
  188. copy_constructor<ContainerType>(type, m);
  189. operators::assign<ContainerType>(m);
  190. }
  191. template<typename ContainerType>
  192. ModulePtr assignable_type(const std::string &type)
  193. {
  194. auto m = std::make_shared<Module>();
  195. assignable_type<ContainerType>(type, *m);
  196. return m;
  197. }
  198. /// Add container resize concept to the given ContainerType
  199. /// http://www.cplusplus.com/reference/stl/
  200. template<typename ContainerType>
  201. void resizable_type(const std::string &/*type*/, Module& m)
  202. {
  203. m.add(fun([](ContainerType *a, typename ContainerType::size_type n, const typename ContainerType::value_type& val) { return a->resize(n, val); } ), "resize");
  204. m.add(fun([](ContainerType *a, typename ContainerType::size_type n) { return a->resize(n); } ), "resize");
  205. }
  206. template<typename ContainerType>
  207. ModulePtr resizable_type(const std::string &type)
  208. {
  209. auto m = std::make_shared<Module>();
  210. resizable_type<ContainerType>(type, *m);
  211. return m;
  212. }
  213. /// Add container reserve concept to the given ContainerType
  214. /// http://www.cplusplus.com/reference/stl/
  215. template<typename ContainerType>
  216. void reservable_type(const std::string &/*type*/, Module& m)
  217. {
  218. m.add(fun([](ContainerType *a, typename ContainerType::size_type n) { return a->reserve(n); } ), "reserve");
  219. m.add(fun([](const ContainerType *a) { return a->capacity(); } ), "capacity");
  220. }
  221. template<typename ContainerType>
  222. ModulePtr reservable_type(const std::string &type)
  223. {
  224. auto m = std::make_shared<Module>();
  225. reservable_type<ContainerType>(type, *m);
  226. return m;
  227. }
  228. /// Add container concept to the given ContainerType
  229. /// http://www.sgi.com/tech/stl/Container.html
  230. template<typename ContainerType>
  231. void container_type(const std::string &/*type*/, Module& m)
  232. {
  233. m.add(fun([](const ContainerType *a) { return a->size(); } ), "size");
  234. m.add(fun([](const ContainerType *a) { return a->empty(); } ), "empty");
  235. m.add(fun([](ContainerType *a) { a->clear(); } ), "clear");
  236. }
  237. template <typename ContainerType>
  238. ModulePtr container_type(const std::string& type)
  239. {
  240. auto m = std::make_shared<Module>();
  241. container_type<ContainerType>(type, *m);
  242. return m;
  243. }
  244. /// Add default constructable concept to the given Type
  245. /// http://www.sgi.com/tech/stl/DefaultConstructible.html
  246. template<typename Type>
  247. void default_constructible_type(const std::string &type, Module& m)
  248. {
  249. m.add(constructor<Type ()>(), type);
  250. }
  251. template <typename Type>
  252. ModulePtr default_constructible_type(const std::string& type)
  253. {
  254. auto m = std::make_shared<Module>();
  255. default_constructible_type<Type>(type, *m);
  256. return m;
  257. }
  258. /// Add sequence concept to the given ContainerType
  259. /// http://www.sgi.com/tech/stl/Sequence.html
  260. template<typename ContainerType>
  261. void sequence_type(const std::string &/*type*/, Module& m)
  262. {
  263. m.add(fun(&detail::insert_at<ContainerType>),
  264. []()->std::string{
  265. if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
  266. return "insert_ref_at";
  267. } else {
  268. return "insert_at";
  269. }
  270. }());
  271. m.add(fun(&detail::erase_at<ContainerType>), "erase_at");
  272. }
  273. template <typename ContainerType>
  274. ModulePtr sequence_type(const std::string &type)
  275. {
  276. auto m = std::make_shared<Module>();
  277. sequence_type<ContainerType>(type, *m);
  278. return m;
  279. }
  280. /// Add back insertion sequence concept to the given ContainerType
  281. /// http://www.sgi.com/tech/stl/BackInsertionSequence.html
  282. template<typename ContainerType>
  283. void back_insertion_sequence_type(const std::string &type, Module& m)
  284. {
  285. m.add(fun([](ContainerType &container)->decltype(auto){
  286. if (container.empty()) {
  287. throw std::range_error("Container empty");
  288. } else {
  289. return (container.back());
  290. }
  291. }
  292. )
  293. , "back");
  294. m.add(fun([](const ContainerType &container)->decltype(auto){
  295. if (container.empty()) {
  296. throw std::range_error("Container empty");
  297. } else {
  298. return (container.back());
  299. }
  300. }
  301. )
  302. , "back");
  303. typedef void (ContainerType::*push_back)(const typename ContainerType::value_type &);
  304. m.add(fun(static_cast<push_back>(&ContainerType::push_back)),
  305. [&]()->std::string{
  306. if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
  307. m.eval(
  308. "# Pushes the second value onto the container while making a clone of the value\n"
  309. "def push_back(" + type + " container, x)\n"
  310. "{ \n"
  311. " if (x.is_var_return_value()) {\n"
  312. " x.reset_var_return_value() \n"
  313. " container.push_back_ref(x) \n"
  314. " } else { \n"
  315. " container.push_back_ref(clone(x)); \n"
  316. " }\n"
  317. "} \n"
  318. );
  319. return "push_back_ref";
  320. } else {
  321. return "push_back";
  322. }
  323. }());
  324. m.add(fun(&ContainerType::pop_back), "pop_back");
  325. }
  326. template<typename ContainerType>
  327. ModulePtr back_insertion_sequence_type(const std::string &type)
  328. {
  329. auto m = std::make_shared<Module>();
  330. back_insertion_sequence_type<ContainerType>(type, *m);
  331. return m;
  332. }
  333. /// Front insertion sequence
  334. /// http://www.sgi.com/tech/stl/FrontInsertionSequence.html
  335. template<typename ContainerType>
  336. void front_insertion_sequence_type(const std::string &type, Module& m)
  337. {
  338. typedef void (ContainerType::*push_ptr)(typename ContainerType::const_reference);
  339. typedef void (ContainerType::*pop_ptr)();
  340. m.add(fun([](ContainerType &container)->decltype(auto){
  341. if (container.empty()) {
  342. throw std::range_error("Container empty");
  343. } else {
  344. return (container.front());
  345. }
  346. }
  347. )
  348. , "front");
  349. m.add(fun([](const ContainerType &container)->decltype(auto){
  350. if (container.empty()) {
  351. throw std::range_error("Container empty");
  352. } else {
  353. return (container.front());
  354. }
  355. }
  356. )
  357. , "front");
  358. m.add(fun(static_cast<push_ptr>(&ContainerType::push_front)),
  359. [&]()->std::string{
  360. if (typeid(typename ContainerType::value_type) == typeid(Boxed_Value)) {
  361. m.eval(
  362. "# Pushes the second value onto the front of container while making a clone of the value\n"
  363. "def push_front(" + type + " container, x)\n"
  364. "{ \n"
  365. " if (x.is_var_return_value()) {\n"
  366. " x.reset_var_return_value() \n"
  367. " container.push_front_ref(x) \n"
  368. " } else { \n"
  369. " container.push_front_ref(clone(x)); \n"
  370. " }\n"
  371. "} \n"
  372. );
  373. return "push_front_ref";
  374. } else {
  375. return "push_front";
  376. }
  377. }());
  378. m.add(fun(static_cast<pop_ptr>(&ContainerType::pop_front)), "pop_front");
  379. }
  380. template<typename ContainerType>
  381. ModulePtr front_insertion_sequence_type(const std::string &type)
  382. {
  383. auto m = std::make_shared<Module>();
  384. front_insertion_sequence_type<ContainerType>(type, *m);
  385. return m;
  386. }
  387. /// bootstrap a given PairType
  388. /// http://www.sgi.com/tech/stl/pair.html
  389. template<typename PairType>
  390. void pair_type(const std::string &type, Module& m)
  391. {
  392. m.add(user_type<PairType>(), type);
  393. m.add(fun(&PairType::first), "first");
  394. m.add(fun(&PairType::second), "second");
  395. basic_constructors<PairType>(type, m);
  396. m.add(constructor<PairType (const typename PairType::first_type &, const typename PairType::second_type &)>(), type);
  397. }
  398. template<typename PairType>
  399. ModulePtr pair_type(const std::string &type)
  400. {
  401. auto m = std::make_shared<Module>();
  402. pair_type<PairType>(type, *m);
  403. return m;
  404. }
  405. /// Add pair associative container concept to the given ContainerType
  406. /// http://www.sgi.com/tech/stl/PairAssociativeContainer.html
  407. template<typename ContainerType>
  408. void pair_associative_container_type(const std::string &type, Module& m)
  409. {
  410. pair_type<typename ContainerType::value_type>(type + "_Pair", m);
  411. }
  412. template<typename ContainerType>
  413. ModulePtr pair_associative_container_type(const std::string &type)
  414. {
  415. auto m = std::make_shared<Module>();
  416. pair_associative_container_type<ContainerType>(type, *m);
  417. return m;
  418. }
  419. /// Add unique associative container concept to the given ContainerType
  420. /// http://www.sgi.com/tech/stl/UniqueAssociativeContainer.html
  421. template<typename ContainerType>
  422. void unique_associative_container_type(const std::string &/*type*/, Module& m)
  423. {
  424. m.add(fun(detail::count<ContainerType>), "count");
  425. typedef size_t (ContainerType::*erase_ptr)(const typename ContainerType::key_type &);
  426. m.add(fun(static_cast<erase_ptr>(&ContainerType::erase)), "erase");
  427. m.add(fun(&detail::insert<ContainerType>), "insert");
  428. m.add(fun(&detail::insert_ref<ContainerType>),
  429. []()->std::string{
  430. if (typeid(typename ContainerType::mapped_type) == typeid(Boxed_Value)) {
  431. return "insert_ref";
  432. } else {
  433. return "insert";
  434. }
  435. }());
  436. }
  437. template<typename ContainerType>
  438. ModulePtr unique_associative_container_type(const std::string &type)
  439. {
  440. auto m = std::make_shared<Module>();
  441. unique_associative_container_type<ContainerType>(type, *m);
  442. return m;
  443. }
  444. /// Add a MapType container
  445. /// http://www.sgi.com/tech/stl/Map.html
  446. template<typename MapType>
  447. void map_type(const std::string &type, Module& m)
  448. {
  449. m.add(user_type<MapType>(), type);
  450. typedef typename MapType::mapped_type &(MapType::*elem_access)(const typename MapType::key_type &);
  451. typedef const typename MapType::mapped_type &(MapType::*const_elem_access)(const typename MapType::key_type &) const;
  452. m.add(fun(static_cast<elem_access>(&MapType::operator[])), "[]");
  453. m.add(fun(static_cast<elem_access>(&MapType::at)), "at");
  454. m.add(fun(static_cast<const_elem_access>(&MapType::at)), "at");
  455. if (typeid(MapType) == typeid(std::map<std::string, Boxed_Value>))
  456. {
  457. m.eval(R"(
  458. def Map::`==`(Map rhs) {
  459. if ( rhs.size() != this.size() ) {
  460. return false;
  461. } else {
  462. auto r1 = range(this);
  463. auto r2 = range(rhs);
  464. while (!r1.empty())
  465. {
  466. if (!eq(r1.front().first, r2.front().first) || !eq(r1.front().second, r2.front().second))
  467. {
  468. return false;
  469. }
  470. r1.pop_front();
  471. r2.pop_front();
  472. }
  473. true;
  474. }
  475. } )"
  476. );
  477. }
  478. container_type<MapType>(type, m);
  479. default_constructible_type<MapType>(type, m);
  480. assignable_type<MapType>(type, m);
  481. unique_associative_container_type<MapType>(type, m);
  482. pair_associative_container_type<MapType>(type, m);
  483. input_range_type<MapType>(type, m);
  484. }
  485. template<typename MapType>
  486. ModulePtr map_type(const std::string &type)
  487. {
  488. auto m = std::make_shared<Module>();
  489. map_type<MapType>(type, *m);
  490. return m;
  491. }
  492. /// http://www.sgi.com/tech/stl/List.html
  493. template<typename ListType>
  494. void list_type(const std::string &type, Module& m)
  495. {
  496. m.add(user_type<ListType>(), type);
  497. front_insertion_sequence_type<ListType>(type, m);
  498. back_insertion_sequence_type<ListType>(type, m);
  499. sequence_type<ListType>(type, m);
  500. resizable_type<ListType>(type, m);
  501. container_type<ListType>(type, m);
  502. default_constructible_type<ListType>(type, m);
  503. assignable_type<ListType>(type, m);
  504. input_range_type<ListType>(type, m);
  505. }
  506. template<typename ListType>
  507. ModulePtr list_type(const std::string &type)
  508. {
  509. auto m = std::make_shared<Module>();
  510. list_type<ListType>(type, m);
  511. return m;
  512. }
  513. /// Create a vector type with associated concepts
  514. /// http://www.sgi.com/tech/stl/Vector.html
  515. template<typename VectorType>
  516. void vector_type(const std::string &type, Module& m)
  517. {
  518. m.add(user_type<VectorType>(), type);
  519. m.add(fun([](VectorType &container)->decltype(auto){
  520. if (container.empty()) {
  521. throw std::range_error("Container empty");
  522. } else {
  523. return (container.front());
  524. }
  525. }
  526. )
  527. , "front");
  528. m.add(fun([](const VectorType &container)->decltype(auto){
  529. if (container.empty()) {
  530. throw std::range_error("Container empty");
  531. } else {
  532. return (container.front());
  533. }
  534. }
  535. )
  536. , "front");
  537. back_insertion_sequence_type<VectorType>(type, m);
  538. sequence_type<VectorType>(type, m);
  539. random_access_container_type<VectorType>(type, m);
  540. resizable_type<VectorType>(type, m);
  541. reservable_type<VectorType>(type, m);
  542. container_type<VectorType>(type, m);
  543. default_constructible_type<VectorType>(type, m);
  544. assignable_type<VectorType>(type, m);
  545. input_range_type<VectorType>(type, m);
  546. if (typeid(VectorType) == typeid(std::vector<Boxed_Value>))
  547. {
  548. m.eval(R"(
  549. def Vector::`==`(Vector rhs) {
  550. if ( rhs.size() != this.size() ) {
  551. return false;
  552. } else {
  553. auto r1 = range(this);
  554. auto r2 = range(rhs);
  555. while (!r1.empty())
  556. {
  557. if (!eq(r1.front(), r2.front()))
  558. {
  559. return false;
  560. }
  561. r1.pop_front();
  562. r2.pop_front();
  563. }
  564. true;
  565. }
  566. } )"
  567. );
  568. }
  569. }
  570. template<typename VectorType>
  571. ModulePtr vector_type(const std::string &type)
  572. {
  573. auto m = std::make_shared<Module>();
  574. vector_type<VectorType>(type, *m);
  575. return m;
  576. }
  577. /// Add a String container
  578. /// http://www.sgi.com/tech/stl/basic_string.html
  579. template<typename String>
  580. void string_type(const std::string &type, Module& m)
  581. {
  582. m.add(user_type<String>(), type);
  583. operators::addition<String>(m);
  584. operators::assign_sum<String>(m);
  585. opers_comparison<String>(m);
  586. random_access_container_type<String>(type, m);
  587. sequence_type<String>(type, m);
  588. default_constructible_type<String>(type, m);
  589. // container_type<String>(type, m);
  590. assignable_type<String>(type, m);
  591. input_range_type<String>(type, m);
  592. //Special case: add push_back to string (which doesn't support other back_insertion operations
  593. m.add(fun(&String::push_back),
  594. []()->std::string{
  595. if (typeid(typename String::value_type) == typeid(Boxed_Value)) {
  596. return "push_back_ref";
  597. } else {
  598. return "push_back";
  599. }
  600. }());
  601. m.add(fun([](const String *s, const String &f, size_t pos) { return s->find(f, pos); } ), "find");
  602. m.add(fun([](const String *s, typename String::value_type c, size_t pos) { return s->find(c, pos); } ), "find");
  603. m.add(fun([](const String *s, const String &f, size_t pos) { return s->rfind(f, pos); } ), "rfind");
  604. m.add(fun([](const String *s, typename String::value_type c, size_t pos) { return s->rfind(c, pos); } ), "rfind");
  605. m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_of(f, pos); } ), "find_first_of");
  606. m.add(fun([](const String *s, typename String::value_type c, size_t pos) { return s->find_first_of(c, pos); } ), "find_first_of");
  607. m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_of(f, pos); } ), "find_last_of");
  608. m.add(fun([](const String *s, typename String::value_type c, size_t pos) { return s->find_last_of(c, pos); } ), "find_last_of");
  609. m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_last_not_of(f, pos); } ), "find_last_not_of");
  610. m.add(fun([](const String *s, typename String::value_type c, size_t pos) { return s->find_last_not_of(c, pos); } ), "find_last_not_of");
  611. m.add(fun([](const String *s, const String &f, size_t pos) { return s->find_first_not_of(f, pos); } ), "find_first_not_of");
  612. m.add(fun([](const String *s, typename String::value_type c, size_t pos) { return s->find_first_not_of(c, pos); } ), "find_first_not_of");
  613. m.add(fun([](String *s, typename String::value_type c) -> decltype(auto) { return (*s += c); } ), "+=");
  614. m.add(fun([](String *s, typename String::value_type c) -> decltype(auto) { return (*s + c); } ), "+");
  615. m.add(fun([](String *s) { std::reverse(s->begin(), s->end()); } ), "reverse");
  616. m.add(fun([](String *s, const String &from, const String &to)
  617. {
  618. std::string newString;
  619. newString.reserve(s->length()); // avoids a few memory allocations
  620. std::string::size_type lastPos = 0;
  621. std::string::size_type findPos;
  622. while (std::string::npos != (findPos = s->find(from, lastPos)))
  623. {
  624. newString.append(*s, lastPos, findPos - lastPos);
  625. newString += to;
  626. lastPos = findPos + from.length();
  627. }
  628. // Care for the rest after last occurrence
  629. newString += s->substr(lastPos);
  630. return newString;
  631. }), "replace");
  632. m.add(fun([](String *s) { s->clear(); } ), "clear");
  633. m.add(fun([](const String *s) { return s->empty(); } ), "empty");
  634. m.add(fun([](const String *s) { return s->size(); } ), "size");
  635. m.add(fun([](const String *s) { return s->c_str(); } ), "c_str");
  636. m.add(fun([](const String *s) { return s->data(); } ), "data");
  637. m.add(fun([](const String *s, size_t pos, size_t len) { return s->substr(pos, len); } ), "substr");
  638. }
  639. template<typename String>
  640. ModulePtr string_type(const std::string &type)
  641. {
  642. auto m = std::make_shared<Module>();
  643. string_type<String>(type, *m);
  644. return m;
  645. }
  646. /// Add a MapType container
  647. /// http://www.sgi.com/tech/stl/Map.html
  648. template<typename FutureType>
  649. void future_type(const std::string &type, Module& m)
  650. {
  651. m.add(user_type<FutureType>(), type);
  652. m.add(fun([](const FutureType &t) { return t.valid(); }), "valid");
  653. m.add(fun(&FutureType::get), "get");
  654. m.add(fun(&FutureType::wait), "wait");
  655. }
  656. template<typename FutureType>
  657. ModulePtr future_type(const std::string &type)
  658. {
  659. auto m = std::make_shared<Module>();
  660. future_type<FutureType>(type, *m);
  661. return m;
  662. }
  663. }
  664. }
  665. }
  666. #endif