Commit 05c9a4608e3893766b42c30990a61dfc2e0451ee

Authored by m-holger
1 parent c0137dac

Refactor `NNTreeIterator` and `NNTreeImpl` to inline implementation details for simplicity

- Marked `NNTreeIterator` and `NNTreeImpl` as `final` to prevent inheritance.
- Inlined `NNTreeIterator` and `NNTreeImpl` methods to streamline code and reduce redundancy.
- Updated constructors, operators, and utility functions in `NNTreeIterator` and `NNTreeImpl` for improved readability and maintainability.
libqpdf/NNTree.cc
... ... @@ -38,11 +38,6 @@ NNTreeImpl::error(QPDFObjectHandle const& node, std::string const& msg) const
38 38 throw QPDFExc(qpdf_e_damaged_pdf, qpdf.getFilename(), get_description(node), 0, msg);
39 39 }
40 40  
41   -NNTreeIterator::NNTreeIterator(NNTreeImpl& impl) :
42   - impl(impl)
43   -{
44   -}
45   -
46 41 void
47 42 NNTreeIterator::updateIValue(bool allow_invalid)
48 43 {
... ... @@ -102,15 +97,6 @@ NNTreeIterator::getNextKid(PathElement& pe, bool backward)
102 97 }
103 98 }
104 99 }
105   -
106   -// iterator can be incremented or decremented, or dereferenced. This does not imply that it points
107   -// to a valid item.
108   -bool
109   -NNTreeIterator::valid() const
110   -{
111   - return item_number >= 0;
112   -}
113   -
114 100 void
115 101 NNTreeIterator::increment(bool backward)
116 102 {
... ... @@ -488,34 +474,6 @@ NNTreeIterator::remove()
488 474 }
489 475 }
490 476  
491   -NNTreeIterator&
492   -NNTreeIterator::operator++()
493   -{
494   - increment(false);
495   - return *this;
496   -}
497   -
498   -NNTreeIterator&
499   -NNTreeIterator::operator--()
500   -{
501   - increment(true);
502   - return *this;
503   -}
504   -
505   -NNTreeIterator::reference
506   -NNTreeIterator::operator*()
507   -{
508   - updateIValue(false);
509   - return ivalue;
510   -}
511   -
512   -NNTreeIterator::pointer
513   -NNTreeIterator::operator->()
514   -{
515   - updateIValue(false);
516   - return &ivalue;
517   -}
518   -
519 477 bool
520 478 NNTreeIterator::operator==(NNTreeIterator const& other) const
521 479 {
... ... @@ -537,20 +495,6 @@ NNTreeIterator::operator==(NNTreeIterator const& other) const
537 495 return item_number == other.item_number;
538 496 }
539 497  
540   -void
541   -NNTreeIterator::setItemNumber(QPDFObjectHandle const& a_node, int n)
542   -{
543   - node = a_node;
544   - item_number = n;
545   - updateIValue();
546   -}
547   -
548   -void
549   -NNTreeIterator::addPathElement(QPDFObjectHandle const& a_node, int kid_number)
550   -{
551   - path.emplace_back(a_node, kid_number);
552   -}
553   -
554 498 bool
555 499 NNTreeIterator::deepen(QPDFObjectHandle a_node, bool first, bool allow_empty)
556 500 {
... ... @@ -621,27 +565,6 @@ NNTreeIterator::deepen(QPDFObjectHandle a_node, bool first, bool allow_empty)
621 565 return true;
622 566 }
623 567  
624   -NNTreeImpl::NNTreeImpl(
625   - QPDF& qpdf,
626   - QPDFObjectHandle& oh,
627   - qpdf_object_type_e key_type,
628   - std::function<bool(QPDFObjectHandle const&)> value_validator,
629   - bool auto_repair) :
630   - qpdf(qpdf),
631   - oh(oh),
632   - key_type(key_type),
633   - items_key(key_type == ::ot_string ? "/Names" : "/Nums"),
634   - value_valid(value_validator),
635   - auto_repair(auto_repair)
636   -{
637   -}
638   -
639   -void
640   -NNTreeImpl::setSplitThreshold(int threshold)
641   -{
642   - split_threshold = threshold;
643   -}
644   -
645 568 NNTreeImpl::iterator
646 569 NNTreeImpl::begin()
647 570 {
... ... @@ -651,12 +574,6 @@ NNTreeImpl::begin()
651 574 }
652 575  
653 576 NNTreeImpl::iterator
654   -NNTreeImpl::end()
655   -{
656   - return {*this};
657   -}
658   -
659   -NNTreeImpl::iterator
660 577 NNTreeImpl::last()
661 578 {
662 579 iterator result(*this);
... ...
libqpdf/qpdf/NNTree.hh
... ... @@ -9,7 +9,7 @@
9 9 #include <memory>
10 10  
11 11 class NNTreeImpl;
12   -class NNTreeIterator
  12 +class NNTreeIterator final
13 13 {
14 14 friend class NNTreeImpl;
15 15  
... ... @@ -21,9 +21,20 @@ class NNTreeIterator
21 21 using pointer = T*;
22 22 using reference = T&;
23 23  
24   - virtual ~NNTreeIterator() = default;
25   - bool valid() const;
26   - NNTreeIterator& operator++();
  24 + ~NNTreeIterator() = default;
  25 + // iterator can be incremented or decremented, or dereferenced. This does not imply that it
  26 + // points to a valid item.
  27 + bool
  28 + valid() const
  29 + {
  30 + return item_number >= 0;
  31 + }
  32 + NNTreeIterator&
  33 + operator++()
  34 + {
  35 + increment(false);
  36 + return *this;
  37 + }
27 38 NNTreeIterator
28 39 operator++(int)
29 40 {
... ... @@ -31,7 +42,12 @@ class NNTreeIterator
31 42 ++(*this);
32 43 return t;
33 44 }
34   - NNTreeIterator& operator--();
  45 + NNTreeIterator&
  46 + operator--()
  47 + {
  48 + increment(true);
  49 + return *this;
  50 + }
35 51 NNTreeIterator
36 52 operator--(int)
37 53 {
... ... @@ -39,8 +55,18 @@ class NNTreeIterator
39 55 --(*this);
40 56 return t;
41 57 }
42   - reference operator*();
43   - pointer operator->();
  58 + reference
  59 + operator*()
  60 + {
  61 + updateIValue(false);
  62 + return ivalue;
  63 + }
  64 + pointer
  65 + operator->()
  66 + {
  67 + updateIValue(false);
  68 + return &ivalue;
  69 + }
44 70 bool operator==(NNTreeIterator const& other) const;
45 71 bool
46 72 operator!=(NNTreeIterator const& other) const
... ... @@ -61,11 +87,24 @@ class NNTreeIterator
61 87 int kid_number;
62 88 };
63 89  
64   - NNTreeIterator(NNTreeImpl& impl);
  90 + NNTreeIterator(NNTreeImpl& impl) :
  91 + impl(impl)
  92 + {
  93 + }
65 94 void updateIValue(bool allow_invalid = true);
66 95 bool deepen(QPDFObjectHandle node, bool first, bool allow_empty);
67   - void setItemNumber(QPDFObjectHandle const& node, int);
68   - void addPathElement(QPDFObjectHandle const& node, int kid_number);
  96 + void
  97 + setItemNumber(QPDFObjectHandle const& a_node, int n)
  98 + {
  99 + node = a_node;
  100 + item_number = n;
  101 + updateIValue();
  102 + }
  103 + void
  104 + addPathElement(QPDFObjectHandle const& a_node, int kid_number)
  105 + {
  106 + path.emplace_back(a_node, kid_number);
  107 + }
69 108 QPDFObjectHandle getNextKid(PathElement& element, bool backward);
70 109 void increment(bool backward);
71 110 void resetLimits(QPDFObjectHandle node, std::list<PathElement>::iterator parent);
... ... @@ -80,7 +119,7 @@ class NNTreeIterator
80 119 value_type ivalue;
81 120 };
82 121  
83   -class NNTreeImpl
  122 +class NNTreeImpl final
