1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539 1540 1541 1542 1543 1544 1545 1546 1547 1548 1549 1550 1551 1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 1569 1570 1571 1572 1573 1574 1575 1576 1577 1578 1579 1580 1581 1582 1583 1584 1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 1604 1605 1606 1607 1608 1609 1610 1611 1612 1613 1614 1615 1616 1617 1618 1619 1620 1621 1622 1623 1624 1625 1626 1627 1628 1629 1630 1631 1632 1633 1634 1635 1636 1637 1638 1639 1640 1641 1642 1643 1644 1645 1646 1647
|
/*-
* SPDX-License-Identifier: BSD-2-Clause
*
* Copyright (c) 2024 Maarten L. Hekkelman
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
/// \file
/// the core of the zeem XML library defining the main classes in the DOM API
#include "zeem/error.hpp"
#include "zeem/version.hpp"
#include <algorithm>
#include <cassert>
#include <compare>
#include <cstddef>
#include <cstdint>
#include <initializer_list>
#include <iosfwd>
#include <iterator>
#include <string>
#include <string_view>
#include <type_traits>
#include <utility>
#include <vector>
namespace zeem
{
// forward declarations
class attribute;
class element;
class element_container;
class node;
class text;
using node_set = std::vector<node *>;
using element_set = std::vector<element *>;
template <typename T>
concept NodeType = std::is_base_of_v<zeem::node, std::remove_cvref_t<T>>;
/**
* @brief An enum used as a poor mans RTTI, i.e. you can use this type
* to find out the actual type of a node
*/
enum class node_type : uint8_t
{
element,
text,
attribute,
comment,
cdata,
document,
processing_instruction,
header
};
// --------------------------------------------------------------------
/// \brief specification of how XML data should be written out
struct format_info
{
bool indent = false;
bool indent_attributes = false;
bool collapse_tags = true;
bool suppress_comments = false;
bool escape_white_space = false;
bool escape_double_quote = true;
bool html = false; ///< This flag can be used to collapse only 'empty elements'
std::size_t indent_width = 0;
std::size_t indent_level = 0;
version_type version{ 1, 0 };
};
// --------------------------------------------------------------------
/**
* \brief The node base class
*
* Node is the abstract base class for all data contained in zeep XML documents.
* The DOM tree consists of nodes that are linked to each other, each
* node can have a parent and siblings pointed to by the next and
* previous members. All nodes in a DOM tree share a common root node.
*
* Nodes can have a name, and the XPath specification requires that a node can
* have a so-called expanded-name. This name consists of a local-name and a
* namespace which is a URI. And we can have a QName which is a concatenation of
* a prefix (that points to a namespace URI) and a local-name separated by a colon.
*
* To reduce storage requirements, names are stored in nodes as qnames, if at all.
* the convenience functions name() and prefix() parse the qname(). ns() returns
* the namespace URI for the node, if it can be resolved.
*
* Nodes inherit the namespace of their parent unless they override it which means
* resolving prefixes and namespaces is done hierarchically
*
* Nodes are stored in a node_list, a generic list class that resembles std::list
*/
class node
{
public:
/** @cond */
node &operator=(const node &n) = delete;
node &operator=(node &&n) = delete;
virtual ~node() = default;
/** @endcond */
/// \brief node_type to be returned by each implementation of this node class
[[nodiscard]] virtual constexpr node_type type() const = 0;
/// content of a xml:lang attribute of this element, or its nearest ancestor
[[nodiscard]] virtual std::string lang() const;
/**
* @brief Get the qualified name
*
* Nodes can have a name, and the XPath specification requires that a node can
* have a so-called expanded-name. This name consists of a local-name and a
* namespace which is a URI. And we can have a QName which is a concatenation of
* a prefix (that points to a namespace URI) and a local-name separated by a colon.
*
* To reduce storage requirements, names are stored in nodes as qnames, if at all.
*
* @return std::string
*/
[[nodiscard]] virtual std::string get_qname() const;
/**
* @brief Set the qualified name to @a qn
*
* This is only meaningful in attributes and elements.
*
* @param qn
*/
virtual void set_qname([[maybe_unused]] std::string qn) {} // NOLINT(performance-unnecessary-value-param)
/**
* \brief set the qname with two parameters, if \a prefix is empty the qname will be simply \a name
* otherwise the name will be `prefix:name`
*
* \param prefix The namespace prefix to use
* \param name The actual name to use
*/
void set_qname(const std::string &prefix, std::string name)
{
set_qname(prefix.empty() ? std::move(name) : prefix + ':' + name);
}
[[nodiscard]] virtual std::string name() const; ///< The name for the node as parsed from the qname.
[[nodiscard]] virtual std::string get_prefix() const; ///< The prefix for the node as parsed from the qname.
[[nodiscard]] virtual std::string get_ns() const; ///< Returns the namespace URI for the node, if it can be resolved.
/// Return the namespace URI for a prefix
[[nodiscard]] virtual std::string namespace_for_prefix(std::string_view prefix) const;
/// Return the prefix for a namespace URI
[[nodiscard]] virtual std::pair<std::string, bool> prefix_for_namespace(std::string_view uri) const;
/// Prefix the \a tag with the namespace prefix for \a uri
[[nodiscard]] virtual std::string prefix_tag(const std::string &tag, std::string_view uri) const;
/// return all content concatenated, including that of children.
[[nodiscard]] virtual std::string str() const = 0;
// --------------------------------------------------------------------
// low level routines
// basic access
// All nodes should have a single root node
virtual element_container *root(); ///< The root node for this node
[[nodiscard]] virtual const element_container *root() const; ///< The root node for this node
void parent(element_container *p) noexcept { m_parent = p; } ///< Set parent to \a p
element_container *parent() { return m_parent; } ///< The parent node for this node
[[nodiscard]] const element_container *parent() const { return m_parent; } ///< The parent node for this node
void next(const node *n) noexcept { m_next = const_cast<node *>(n); } ///< Set next to \a n
node *next() { return m_next; } ///< The next sibling
[[nodiscard]] const node *next() const { return m_next; } ///< The next sibling
void prev(const node *n) noexcept { m_prev = const_cast<node *>(n); } ///< Set prev to \a n
node *prev() { return m_prev; } ///< The previous sibling
[[nodiscard]] const node *prev() const { return m_prev; } ///< The previous sibling
/// Compare the node with \a n
virtual bool equals(const node *n) const;
/// \brief low level routine for writing out XML
///
/// This method is usually called by operator<<(std::ostream&, zeem::document&)
virtual void write(std::ostream &os, format_info fmt) const = 0;
protected:
/** @cond */
friend class basic_node_list;
template <typename>
friend class node_list;
friend class element;
node()
{
init();
}
node([[maybe_unused]] const node &n)
{
init();
}
node([[maybe_unused]] node &&n) noexcept
{
init();
}
friend void swap(node &a, node &b) noexcept
{
if (a.m_next == &a) // a empty?
{
if (b.m_next != &b) // b empty?
{
a.m_next = b.m_next;
a.m_prev = b.m_prev;
b.init();
}
}
else if (b.m_next == &b)
{
b.m_next = a.m_next;
b.m_prev = a.m_prev;
a.init();
}
else
{
std::swap(a.m_next, b.m_next);
std::swap(a.m_prev, b.m_prev);
}
a.m_next->m_prev = a.m_prev->m_next = &a;
b.m_next->m_prev = b.m_prev->m_next = &b;
}
protected:
void init()
{
m_next = m_prev = this;
}
element_container *m_parent = nullptr;
node *m_next{};
node *m_prev{};
/** @endcond */
};
// --------------------------------------------------------------------
// Basic node list is a private class, it is the base class
// for node_list
/** @cond */
class basic_node_list
{
protected:
struct node_list_header : public node
{
[[nodiscard]] constexpr node_type type() const override { return node_type::header; }
void write(std::ostream & /* os */, format_info /* fmt */) const override {}
[[nodiscard]] std::string str() const override { return {}; }
friend void swap(node_list_header &a, node_list_header &b) noexcept
{
swap(static_cast<node &>(a), static_cast<node &>(b));
for (node *n = a.m_next; n != &a; n = n->next())
n->parent(a.m_parent);
for (node *n = b.m_next; n != &b; n = n->next())
n->parent(b.m_parent);
}
};
private:
node_list_header m_header_node;
node *m_header = nullptr;
bool m_owner = false;
template <typename T>
friend class node_list;
protected:
explicit basic_node_list(element_container *e)
: m_header(&m_header_node)
{
m_header_node.parent(e);
}
public:
basic_node_list(basic_node_list &&nl) = delete;
basic_node_list &operator=(const basic_node_list &nl) = delete;
basic_node_list &operator=(basic_node_list &&nl) = delete;
virtual ~basic_node_list() = default;
bool operator==(const basic_node_list &b) const;
/// \brief remove all nodes
virtual void clear();
protected:
friend void swap(basic_node_list &a, basic_node_list &b) noexcept
{
if (a.m_header != &a.m_header_node and b.m_header != &b.m_header_node)
std::swap(a.m_header, b.m_header);
else
{
assert((a.m_header != &a.m_header_node) == (b.m_header != &b.m_header_node));
swap(a.m_header_node, b.m_header_node);
}
}
protected:
// proxy methods for every insertion
virtual node *insert_impl(const node *p, node *n);
node *erase_impl(node *n);
};
/** @endcond */
// --------------------------------------------------------------------
/**
* \brief generic iterator class.
*
* We can have iterators that point to nodes, elements and attributes.
* Iterating over nodes is simply following next/prev. But iterating
* elements is a bit more difficult, since you then have to skip nodes
* in between that are not an element, like comments or text.
*
* This iterator is used for iterators over elements, attributes and
* simply all nodes
*/
template <typename T>
class iterator_impl
{
public:
/** @cond */
template <typename T2>
friend class iterator_impl;
/** @endcond */
using iterator_category = std::bidirectional_iterator_tag;
using value_type = T;
using pointer = value_type *;
using reference = value_type &;
using difference_type = std::ptrdiff_t;
iterator_impl() = default;
// NOLINTBEGIN(hicpp-explicit-conversions)
iterator_impl(node *current)
: m_current(current)
{
skip();
}
iterator_impl(const node *current)
requires(std::is_const_v<value_type>)
: iterator_impl(const_cast<node *>(current))
{
}
iterator_impl(const iterator_impl &i) = default;
/**
* @brief Copy constructor
*
* This copy constructor allows to copy from the same value_type
* and from derived types. That means that you can assign an
* iterator pointing to an element to a new iterator pointing
* to nodes. But vice versa is not possible.
*/
template <typename Iterator>
requires(std::is_base_of_v<value_type, typename Iterator::value_type>)
iterator_impl(const Iterator &i)
: m_current(const_cast<node *>(i.m_current))
{
skip();
}
// NOLINTEND(hicpp-explicit-conversions)
iterator_impl &operator=(iterator_impl i)
{
m_current = i.m_current;
return *this;
}
template <typename Iterator>
requires(std::is_base_of_v<value_type, typename Iterator::value_type>)
iterator_impl &operator=(const Iterator &i)
{
m_current = i.m_current;
return *this;
}
reference operator*() const { return *static_cast<value_type *>(m_current); }
pointer operator->() const { return static_cast<value_type *>(m_current); }
iterator_impl &operator++()
{
m_current = m_current->next();
skip();
return *this;
}
iterator_impl operator++(int)
{
iterator_impl iter(*this);
operator++();
return iter;
}
iterator_impl &operator--()
{
m_current = m_current->prev();
if constexpr (std::is_same_v<std::remove_cv_t<value_type>, element>)
{
while (m_current->type() != node_type::element and m_current->type() != node_type::header)
m_current = m_current->prev();
}
return *this;
}
iterator_impl operator--(int)
{
iterator_impl iter(*this);
operator--();
return iter;
}
template <typename IteratorType>
requires NodeType<typename IteratorType::node_type>
bool operator==(const IteratorType &other) const
{
return m_current == other.m_current;
}
bool operator==(const iterator_impl &other) const
{
return m_current == other.m_current;
}
template <NodeType T2>
bool operator==(const T2 *n) const { return m_current == n; }
explicit operator pointer() const { return static_cast<pointer>(m_current); }
private:
using node_base_type = std::conditional_t<std::is_const_v<T>, const node, node>;
void skip()
{
if constexpr (std::is_same_v<std::remove_cv_t<value_type>, element>)
{
while (m_current->type() != node_type::element and m_current->type() != node_type::header)
m_current = m_current->next();
}
}
node_base_type *m_current = nullptr;
};
// --------------------------------------------------------------------
/**
* @brief An abstract base class for lists of type \a T
*
* This base class should offer all methods required for a
* SequenceContainer.
*
* This class is not exported.
*
* Note that this class can act as a real container, which
* stores data, or it can act as a view on another node_list
* optionally changing what is made visible.
*
* An element derives from node_list<element>, so it exposes
* access to all its children of type element. However, since
* node_lists store pointers to nodes, the list can contain
* more than just element nodes. To access these other nodes
* you can use a node_list<node> constructed with an element
* as parameter. This node_list<node> will expose all nodes
* in the element.
*
* @tparam T The type of node contained, either element, attribute or node
*/
template <typename T = node>
class node_list : public basic_node_list
{
public:
using value_type = T;
using allocator_type = std::allocator<value_type>;
using size_type = size_t;
using difference_type = std::ptrdiff_t;
using reference = value_type &;
using const_reference = const value_type &;
using pointer = value_type *;
using const_pointer = const value_type *;
/**
* @brief Construct a new node list for an element_container \a e
*
* @param e The element_container
*/
private:
node_list(element_container *e); // NOLINT(hicpp-explicit-conversions)
friend class element_container;
friend class attribute_set;
public:
/// @brief The iterator class
using iterator = iterator_impl<value_type>;
static_assert(std::input_iterator<iterator>);
/// @brief The const iterator class
using const_iterator = iterator_impl<const value_type>;
static_assert(std::input_iterator<const_iterator>);
[[nodiscard]] iterator begin() { return iterator(m_header->m_next); }
[[nodiscard]] iterator end() { return iterator(m_header); }
[[nodiscard]] const_iterator cbegin() { return const_iterator(m_header->m_next); }
[[nodiscard]] const_iterator cend() { return const_iterator(m_header); }
[[nodiscard]] const_iterator begin() const { return const_iterator(m_header->m_next); }
[[nodiscard]] const_iterator end() const { return const_iterator(m_header); }
[[nodiscard]] value_type &front() { return *begin(); }
[[nodiscard]] const value_type &front() const { return *begin(); }
[[nodiscard]] value_type &back() { return *std::prev(end()); }
[[nodiscard]] const value_type &back() const { return *std::prev(end()); }
/// @brief The size of the visible items
/// @return The count of items visible
[[nodiscard]] size_t size() const { return std::distance(begin(), end()); }
[[nodiscard]] bool empty() const { return size() == 0; }
explicit operator bool() const { return not empty(); }
/// \brief insert a copy of \a e
iterator insert(const_iterator pos, const value_type &e);
/// \brief insert a copy of \a e at position \a pos, moving its data
iterator insert(const_iterator pos, value_type &&e);
/// \brief construct a new node using arguments provided in \a a
// TODO: maarten - When users try to emplace/insert e.g. a cdata node in an element
// this will fail, since they need to use the nodes() variant. However,
// a better error is required in that case. Perhaps using concepts?
template <typename... Args>
iterator insert(const_iterator p, Args &&...args)
requires(sizeof...(Args) > 1 or not std::is_base_of_v<node, std::remove_cvref_t<Args>...>)
{
return insert_impl(p, new value_type(std::forward<Args>(args)...));
}
iterator insert(const_iterator pos, size_t count, const value_type &n)
{
iterator p(const_cast<value_type *>(&*pos));
while (count-- > 0)
p = insert(p, n);
return p;
}
/// \brief insert copies of the nodes from \a first to \a last at position \a pos
template <typename InputIter>
iterator insert(const_iterator pos, InputIter first, InputIter last)
{
iterator p(const_cast<value_type *>(&*pos));
iterator result = p;
bool f = true;
for (auto i = first; i != last; ++i, ++p)
{
p = insert(p, *i);
if (std::exchange(f, false))
result = p;
}
return result;
}
/// \brief insert copies of the nodes in \a nodes at position \a pos
iterator insert(const_iterator pos, std::initializer_list<value_type> nodes)
{
return insert(pos, nodes.begin(), nodes.end());
}
/// \brief replace content with copies of the nodes from \a first to \a last
template <typename InputIter>
void assign(InputIter first, InputIter last)
{
basic_node_list::clear();
insert(begin(), first, last);
}
/// \brief emplace an element at position \a p using arguments \a args
template <typename... Args>
iterator emplace(const_iterator p, Args &&...args)
{
return insert(p, std::forward<Args>(args)...);
}
/// \brief emplace an element at the front using arguments \a args
template <typename... Args>
iterator emplace_front(Args &&...args)
{
return emplace(begin(), std::forward<Args>(args)...);
}
/// \brief emplace an element at the back using arguments \a args
template <typename... Args>
iterator emplace_back(Args &&...args)
{
return emplace(end(), std::forward<Args>(args)...);
}
/// \brief erase the node at \a pos
iterator erase(const_iterator pos)
{
return erase_impl(pos);
}
/// \brief erase the nodes from \a first to \a last
iterator erase(iterator first, iterator last)
{
while (first != last)
{
auto next = first;
++next;
erase(first);
first = next;
}
return last;
}
/// \brief erase the first node
void pop_front()
{
erase(begin());
}
/// \brief erase the last node
void pop_back()
{
erase(std::prev(end()));
}
/// \brief move the value_type \a e to the front of this value_type.
void push_front(value_type &&e)
{
emplace(begin(), std::forward<value_type>(e));
}
/// \brief copy the value_type \a e to the front of this value_type.
void push_front(const value_type &e)
{
emplace(begin(), e);
}
/// \brief move the value_type \a e to the back of this value_type.
void push_back(value_type &&e)
{
emplace(end(), std::forward<value_type>(e));
}
/// \brief copy the value_type \a e to the back of this value_type.
void push_back(const value_type &e)
{
emplace(end(), e);
}
/// \brief Sort the nodes
template <typename Pred>
void sort(const Pred &pred);
protected:
using basic_node_list::insert_impl;
node *insert_impl(const_iterator pos, node *n)
{
return insert_impl(&*pos, n);
}
using basic_node_list::erase_impl;
node *erase_impl(iterator pos)
{
return basic_node_list::erase_impl(&*pos);
}
friend T;
};
// --------------------------------------------------------------------
/**
* @brief internal class as base class for element and document
*
* Both element and document can have a list of child nodes and
* both are nodes implementing the namespace routines e.g.
*
* However, element has attributes whereas document does not.
* And document has the constraint that it can have at most
* one child element. But since the rest is so similar they
* have a common base class: element_container.
*
* element_container is not exported.
*/
class element_container : public node, public node_list<element>
{
public:
/// @brief Default constructor
element_container()
: node_list<element>(this)
{
}
/// @brief Copy constructor
element_container(const element_container &e)
: node(e)
, node_list<element>(this)
{
auto a = nodes();
auto b = e.nodes();
a.assign(b.begin(), b.end());
}
/// @brief Destructor
~element_container() override
{
clear();
}
// --------------------------------------------------------------------
/** @cond */
friend void swap(element_container &a, element_container &b) noexcept
{
swap(static_cast<node_list<element> &>(a), static_cast<node_list<element> &>(b));
}
/** @endcond */
// --------------------------------------------------------------------
// children
/**
* @brief This method allows access to the nodes not visible using
* the regular interface of this class itself.
*
* @return node_list<> The node_list for nodes of all types
*/
node_list<> nodes() { return { this }; }
/**
* @brief This method allows read access to the nodes not visible using
* the regular interface of this class itself.
*
* @return node_list<> The node_list for nodes of all types
*/
[[nodiscard]] const node_list<> nodes() const { return node_list<node>(const_cast<element_container *>(this)); }
/// \brief will return the concatenation of str() from all child nodes
[[nodiscard]] std::string str() const override;
/// \brief return the elements that match XPath \a path.
///
/// If you need to find other classes than xml::element, of if your XPath
/// contains variables, you should create a zeem::xpath object and use
/// its evaluate method.
[[nodiscard]] element_set find(std::string_view path) const;
/// \brief return the first element that matches XPath \a path.
///
/// If you need to find other classes than xml::element, of if your XPath
/// contains variables, you should create a zeem::xpath object and use
/// its evaluate method.
[[nodiscard]] iterator find_first(std::string_view path);
[[nodiscard]] const_iterator find_first(std::string_view path) const;
/** @cond */
void write(std::ostream &os, format_info fmt) const override;
/** @endcond */
};
// --------------------------------------------------------------------
// internal node base class for storing text
/**
* @brief An abstract base class for nodes that contain text
*
*/
class node_with_text : public node
{
protected:
/** @cond */
node_with_text() = default;
explicit node_with_text(std::string s)
: m_text(std::move(s))
{
}
node_with_text(const node_with_text &n) = default;
node_with_text(node_with_text &&n) = default;
public:
friend void swap(node_with_text &a, node_with_text &b) noexcept
{
std::swap(a.m_text, b.m_text);
}
/** @endcond */
/// \brief return the text content
[[nodiscard]] std::string str() const override { return m_text; }
/// \brief return the text content, same as str()
[[nodiscard]] virtual std::string get_text() const { return m_text; }
/// \brief set the text content
virtual void set_text(std::string text) { m_text = std::move(text); }
/// @brief Compare two nodes with text for equality
bool equals(const node *n) const override
{
return type() == n->type() and
static_cast<const node_with_text *>(n)->m_text == m_text;
}
/** @cond */
protected:
void append_text(std::string_view txt)
{
m_text += txt;
}
std::string m_text;
/** @endcond */
};
// --------------------------------------------------------------------
/**
* @brief A node containing a XML comment
*
*/
class comment final : public node_with_text
{
public:
[[nodiscard]] constexpr node_type type() const override { return node_type::comment; }
/// @brief default constructor
explicit comment(std::string text = {})
: node_with_text(std::move(text))
{
}
/// @brief copy constructor
comment(const comment &c) = default;
/// @brief move constructor
comment(comment &&c) noexcept
{
swap(*this, c);
}
/// @brief assignment operator
comment &operator=(comment c) noexcept
{
swap(*this, c);
return *this;
}
/// \brief compare nodes for equality
bool equals(const node *n) const override
{
return this == n or (n->type() == node_type::comment and node_with_text::equals(n));
}
/** @cond */
void write(std::ostream &os, format_info fmt) const override;
/** @endcond */
};
// --------------------------------------------------------------------
/**
* @brief A node containing a XML processing instruction (like e.g. \<?php ?\>)
*
*/
class processing_instruction final : public node_with_text
{
public:
[[nodiscard]] constexpr node_type type() const override { return node_type::processing_instruction; }
/// @brief default constructor
processing_instruction() = default;
/// \brief constructor with parameters
///
/// This constructs a processing instruction with the specified parameters
/// \param target The target, this will follow the <? characters, e.g. `php` will generate <?php ... ?>
/// \param text The text inside this node, e.g. the PHP code.
processing_instruction(std::string target, std::string text)
: node_with_text(std::move(text))
, m_target(std::move(target))
{
}
/// @brief copy constructor
processing_instruction(const processing_instruction &pi) = default;
/// @brief move constructor
processing_instruction(processing_instruction &&pi) noexcept = default;
/// @brief assignment operator
processing_instruction &operator=(processing_instruction pi) noexcept
{
swap(*this, pi);
return *this;
}
/** @cond */
friend void swap(processing_instruction &a, processing_instruction &b) noexcept
{
swap(static_cast<node_with_text &>(a), static_cast<node_with_text &>(b));
std::swap(a.m_target, b.m_target);
}
/** @endcond */
/// \brief return the qname which is the same as the target in this case
[[nodiscard]] std::string get_qname() const override { return m_target; }
/// \brief return the target
[[nodiscard]] std::string get_target() const { return m_target; }
/// \brief set the target
void set_target(std::string target) { m_target = std::move(target); }
/// \brief compare nodes for equality
bool equals(const node *n) const override
{
return this == n or (n->type() == node_type::processing_instruction and node_with_text::equals(n));
}
/** @cond */
void write(std::ostream &os, format_info fmt) const override;
private:
std::string m_target;
/** @endcond */
};
// --------------------------------------------------------------------
/**
* @brief A node containing text.
*
*/
class text final : public node_with_text
{
public:
[[nodiscard]] constexpr node_type type() const override { return node_type::text; }
/// @brief default constructor
explicit text(std::string text = {})
: node_with_text(std::move(text))
{
}
/// @brief copy constructor
text(const text &t) = default;
/// @brief move constructor
text(text &&t) noexcept = default;
/// @brief assignment operator
text &operator=(text txt) noexcept
{
swap(*this, txt);
return *this;
}
/// \brief append \a text to the stored text
void append(std::string_view text) { append_text(text); }
/// \brief compare nodes for equality
bool equals(const node *n) const override;
/// \brief returns true if this text contains only whitespace characters
[[nodiscard]] bool is_space() const;
/** @cond */
void write(std::ostream &os, format_info fmt) const override;
/** @endcond */
};
// --------------------------------------------------------------------
/**
* @brief A node containing the contents of a CDATA section. Normally, these nodes are
* converted to text nodes but you can specify to preserve them when parsing a document.
*
*/
class cdata final : public node_with_text
{
public:
[[nodiscard]] constexpr node_type type() const override { return node_type::cdata; }
/// @brief default constructor
explicit cdata(std::string s = {})
: node_with_text(std::move(s))
{
}
/// @brief copy constructor
cdata(const cdata &cd) = default;
/// @brief move constructor
cdata(cdata &&cd) noexcept = default;
/// @brief assignment operator
cdata &operator=(cdata cd) noexcept
{
swap(*this, cd);
return *this;
}
/// \brief append \a text to the stored text
void append(std::string_view text) { append_text(text); }
/// \brief compare nodes for equality
bool equals(const node *n) const override
{
return this == n or (n->type() == node_type::cdata and node_with_text::equals(n));
}
/** @cond */
void write(std::ostream &os, format_info fmt) const override;
/** @endcond */
};
// --------------------------------------------------------------------
/**
* @brief An attribute is a node, has an element as parent, but is not a child of this parent (!)
*
*/
class attribute final : public node
{
public:
[[nodiscard]] constexpr node_type type() const override { return node_type::attribute; }
/// @brief constructor
/// @param qname The qualified name
/// @param value The value
/// @param id Flag to indicate if this is an ID attribute
attribute(std::string_view qname, std::string_view value, bool id = false)
: m_qname(qname)
, m_value(value)
, m_id(id)
{
}
/// @brief copy constructor
attribute(const attribute &attr) = default;
/// @brief move constructor
attribute(attribute &&attr) noexcept
: node(std::forward<attribute>(attr))
{
swap(*this, attr);
}
/// @brief assignment operator
attribute &operator=(attribute attr) noexcept
{
swap(*this, attr);
return *this;
}
/** @cond */
friend void swap(attribute &a, attribute &b) noexcept
{
std::swap(a.m_qname, b.m_qname);
std::swap(a.m_value, b.m_value);
std::swap(a.m_id, b.m_id);
}
/** @endcond */
/// @brief Attributes can be sorted
friend std::strong_ordering operator<=>(const attribute &a, const attribute &b)
{
if (auto cmp = (a.m_qname <=> b.m_qname); cmp != 0)
return cmp;
if (auto cmp = (a.m_id <=> b.m_id); cmp != 0)
return cmp;
return a.m_value <=> b.m_value;
}
/// @brief Compare two attributes for equality
bool operator==(const attribute &rhs) const
{
return equals(&rhs);
}
/// @brief Get the qualified name for this attribute
[[nodiscard]] std::string get_qname() const override { return m_qname; }
/// @brief Set the qualified name to \a qn
void set_qname(std::string qn) override { m_qname = std::move(qn); }
using node::set_qname;
/// \brief Is this attribute an xmlns attribute?
[[nodiscard]] bool is_namespace() const
{
return m_qname.starts_with("xmlns") and (m_qname.length() == 5 or m_qname[5] == ':');
}
/// @brief Return the value of this attribute
[[nodiscard]] std::string value() const { return m_value; }
/// @brief Set the value of this attribute to \a v
void set_value(std::string v) { m_value = std::move(v); }
/// @brief Set the value of this attribute to \a v
void set_value(std::string_view v) { m_value = v; }
/// \brief same as value, but checks to see if this really is a namespace attribute
[[nodiscard]] std::string uri() const;
/// @brief Returns the value of this attribute
[[nodiscard]] std::string str() const override { return m_value; }
/// \brief compare nodes for equality
bool equals(const node *n) const override
{
bool result = false;
if (type() == n->type())
{
auto an = static_cast<const attribute *>(n);
result = an->m_id == m_id and an->m_qname == m_qname and an->m_value == m_value;
}
return result;
}
/// \brief returns whether this attribute is an ID attribute, as defined in an accompanying DTD
[[nodiscard]] bool is_id() const { return m_id; }
/// \brief support for structured binding
template <size_t N>
[[nodiscard]] decltype(auto) get() const
{
if constexpr (N == 0)
return name();
else if constexpr (N == 1)
return value();
}
/** @cond */
void write(std::ostream &os, format_info fmt) const override;
private:
std::string m_qname, m_value;
bool m_id{};
/** @endcond */
};
// --------------------------------------------------------------------
/**
* @brief set of attributes and name_spaces. Is a node_list but with a set interface
*
*/
class attribute_set : public node_list<attribute>
{
public:
/// @brief constructor to create an attribute_set for an element
explicit attribute_set(element_container *el)
: node_list(el)
{
}
/// @brief destructor
~attribute_set() override
{
clear();
}
/** @cond */
friend void swap(attribute_set &a, attribute_set &b) noexcept
{
swap(static_cast<node_list<attribute> &>(a), static_cast<node_list<attribute> &>(b));
}
/** @endcond */
/// \brief return true if the attribute with name \a key is defined
[[nodiscard]] bool contains(std::string_view key) const
{
return find(key) != end();
}
/// \brief return const_iterator to the attribute with name \a key
[[nodiscard]] const_iterator find(std::string_view key) const
{
for (auto i = begin(); i != end(); ++i)
{
if (i->get_qname() == key)
return i;
}
return end();
}
/// \brief return iterator to the attribute with name \a key
iterator find(std::string_view key)
{
return iterator{ const_cast<const attribute_set &>(*this).find(key) };
}
/// \brief emplace a newly constructed attribute with argumenst \a args
template <typename... Args>
std::pair<iterator, bool> emplace(Args &&...args)
{
return emplace(value_type{ std::forward<decltype(args)>(args)... });
}
/// \brief emplace an attribute move constructed from \a a
/// \return returns a std::pair with an iterator pointing to the inserted attribute
/// and a boolean indicating if this attribute was inserted instead of replaced.
std::pair<iterator, bool> emplace(value_type &&a)
{
bool inserted = false;
auto i = find(a.get_qname());
if (i != node_list::end())
*i = std::forward<value_type>(a); // move assign value of a
else
{
i = iterator{ node_list::insert_impl(node_list::end(), new attribute(std::forward<value_type>(a))) };
inserted = true;
}
return std::make_pair(i, inserted);
}
using node_list::erase;
/// \brief remove attribute with name \a key
size_type erase(std::string_view key)
{
size_type result = 0;
auto i = find(key);
if (i != node_list::end())
{
erase(i);
result = 1;
}
return result;
}
};
// --------------------------------------------------------------------
/**
* @brief the element class modelling a XML element
*
* element is the most important zeem::node object. It encapsulates a
* XML element as found in the XML document. It has a qname, can have children,
* attributes and a namespace.
*/
class element final : public element_container
{
public:
[[nodiscard]] constexpr node_type type() const override { return node_type::element; }
/// @brief default constructor
element()
: m_attributes(this)
{
}
/// @brief constructor taking a \a qname and a list of \a attributes
explicit element(std::string_view qname, std::initializer_list<attribute> attributes = {})
: m_qname(qname)
, m_attributes(this)
{
m_attributes.assign(attributes.begin(), attributes.end());
}
/// @brief constructor taking a \a qname and a list of child elements
element(std::string_view qname, std::initializer_list<element> il)
: m_qname(qname)
, m_attributes(this)
{
assign(il.begin(), il.end());
}
/// @brief copy constructor
element(const element &e)
: element_container(e)
, m_qname(e.m_qname)
, m_attributes(this)
{
m_attributes.assign(e.m_attributes.begin(), e.m_attributes.end());
}
/// @brief move constructor
element(element &&e) noexcept
: m_attributes(this)
{
swap(*this, e);
}
/// @brief assignment operator
element &operator=(element e) noexcept
{
swap(*this, e);
return *this;
}
/** @cond */
friend void swap(element &a, element &b) noexcept
{
// swap(static_cast<node&>(a), static_cast<node&>(b));
swap(static_cast<element_container &>(a), static_cast<element_container &>(b));
std::swap(a.m_qname, b.m_qname);
swap(a.m_attributes, b.m_attributes);
}
/** @endcond */
using node::set_qname;
/// @brief Return the qualified name
[[nodiscard]] std::string get_qname() const override { return m_qname; }
/// @brief Set the qualified name to \a qn
void set_qname(std::string qn) override { m_qname = std::move(qn); }
/// \brief content of a xml:lang attribute of this element, or its nearest ancestor
[[nodiscard]] std::string lang() const override;
/// \brief content of the xml:id attribute, or the attribute that was defined to be
/// of type ID by the DOCTYPE.
[[nodiscard]] std::string id() const;
/// @brief Compare two elements for equality
bool operator==(const element &e) const
{
return equals(&e);
}
/// @brief Compare two elements for equality
bool equals(const node *n) const override;
// --------------------------------------------------------------------
// attribute support
/// \brief return the set of attributes for this element
attribute_set &attributes() { return m_attributes; }
/// \brief return the set of attributes for this element
[[nodiscard]] const attribute_set &attributes() const { return m_attributes; }
// --------------------------------------------------------------------
/// \brief return the URI of the namespace for \a prefix
[[nodiscard]] std::string namespace_for_prefix(std::string_view prefix) const override;
/// \brief return the prefix for the XML namespace with uri \a uri.
/// \return The result is a pair of a std::string containing the actual prefix value
/// and a boolean indicating if the namespace was found at all, needed since empty prefixes
/// are allowed.
[[nodiscard]] std::pair<std::string, bool> prefix_for_namespace(std::string_view uri) const override;
/// \brief move this element and optionally everyting beneath it to the
/// specified namespace/prefix
///
/// \param prefix The new prefix name
/// \param uri The new namespace uri
/// \param recursive Apply this to the child nodes as well
/// \param including_attributes Move the attributes to this new namespace as well
void move_to_name_space(const std::string &prefix, std::string_view uri,
bool recursive, bool including_attributes);
// --------------------------------------------------------------------
/// \brief write the element to \a os
friend std::ostream &operator<<(std::ostream &os, const element &e);
// friend class document;
/// \brief return the concatenation of the content of all enclosed zeem::text nodes
[[nodiscard]] std::string get_content() const;
/// \brief replace all existing child text nodes with a new single text node containing \a content
void set_content(std::string content);
/// \brief return the value of attribute name \a qname or the empty string if not found
[[nodiscard]] std::string get_attribute(std::string_view qname) const;
/// \brief set the value of attribute named \a qname to the value \a value
void set_attribute(std::string_view qname, std::string_view value);
/// \brief The set_text method replaces any text node with the new text (call set_content)
void set_text(std::string s);
/// The add_text method checks if the last added child is a text node,
/// and if so, it appends the string to this node's value. Otherwise,
/// it adds a new text node child with the new text.
void add_text(std::string s);
/// To combine all adjacent child text nodes into one
void flatten_text();
/** @cond */
void write(std::ostream &os, format_info fmt) const override;
private:
std::string m_qname;
attribute_set m_attributes;
/** @endcond */
};
// --------------------------------------------------------------------
/** @cond */
template <typename T>
inline node_list<T>::node_list(element_container *e)
: basic_node_list(e)
{
if constexpr (std::is_same_v<value_type, node>)
m_header = e->m_header;
}
template <>
inline auto node_list<element>::insert(const_iterator pos, const element &e) -> iterator
{
return iterator{ insert_impl(pos, new element(e)) };
}
/// \brief insert a copy of \a e at position \a pos, moving its data
template <>
inline auto node_list<element>::insert(const_iterator pos, element &&e) -> iterator
{
return iterator{ insert_impl(pos, new element(std::forward<value_type>(e))) };
}
template <>
inline auto node_list<attribute>::insert(const_iterator pos, const attribute &e) -> iterator
{
return iterator{ insert_impl(pos, new attribute(e)) };
}
/// \brief insert a copy of \a e at position \a pos, moving its data
template <>
inline auto node_list<attribute>::insert(const_iterator pos, attribute &&e) -> iterator
{
return iterator{ insert_impl(pos, new attribute(std::forward<value_type>(e))) };
}
// NOLINTBEGIN(cppcoreguidelines-owning-memory,cppcoreguidelines-pro-type-static-cast-downcast)
template <>
inline auto node_list<node>::insert(const_iterator pos, const value_type &e) -> iterator
{
switch (e.type())
{
case node_type::element:
return insert_impl(pos, new element(static_cast<const element &>(e)));
break;
case node_type::text:
return insert_impl(pos, new text(static_cast<const text &>(e)));
break;
case node_type::attribute:
return insert_impl(pos, new attribute(static_cast<const attribute &>(e)));
break;
case node_type::comment:
return insert_impl(pos, new comment(static_cast<const comment &>(e)));
break;
case node_type::cdata:
return insert_impl(pos, new cdata(static_cast<const cdata &>(e)));
break;
case node_type::processing_instruction:
return insert_impl(pos, new processing_instruction(static_cast<const processing_instruction &>(e)));
break;
default:
throw exception("internal error");
}
}
/// \brief insert a copy of \a e at position \a pos, moving its data
template <>
inline auto node_list<node>::insert(const_iterator pos, value_type &&e) -> iterator
{
switch (e.type())
{
case node_type::element:
return insert_impl(pos, new element(std::forward<element &&>(static_cast<element &&>(e))));
break;
case node_type::text:
return insert_impl(pos, new text(std::forward<text &&>(static_cast<text &&>(e))));
break;
case node_type::attribute:
return insert_impl(pos, new attribute(std::forward<attribute &&>(static_cast<attribute &&>(e))));
break;
case node_type::comment:
return insert_impl(pos, new comment(std::forward<comment &&>(static_cast<comment &&>(e))));
break;
case node_type::cdata:
return insert_impl(pos, new cdata(std::forward<cdata &&>(static_cast<cdata &&>(e))));
break;
case node_type::processing_instruction:
return insert_impl(pos, new processing_instruction(std::forward<processing_instruction &&>(static_cast<processing_instruction &&>(e))));
break;
default:
throw exception("internal error");
}
}
// NOLINTEND(cppcoreguidelines-owning-memory,cppcoreguidelines-pro-type-static-cast-downcast)
template <typename T>
template <typename Pred>
void node_list<T>::sort(const Pred &pred)
{
std::vector<node *> t;
for (auto n = m_header->m_next; n != m_header; n = n->m_next)
t.push_back(n);
std::sort(t.begin(), t.end(), [pred](node *a, node *b)
{ return pred(static_cast<T &>(*a), static_cast<T &>(*b)); });
auto p = m_header;
for (auto n : t)
{
n->m_prev = p;
p->m_next = n;
p = n;
}
p->m_next = m_header;
m_header->m_prev = p;
}
/** @endcond */
// --------------------------------------------------------------------
/**
* \brief This method fixes namespace attribute when transferring an element
* from one document to another (replaces prefixes e.g.)
*
* When moving an element from one document to another, we need to fix the
* namespaces, make sure the destination has all the namespace specifications
* required by the element and make sure the prefixes used are correct.
* \param e The element that is being transferred
* \param source The (usually) document element that was the source
* \param dest The (usually) document element that is the destination
*/
void fix_namespaces(element &e, const element &source, const element &dest);
} // namespace zeem
// --------------------------------------------------------------------
// structured binding support
/** @cond */
namespace std
{
template <>
struct tuple_size<::zeem::attribute>
: public std::integral_constant<std::size_t, 2>
{
};
template <>
struct tuple_element<0, ::zeem::attribute>
{
using type = decltype(std::declval<::zeem::attribute>().name());
};
template <>
struct tuple_element<1, ::zeem::attribute>
{
using type = decltype(std::declval<::zeem::attribute>().value());
};
/** @endcond */
} // namespace std
|