cmList.h 46 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #pragma once
  4. #include "cmConfigure.h" // IWYU pragma: keep
  5. #include <algorithm>
  6. #include <cstdint>
  7. #include <initializer_list>
  8. #include <iterator>
  9. #include <memory>
  10. #include <stdexcept>
  11. #include <string>
  12. #include <utility>
  13. #include <vector>
  14. #include <cm/string_view>
  15. #include <cm/type_traits>
  16. #include <cmext/iterator>
  17. #include "cmValue.h"
  18. template <typename T>
  19. class BT;
  20. /**
  21. * CMake lists management
  22. * A CMake list is a string where list elements are separated by the ';'
  23. * character.
  24. *
  25. * For all operations, input arguments (single value like cm::string_view or
  26. * multiple values specified through pair of iterators) are, by default,
  27. * expanded. The expansion can be controlled by the cmList::ExpandElements
  28. * option.
  29. *
  30. * There ate some exceptions to this rule:
  31. * * When the input argument is a cmList instance, the value is not expanded.
  32. * * The following methods do not expand their argument: cmList::push_back,
  33. * cmList::emplace and cmList::emplace_back.
  34. */
  35. class cmList
  36. {
  37. public:
  38. using container_type = std::vector<std::string>;
  39. using value_type = container_type::value_type;
  40. using allocator_type = container_type::allocator_type;
  41. using index_type = std::intptr_t;
  42. using size_type = container_type::size_type;
  43. using difference_type = container_type::difference_type;
  44. using reference = container_type::reference;
  45. using const_reference = container_type::const_reference;
  46. using iterator = container_type::iterator;
  47. using const_iterator = container_type::const_iterator;
  48. using reverse_iterator = container_type::reverse_iterator;
  49. using const_reverse_iterator = container_type::const_reverse_iterator;
  50. static const size_type npos = static_cast<size_type>(-1);
  51. static cm::string_view element_separator;
  52. enum class EmptyElements
  53. {
  54. No,
  55. Yes,
  56. };
  57. enum class ExpandElements
  58. {
  59. No,
  60. Yes,
  61. };
  62. cmList() = default;
  63. cmList(const cmList&) = default;
  64. cmList(cmList&&) = default;
  65. cmList(cm::string_view value,
  66. ExpandElements expandElements = ExpandElements::Yes,
  67. EmptyElements emptyElements = EmptyElements::No)
  68. {
  69. this->assign(value, expandElements, emptyElements);
  70. }
  71. cmList(cm::string_view value, EmptyElements emptyElements)
  72. : cmList(value, ExpandElements::Yes, emptyElements)
  73. {
  74. }
  75. cmList(std::string const& value,
  76. ExpandElements expandElements = ExpandElements::Yes,
  77. EmptyElements emptyElements = EmptyElements::No)
  78. {
  79. this->assign(value, expandElements, emptyElements);
  80. }
  81. cmList(std::string const& value, EmptyElements emptyElements)
  82. : cmList(value, ExpandElements::Yes, emptyElements)
  83. {
  84. }
  85. cmList(cmValue list, ExpandElements expandElements = ExpandElements::Yes,
  86. EmptyElements emptyElements = EmptyElements::No)
  87. {
  88. if (list) {
  89. this->assign(*list, expandElements, emptyElements);
  90. }
  91. }
  92. cmList(cmValue list, EmptyElements emptyElements)
  93. : cmList(list, ExpandElements::Yes, emptyElements)
  94. {
  95. }
  96. template <typename InputIterator>
  97. cmList(InputIterator first, InputIterator last,
  98. ExpandElements expandElements = ExpandElements::Yes,
  99. EmptyElements emptyElements = EmptyElements::No)
  100. {
  101. this->assign(first, last, expandElements, emptyElements);
  102. }
  103. template <typename InputIterator>
  104. cmList(InputIterator first, InputIterator last, EmptyElements emptyElements)
  105. : cmList(first, last, ExpandElements::Yes, emptyElements)
  106. {
  107. }
  108. cmList(const container_type& init,
  109. ExpandElements expandElements = ExpandElements::Yes,
  110. EmptyElements emptyElements = EmptyElements::No)
  111. : cmList(init.begin(), init.end(), expandElements, emptyElements)
  112. {
  113. }
  114. cmList(const container_type& init, EmptyElements emptyElements)
  115. : cmList(init, ExpandElements::Yes, emptyElements)
  116. {
  117. }
  118. cmList(container_type&& init,
  119. ExpandElements expandElements = ExpandElements::Yes,
  120. EmptyElements emptyElements = EmptyElements::No)
  121. : cmList(std::make_move_iterator(init.begin()),
  122. std::make_move_iterator(init.end()), expandElements,
  123. emptyElements)
  124. {
  125. init.clear();
  126. }
  127. cmList(container_type&& init, EmptyElements emptyElements)
  128. : cmList(std::move(init), ExpandElements::Yes, emptyElements)
  129. {
  130. }
  131. cmList(std::initializer_list<std::string> init) { this->assign(init); }
  132. ~cmList() = default;
  133. cmList& operator=(const cmList&) = default;
  134. cmList& operator=(cmList&&) = default;
  135. cmList& operator=(cm::string_view value)
  136. {
  137. this->assign(value);
  138. return *this;
  139. }
  140. cmList& operator=(std::string const& value)
  141. {
  142. this->assign(value);
  143. return *this;
  144. }
  145. cmList& operator=(cmValue value)
  146. {
  147. if (value) {
  148. this->operator=(*value);
  149. } else {
  150. this->clear();
  151. }
  152. return *this;
  153. }
  154. cmList& operator=(const container_type& init)
  155. {
  156. this->assign(init);
  157. return *this;
  158. }
  159. cmList& operator=(container_type&& init)
  160. {
  161. this->assign(std::move(init));
  162. return *this;
  163. }
  164. cmList& operator=(std::initializer_list<std::string> init)
  165. {
  166. this->assign(init);
  167. return *this;
  168. }
  169. void assign(cm::string_view value,
  170. ExpandElements expandElements = ExpandElements::Yes,
  171. EmptyElements emptyElements = EmptyElements::No)
  172. {
  173. this->clear();
  174. this->append(value, expandElements, emptyElements);
  175. }
  176. void assign(cm::string_view value, EmptyElements emptyElements)
  177. {
  178. this->assign(value, ExpandElements::Yes, emptyElements);
  179. }
  180. void assign(std::string const& value,
  181. ExpandElements expandElements = ExpandElements::Yes,
  182. EmptyElements emptyElements = EmptyElements::No)
  183. {
  184. this->clear();
  185. this->append(value, expandElements, emptyElements);
  186. }
  187. void assign(std::string const& value, EmptyElements emptyElements)
  188. {
  189. this->assign(value, ExpandElements::Yes, emptyElements);
  190. }
  191. void assign(cmValue value,
  192. ExpandElements expandElements = ExpandElements::Yes,
  193. EmptyElements emptyElements = EmptyElements::No)
  194. {
  195. if (value) {
  196. this->assign(*value, expandElements, emptyElements);
  197. } else {
  198. this->clear();
  199. }
  200. }
  201. void assign(cmValue value, EmptyElements emptyElements)
  202. {
  203. this->assign(value, ExpandElements::Yes, emptyElements);
  204. }
  205. template <typename InputIterator>
  206. void assign(InputIterator first, InputIterator last,
  207. ExpandElements expandElements = ExpandElements::Yes,
  208. EmptyElements emptyElements = EmptyElements::No)
  209. {
  210. this->clear();
  211. this->append(first, last, expandElements, emptyElements);
  212. }
  213. template <typename InputIterator>
  214. void assign(InputIterator first, InputIterator last,
  215. EmptyElements emptyElements)
  216. {
  217. this->assign(first, last, ExpandElements::Yes, emptyElements);
  218. }
  219. void assign(const cmList& init,
  220. ExpandElements expandElements = ExpandElements::No,
  221. EmptyElements emptyElements = EmptyElements::No)
  222. {
  223. this->assign(init.begin(), init.end(), expandElements, emptyElements);
  224. }
  225. void assign(const cmList& init, EmptyElements emptyElements)
  226. {
  227. this->assign(init, ExpandElements::No, emptyElements);
  228. }
  229. void assign(cmList&& init,
  230. ExpandElements expandElements = ExpandElements::No,
  231. EmptyElements emptyElements = EmptyElements::No)
  232. {
  233. this->assign(std::make_move_iterator(init.begin()),
  234. std::make_move_iterator(init.end()), expandElements,
  235. emptyElements);
  236. init.clear();
  237. }
  238. void assign(cmList&& init, EmptyElements emptyElements)
  239. {
  240. this->assign(std::move(init), ExpandElements::No, emptyElements);
  241. }
  242. void assign(const container_type& init,
  243. ExpandElements expandElements = ExpandElements::Yes,
  244. EmptyElements emptyElements = EmptyElements::No)
  245. {
  246. this->assign(init.begin(), init.end(), expandElements, emptyElements);
  247. }
  248. void assign(const container_type& init, EmptyElements emptyElements)
  249. {
  250. this->assign(init, ExpandElements::Yes, emptyElements);
  251. }
  252. void assign(container_type&& init,
  253. ExpandElements expandElements = ExpandElements::Yes,
  254. EmptyElements emptyElements = EmptyElements::No)
  255. {
  256. this->assign(std::make_move_iterator(init.begin()),
  257. std::make_move_iterator(init.end()), expandElements,
  258. emptyElements);
  259. init.clear();
  260. }
  261. void assign(container_type&& init, EmptyElements emptyElements)
  262. {
  263. this->assign(std::move(init), ExpandElements::Yes, emptyElements);
  264. }
  265. void assign(std::initializer_list<std::string> init)
  266. {
  267. this->assign(init.begin(), init.end());
  268. }
  269. // Conversions
  270. std::string to_string() const
  271. {
  272. return this->join(cmList::element_separator);
  273. }
  274. operator container_type&() & noexcept { return this->Values; }
  275. operator const container_type&() const& noexcept { return this->Values; }
  276. operator container_type&&() && noexcept { return std::move(this->Values); }
  277. // Element access
  278. reference at(size_type pos) { return this->Values.at(pos); }
  279. const_reference at(size_type pos) const { return this->Values.at(pos); }
  280. reference operator[](size_type pos) { return this->Values[pos]; }
  281. const_reference operator[](size_type pos) const { return this->Values[pos]; }
  282. reference get_item(index_type pos)
  283. {
  284. return this->Values.at(this->ComputeIndex(pos));
  285. }
  286. const_reference get_item(index_type pos) const
  287. {
  288. return this->Values.at(this->ComputeIndex(pos));
  289. }
  290. reference front() { return this->Values.front(); }
  291. const_reference front() const { return this->Values.front(); }
  292. reference back() { return this->Values.back(); }
  293. const_reference back() const { return this->Values.back(); }
  294. // extract sublist in range [first, last)
  295. cmList sublist(const_iterator first, const_iterator last) const
  296. {
  297. return cmList{ first, last, ExpandElements::No, EmptyElements::Yes };
  298. }
  299. // Extract sublist in range [first, last)
  300. // Throw std::out_of_range if pos is invalid
  301. cmList sublist(size_type pos = 0, size_type length = npos) const;
  302. // Returns the list of elements
  303. // Throw std::out_of_range if any index is invalid
  304. cmList get_items(std::initializer_list<index_type> indexes) const
  305. {
  306. return this->GetItems(
  307. std::vector<index_type>{ indexes.begin(), indexes.end() });
  308. }
  309. template <typename InputIterator>
  310. cmList get_items(InputIterator first, InputIterator last) const
  311. {
  312. return this->GetItems(std::vector<index_type>{ first, last });
  313. }
  314. size_type find(cm::string_view value) const;
  315. size_type find(cmValue value) const
  316. {
  317. if (value) {
  318. return this->find(*value);
  319. }
  320. return npos;
  321. }
  322. container_type& data() noexcept { return this->Values; }
  323. const container_type& data() const noexcept { return this->Values; }
  324. // Iterators
  325. iterator begin() noexcept { return this->Values.begin(); }
  326. const_iterator begin() const noexcept { return this->Values.begin(); }
  327. const_iterator cbegin() const noexcept { return this->Values.cbegin(); }
  328. iterator end() noexcept { return this->Values.end(); }
  329. const_iterator end() const noexcept { return this->Values.end(); }
  330. const_iterator cend() const noexcept { return this->Values.cend(); }
  331. reverse_iterator rbegin() noexcept { return this->Values.rbegin(); }
  332. const_reverse_iterator rbegin() const noexcept
  333. {
  334. return this->Values.rbegin();
  335. }
  336. const_reverse_iterator crbegin() const noexcept
  337. {
  338. return this->Values.crbegin();
  339. }
  340. reverse_iterator rend() noexcept { return this->Values.rend(); }
  341. const_reverse_iterator rend() const noexcept { return this->Values.rend(); }
  342. const_reverse_iterator crend() const noexcept
  343. {
  344. return this->Values.crend();
  345. }
  346. // Capacity
  347. bool empty() const noexcept { return this->Values.empty(); }
  348. size_type size() const noexcept { return this->Values.size(); }
  349. // Modifiers
  350. void clear() noexcept { this->Values.clear(); }
  351. iterator insert(const_iterator pos, cm::string_view value,
  352. ExpandElements expandElements = ExpandElements::Yes,
  353. EmptyElements emptyElements = EmptyElements::No)
  354. {
  355. return cmList::Insert(this->Values, pos, std::string(value),
  356. expandElements, emptyElements);
  357. }
  358. iterator insert(const_iterator pos, cm::string_view value,
  359. EmptyElements emptyElements)
  360. {
  361. return this->insert(pos, value, ExpandElements::Yes, emptyElements);
  362. }
  363. iterator insert(const_iterator pos, std::string const& value,
  364. ExpandElements expandElements = ExpandElements::Yes,
  365. EmptyElements emptyElements = EmptyElements::No)
  366. {
  367. return cmList::Insert(this->Values, pos, value, expandElements,
  368. emptyElements);
  369. }
  370. iterator insert(const_iterator pos, std::string const& value,
  371. EmptyElements emptyElements)
  372. {
  373. return this->insert(pos, value, ExpandElements::Yes, emptyElements);
  374. }
  375. iterator insert(const_iterator pos, cmValue value,
  376. ExpandElements expandElements = ExpandElements::Yes,
  377. EmptyElements emptyElements = EmptyElements::No)
  378. {
  379. if (value) {
  380. return this->insert(pos, *value, expandElements, emptyElements);
  381. }
  382. auto delta = std::distance(this->cbegin(), pos);
  383. return this->begin() + delta;
  384. }
  385. iterator insert(const_iterator pos, cmValue value,
  386. EmptyElements emptyElements)
  387. {
  388. return this->insert(pos, value, ExpandElements::Yes, emptyElements);
  389. }
  390. template <typename InputIterator>
  391. iterator insert(const_iterator pos, InputIterator first, InputIterator last,
  392. ExpandElements expandElements = ExpandElements::Yes,
  393. EmptyElements emptyElements = EmptyElements::No)
  394. {
  395. return cmList::Insert(this->Values, pos, first, last, expandElements,
  396. emptyElements);
  397. }
  398. template <typename InputIterator>
  399. iterator insert(const_iterator pos, InputIterator first, InputIterator last,
  400. EmptyElements emptyElements)
  401. {
  402. return this->insert(pos, first, last, ExpandElements::Yes, emptyElements);
  403. }
  404. iterator insert(const_iterator pos, const cmList& values,
  405. ExpandElements expandElements = ExpandElements::No,
  406. EmptyElements emptyElements = EmptyElements::No)
  407. {
  408. return this->insert(pos, values.begin(), values.end(), expandElements,
  409. emptyElements);
  410. }
  411. iterator insert(const_iterator pos, const cmList& values,
  412. EmptyElements emptyElements)
  413. {
  414. return this->insert(pos, values, ExpandElements::No, emptyElements);
  415. }
  416. iterator insert(const_iterator pos, cmList&& values,
  417. ExpandElements expandElements = ExpandElements::No,
  418. EmptyElements emptyElements = EmptyElements::No)
  419. {
  420. auto result = this->insert(pos, std::make_move_iterator(values.begin()),
  421. std::make_move_iterator(values.end()),
  422. expandElements, emptyElements);
  423. values.clear();
  424. return result;
  425. }
  426. iterator insert(const_iterator pos, cmList&& values,
  427. EmptyElements emptyElements)
  428. {
  429. return this->insert(pos, std::move(values), ExpandElements::No,
  430. emptyElements);
  431. }
  432. iterator insert(const_iterator pos, const container_type& values,
  433. ExpandElements expandElements = ExpandElements::Yes,
  434. EmptyElements emptyElements = EmptyElements::No)
  435. {
  436. return this->insert(pos, values.begin(), values.end(), expandElements,
  437. emptyElements);
  438. }
  439. iterator insert(const_iterator pos, const container_type& values,
  440. EmptyElements emptyElements)
  441. {
  442. return this->insert(pos, values, ExpandElements::Yes, emptyElements);
  443. }
  444. iterator insert(const_iterator pos, container_type&& values,
  445. ExpandElements expandElements = ExpandElements::Yes,
  446. EmptyElements emptyElements = EmptyElements::No)
  447. {
  448. auto result = this->insert(pos, std::make_move_iterator(values.begin()),
  449. std::make_move_iterator(values.end()),
  450. expandElements, emptyElements);
  451. values.clear();
  452. return result;
  453. }
  454. iterator insert(const_iterator pos, container_type&& values,
  455. EmptyElements emptyElements)
  456. {
  457. return this->insert(pos, std::move(values), ExpandElements::Yes,
  458. emptyElements);
  459. }
  460. iterator insert(const_iterator pos, std::initializer_list<std::string> ilist)
  461. {
  462. return this->insert(pos, ilist.begin(), ilist.end());
  463. }
  464. iterator append(cm::string_view value,
  465. ExpandElements expandElements = ExpandElements::Yes,
  466. EmptyElements emptyElements = EmptyElements::No)
  467. {
  468. return this->insert(this->cend(), value, expandElements, emptyElements);
  469. }
  470. iterator append(cm::string_view value, EmptyElements emptyElements)
  471. {
  472. return this->append(value, ExpandElements::Yes, emptyElements);
  473. }
  474. iterator append(std::string const& value,
  475. ExpandElements expandElements = ExpandElements::Yes,
  476. EmptyElements emptyElements = EmptyElements::No)
  477. {
  478. return this->insert(this->cend(), value, expandElements, emptyElements);
  479. }
  480. iterator append(std::string const& value, EmptyElements emptyElements)
  481. {
  482. return this->append(value, ExpandElements::Yes, emptyElements);
  483. }
  484. iterator append(cmValue value,
  485. ExpandElements expandElements = ExpandElements::Yes,
  486. EmptyElements emptyElements = EmptyElements::No)
  487. {
  488. if (value) {
  489. return this->append(*value, expandElements, emptyElements);
  490. }
  491. return this->end();
  492. }
  493. iterator append(cmValue value, EmptyElements emptyElements)
  494. {
  495. return this->append(value, ExpandElements::Yes, emptyElements);
  496. }
  497. template <typename InputIterator>
  498. iterator append(InputIterator first, InputIterator last,
  499. ExpandElements expandElements = ExpandElements::Yes,
  500. EmptyElements emptyElements = EmptyElements::No)
  501. {
  502. return this->insert(this->cend(), first, last, expandElements,
  503. emptyElements);
  504. }
  505. template <typename InputIterator>
  506. iterator append(InputIterator first, InputIterator last,
  507. EmptyElements emptyElements)
  508. {
  509. return this->append(first, last, ExpandElements::Yes, emptyElements);
  510. }
  511. iterator append(const cmList& values,
  512. ExpandElements expandElements = ExpandElements::No,
  513. EmptyElements emptyElements = EmptyElements::No)
  514. {
  515. return this->append(values.begin(), values.end(), expandElements,
  516. emptyElements);
  517. }
  518. iterator append(const cmList& values, EmptyElements emptyElements)
  519. {
  520. return this->append(values, ExpandElements::No, emptyElements);
  521. }
  522. iterator append(cmList&& values,
  523. ExpandElements expandElements = ExpandElements::No,
  524. EmptyElements emptyElements = EmptyElements::No)
  525. {
  526. auto result = this->append(std::make_move_iterator(values.begin()),
  527. std::make_move_iterator(values.end()),
  528. expandElements, emptyElements);
  529. values.clear();
  530. return result;
  531. }
  532. iterator append(cmList&& values, EmptyElements emptyElements)
  533. {
  534. return this->append(std::move(values), ExpandElements::No, emptyElements);
  535. }
  536. iterator append(const container_type& values,
  537. ExpandElements expandElements = ExpandElements::Yes,
  538. EmptyElements emptyElements = EmptyElements::No)
  539. {
  540. return this->append(values.begin(), values.end(), expandElements,
  541. emptyElements);
  542. }
  543. iterator append(const container_type& values, EmptyElements emptyElements)
  544. {
  545. return this->append(values, ExpandElements::Yes, emptyElements);
  546. }
  547. iterator append(container_type&& values,
  548. ExpandElements expandElements = ExpandElements::Yes,
  549. EmptyElements emptyElements = EmptyElements::No)
  550. {
  551. auto result = this->append(std::make_move_iterator(values.begin()),
  552. std::make_move_iterator(values.end()),
  553. expandElements, emptyElements);
  554. values.clear();
  555. return result;
  556. }
  557. iterator append(container_type&& values, EmptyElements emptyElements)
  558. {
  559. return this->append(std::move(values), ExpandElements::Yes, emptyElements);
  560. }
  561. iterator append(std::initializer_list<std::string> ilist)
  562. {
  563. return this->insert(this->cend(), ilist);
  564. }
  565. iterator prepend(cm::string_view value,
  566. ExpandElements expandElements = ExpandElements::Yes,
  567. EmptyElements emptyElements = EmptyElements::No)
  568. {
  569. return this->insert(this->cbegin(), value, expandElements, emptyElements);
  570. }
  571. iterator prepend(cm::string_view value, EmptyElements emptyElements)
  572. {
  573. return this->prepend(value, ExpandElements::Yes, emptyElements);
  574. }
  575. iterator prepend(std::string const& value,
  576. ExpandElements expandElements = ExpandElements::Yes,
  577. EmptyElements emptyElements = EmptyElements::No)
  578. {
  579. return this->insert(this->cbegin(), value, expandElements, emptyElements);
  580. }
  581. iterator prepend(std::string const& value, EmptyElements emptyElements)
  582. {
  583. return this->prepend(value, ExpandElements::Yes, emptyElements);
  584. }
  585. iterator prepend(cmValue value,
  586. ExpandElements expandElements = ExpandElements::Yes,
  587. EmptyElements emptyElements = EmptyElements::No)
  588. {
  589. if (value) {
  590. return this->prepend(*value, expandElements, emptyElements);
  591. }
  592. return this->begin();
  593. }
  594. iterator prepend(cmValue value, EmptyElements emptyElements)
  595. {
  596. return this->prepend(value, ExpandElements::Yes, emptyElements);
  597. }
  598. template <typename InputIterator>
  599. iterator prepend(InputIterator first, InputIterator last,
  600. ExpandElements expandElements = ExpandElements::Yes,
  601. EmptyElements emptyElements = EmptyElements::No)
  602. {
  603. return this->insert(this->cbegin(), first, last, expandElements,
  604. emptyElements);
  605. }
  606. template <typename InputIterator>
  607. iterator prepend(InputIterator first, InputIterator last,
  608. EmptyElements emptyElements)
  609. {
  610. return this->prepend(first, last, ExpandElements::Yes, emptyElements);
  611. }
  612. iterator prepend(const cmList& values,
  613. ExpandElements expandElements = ExpandElements::No,
  614. EmptyElements emptyElements = EmptyElements::No)
  615. {
  616. return this->prepend(values.begin(), values.end(), expandElements,
  617. emptyElements);
  618. }
  619. iterator prepend(const cmList& values, EmptyElements emptyElements)
  620. {
  621. return this->prepend(values, ExpandElements::No, emptyElements);
  622. }
  623. iterator prepend(cmList&& values,
  624. ExpandElements expandElements = ExpandElements::No,
  625. EmptyElements emptyElements = EmptyElements::No)
  626. {
  627. auto result = this->prepend(std::make_move_iterator(values.begin()),
  628. std::make_move_iterator(values.end()),
  629. expandElements, emptyElements);
  630. values.clear();
  631. return result;
  632. }
  633. iterator prepend(cmList&& values, EmptyElements emptyElements)
  634. {
  635. return this->prepend(std::move(values), ExpandElements::No, emptyElements);
  636. }
  637. iterator prepend(const container_type& values,
  638. ExpandElements expandElements = ExpandElements::Yes,
  639. EmptyElements emptyElements = EmptyElements::No)
  640. {
  641. return this->prepend(values.begin(), values.end(), expandElements,
  642. emptyElements);
  643. }
  644. iterator prepend(const container_type& values, EmptyElements emptyElements)
  645. {
  646. return this->prepend(values, ExpandElements::Yes, emptyElements);
  647. }
  648. iterator prepend(container_type&& values,
  649. ExpandElements expandElements = ExpandElements::Yes,
  650. EmptyElements emptyElements = EmptyElements::No)
  651. {
  652. auto result = this->prepend(std::make_move_iterator(values.begin()),
  653. std::make_move_iterator(values.end()),
  654. expandElements, emptyElements);
  655. values.clear();
  656. return result;
  657. }
  658. iterator prepend(container_type&& values, EmptyElements emptyElements)
  659. {
  660. return this->prepend(std::move(values), ExpandElements::Yes,
  661. emptyElements);
  662. }
  663. iterator prepend(std::initializer_list<std::string> ilist)
  664. {
  665. return this->insert(this->cbegin(), ilist);
  666. }
  667. void push_back(std::string const& value) { this->Values.push_back(value); }
  668. void push_back(cm::string_view value)
  669. {
  670. this->Values.push_back(std::string{ value });
  671. }
  672. void push_back(std::string&& value)
  673. {
  674. this->Values.push_back(std::move(value));
  675. }
  676. template <typename... Args>
  677. iterator emplace(const_iterator pos, Args&&... args)
  678. {
  679. return this->Values.emplace(pos, std::forward<Args>(args)...);
  680. }
  681. template <typename... Args>
  682. void emplace_back(Args&&... args)
  683. {
  684. this->Values.emplace_back(std::forward<Args>(args)...);
  685. }
  686. // Inserts elements in the list
  687. // Throw std::out_of_range if index is invalid
  688. template <typename InputIterator>
  689. cmList& insert_items(index_type index, InputIterator first,
  690. InputIterator last,
  691. ExpandElements expandElements = ExpandElements::Yes,
  692. EmptyElements emptyElements = EmptyElements::No)
  693. {
  694. auto const offset =
  695. static_cast<difference_type>(this->ComputeInsertIndex(index));
  696. this->insert(this->begin() + offset, first, last, expandElements,
  697. emptyElements);
  698. return *this;
  699. }
  700. template <typename InputIterator>
  701. cmList& insert_items(index_type index, InputIterator first,
  702. InputIterator last, EmptyElements emptyElements)
  703. {
  704. this->insert(this->begin() + this->ComputeInsertIndex(index), first, last,
  705. ExpandElements::Yes, emptyElements);
  706. return *this;
  707. }
  708. cmList& insert_items(index_type index,
  709. std::initializer_list<std::string> values)
  710. {
  711. return this->insert_items(index, values.begin(), values.end());
  712. }
  713. iterator erase(const_iterator pos)
  714. {
  715. // convert const_iterator in iterator to please non standard c++11
  716. // compilers (gcc 4.8 for example)
  717. auto pos2 =
  718. this->Values.begin() + std::distance(this->Values.cbegin(), pos);
  719. return this->Values.erase(pos2);
  720. }
  721. iterator erase(const_iterator first, const_iterator last)
  722. {
  723. // convert const_iterator in iterator to please non standard c++11
  724. // compilers (gcc 4.8 for example)
  725. auto first2 =
  726. this->Values.begin() + std::distance(this->Values.cbegin(), first);
  727. auto last2 =
  728. this->Values.begin() + std::distance(this->Values.cbegin(), last);
  729. return this->Values.erase(first2, last2);
  730. }
  731. void pop_back() { this->Values.pop_back(); }
  732. void pop_front() { this->Values.erase(this->begin()); }
  733. // Removes elements from the list
  734. // Throw std::out_of_range if any index is invalid
  735. cmList& remove_items(std::initializer_list<index_type> indexes)
  736. {
  737. return this->RemoveItems(
  738. std::vector<index_type>{ indexes.begin(), indexes.end() });
  739. }
  740. cmList& remove_items(std::initializer_list<std::string> values)
  741. {
  742. return this->RemoveItems(
  743. std::vector<std::string>{ values.begin(), values.end() });
  744. }
  745. template <typename InputIterator>
  746. cmList& remove_items(InputIterator first, InputIterator last)
  747. {
  748. return this->RemoveItems(
  749. std::vector<typename InputIterator::value_type>{ first, last });
  750. }
  751. cmList& remove_duplicates();
  752. void resize(size_type count) { this->Values.resize(count); }
  753. enum class FilterMode
  754. {
  755. INCLUDE,
  756. EXCLUDE
  757. };
  758. // Includes or removes items from the list
  759. // Throw std::invalid_argument if regular expression is invalid
  760. cmList& filter(cm::string_view regex, FilterMode mode);
  761. cmList& reverse()
  762. {
  763. std::reverse(this->Values.begin(), this->Values.end());
  764. return *this;
  765. }
  766. struct SortConfiguration
  767. {
  768. enum class OrderMode
  769. {
  770. DEFAULT,
  771. ASCENDING,
  772. DESCENDING,
  773. } Order = OrderMode::DEFAULT;
  774. enum class CompareMethod
  775. {
  776. DEFAULT,
  777. STRING,
  778. FILE_BASENAME,
  779. NATURAL,
  780. } Compare = CompareMethod::DEFAULT;
  781. enum class CaseSensitivity
  782. {
  783. DEFAULT,
  784. SENSITIVE,
  785. INSENSITIVE,
  786. } Case = CaseSensitivity::DEFAULT;
  787. // declare the default constructor to work-around clang bug
  788. SortConfiguration();
  789. SortConfiguration(OrderMode order, CompareMethod compare,
  790. CaseSensitivity caseSensitivity)
  791. : Order(order)
  792. , Compare(compare)
  793. , Case(caseSensitivity)
  794. {
  795. }
  796. };
  797. cmList& sort(const SortConfiguration& config = SortConfiguration{});
  798. // exception raised on error during transform operations
  799. class transform_error : public std::runtime_error
  800. {
  801. public:
  802. transform_error(const std::string& error)
  803. : std::runtime_error(error)
  804. {
  805. }
  806. };
  807. class TransformSelector
  808. {
  809. public:
  810. using index_type = cmList::index_type;
  811. // define some structs used as template selector
  812. struct AT;
  813. struct FOR;
  814. struct REGEX;
  815. virtual ~TransformSelector() = default;
  816. virtual const std::string& GetTag() = 0;
  817. // method NEW is used to allocate a selector of the needed type.
  818. // For example:
  819. // cmList::TransformSelector::New<AT>({1, 2, 5, 6});
  820. // or
  821. // cmList::TransformSelector::New<REGEX>("^XX.*");
  822. template <typename Type>
  823. static std::unique_ptr<TransformSelector> New(
  824. std::initializer_list<index_type>);
  825. template <typename Type>
  826. static std::unique_ptr<TransformSelector> New(
  827. std::vector<index_type> const&);
  828. template <typename Type>
  829. static std::unique_ptr<TransformSelector> New(std::vector<index_type>&&);
  830. template <typename Type>
  831. static std::unique_ptr<TransformSelector> New(std::string const&);
  832. template <typename Type>
  833. static std::unique_ptr<TransformSelector> New(std::string&&);
  834. private:
  835. static std::unique_ptr<TransformSelector> NewAT(
  836. std::initializer_list<index_type> init);
  837. static std::unique_ptr<TransformSelector> NewAT(
  838. std::vector<index_type> const& init);
  839. static std::unique_ptr<TransformSelector> NewAT(
  840. std::vector<index_type>&& init);
  841. static std::unique_ptr<TransformSelector> NewFOR(
  842. std::initializer_list<index_type> init);
  843. static std::unique_ptr<TransformSelector> NewFOR(
  844. std::vector<index_type> const& init);
  845. static std::unique_ptr<TransformSelector> NewFOR(
  846. std::vector<index_type>&& init);
  847. static std::unique_ptr<TransformSelector> NewREGEX(
  848. std::string const& init);
  849. static std::unique_ptr<TransformSelector> NewREGEX(std::string&& init);
  850. };
  851. enum class TransformAction
  852. {
  853. APPEND,
  854. PREPEND,
  855. TOLOWER,
  856. TOUPPER,
  857. STRIP,
  858. GENEX_STRIP,
  859. REPLACE
  860. };
  861. // Transforms the list by applying an action
  862. // Throw std::transform_error is any of arguments specified are invalid
  863. cmList& transform(TransformAction action,
  864. std::unique_ptr<TransformSelector> = {});
  865. cmList& transform(TransformAction action, std::string const& arg,
  866. std::unique_ptr<TransformSelector> = {});
  867. cmList& transform(TransformAction action, std::string const& arg1,
  868. std::string const& arg2,
  869. std::unique_ptr<TransformSelector> = {});
  870. cmList& transform(TransformAction action,
  871. std::vector<std::string> const& args,
  872. std::unique_ptr<TransformSelector> = {});
  873. std::string join(cm::string_view glue) const
  874. {
  875. return cmList::Join(this->Values, glue);
  876. }
  877. void swap(cmList& other) noexcept { this->Values.swap(other.Values); }
  878. // static members
  879. // ==============
  880. // these methods can be used to store CMake list expansion directly in a
  881. // std::vector.
  882. static void assign(std::vector<std::string>& container,
  883. cm::string_view value,
  884. EmptyElements emptyElements = EmptyElements::No)
  885. {
  886. container.clear();
  887. cmList::append(container, value, emptyElements);
  888. }
  889. static void assign(std::vector<std::string>& container,
  890. std::string const& value,
  891. EmptyElements emptyElements = EmptyElements::No)
  892. {
  893. container.clear();
  894. cmList::append(container, value, emptyElements);
  895. }
  896. static void assign(std::vector<std::string>& container, cmValue value,
  897. EmptyElements emptyElements = EmptyElements::No)
  898. {
  899. if (value) {
  900. cmList::assign(container, *value, emptyElements);
  901. } else {
  902. container.clear();
  903. }
  904. }
  905. template <typename InputIterator>
  906. static void assign(std::vector<std::string>& container, InputIterator first,
  907. InputIterator last,
  908. EmptyElements emptyElements = EmptyElements::No)
  909. {
  910. container.clear();
  911. cmList::append(container, first, last, emptyElements);
  912. }
  913. static std::vector<std::string>::iterator insert(
  914. std::vector<std::string>& container,
  915. std::vector<std::string>::const_iterator pos, cm::string_view value,
  916. EmptyElements emptyElements = EmptyElements::No)
  917. {
  918. return cmList::Insert(container, pos, std::string(value),
  919. ExpandElements::Yes, emptyElements);
  920. }
  921. static std::vector<std::string>::iterator insert(
  922. std::vector<std::string>& container,
  923. std::vector<std::string>::const_iterator pos, std::string const& value,
  924. EmptyElements emptyElements = EmptyElements::No)
  925. {
  926. return cmList::Insert(container, pos, value, ExpandElements::Yes,
  927. emptyElements);
  928. }
  929. static std::vector<std::string>::iterator insert(
  930. std::vector<std::string>& container,
  931. std::vector<std::string>::const_iterator pos, cmValue value,
  932. EmptyElements emptyElements = EmptyElements::No)
  933. {
  934. if (value) {
  935. return cmList::insert(container, pos, *value, emptyElements);
  936. }
  937. auto delta = std::distance(container.cbegin(), pos);
  938. return container.begin() + delta;
  939. }
  940. template <typename InputIterator>
  941. static std::vector<std::string>::iterator insert(
  942. std::vector<std::string>& container,
  943. std::vector<std::string>::const_iterator pos, InputIterator first,
  944. InputIterator last, EmptyElements emptyElements = EmptyElements::No)
  945. {
  946. return cmList::Insert(container, pos, first, last, ExpandElements::Yes,
  947. emptyElements);
  948. }
  949. static std::vector<std::string>::iterator append(
  950. std::vector<std::string>& container, cm::string_view value,
  951. EmptyElements emptyElements = EmptyElements::No)
  952. {
  953. return cmList::insert(container, container.cend(), value, emptyElements);
  954. }
  955. static std::vector<std::string>::iterator append(
  956. std::vector<std::string>& container, std::string const& value,
  957. EmptyElements emptyElements = EmptyElements::No)
  958. {
  959. return cmList::insert(container, container.cend(), value, emptyElements);
  960. }
  961. static std::vector<std::string>::iterator append(
  962. std::vector<std::string>& container, cmValue value,
  963. EmptyElements emptyElements = EmptyElements::No)
  964. {
  965. if (value) {
  966. return cmList::append(container, *value, emptyElements);
  967. }
  968. return container.end();
  969. }
  970. template <typename InputIterator>
  971. static std::vector<std::string>::iterator append(
  972. std::vector<std::string>& container, InputIterator first,
  973. InputIterator last, EmptyElements emptyElements = EmptyElements::No)
  974. {
  975. return cmList::insert(container, container.cend(), first, last,
  976. emptyElements);
  977. }
  978. static std::vector<std::string>::iterator prepend(
  979. std::vector<std::string>& container, cm::string_view value,
  980. EmptyElements emptyElements = EmptyElements::No)
  981. {
  982. return cmList::insert(container, container.cbegin(), value, emptyElements);
  983. }
  984. static std::vector<std::string>::iterator prepend(
  985. std::vector<std::string>& container, std::string const& value,
  986. EmptyElements emptyElements = EmptyElements::No)
  987. {
  988. return cmList::insert(container, container.cbegin(), value, emptyElements);
  989. }
  990. static std::vector<std::string>::iterator prepend(
  991. std::vector<std::string>& container, cmValue value,
  992. EmptyElements emptyElements = EmptyElements::No)
  993. {
  994. if (value) {
  995. return cmList::prepend(container, *value, emptyElements);
  996. }
  997. return container.begin();
  998. }
  999. template <typename InputIterator>
  1000. static std::vector<std::string>::iterator prepend(
  1001. std::vector<std::string>& container, InputIterator first,
  1002. InputIterator last, EmptyElements emptyElements = EmptyElements::No)
  1003. {
  1004. return cmList::insert(container, container.cbegin(), first, last,
  1005. emptyElements);
  1006. }
  1007. // The following methods offer the possibility to extend a CMake list
  1008. // but without any intermediate expansion. So the operation is simply a
  1009. // string concatenation with special handling for the CMake list item
  1010. // separator
  1011. static std::string& append(std::string& list, std::string&& value);
  1012. static std::string& append(std::string& list, cm::string_view value);
  1013. template <typename InputIterator>
  1014. static std::string& append(std::string& list, InputIterator first,
  1015. InputIterator last)
  1016. {
  1017. if (first == last) {
  1018. return list;
  1019. }
  1020. return cmList::append(
  1021. list, cmList::Join(first, last, cmList::element_separator));
  1022. }
  1023. static std::string& prepend(std::string& list, std::string&& value);
  1024. static std::string& prepend(std::string& list, cm::string_view value);
  1025. template <typename InputIterator>
  1026. static std::string& prepend(std::string& list, InputIterator first,
  1027. InputIterator last)
  1028. {
  1029. if (first == last) {
  1030. return list;
  1031. }
  1032. return cmList::prepend(
  1033. list, cmList::Join(first, last, cmList::element_separator));
  1034. }
  1035. template <typename Range,
  1036. cm::enable_if_t<cm::is_range<Range>::value, int> = 0>
  1037. static std::string to_string(Range const& r)
  1038. {
  1039. return cmList::Join(r, cmList::element_separator);
  1040. }
  1041. // Non-members
  1042. // ===========
  1043. friend inline bool operator==(const cmList& lhs, const cmList& rhs) noexcept
  1044. {
  1045. return lhs.Values == rhs.Values;
  1046. }
  1047. friend inline bool operator!=(const cmList& lhs, const cmList& rhs) noexcept
  1048. {
  1049. return lhs.Values != rhs.Values;
  1050. }
  1051. private:
  1052. size_type ComputeIndex(index_type pos, bool boundCheck = true) const;
  1053. size_type ComputeInsertIndex(index_type pos, bool boundCheck = true) const;
  1054. cmList GetItems(std::vector<index_type>&& indexes) const;
  1055. cmList& RemoveItems(std::vector<index_type>&& indexes);
  1056. cmList& RemoveItems(std::vector<std::string>&& items);
  1057. static container_type::iterator Insert(container_type& container,
  1058. container_type::const_iterator pos,
  1059. std::string&& value,
  1060. ExpandElements expandElements,
  1061. EmptyElements emptyElements);
  1062. static container_type::iterator Insert(container_type& container,
  1063. container_type::const_iterator pos,
  1064. const std::string& value,
  1065. ExpandElements expandElements,
  1066. EmptyElements emptyElements)
  1067. {
  1068. auto tmp = value;
  1069. return cmList::Insert(container, pos, std::move(tmp), expandElements,
  1070. emptyElements);
  1071. }
  1072. template <typename InputIterator>
  1073. static container_type::iterator Insert(container_type& container,
  1074. container_type::const_iterator pos,
  1075. InputIterator first,
  1076. InputIterator last,
  1077. ExpandElements expandElements,
  1078. EmptyElements emptyElements)
  1079. {
  1080. auto delta = std::distance(container.cbegin(), pos);
  1081. if (first == last) {
  1082. return container.begin() + delta;
  1083. }
  1084. auto insertPos = container.begin() + delta;
  1085. if (expandElements == ExpandElements::Yes) {
  1086. for (; first != last; ++first) {
  1087. auto size = container.size();
  1088. insertPos = cmList::Insert(container, insertPos, *first,
  1089. expandElements, emptyElements);
  1090. insertPos += static_cast<decltype(delta)>(container.size() - size);
  1091. }
  1092. } else {
  1093. for (; first != last; ++first) {
  1094. if (!(*first).empty() || emptyElements == EmptyElements::Yes) {
  1095. insertPos = container.insert(insertPos, *first);
  1096. ++insertPos;
  1097. }
  1098. }
  1099. }
  1100. return container.begin() + delta;
  1101. }
  1102. static std::string const& ToString(std::string const& s) { return s; }
  1103. static std::string ToString(cm::string_view s) { return std::string{ s }; }
  1104. static std::string const& ToString(BT<std::string> const&);
  1105. template <typename Range>
  1106. static std::string Join(Range const& r, cm::string_view glue)
  1107. {
  1108. if (cm::size(r) == 0) {
  1109. return std::string{};
  1110. }
  1111. return cmList::Join(std::begin(r), std::end(r), glue);
  1112. }
  1113. template <typename InputIterator>
  1114. static std::string Join(InputIterator first, InputIterator last,
  1115. cm::string_view glue)
  1116. {
  1117. if (first == last) {
  1118. return std::string{};
  1119. }
  1120. const auto sep = std::string{ glue };
  1121. std::string joined = cmList::ToString(*first);
  1122. for (auto it = std::next(first); it != last; ++it) {
  1123. joined += sep;
  1124. joined += cmList::ToString(*it);
  1125. }
  1126. return joined;
  1127. }
  1128. container_type Values;
  1129. };
  1130. // specializations for cmList::TransformSelector allocators
  1131. // ========================================================
  1132. template <>
  1133. inline std::unique_ptr<cmList::TransformSelector>
  1134. cmList::TransformSelector::New<cmList::TransformSelector::AT>(
  1135. std::initializer_list<index_type> init)
  1136. {
  1137. return cmList::TransformSelector::NewAT(init);
  1138. }
  1139. template <>
  1140. inline std::unique_ptr<cmList::TransformSelector>
  1141. cmList::TransformSelector::New<cmList::TransformSelector::AT>(
  1142. std::vector<index_type> const& init)
  1143. {
  1144. return cmList::TransformSelector::NewAT(init);
  1145. }
  1146. template <>
  1147. inline std::unique_ptr<cmList::TransformSelector>
  1148. cmList::TransformSelector::New<cmList::TransformSelector::AT>(
  1149. std::vector<index_type>&& init)
  1150. {
  1151. return cmList::TransformSelector::NewAT(std::move(init));
  1152. }
  1153. template <>
  1154. inline std::unique_ptr<cmList::TransformSelector>
  1155. cmList::TransformSelector::New<cmList::TransformSelector::FOR>(
  1156. std::initializer_list<index_type> init)
  1157. {
  1158. return cmList::TransformSelector::NewFOR(init);
  1159. }
  1160. template <>
  1161. inline std::unique_ptr<cmList::TransformSelector>
  1162. cmList::TransformSelector::New<cmList::TransformSelector::FOR>(
  1163. std::vector<index_type> const& init)
  1164. {
  1165. return cmList::TransformSelector::NewFOR(init);
  1166. }
  1167. template <>
  1168. inline std::unique_ptr<cmList::TransformSelector>
  1169. cmList::TransformSelector::New<cmList::TransformSelector::FOR>(
  1170. std::vector<index_type>&& init)
  1171. {
  1172. return cmList::TransformSelector::NewFOR(std::move(init));
  1173. }
  1174. template <>
  1175. inline std::unique_ptr<cmList::TransformSelector>
  1176. cmList::TransformSelector::New<cmList::TransformSelector::REGEX>(
  1177. std::string const& init)
  1178. {
  1179. return cmList::TransformSelector::NewREGEX(init);
  1180. }
  1181. template <>
  1182. inline std::unique_ptr<cmList::TransformSelector>
  1183. cmList::TransformSelector::New<cmList::TransformSelector::REGEX>(
  1184. std::string&& init)
  1185. {
  1186. return cmList::TransformSelector::NewREGEX(std::move(init));
  1187. }
  1188. // Non-member functions
  1189. // ====================
  1190. inline std::vector<std::string>& operator+=(std::vector<std::string>& l,
  1191. const cmList& r)
  1192. {
  1193. l.insert(l.end(), r.begin(), r.end());
  1194. return l;
  1195. }
  1196. inline std::vector<std::string>& operator+=(std::vector<std::string>& l,
  1197. cmList&& r)
  1198. {
  1199. std::move(r.begin(), r.end(), std::back_inserter(l));
  1200. r.clear();
  1201. return l;
  1202. }
  1203. namespace std {
  1204. inline void swap(cmList& lhs, cmList& rhs) noexcept
  1205. {
  1206. lhs.swap(rhs);
  1207. }
  1208. } // namespace std
  1209. namespace cm {
  1210. inline void erase(cmList& list, const std::string& value)
  1211. {
  1212. list.erase(std::remove(list.begin(), list.end(), value), list.end());
  1213. }
  1214. template <typename Predicate>
  1215. inline void erase_if(cmList& list, Predicate pred)
  1216. {
  1217. list.erase(std::remove_if(list.begin(), list.end(), pred), list.end());
  1218. }
  1219. //
  1220. // Provide a special implementation of cm::append because, in this case,
  1221. // expansion must not be applied to the inserted elements
  1222. //
  1223. #if defined(__SUNPRO_CC) && defined(__sparc)
  1224. // Oracle DeveloperStudio C++ compiler on Solaris/Sparc fails to compile
  1225. // templates with constraints.
  1226. // So, on this platform, use only simple templates.
  1227. template <typename InputIt,
  1228. cm::enable_if_t<cm::is_input_iterator<InputIt>::value, int> = 0>
  1229. void append(cmList& v, InputIt first, InputIt last)
  1230. {
  1231. v.append(first, last, cmList::ExpandElements::No);
  1232. }
  1233. template <typename Range,
  1234. cm::enable_if_t<cm::is_input_range<Range>::value, int> = 0>
  1235. void append(cmList& v, Range const& r)
  1236. {
  1237. v.append(r.begin(), r.end(), cmList::ExpandElements::No);
  1238. }
  1239. #else
  1240. template <
  1241. typename InputIt,
  1242. cm::enable_if_t<
  1243. cm::is_input_iterator<InputIt>::value &&
  1244. std::is_convertible<typename std::iterator_traits<InputIt>::value_type,
  1245. cmList::value_type>::value,
  1246. int> = 0>
  1247. void append(cmList& v, InputIt first, InputIt last)
  1248. {
  1249. v.append(first, last, cmList::ExpandElements::No);
  1250. }
  1251. template <typename Range,
  1252. cm::enable_if_t<cm::is_input_range<Range>::value &&
  1253. std::is_convertible<typename Range::value_type,
  1254. cmList::value_type>::value,
  1255. int> = 0>
  1256. void append(cmList& v, Range const& r)
  1257. {
  1258. v.append(r.begin(), r.end(), cmList::ExpandElements::No);
  1259. }
  1260. #endif
  1261. } // namespace cm
  1262. /**
  1263. * Helper functions for legacy support. Use preferably cmList class directly
  1264. * or the static methods of the class.
  1265. */
  1266. inline void cmExpandList(
  1267. cm::string_view arg, std::vector<std::string>& argsOut,
  1268. cmList::EmptyElements emptyElements = cmList::EmptyElements::No)
  1269. {
  1270. cmList::append(argsOut, arg, emptyElements);
  1271. }
  1272. inline void cmExpandList(
  1273. cmValue arg, std::vector<std::string>& argsOut,
  1274. cmList::EmptyElements emptyElements = cmList::EmptyElements::No)
  1275. {
  1276. cmList::append(argsOut, arg, emptyElements);
  1277. }