84 123 {
85 124 friend class NNTreeIterator;
86 125  
... ... @@ -88,13 +127,25 @@ class NNTreeImpl
88 127 typedef NNTreeIterator iterator;
89 128  
90 129 NNTreeImpl(
91   - QPDF&,
92   - QPDFObjectHandle&,
  130 + QPDF& qpdf,
  131 + QPDFObjectHandle& oh,
93 132 qpdf_object_type_e key_type,
94 133 std::function<bool(QPDFObjectHandle const&)> value_validator,
95   - bool auto_repair = true);
  134 + bool auto_repair) :
  135 + qpdf(qpdf),
  136 + oh(oh),
  137 + key_type(key_type),
  138 + items_key(key_type == ::ot_string ? "/Names" : "/Nums"),
  139 + value_valid(value_validator),
  140 + auto_repair(auto_repair)
  141 + {
  142 + }
96 143 iterator begin();
97   - iterator end();
  144 + iterator
  145 + end()
  146 + {
  147 + return {*this};
  148 + }
98 149 iterator last();
99 150 iterator find(QPDFObjectHandle key, bool return_prev_if_not_found = false);
100 151 iterator insertFirst(QPDFObjectHandle const& key, QPDFObjectHandle const& value);
... ... @@ -105,7 +156,11 @@ class NNTreeImpl
105 156  
106 157 // Change the split threshold for easier testing. There's no real reason to expose this to
107 158 // downstream tree helpers, but it has to be public so we can call it from the test suite.
108   - void setSplitThreshold(int split_threshold);
  159 + void
  160 + setSplitThreshold(int threshold)
  161 + {
  162 + split_threshold = threshold;
  163 + }
109 164  
110 165 private:
111 166 void repair();
... ...