All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Pages
NegateMatch.h
Go to the documentation of this file.
1 // Copyright 2015, 2016 Thomas Trapp
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #ifndef HEXT_NEGATE_MATCH_H_INCLUDED
16 #define HEXT_NEGATE_MATCH_H_INCLUDED
17 
18 /// @file
19 /// Declares hext::NegateMatch
20 
21 #include "hext/Cloneable.h"
22 #include "hext/Match.h"
23 
24 #include <memory>
25 #include <vector>
26 
27 #include <gumbo.h>
28 
29 
30 namespace hext {
31 
32 
33 /// Matches HTML nodes for which every given Match returns false.
34 ///
35 /// @par Example:
36 /// ~~~~~~~~~~~~~
37 /// GumboNode * attr_none = ...; // <hr/>
38 /// GumboNode * attr_one = ...; // <h1 class="main-heading"></h1>
39 /// GumboNode * attr_two = ...; // <div id="cart" class="my-cart"></div>
40 ///
41 /// NegateMatch not_one (std::make_unique<AttributeCountMatch>(1));
42 /// NegateMatch not_none(std::make_unique<AttributeCountMatch>(0));
43 ///
44 /// assert(!not_none.matches(attr_none));
45 /// assert( not_none.matches(attr_one));
46 /// assert( not_none.matches(attr_two));
47 ///
48 /// assert( not_one.matches(attr_none));
49 /// assert(!not_one.matches(attr_one));
50 /// assert( not_one.matches(attr_two));
51 /// ~~~~~~~~~~~~~
52 class NegateMatch final : public Cloneable<NegateMatch, Match>
53 {
54 public:
55  /// Constructs a NegateMatch that matches nodes for which every contained
56  /// Match returns false.
57  ///
58  /// @param v_matches: An optional vector containing Matches.
59  explicit
60  NegateMatch(std::vector<std::unique_ptr<Match>> v_matches = {}) noexcept;
61 
62  /// Constructs a NegateMatch and appends a Match.
63  ///
64  /// @param match: The Match to append.
65  explicit NegateMatch(std::unique_ptr<Match> match);
66 
67  ~NegateMatch() noexcept = default;
68  NegateMatch(NegateMatch&& other) noexcept = default;
69  NegateMatch(const NegateMatch& other);
70  NegateMatch& operator=(NegateMatch&& other) noexcept = default;
71  NegateMatch& operator=(const NegateMatch& other);
72 
73  /// Appends a Match.
74  ///
75  /// @param match: The Match to append.
76  void append_match(std::unique_ptr<Match> match);
77 
78  /// Returns true if every contained Match returns false for node.
79  ///
80  /// @param node: A pointer to a GumboNode.
81  bool matches(const GumboNode * node) const override;
82 
83 private:
84  /// A vector containing Matches whose result will be negated.
85  std::vector<std::unique_ptr<Match>> matches_;
86 };
87 
88 
89 } // namespace hext
90 
91 
92 #endif // HEXT_NEGATE_MATCH_H_INCLUDED
93 
void append_match(std::unique_ptr< Match > match)
Appends a Match.
NegateMatch(std::vector< std::unique_ptr< Match >> v_matches={}) noexcept
Constructs a NegateMatch that matches nodes for which every contained Match returns false...
~NegateMatch() noexcept=default
Curiously recurring template pattern that extends a base class to provide a virtual method Cloneable:...
Definition: Cloneable.h:34
Declares hext::Match.
bool matches(const GumboNode *node) const override
Returns true if every contained Match returns false for node.
Defines template hext::Cloneable.
NegateMatch & operator=(NegateMatch &&other) noexcept=default
Matches HTML nodes for which every given Match returns false.
Definition: NegateMatch.h:52