|
| 1 | +#include <odr/internal/ooxml/presentation/ooxml_presentation_element_registry.hpp> |
| 2 | + |
| 3 | +namespace odr::internal::ooxml::presentation { |
| 4 | + |
| 5 | +void ElementRegistry::clear() noexcept { |
| 6 | + m_elements.clear(); |
| 7 | + m_tables.clear(); |
| 8 | + m_texts.clear(); |
| 9 | +} |
| 10 | + |
| 11 | +[[nodiscard]] std::size_t ElementRegistry::size() const noexcept { |
| 12 | + return m_elements.size(); |
| 13 | +} |
| 14 | + |
| 15 | +ExtendedElementIdentifier ElementRegistry::create_element() { |
| 16 | + m_elements.emplace_back(); |
| 17 | + return ExtendedElementIdentifier(m_elements.size()); |
| 18 | +} |
| 19 | + |
| 20 | +ElementRegistry::Table & |
| 21 | +ElementRegistry::create_table_element(const ExtendedElementIdentifier id) { |
| 22 | + check_element_id(id); |
| 23 | + auto [it, success] = m_tables.emplace(id.element_id(), Table{}); |
| 24 | + return it->second; |
| 25 | +} |
| 26 | + |
| 27 | +ElementRegistry::Text & |
| 28 | +ElementRegistry::create_text_element(const ExtendedElementIdentifier id) { |
| 29 | + check_element_id(id); |
| 30 | + auto [it, success] = m_texts.emplace(id.element_id(), Text{}); |
| 31 | + return it->second; |
| 32 | +} |
| 33 | + |
| 34 | +[[nodiscard]] ElementRegistry::Element & |
| 35 | +ElementRegistry::element(const ExtendedElementIdentifier id) { |
| 36 | + check_element_id(id); |
| 37 | + return m_elements.at(id.element_id() - 1); |
| 38 | +} |
| 39 | + |
| 40 | +[[nodiscard]] const ElementRegistry::Element & |
| 41 | +ElementRegistry::element(const ExtendedElementIdentifier id) const { |
| 42 | + check_element_id(id); |
| 43 | + return m_elements.at(id.element_id() - 1); |
| 44 | +} |
| 45 | + |
| 46 | +[[nodiscard]] ElementRegistry::Table & |
| 47 | +ElementRegistry::table_element(const ExtendedElementIdentifier id) { |
| 48 | + check_table_id(id); |
| 49 | + return m_tables.at(id.element_id()); |
| 50 | +} |
| 51 | + |
| 52 | +[[nodiscard]] const ElementRegistry::Table & |
| 53 | +ElementRegistry::table_element(const ExtendedElementIdentifier id) const { |
| 54 | + check_table_id(id); |
| 55 | + return m_tables.at(id.element_id()); |
| 56 | +} |
| 57 | + |
| 58 | +[[nodiscard]] ElementRegistry::Text & |
| 59 | +ElementRegistry::text_element(const ExtendedElementIdentifier id) { |
| 60 | + check_text_id(id); |
| 61 | + return m_texts.at(id.element_id()); |
| 62 | +} |
| 63 | + |
| 64 | +[[nodiscard]] const ElementRegistry::Text & |
| 65 | +ElementRegistry::text_element(const ExtendedElementIdentifier id) const { |
| 66 | + check_text_id(id); |
| 67 | + return m_texts.at(id.element_id()); |
| 68 | +} |
| 69 | + |
| 70 | +void ElementRegistry::check_element_id( |
| 71 | + const ExtendedElementIdentifier id) const { |
| 72 | + if (id.is_null()) { |
| 73 | + throw std::out_of_range( |
| 74 | + "DocumentElementRegistry::check_id: null identifier"); |
| 75 | + } |
| 76 | + if (id.element_id() - 1 >= m_elements.size()) { |
| 77 | + throw std::out_of_range( |
| 78 | + "DocumentElementRegistry::check_id: identifier out of range"); |
| 79 | + } |
| 80 | +} |
| 81 | + |
| 82 | +void ElementRegistry::check_table_id(const ExtendedElementIdentifier id) const { |
| 83 | + check_element_id(id); |
| 84 | + if (!m_tables.contains(id.element_id())) { |
| 85 | + throw std::out_of_range( |
| 86 | + "DocumentElementRegistry::check_id: identifier not found"); |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +void ElementRegistry::check_text_id(const ExtendedElementIdentifier id) const { |
| 91 | + check_element_id(id); |
| 92 | + if (!m_texts.contains(id.element_id())) { |
| 93 | + throw std::out_of_range( |
| 94 | + "DocumentElementRegistry::check_id: identifier not found"); |
| 95 | + } |
| 96 | +} |
| 97 | + |
| 98 | +void ElementRegistry::append_child(const ExtendedElementIdentifier parent_id, |
| 99 | + const ExtendedElementIdentifier child_id) { |
| 100 | + check_element_id(parent_id); |
| 101 | + check_element_id(child_id); |
| 102 | + |
| 103 | + const ExtendedElementIdentifier previous_sibling_id( |
| 104 | + element(parent_id).last_child_id); |
| 105 | + |
| 106 | + element(child_id).parent_id = parent_id; |
| 107 | + element(child_id).first_child_id = null_element_id; |
| 108 | + element(child_id).last_child_id = null_element_id; |
| 109 | + element(child_id).previous_sibling_id = previous_sibling_id.element_id(); |
| 110 | + element(child_id).next_sibling_id = null_element_id; |
| 111 | + |
| 112 | + if (element(parent_id).first_child_id == null_element_id) { |
| 113 | + element(parent_id).first_child_id = child_id.element_id(); |
| 114 | + } else { |
| 115 | + element(previous_sibling_id).next_sibling_id = child_id.element_id(); |
| 116 | + } |
| 117 | + element(parent_id).last_child_id = child_id.element_id(); |
| 118 | +} |
| 119 | + |
| 120 | +void ElementRegistry::append_column(const ExtendedElementIdentifier table_id, |
| 121 | + const ExtendedElementIdentifier column_id) { |
| 122 | + check_table_id(table_id); |
| 123 | + check_element_id(column_id); |
| 124 | + |
| 125 | + const ExtendedElementIdentifier previous_sibling_id( |
| 126 | + table_element(table_id).last_column_id); |
| 127 | + |
| 128 | + element(column_id).parent_id = table_id; |
| 129 | + element(column_id).first_child_id = null_element_id; |
| 130 | + element(column_id).last_child_id = null_element_id; |
| 131 | + element(column_id).previous_sibling_id = previous_sibling_id.element_id(); |
| 132 | + element(column_id).next_sibling_id = null_element_id; |
| 133 | + |
| 134 | + if (table_element(table_id).first_column_id == null_element_id) { |
| 135 | + table_element(table_id).first_column_id = column_id.element_id(); |
| 136 | + } else { |
| 137 | + element(previous_sibling_id).next_sibling_id = column_id.element_id(); |
| 138 | + } |
| 139 | + table_element(table_id).last_column_id = column_id.element_id(); |
| 140 | +} |
| 141 | + |
| 142 | +} // namespace odr::internal::ooxml::presentation |
0 commit comments